[Ffmpeg-devel] [PATCH] fixing output_example to work with current CVS libavcodec/libavformat

Andreas U. Trottmann andreas.trottmann
Wed Jul 6 11:58:14 CEST 2005


Hello ffmpeg-devel,

It looks like output_example.c got out of sync with recent
libavcodec/libavformat changes. I have identified three issues
that made it fail on various stages, and it looks like I was able
to fix them.


The first issue was that output_example.c didn't set any pix_fmt
for the codec. This made it fail with "only YUV420 is supported",
and was the issue that a couple of people seem to have ran
accross on ffmpeg-user. I fixed it by setting c->pix_fmt to
PIX_FMT_YUV420P (configurable in a #define).


The second issue was that the pts calculations were not scaled
correctly, making it produce very wrong output for mpeg files,
for example. I fixed it by using the appropriate av_rescale_q on
pkt.pts.


The third issue was that passing a NULL picture pointer to
avcodec_encode_video wouldn't produce any output anymore, so
output_example.c would then spin endlessly in the "frame_count >=
STREAM_NB_FRAMES" case. I fixed this by just feeding the last
frame again in this case, getting rid of the now unused temporary
variable picture_ptr at the same time.


I was able to use the patched output_example.c to produce working
MPEG-1 (.mpg), MPEG-2 (.vob), MPEG-4 (.avi) and MSMPEG4 (.asf)
files.


Please feel free to apply the attached patch if you think it
fixes the problems in a correct way.

-- 
Andreas Trottmann
Werft22 AG
-------------- next part --------------
Index: output_example.c
===================================================================
RCS file: /cvsroot/ffmpeg/ffmpeg/output_example.c,v
retrieving revision 1.13
diff -u -r1.13 output_example.c
--- output_example.c	30 Apr 2005 21:43:55 -0000	1.13
+++ output_example.c	6 Jul 2005 09:35:12 -0000
@@ -37,6 +37,7 @@
 #define STREAM_DURATION   5.0
 #define STREAM_FRAME_RATE 25 /* 25 images/s */
 #define STREAM_NB_FRAMES  ((int)(STREAM_DURATION * STREAM_FRAME_RATE))
+#define STREAM_PIX_FMT PIX_FMT_YUV420P /* default pix_fmt */
 
 /**************************************************************/
 /* audio output */
@@ -150,7 +151,7 @@
 
     pkt.size= avcodec_encode_audio(c, audio_outbuf, audio_outbuf_size, samples);
 
-    pkt.pts= c->coded_frame->pts;
+    pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
     pkt.flags |= PKT_FLAG_KEY;
     pkt.stream_index= st->index;
     pkt.data= audio_outbuf;
@@ -202,6 +203,7 @@
     c->time_base.den = STREAM_FRAME_RATE;  
     c->time_base.num = 1;
     c->gop_size = 12; /* emit one intra frame every twelve frames at most */
+    c->pix_fmt = STREAM_PIX_FMT;
     if (c->codec_id == CODEC_ID_MPEG2VIDEO) {
         /* just for testing, we also add B frames */
         c->max_b_frames = 2;
@@ -314,15 +316,13 @@
 {
     int out_size, ret;
     AVCodecContext *c;
-    AVFrame *picture_ptr;
     
     c = &st->codec;
     
     if (frame_count >= STREAM_NB_FRAMES) {
         /* no more frame to compress. The codec has a latency of a few
            frames if using B frames, so we get the last frames by
-           passing a NULL picture */
-        picture_ptr = NULL;
+           passing the same picture again */
     } else {
         if (c->pix_fmt != PIX_FMT_YUV420P) {
             /* as we only generate a YUV420P picture, we must convert it
@@ -334,7 +334,6 @@
         } else {
             fill_yuv_image(picture, frame_count, c->width, c->height);
         }
-        picture_ptr = picture;
     }
 
     
@@ -346,19 +345,19 @@
         
         pkt.flags |= PKT_FLAG_KEY;
         pkt.stream_index= st->index;
-        pkt.data= (uint8_t *)picture_ptr;
+        pkt.data= (uint8_t *)picture;
         pkt.size= sizeof(AVPicture);
         
         ret = av_write_frame(oc, &pkt);
     } else {
         /* encode the image */
-        out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, picture_ptr);
+        out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, picture);
         /* if zero size, it means the image was buffered */
         if (out_size != 0) {
             AVPacket pkt;
             av_init_packet(&pkt);
             
-            pkt.pts= c->coded_frame->pts;
+            pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
             if(c->coded_frame->key_frame)
                 pkt.flags |= PKT_FLAG_KEY;
             pkt.stream_index= st->index;



More information about the ffmpeg-devel mailing list