[Libav-user] H.264 decoding picture does not display properly

김희숙 hskim095 at naver.com
Tue Sep 20 12:33:22 CEST 2011


Hi All,
 
H.264 decoding picture does not display properly.
I refer to output-example.c in ffmpeg and SDL display example.
SDL does not display a picture. I can see gray color only.
Are there any missing point to decode H.264?
 
I'm writing my code as below.
When I tested H.263 decoding, there was no problem.
 
Thank you for your help.
 
-------------------------------------------------
codec = avcodec_find_decoder(CODEC_ID_H264);
if (!codec) {
 fprintf(stderr, "codec not found\n");
 exit(1);
 }
dec_picture= avcodec_alloc_frame();
c= avcodec_alloc_context();
c->codec_type = AVMEDIA_TYPE_VIDEO;
c->codec_id = CODEC_ID_H264;
c->width = width;
c->height = height;
c->pix_fmt = PIX_FMT_YUV420P; 
c->bits_per_raw_sample = 8;
if(codec->capabilities&CODEC_CAP_TRUNCATED)
 c->flags|= CODEC_FLAG_TRUNCATED; /* we do not send complete frames */
 /* open it */
 if (avcodec_open(c, codec) < 0) {
 fprintf(stderr, "could not open codec\n");
 exit(1);
 }
 
 // Allocate an AVFrame structure
 pFrameRGB = avcodec_alloc_frame();
 if (pFrameRGB == NULL) return;
 numBytes = avpicture_get_size(PIX_FMT_RGB24, width,height);
 buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
 avpicture_fill((AVPicture *) pFrameRGB, buffer, PIX_FMT_RGB24,width, height);
 
 f = fopen("h264.sample", "rb");
 if (!f) {
 fprintf(stderr, "could not open %s\n", "sample file");
 exit(1);
 }
 
for(;;) {
 len = fread(inbuf, 1, INBUF_SIZE, f);
 if (len == 0)
 break;
 
 av_new_packet(&avpkt, len);
 avpkt.data = inbuf;
 
 while (avpkt.size > 0) { 
 len = avcodec_decode_video2(c, dec_picture, &got_picture, &avpkt);
 if (len < 0) {
 fprintf(stderr, "Error while decoding frame %d\n", frame);
 // exit(1);
 break;
 }
 if (got_picture) {
/////////////////////////SDL Copy Start//////////////////////////// 
 SDL_LockYUVOverlay(bmp);
// AVPicture pict;
 pict.data[0] = bmp->pixels[0];
 pict.data[1] = bmp->pixels[2];
 pict.data[2] = bmp->pixels[1];
 pict.linesize[0] = bmp->pitches[0];
 pict.linesize[1] = bmp->pitches[2];
 pict.linesize[2] = bmp->pitches[1];
/////////////////////////SDL Copy End////////////////////////////
 img_convert_ctx = sws_getContext(c->width,c->height, PIX_FMT_YUV420P,
 c ->width,c->height, PIX_FMT_YUV420P,
 sws_flags, NULL, NULL, NULL);
 sws_scale(img_convert_ctx, dec_picture->data, dec_picture->linesize, 0,
 c->height, pict.data, pict.linesize);
 sws_freeContext(img_convert_ctx);
 SDL_UnlockYUVOverlay(bmp);
 rect.x = 0;
 rect.y = 0;
 rect.w = c->width;
 rect.h = c->height;
 SDL_DisplayYUVOverlay(bmp, &rect)
 }
 avpkt.size -= len;
 avpkt.data += len;
 } 
 } //end of for();
 
Best Regards,
HSK
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20110920/a50b6940/attachment.html>


More information about the Libav-user mailing list