0

I am trying to decode video encoded with H264. I am sending AVPacket's data and its size to decoder code. there I am trying to decode the frame and display it on a GUI. problem is when I am decoding the frame it is returning same number of frame byte as the size of packet means it is not decompressing the data. Can any one tell what will be the problem. My encoding program is working fine.

here is code for encoding

  static struct SwsContext *img_convert_ctx;
  pkt.data = NULL;   
  pkt.size = 0; 


  avpicture_fill((AVPicture *)srcFrame, frame,AV_PIX_FMT_BGR24, 640, 480);
 if(img_convert_ctx == NULL) {
  int w = 640;
  int h = 480;
  img_convert_ctx = sws_getContext(w, h, 
      AV_PIX_FMT_BGR24, c->width, c->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
   if(img_convert_ctx == NULL) {
    fprintf(stderr, "Cannot initialize the conversion context!\n");
  }
}
  sws_scale(img_convert_ctx, srcFrame->data, srcFrame->linesize, 0,480,picture->data, picture->linesize);


  fflush(stdout);

  picture->pts=counter;


  ret = avcodec_encode_video2(c, &pkt, picture, &got_output);
    if (ret < 0) {
        fprintf(stderr, "Error encoding frame\n");
    }

    if (got_output) {

        vdec.decode_frame(pkt.data ,pkt.size);

        av_free_packet(&pkt);
    }

decoder code...

    int len ,got_frame;

avpkt.size = data_length;

avpkt.data = frame_buffer;

if(!frame_buffer){

return "frame buffer empty\n";

}

len = avcodec_decode_video2(avctx ,frame ,&got_frame ,&avpkt);

if( len < 0){

    return "error while decoding\n";

}

if( got_frame ){

static struct SwsContext *img_convert_ctx;  

 if(img_convert_ctx == NULL) {

  img_convert_ctx = sws_getContext(w, h, 
      PIX_FMT_YUV420P, avctx->width,
      avctx->height, PIX_FMT_BGR24, 
      SWS_BICUBIC, NULL, NULL, NULL);

   if(img_convert_ctx == NULL) {

    return  "Cannot initialize the conversion context!\n";

  }

}

j=sws_scale(img_convert_ctx, 
    frame->data , frame->linesize ,
    0, h ,picture->data,
    picture->linesize );

if(j==0){

exit(1);

}

I am initializing all other code like AVCodecContext and Codec into other method.

Please help me to find the solution.

pogorskiy
  • 4,705
  • 1
  • 22
  • 21
Imrank
  • 1,009
  • 5
  • 15

1 Answers1

0

The avcodec_decode_video2 function should return the number of bytes processed, not the number of bytes of result picture. You just have to check the value of got_frame to find out when decoded a complete frame.

pogorskiy
  • 4,705
  • 1
  • 22
  • 21
  • yes it is decoding the frame because got_frame always return 1 but when I am trying to paint it on window it always shows a distorted frame. – Imrank Feb 05 '13 at 12:43
  • What kind of distortion are you watching? Put an example if possible. – pogorskiy Feb 05 '13 at 12:51
  • I have asked same question in ffmpeg forum link is http://ffmpeg.zeranoe.com/forum/viewtopic.php?f=7&t=949 please see the image. – Imrank Feb 05 '13 at 12:54
  • Eliminate the step of encoding and decoding to make sure that your conversion functions and display are working properly. Without the entire code, it is hard to understand what is not working correctly. – pogorskiy Feb 05 '13 at 13:11
  • encoder is working fine because i am able to write the encoded frames into file and it can be played using vlc. but in decoder code if I am commenting the avcodec_decode_video2() then frame is getting same garbage value. seems like while allocating avframe the frame is getting garbage data from some where. – Imrank Feb 05 '13 at 13:43
  • Try to initialize variable `got_frame = 0` before calling `avcodec_decode_video2` – pogorskiy Feb 05 '13 at 14:24
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/23978/discussion-between-imran-and-pogorskiy) – Imrank Feb 05 '13 at 16:53