[Libav-user] MPEG2 Encoding with a determined bit_rate

francesco at bltitalia.com francesco at bltitalia.com
Tue Mar 13 09:34:45 CET 2012


Hi to all
I am attempting to convert in mpeg2 with a bitrate of 30Mbit/sec and with a
50Mbit/sec but it seems that the bit rate  variation doesn't work.

Here is my routine that gets as input a buffer with my originary coded frame
(not interlaced),
decompress and interlace it and recompress using MPEG2 compressor (Hsize and
VSize are field dimension,this is why the VSize is doubled):

int Expand_Inter_MPEG2Comp_Image(unsigned char * src_buff, unsigned char
tv_std,void *pCodecCtx, void *pFrame,void *p_codec)
{

AVFrame         *locpFrame;
AVCodecContext  *locpCodecCtx;
AVCodec         *codec;

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

    locpCodecCtx->bit_rate = 30000;  // This should set the 30Mbit bit rate
    /* resolution must be a multiple of two */
    locpCodecCtx->width = Hsize;
    locpCodecCtx->height =  (Vsize<<1) + 32;  // Add 32 blank rows on top 

    /* frames per second */
    locpCodecCtx->time_base.num = (tv_std) ? 1001 : 1;  // tv_std = 1 for
59,94fps, 0 for 25fps
    locpCodecCtx->time_base.den = (tv_std) ? 30000 : 25;
    locpCodecCtx->gop_size = 1;     /* emit only intra frame  */
    locpCodecCtx->max_b_frames=0;
    locpCodecCtx->pix_fmt = PIX_FMT_YUV422P;  ///< planar YUV 4:2:2, 16bpp,
(1 Cr & Cb sample per 2x1 Y samples)
    locpCodecCtx->thread_count = 2;  // 2 CPU on board


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

    if(rtv<0) goto __end_rdec;

  // ## DECODING CODE THAT LEAVE RAW DATA IN  locpFrame->data OMITTED IN
ORDER TO HAVE
  // ##  A SHORTER MAIL


    if (avcodec_open(locpCodecCtx, codec) < 0) {rtv = -6; goto __end_rdec; }

    encBufSize = 0x500000;
    out_size = avcodec_encode_video(locpCodecCtx, tempBuffer,encBufSize ,
locpFrame);

    if(out_size<=0) {rtv = -7; avcodec_close(locpCodecCtx); goto __end_rdec; }
    tempBuffer[out_size] = 0x00;
    tempBuffer[out_size+1] = 0x00;
    tempBuffer[out_size+2] = 0x01;
    tempBuffer[out_size+3] = 0xb7;
    rtv = out_size + 4 ;
    avcodec_close(locpCodecCtx);
__end_rdec:

  return rtv;
}

Before calling this routine the setup was:

   /* find the mpeg2 video encoder */
 pcodec = avcodec_find_encoder(CODEC_ID_MPEG2VIDEO);
 if (!pcodec) {str1="Unable to find codec MPEG2";crderr=-1; goto
_CreateD10MxfFilerr;}

 pCodecCtx = avcodec_alloc_context3(pcodec);
 if(pCodecCtx==NULL) {str1="Unable to alloc codec context";crderr=-2; goto
_CreateD10MxfFilerr;}
 pFrame = avcodec_alloc_frame();
 if(pFrame==NULL) {str1="Unable to alloc frame";crderr=-3; goto
_CreateD10MxfFilerr;}

The result is the same if in the locpCodecCtx->bit_rate I insert the value
of 50000 or
30000000 or 50000000.  Where is the error ?




More information about the Libav-user mailing list