[Libav-user] Bit rate not changing

francesco at bltitalia.com francesco at bltitalia.com
Tue Apr 3 08:48:45 CEST 2012

Hi to all
I am attempting to transcode compressed SD video in JP2K format to MPEG-2
compressed video at 30, 40 and 50Mbit
using libavcodec only for compressing.  Here is the code:

 pQTcodec = avcodec_find_encoder(CODEC_ID_MPEG2VIDEO); // find the mpeg2
video encoder
 if (!pQTcodec) {str1="Unable to find codec MPEG2";ferr=-1; goto

 pQTCodecCtx = avcodec_alloc_context3(pQTcodec);
 if(pQTCodecCtx==NULL) {str1="Unable to alloc codec context";ferr=-2; goto
 pQTFrame = avcodec_alloc_frame();
 if(pQTFrame==NULL) {str1="Unable to alloc frame";ferr=-3; goto

  case  0 :cvbr=30000000;  break;   // 30Mbs
  case  1 :cvbr=40000000;  break;   // 40Mbs
  case  2 :cvbr=50000000;  break;   // 50Mbs
  default :cvbr=25000000;  break;   // 25Mbs

 pQTCodecCtx->bit_rate = cvbr;

 pQTCodecCtx->rc_max_rate = pQTCodecCtx->rc_min_rate =pQTCodecCtx->bit_rate
= cvbr;



 pQTCodecCtx->qmin = 1;
 pQTCodecCtx->vbv_delay = 3600;
 pQTCodecCtx->rtp_payload_size = 1;
 pQTCodecCtx->rc_buffer_size = 2000000;
 pQTCodecCtx->rc_initial_buffer_occupancy = 2000000;

  pQTCodecCtx->lmin = 1*FF_QP2LAMBDA;
  pQTCodecCtx->rc_max_available_vbv_use = 1;
  pQTCodecCtx->rc_min_vbv_overflow_use = 1;
  vform.num = 4;
  vform.den = 3;
  pQTCodecCtx->sample_aspect_ratio = vform;
  pQTCodecCtx->rc_buffer_aggressivity = 0.25F;
  pQTCodecCtx->intra_dc_precision = 2;
  pQTCodecCtx->qmax = 3;

after this I call a routine that expands original frame (jpeg 2000
compressed) and recompress in MPEG2 format.  To this routine I pass the
pointer to Codec Context (pQTCodecCtx) , the pointer to a frame (pQTFrame)
and the pointer to the codec; that is

int Expand_Inter_MPEG2Comp_Image(unsigned char * frame, void *pCodecCtx,
void *pFrame,void *p_codec)
 AVFrame         *locpFrame;
 AVCodecContext  *locpCodecCtx;
 AVCodec         *codec;

 locpCodecCtx = (AVCodecContext*) pCodecCtx;
 locpFrame = (AVFrame*)pFrame;
 codec = (AVCodec*) p_codec;

 locpCodecCtx->width = Hsize;
 locpCodecCtx->height =  (Vsize<<1) + 32;  // Add 32 blank rows on top for
    /* frames per second */

 locpCodecCtx->time_base.num =  1;
 locpCodecCtx->time_base.den =  25;
 locpCodecCtx->gop_size = 0;     /* emit only intra frame  */
 locpCodecCtx->pix_fmt = PIX_FMT_YUV422P;  ///< planar YUV 4:2:2, 16bpp, (1
Cr & Cb sample per 2x1 Y samples)

 rtv = av_image_alloc(locpFrame->data, locpFrame->linesize,
                   locpCodecCtx->width, locpCodecCtx->height,
locpCodecCtx->pix_fmt, 1);

<code for expanding original JP2K frame and setting raw data in
locpFrame->data array>

 locpFrame->qscale_type = 1;
 locpFrame->top_field_first = 1;
 locpFrame->interlaced_frame = 1;

 if (avcodec_open2(locpCodecCtx, codec,NULL) < 0) {rtv=-6;str1="on av codec
open";goto __end_rdec;}

 encBufSize = 0x500000;
 out_size = avcodec_encode_video(locpCodecCtx, &tempBuffer[kagsz],encBufSize
, locpFrame);

 return out_size;

in this case if I set different frame rates, the result is a file of
different size for each frame.
The size is congruent with frame rate, the headers are set accordingly, but
the remainig content is the same! !
As if the only thing is to adjust headers.  The only difference is that
files with highter frame rate are filled with zeroes at the end.
I upload the compressed images of the first frame compressed at 50M and at
40M. Here you can see that the only diference is the header and the zero
fill at the end in order to obtain the size. The same frame recompressed
with final cut pro gives compressed stream very different.

More information about the Libav-user mailing list