[Libav-user] Bit rate not changing

jan hölscher jan.hoelscher at codergrid.de
Tue Apr 3 09:09:52 CEST 2012


2012/4/3 <francesco at bltitalia.com>

> 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
> _CoreMakeMPEG2Video_err;}
>
>  pQTCodecCtx = avcodec_alloc_context3(pQTcodec);
>  if(pQTCodecCtx==NULL) {str1="Unable to alloc codec context";ferr=-2; goto
> _CoreMakeMPEG2Video_err;}
>  pQTFrame = avcodec_alloc_frame();
>  if(pQTFrame==NULL) {str1="Unable to alloc frame";ferr=-3; goto
> _CoreMakeMPEG2Video_err;}
>
>  switch(comp_drate)
>  {
>  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->flags =  CODEC_FLAG_INTERLACED_DCT |
> CODEC_CAP_AUTO_THREADS;//
> | CODEC_FLAG_SVCD_SCAN_OFFSET ;
>
>  pQTCodecCtx->flags2 =  CODEC_FLAG2_INTRA_VLC |
> CODEC_FLAG2_NON_LINEAR_QUANT;
>
>  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
> compatibility
>    /* frames per second */
>
>  locpCodecCtx->time_base.num =  1;
>  locpCodecCtx->time_base.den =  25;
>  locpCodecCtx->gop_size = 0;     /* 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)
>
>  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.
>
> _______________________________________________
> Libav-user mailing list
> Libav-user at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/libav-user
>

did you set the right PTS Timestamps for the Frames to be encoded?
The PTS Timestamps are needed by the Encoder to calculate the right bitrate.


-- 
*Media Encoding Cluster <http://codergrid.de>, *
*the first Open Source Cluster Encoding Solution *
*for distributed Media Encoding.*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20120403/b89270b9/attachment.html>


More information about the Libav-user mailing list