Skip to content

Commit ce82163

Browse files
committed
VideoProperties: compute bit rate if constant and not specified
1 parent 1415a54 commit ce82163

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

src/AvTranscoder/mediaProperty/VideoProperties.cpp

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,55 @@ size_t VideoProperties::getBitRate() const
392392
{
393393
if( ! _codecContext )
394394
throw std::runtime_error( "unknown codec context" );
395-
return _codecContext->bit_rate;
395+
// return bit rate of stream if present or VBR mode
396+
if( _codecContext->bit_rate || _codecContext->rc_max_rate )
397+
return _codecContext->bit_rate;
398+
399+
// else compute bit rate from the first GOP
400+
int gopFramesSize = 0;
401+
if( !_formatContext || !_codecContext || !_codec )
402+
return 0;
403+
404+
if( _codecContext->width && _codecContext->height )
405+
{
406+
// discard no frame type when decode
407+
_codecContext->skip_frame = AVDISCARD_NONE;
408+
409+
#if LIBAVCODEC_VERSION_MAJOR > 54
410+
AVFrame* frame = av_frame_alloc();
411+
#else
412+
AVFrame* frame = avcodec_alloc_frame();
413+
#endif
414+
AVPacket pkt;
415+
av_init_packet( &pkt );
416+
avcodec_open2( _codecContext, _codec, NULL );
417+
int gotFrame = 0;
418+
int count = 0;
419+
420+
while( ! av_read_frame( const_cast<AVFormatContext*>( _formatContext ), &pkt ) )
421+
{
422+
if( pkt.stream_index == (int)_streamIndex )
423+
{
424+
avcodec_decode_video2( _codecContext, frame, &gotFrame, &pkt );
425+
if( gotFrame )
426+
{
427+
gopFramesSize += frame->pkt_size;
428+
++count;
429+
}
430+
}
431+
av_free_packet( &pkt );
432+
if( _codecContext->gop_size == count )
433+
break;
434+
}
435+
#if LIBAVCODEC_VERSION_MAJOR > 54
436+
av_frame_free( &frame );
437+
#elif LIBAVCODEC_VERSION_MAJOR > 53
438+
avcodec_free_frame( &frame );
439+
#else
440+
av_free( frame );
441+
#endif
442+
}
443+
return (gopFramesSize / _codecContext->gop_size) * 8 * getFps();
396444
}
397445

398446
size_t VideoProperties::getMaxBitRate() const

0 commit comments

Comments
 (0)