[Libav-user] Provided packet is too small, needs to be x

Karthik Viswanathan vkarthik90 at gmail.com
Thu Jun 6 12:21:03 CEST 2013


Hello,

I got the ffmpeg builds from zeranoe[20130601-git-716dbc7] & used gcc
4.8.0 on windows to compile.

I am trying to convert a h264 video to webm. The output video is empty
and the console is filled with 'Provided packet is too small, needs to
be 612'.

////////////////////// the decoder:

  pFormatCtxIn = avformat_alloc_context();

  ret = avformat_open_input(&pFormatCtxIn, "bump.mp4", NULL, NULL);

  if(avformat_find_stream_info(pFormatCtxIn,NULL)<0)
    return -1;

  video_stream_idx = -1;
  for(i=0; i<pFormatCtxIn->nb_streams; i++){
    if(pFormatCtxIn->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
    {
      video_stream_idx=i;
    }
  }

  if(video_stream_idx==-1)
    return -1;

  pCodecCtxIn = pFormatCtxIn->streams[video_stream_idx]->codec;

  pCodecIn = avcodec_find_decoder(pCodecCtxIn->codec_id);

  avcodec_open2(pCodecCtxIn,pCodecIn, NULL);

  if(pCodecCtxIn->time_base.num>1000 && pCodecCtxIn->time_base.den==1)
    pCodecCtxIn->time_base.den=1000;

/////////////////// the encoder:

  pOfmtOut = av_guess_format(NULL, filename,NULL);

  pFormatCtxOut = avformat_alloc_context();

  pOfmtOut->video_codec = AV_CODEC_ID_VP8;

  pFormatCtxOut->oformat = pOfmtOut;

  snprintf(pFormatCtxOut->filename,sizeof(pFormatCtxOut->filename),"%s",filename);

  strmVdoOut = avformat_new_stream(pFormatCtxOut,NULL);

  pCodecCtxOut = strmVdoOut->codec;

  pCodecCtxOut->codec_id = pOfmtOut->video_codec;
  pCodecCtxOut->codec_type = AVMEDIA_TYPE_VIDEO;

  pCodecCtxOut->bit_rate = 4000; /* open it */
  pCodecCtxOut->pix_fmt = AV_PIX_FMT_YUV420P;
  pCodecCtxOut->width = 1360;
  pCodecCtxOut->height = 760;
  pCodecCtxOut->time_base= (AVRational){1,30};

  if(pFormatCtxOut->oformat->flags & AVFMT_GLOBALHEADER)
    pCodecCtxOut->flags |= CODEC_FLAG_GLOBAL_HEADER;

  pCodecOut = avcodec_find_encoder(pCodecCtxOut->codec_id);

  avcodec_open2(pCodecCtxOut, pCodecOut,NULL);

  avio_open(&pFormatCtxOut->pb, filename, AVIO_FLAG_WRITE);

  ret = avformat_write_header(pFormatCtxOut, NULL);

/////////////////// frame allocation

  frame = avcodec_alloc_frame();

  frame->format = pCodecCtxOut->pix_fmt;
  frame->width  = pCodecCtxOut->width;
  frame->height = pCodecCtxOut->height;

  ret = av_image_alloc(frame->data, frame->linesize,
pCodecCtxOut->width, pCodecCtxOut->height,pCodecCtxOut->pix_fmt, 32);

//////////////////// the transcoding

 i = 0;
  while(++i) {
    av_init_packet(&packet);

    ret = av_read_frame(pFormatCtxIn,&packet);
    if(ret < 0){
      fprintf(stderr,"Error reading frame\n");
      break;
    }

    if(packet.stream_index == video_stream_idx){

      ret = avcodec_decode_video2(pCodecCtxIn,frame,&got_output,&packet);

      if (got_output) {

        frame->pts=i;

        ret = avcodec_encode_video2(pCodecCtxOut, &packet, frame, &got_output);

        if (got_output) {

          if (pCodecCtxOut->coded_frame->pts != (0x8000000000000000LL))
            packet.pts = av_rescale_q(pCodecCtxOut->coded_frame->pts,
pCodecCtxOut->time_base, strmVdoOut->time_base);

          if(pCodecCtxOut->coded_frame->key_frame)
            packet.flags |= AV_PKT_FLAG_KEY;

          if(av_interleaved_write_frame(pFormatCtxOut,&packet) < 0){
            fprintf(stderr,"error writing frame");
            exit(1);
          }
        }
      }
    }
    av_free_packet(&packet);
  }

The full code is available here: https://www.dropbox.com/s/agltcde1o1mej7h/try.c


More information about the Libav-user mailing list