]> git.sesse.net Git - casparcg/blobdiff - dependencies64/ffmpeg/doc/examples/demuxing_decoding.c
Fixed bug in server shut down.
[casparcg] / dependencies64 / ffmpeg / doc / examples / demuxing_decoding.c
index 077fc87b5b9495cdfed361746be20c0a6799c9c2..feeeb967f82e71e76fb4724010547133b827f143 100644 (file)
@@ -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,20 @@ 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) {
+            /* To handle this change, one could call av_image_alloc again and
+             * decode the following frames into another rawvideo file. */
+            fprintf(stderr, "Error: Width, height and pixel format have to be "
+                    "constant in a rawvideo file, but the width, height or "
+                    "pixel format of the input video 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 +106,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);
@@ -138,7 +154,7 @@ static int decode_packet(int *got_frame, int cached)
 static int open_codec_context(int *stream_idx,
                               AVFormatContext *fmt_ctx, enum AVMediaType type)
 {
-    int ret;
+    int ret, stream_index;
     AVStream *st;
     AVCodecContext *dec_ctx = NULL;
     AVCodec *dec = NULL;
@@ -150,8 +166,8 @@ static int open_codec_context(int *stream_idx,
                 av_get_media_type_string(type), src_filename);
         return ret;
     } else {
-        *stream_idx = ret;
-        st = fmt_ctx->streams[*stream_idx];
+        stream_index = ret;
+        st = fmt_ctx->streams[stream_index];
 
         /* find decoder for the stream */
         dec_ctx = st->codec;
@@ -170,6 +186,7 @@ static int open_codec_context(int *stream_idx,
                     av_get_media_type_string(type));
             return ret;
         }
+        *stream_idx = stream_index;
     }
 
     return 0;
@@ -264,9 +281,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;
@@ -279,7 +298,7 @@ int main (int argc, char **argv)
         audio_dec_ctx = audio_stream->codec;
         audio_dst_file = fopen(audio_dst_filename, "wb");
         if (!audio_dst_file) {
-            fprintf(stderr, "Could not open destination file %s\n", video_dst_filename);
+            fprintf(stderr, "Could not open destination file %s\n", audio_dst_filename);
             ret = 1;
             goto end;
         }
@@ -341,7 +360,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);
     }