@@ -497,75 +497,74 @@ std::vector<std::pair<char, int> > VideoProperties::getGopStructure() const
497
497
498
498
void VideoProperties::analyseGopStructure (IProgress& progress)
499
499
{
500
- if (_formatContext && _codecContext && _codec)
501
- {
502
- if (_codecContext->width && _codecContext->height )
503
- {
504
- // Discard no frame type when decode
505
- _codecContext->skip_frame = AVDISCARD_NONE;
500
+ if (! _formatContext || ! _codecContext || ! _codec)
501
+ return ;
502
+ if (! _codecContext->width || ! _codecContext->height )
503
+ return ;
504
+
505
+ // Discard no frame type when decode
506
+ _codecContext->skip_frame = AVDISCARD_NONE;
506
507
507
- AVPacket pkt;
508
- av_init_packet (&pkt);
508
+ AVPacket pkt;
509
+ av_init_packet (&pkt);
509
510
510
- // Initialize the AVCodecContext to use the given AVCodec
511
- avcodec_open2 (_codecContext, _codec, NULL );
511
+ // Initialize the AVCodecContext to use the given AVCodec
512
+ avcodec_open2 (_codecContext, _codec, NULL );
512
513
513
- Frame frame;
514
- size_t count = 0 ;
515
- int gotFrame = 0 ;
516
- int positionOfFirstKeyFrame = -1 ;
517
- int positionOfLastKeyFrame = -1 ;
514
+ Frame frame;
515
+ size_t count = 0 ;
516
+ int gotFrame = 0 ;
517
+ int positionOfFirstKeyFrame = -1 ;
518
+ int positionOfLastKeyFrame = -1 ;
518
519
519
- while (!av_read_frame (const_cast <AVFormatContext*>(_formatContext), &pkt))
520
+ while (!av_read_frame (const_cast <AVFormatContext*>(_formatContext), &pkt))
521
+ {
522
+ if (pkt.stream_index == (int )_streamIndex)
523
+ {
524
+ avcodec_decode_video2 (_codecContext, &frame.getAVFrame (), &gotFrame, &pkt);
525
+ if (gotFrame)
520
526
{
521
- if (pkt.stream_index == (int )_streamIndex)
522
- {
523
- avcodec_decode_video2 (_codecContext, &frame.getAVFrame (), &gotFrame, &pkt);
524
- if (gotFrame)
525
- {
526
- AVFrame& avFrame = frame.getAVFrame ();
527
-
528
- _gopStructure.push_back (
529
- std::make_pair (av_get_picture_type_char (avFrame.pict_type ), frame.getEncodedSize ()));
530
- _isInterlaced = avFrame.interlaced_frame ;
531
- _isTopFieldFirst = avFrame.top_field_first ;
532
- if (avFrame.pict_type == AV_PICTURE_TYPE_I)
533
- {
534
- if (positionOfFirstKeyFrame == -1 )
535
- positionOfFirstKeyFrame = count;
536
- else
537
- positionOfLastKeyFrame = count;
538
- }
539
-
540
- _gopSize = ++count;
541
- }
542
- }
543
- av_free_packet (&pkt);
527
+ AVFrame& avFrame = frame.getAVFrame ();
544
528
545
- // If the first 2 key frames are found
546
- if (positionOfFirstKeyFrame != -1 && positionOfLastKeyFrame != -1 )
529
+ _gopStructure.push_back (
530
+ std::make_pair (av_get_picture_type_char (avFrame.pict_type ), frame.getEncodedSize ()));
531
+ _isInterlaced = avFrame.interlaced_frame ;
532
+ _isTopFieldFirst = avFrame.top_field_first ;
533
+ if (avFrame.pict_type == AV_PICTURE_TYPE_I)
547
534
{
548
- // Set gop size as distance between these 2 key frames
549
- _gopSize = positionOfLastKeyFrame - positionOfFirstKeyFrame;
550
- // Update gop structure to keep only one gop
551
- while (_gopStructure.size () > _gopSize)
552
- _gopStructure.pop_back ();
553
- break ;
535
+ if (positionOfFirstKeyFrame == -1 )
536
+ positionOfFirstKeyFrame = count;
537
+ else
538
+ positionOfLastKeyFrame = count;
554
539
}
540
+
541
+ _gopSize = ++count;
555
542
}
543
+ }
544
+ av_free_packet (&pkt);
556
545
557
- // Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext itself)
558
- avcodec_close (_codecContext);
546
+ // If the first 2 key frames are found
547
+ if (positionOfFirstKeyFrame != -1 && positionOfLastKeyFrame != -1 )
548
+ {
549
+ // Set gop size as distance between these 2 key frames
550
+ _gopSize = positionOfLastKeyFrame - positionOfFirstKeyFrame;
551
+ // Update gop structure to keep only one gop
552
+ while (_gopStructure.size () > _gopSize)
553
+ _gopStructure.pop_back ();
554
+ break ;
555
+ }
556
+ }
559
557
560
- // Returns at the beginning of the stream
561
- const_cast <FormatContext*>(&_fileProperties-> getFormatContext ())-> seek ( 0 , AVSEEK_FLAG_BYTE );
558
+ // Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext itself)
559
+ avcodec_close (_codecContext );
562
560
563
- // Check GOP size
564
- if (_gopSize <= 0 )
565
- {
566
- throw std::runtime_error (" Invalid GOP size when decoding the first data." );
567
- }
568
- }
561
+ // Returns at the beginning of the stream
562
+ const_cast <FormatContext*>(&_fileProperties->getFormatContext ())->seek (0 , AVSEEK_FLAG_BYTE);
563
+
564
+ // Check GOP size
565
+ if (_gopSize <= 0 )
566
+ {
567
+ throw std::runtime_error (" Invalid GOP size when decoding the first data." );
569
568
}
570
569
}
571
570
0 commit comments