Skip to content

Commit 163a02a

Browse files
author
Clement Champetier
committed
VideoProperties: refactor analyseGOPStructure method
Return immediatly at the beginning of the function if some attributes are not correctly set.
1 parent 8e63e7f commit 163a02a

File tree

1 file changed

+56
-57
lines changed

1 file changed

+56
-57
lines changed

src/AvTranscoder/properties/VideoProperties.cpp

Lines changed: 56 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -497,75 +497,74 @@ std::vector<std::pair<char, int> > VideoProperties::getGopStructure() const
497497

498498
void VideoProperties::analyseGopStructure(IProgress& progress)
499499
{
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;
506507

507-
AVPacket pkt;
508-
av_init_packet(&pkt);
508+
AVPacket pkt;
509+
av_init_packet(&pkt);
509510

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);
512513

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;
518519

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)
520526
{
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();
544528

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)
547534
{
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;
554539
}
540+
541+
_gopSize = ++count;
555542
}
543+
}
544+
av_free_packet(&pkt);
556545

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+
}
559557

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);
562560

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.");
569568
}
570569
}
571570

0 commit comments

Comments
 (0)