Skip to content

Commit c458e33

Browse files
author
Clement Champetier
committed
VideoProperties: do not need to call getGopSize method to compute bitrate
1 parent df07fbd commit c458e33

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

src/AvTranscoder/properties/VideoProperties.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,6 @@ size_t VideoProperties::getBitRate() const
334334
if(!_codecContext->width || !_codecContext->height)
335335
throw std::runtime_error("cannot compute bit rate: invalid frame size");
336336

337-
// Needed to get the gop size
338-
if(getGopSize() == 0)
339-
throw std::runtime_error("cannot compute bit rate: need to get info from the first gop (see eAnalyseLevelFirstGop)");
340-
341337
LOG_WARN("The bitrate of the stream '" << _streamIndex << "' of file '" << _formatContext->filename << "' is unknown.")
342338
LOG_INFO("Decode the first GOP to compute the bitrate.")
343339

@@ -350,8 +346,10 @@ size_t VideoProperties::getBitRate() const
350346
avcodec_open2(_codecContext, _codec, NULL);
351347

352348
int gotFrame = 0;
353-
size_t count = 0;
349+
size_t nbDecodedFrames = 0;
354350
int gopFramesSize = 0;
351+
int positionOfFirstKeyFrame = -1;
352+
int positionOfLastKeyFrame = -1;
355353

356354
while(!av_read_frame(const_cast<AVFormatContext*>(_formatContext), &pkt))
357355
{
@@ -360,20 +358,35 @@ size_t VideoProperties::getBitRate() const
360358
avcodec_decode_video2(_codecContext, &frame.getAVFrame(), &gotFrame, &pkt);
361359
if(gotFrame)
362360
{
361+
// check distance between key frames
362+
AVFrame& avFrame = frame.getAVFrame();
363+
if(avFrame.pict_type == AV_PICTURE_TYPE_I)
364+
{
365+
if(positionOfFirstKeyFrame == -1)
366+
positionOfFirstKeyFrame = nbDecodedFrames;
367+
else
368+
positionOfLastKeyFrame = nbDecodedFrames;
369+
}
370+
++nbDecodedFrames;
371+
372+
// added size of all frames of the same gop
373+
if(positionOfLastKeyFrame == -1)
374+
{
363375
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(54, 7, 100)
364-
gopFramesSize += frame.getEncodedSize();
376+
gopFramesSize += frame.getEncodedSize();
365377
#else
366-
gopFramesSize += pkt.size;
378+
gopFramesSize += pkt.size;
367379
#endif
368-
++count;
380+
}
369381
}
370382
}
371383
av_free_packet(&pkt);
372-
if(getGopSize() == count)
384+
if(positionOfFirstKeyFrame != -1 && positionOfLastKeyFrame != -1)
373385
break;
374386
}
375387

376-
return (gopFramesSize / getGopSize()) * 8 * getFps();
388+
const size_t gopSize = positionOfLastKeyFrame - positionOfFirstKeyFrame;
389+
return (gopFramesSize / gopSize) * 8 * getFps();
377390
}
378391

379392
size_t VideoProperties::getMaxBitRate() const

0 commit comments

Comments
 (0)