avconv: use different variables for decoded and filtered frame.
authorAnton Khirnov <anton@khirnov.net>
Fri, 23 Sep 2011 14:17:42 +0000 (16:17 +0200)
committerAnton Khirnov <anton@khirnov.net>
Tue, 27 Sep 2011 17:17:37 +0000 (19:17 +0200)
Makes the code less obfuscated and fixes encoding one video stream to
several outputs.

Also use avcodec_alloc_frame() instead of allocating AVFrame on stack.

Breaks me_threshold in avconv, as motion vectors aren't passed through
lavfi. They could be copied manually, but I don't think this misfeature
is useful enough to justify ugly hacks.

avconv.c
tests/codec-regression.sh
tests/ref/vsynth1/mpeg2thread
tests/ref/vsynth2/mpeg2thread

index 9d2ad51..ed5d709 100644 (file)
--- a/avconv.c
+++ b/avconv.c
@@ -1520,7 +1520,6 @@ static int output_packet(InputStream *ist, int ist_index,
     OutputStream *ost;
     int ret, i;
     int got_output;
-    AVFrame picture;
     void *buffer_to_free = NULL;
     static unsigned int samples_size= 0;
     AVSubtitle subtitle, *subtitle_to_free;
@@ -1555,6 +1554,7 @@ static int output_packet(InputStream *ist, int ist_index,
     while (avpkt.size > 0 || (!pkt && got_output)) {
         uint8_t *data_buf, *decoded_data_buf;
         int data_size, decoded_data_size;
+        AVFrame *decoded_frame, *filtered_frame;
     handle_eof:
         ist->pts= ist->next_pts;
 
@@ -1564,6 +1564,7 @@ static int output_packet(InputStream *ist, int ist_index,
             ist->showed_multi_packet_warning=1;
 
         /* decode the packet if needed */
+        decoded_frame    = filtered_frame = NULL;
         decoded_data_buf = NULL; /* fail safe */
         decoded_data_size= 0;
         data_buf  = avpkt.data;
@@ -1600,22 +1601,24 @@ static int output_packet(InputStream *ist, int ist_index,
                 break;}
             case AVMEDIA_TYPE_VIDEO:
                     decoded_data_size = (ist->st->codec->width * ist->st->codec->height * 3) / 2;
-                    /* XXX: allocate picture correctly */
-                    avcodec_get_frame_defaults(&picture);
+                    if (!(decoded_frame = avcodec_alloc_frame()))
+                        return AVERROR(ENOMEM);
                     avpkt.pts = pkt_pts;
                     avpkt.dts = ist->pts;
                     pkt_pts = AV_NOPTS_VALUE;
 
                     ret = avcodec_decode_video2(ist->st->codec,
-                                                &picture, &got_output, &avpkt);
-                    quality = same_quant ? picture.quality : 0;
+                                                decoded_frame, &got_output, &avpkt);
+                    quality = same_quant ? decoded_frame->quality : 0;
                     if (ret < 0)
-                        return ret;
+                        goto fail;
                     if (!got_output) {
                         /* no picture yet */
+                        av_freep(&decoded_frame);
                         goto discard_packet;
                     }
-                    ist->next_pts = ist->pts = guess_correct_pts(&ist->pts_ctx, picture.pkt_pts, picture.pkt_dts);
+                    ist->next_pts = ist->pts = guess_correct_pts(&ist->pts_ctx, decoded_frame->pkt_pts,
+                                                                 decoded_frame->pkt_dts);
                     if (ist->st->codec->time_base.num != 0) {
                         int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame;
                         ist->next_pts += ((int64_t)AV_TIME_BASE *
@@ -1624,7 +1627,7 @@ static int output_packet(InputStream *ist, int ist_index,
                     }
                     avpkt.size = 0;
                     buffer_to_free = NULL;
-                    pre_process_video_frame(ist, (AVPicture *)&picture, &buffer_to_free);
+                    pre_process_video_frame(ist, (AVPicture *)decoded_frame, &buffer_to_free);
                     break;
             case AVMEDIA_TYPE_SUBTITLE:
                 ret = avcodec_decode_subtitle2(ist->st->codec,
@@ -1705,16 +1708,22 @@ static int output_packet(InputStream *ist, int ist_index,
                     sar = ist->st->sample_aspect_ratio;
                 else
                     sar = ist->st->codec->sample_aspect_ratio;
-                av_vsrc_buffer_add_frame(ost->input_video_filter, &picture, ist->pts, sar);
+                av_vsrc_buffer_add_frame(ost->input_video_filter, decoded_frame, ist->pts, sar);
+                if (!(filtered_frame = avcodec_alloc_frame())) {
+                    ret = AVERROR(ENOMEM);
+                    goto fail;
+                }
             }
             frame_available = ist->st->codec->codec_type != AVMEDIA_TYPE_VIDEO ||
                 !ost->output_video_filter || avfilter_poll_frame(ost->output_video_filter->inputs[0]);
             while (frame_available) {
                 AVRational ist_pts_tb;
                 if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && ost->output_video_filter)
-                    get_filtered_video_frame(ost->output_video_filter, &picture, &ost->picref, &ist_pts_tb);
+                    get_filtered_video_frame(ost->output_video_filter, filtered_frame, &ost->picref, &ist_pts_tb);
                 if (ost->picref)
                     ist->pts = av_rescale_q(ost->picref->pts, ist_pts_tb, AV_TIME_BASE_Q);
+#else
+                filtered_frame = decoded_frame;
 #endif
                 os = output_files[ost->file_index].ctx;
 
@@ -1732,7 +1741,7 @@ static int output_packet(InputStream *ist, int ist_index,
                         if (ost->picref->video && !ost->frame_aspect_ratio)
                             ost->st->codec->sample_aspect_ratio = ost->picref->video->pixel_aspect;
 #endif
-                        do_video_out(os, ost, ist, &picture, &frame_size,
+                        do_video_out(os, ost, ist, filtered_frame, &frame_size,
                                      same_quant ? quality : ost->st->codec->global_quality);
                         if (vstats_filename && frame_size)
                             do_video_stats(os, ost, frame_size);
@@ -1806,15 +1815,20 @@ static int output_packet(InputStream *ist, int ist_index,
                 if (ost->picref)
                     avfilter_unref_buffer(ost->picref);
             }
+            av_freep(&filtered_frame);
 #endif
             }
 
+fail:
         av_free(buffer_to_free);
         /* XXX: allocate the subtitles in the codec ? */
         if (subtitle_to_free) {
             avsubtitle_free(subtitle_to_free);
             subtitle_to_free = NULL;
         }
+        av_freep(&decoded_frame);
+        if (ret < 0)
+            return ret;
     }
  discard_packet:
 
index 164550b..cafa847 100755 (executable)
@@ -55,11 +55,6 @@ do_video_decoding
 # mpeg2 encoding interlaced using intra vlc
 do_video_encoding mpeg2threadivlc.mpg "-qscale 10 -vcodec mpeg2video -f mpeg1video -bf 2 -flags +ildct+ilme -flags2 +ivlc -threads 2"
 do_video_decoding
-
-# mpeg2 encoding interlaced
-file=${outfile}mpeg2reuse.mpg
-do_avconv $file $DEC_OPTS -me_threshold 256 -i ${target_path}/${outfile}mpeg2thread.mpg $ENC_OPTS -same_quant -me_threshold 256 -mb_threshold 1024 -vcodec mpeg2video -vsync 0 -f mpeg1video -bf 2 -flags +ildct+ilme -threads 4
-do_video_decoding
 fi
 
 if [ -n "$do_msmpeg4v2" ] ; then
index 470c2e9..e32007c 100644 (file)
@@ -6,7 +6,3 @@ stddev:    7.63 PSNR: 30.48 MAXDIFF:  110 bytes:  7603200/  7603200
 791773 ./tests/data/vsynth1/mpeg2threadivlc.mpg
 d1658911ca83f5616c1d32abc40750de *./tests/data/mpeg2thread.vsynth1.out.yuv
 stddev:    7.63 PSNR: 30.48 MAXDIFF:  110 bytes:  7603200/  7603200
-d119fe917dd81d1ff758b4ce684a8d9d *./tests/data/vsynth1/mpeg2reuse.mpg
-2074636 ./tests/data/vsynth1/mpeg2reuse.mpg
-92ced6afe8c02304943c400cce51a5f4 *./tests/data/mpeg2thread.vsynth1.out.yuv
-stddev:    7.66 PSNR: 30.44 MAXDIFF:  111 bytes:  7603200/  7603200
index 3c9c0d0..6d86735 100644 (file)
@@ -6,7 +6,3 @@ stddev:    4.72 PSNR: 34.65 MAXDIFF:   72 bytes:  7603200/  7603200
 178801 ./tests/data/vsynth2/mpeg2threadivlc.mpg
 8c6a7ed2eb73bd18fd2bb9829464100d *./tests/data/mpeg2thread.vsynth2.out.yuv
 stddev:    4.72 PSNR: 34.65 MAXDIFF:   72 bytes:  7603200/  7603200
-864d6bf2982a61e510003a518be65a2d *./tests/data/vsynth2/mpeg2reuse.mpg
-383419 ./tests/data/vsynth2/mpeg2reuse.mpg
-bb20fa080cfd2b0a687ea7376ff4f902 *./tests/data/mpeg2thread.vsynth2.out.yuv
-stddev:    4.73 PSNR: 34.63 MAXDIFF:   72 bytes:  7603200/  7603200