Hi,

the demuxing_decoding example crashes, if width, height or pixel format of the video stream change.

Attached patch fixes this.

Best regards,
Andreas
>From 37979054edd30f3c0ea60c2103c30a18fbbc9448 Mon Sep 17 00:00:00 2001
From: Andreas Cadhalpun <andreas.cadhal...@googlemail.com>
Date: Sat, 31 Jan 2015 20:58:07 +0100
Subject: [PATCH 1/2] examples/demuxing_decoding: abort decoding when width,
 height or pix_fmt change

This is necessary, because avcodec_decode_video2 can change
width, height and/or pixel format of the AVCodecContext. Since
video_dst_data and video_dst_linesize are not updated by calling
av_image_alloc again, av_image_copy[_plane] asserts, because the
destination buffer is too small.

In this case, creating a useable rawvideo is not possible anyway, since
it has fixed width/height/pix_fmt.

Signed-off-by: Andreas Cadhalpun <andreas.cadhal...@googlemail.com>
---
 doc/examples/demuxing_decoding.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/doc/examples/demuxing_decoding.c b/doc/examples/demuxing_decoding.c
index 2ce4018..905df01 100644
--- a/doc/examples/demuxing_decoding.c
+++ b/doc/examples/demuxing_decoding.c
@@ -36,6 +36,8 @@
 
 static AVFormatContext *fmt_ctx = NULL;
 static AVCodecContext *video_dec_ctx = NULL, *audio_dec_ctx;
+static int width, height;
+static enum AVPixelFormat pix_fmt;
 static AVStream *video_stream = NULL, *audio_stream = NULL;
 static const char *src_filename = NULL;
 static const char *video_dst_filename = NULL;
@@ -79,6 +81,16 @@ static int decode_packet(int *got_frame, int cached)
             fprintf(stderr, "Error decoding video frame (%s)\n", av_err2str(ret));
             return ret;
         }
+        if (video_dec_ctx->width != width || video_dec_ctx->height != height ||
+            video_dec_ctx->pix_fmt != pix_fmt) {
+            fprintf(stderr, "Error: input video width/height/format changed:\n"
+                    "old: width = %d, height = %d, format = %s\n"
+                    "new: width = %d, height = %d, format = %s\n",
+                    width, height, av_get_pix_fmt_name(pix_fmt),
+                    video_dec_ctx->width, video_dec_ctx->height,
+                    av_get_pix_fmt_name(video_dec_ctx->pix_fmt));
+            return -1;
+        }
 
         if (*got_frame) {
             printf("video_frame%s n:%d coded_n:%d pts:%s\n",
@@ -90,7 +102,7 @@ static int decode_packet(int *got_frame, int cached)
              * this is required since rawvideo expects non aligned data */
             av_image_copy(video_dst_data, video_dst_linesize,
                           (const uint8_t **)(frame->data), frame->linesize,
-                          video_dec_ctx->pix_fmt, video_dec_ctx->width, video_dec_ctx->height);
+                          pix_fmt, width, height);
 
             /* write to rawvideo file */
             fwrite(video_dst_data[0], 1, video_dst_bufsize, video_dst_file);
@@ -264,9 +276,11 @@ int main (int argc, char **argv)
         }
 
         /* allocate image where the decoded image will be put */
+        width = video_dec_ctx->width;
+        height = video_dec_ctx->height;
+        pix_fmt = video_dec_ctx->pix_fmt;
         ret = av_image_alloc(video_dst_data, video_dst_linesize,
-                             video_dec_ctx->width, video_dec_ctx->height,
-                             video_dec_ctx->pix_fmt, 1);
+                             width, height, pix_fmt, 1);
         if (ret < 0) {
             fprintf(stderr, "Could not allocate raw video buffer\n");
             goto end;
@@ -341,7 +355,7 @@ int main (int argc, char **argv)
     if (video_stream) {
         printf("Play the output video file with the command:\n"
                "ffplay -f rawvideo -pix_fmt %s -video_size %dx%d %s\n",
-               av_get_pix_fmt_name(video_dec_ctx->pix_fmt), video_dec_ctx->width, video_dec_ctx->height,
+               av_get_pix_fmt_name(pix_fmt), width, height,
                video_dst_filename);
     }
 
-- 
2.1.4

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel

Reply via email to