[FFmpeg-cvslog] output example: store the scaling context in the stream context

Anton Khirnov git at videolan.org
Fri Jun 27 00:21:48 CEST 2014


ffmpeg | branch: master | Anton Khirnov <anton at khirnov.net> | Tue Jun 24 10:21:07 2014 +0200| [a7fcd4122b19b0f934020f4e261d0c44c4c32e11] | committer: Anton Khirnov

output example: store the scaling context in the stream context

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=a7fcd4122b19b0f934020f4e261d0c44c4c32e11
---

 doc/examples/output.c |   20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/doc/examples/output.c b/doc/examples/output.c
index ffc6e55..d8321be 100644
--- a/doc/examples/output.c
+++ b/doc/examples/output.c
@@ -55,6 +55,8 @@ typedef struct OutputStream {
     AVFrame *tmp_frame;
 
     float t, tincr, tincr2;
+
+    struct SwsContext *sws_ctx;
 } OutputStream;
 
 /**************************************************************/
@@ -327,7 +329,6 @@ static void write_video_frame(AVFormatContext *oc, OutputStream *ost)
 {
     int ret;
     AVCodecContext *c;
-    static struct SwsContext *img_convert_ctx;
 
     c = ost->st->codec;
 
@@ -339,20 +340,20 @@ static void write_video_frame(AVFormatContext *oc, OutputStream *ost)
         if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
             /* as we only generate a YUV420P picture, we must convert it
              * to the codec pixel format if needed */
-            if (img_convert_ctx == NULL) {
-                img_convert_ctx = sws_getContext(c->width, c->height,
-                                                 AV_PIX_FMT_YUV420P,
-                                                 c->width, c->height,
-                                                 c->pix_fmt,
-                                                 SCALE_FLAGS, NULL, NULL, NULL);
-                if (img_convert_ctx == NULL) {
+            if (!ost->sws_ctx) {
+                ost->sws_ctx = sws_getContext(c->width, c->height,
+                                              AV_PIX_FMT_YUV420P,
+                                              c->width, c->height,
+                                              c->pix_fmt,
+                                              SCALE_FLAGS, NULL, NULL, NULL);
+                if (!ost->sws_ctx) {
                     fprintf(stderr,
                             "Cannot initialize the conversion context\n");
                     exit(1);
                 }
             }
             fill_yuv_image(ost->tmp_frame, frame_count, c->width, c->height);
-            sws_scale(img_convert_ctx, ost->tmp_frame->data, ost->tmp_frame->linesize,
+            sws_scale(ost->sws_ctx, ost->tmp_frame->data, ost->tmp_frame->linesize,
                       0, c->height, ost->frame->data, ost->frame->linesize);
         } else {
             fill_yuv_image(ost->frame, frame_count, c->width, c->height);
@@ -401,6 +402,7 @@ static void close_stream(AVFormatContext *oc, OutputStream *ost)
     avcodec_close(ost->st->codec);
     av_frame_free(&ost->frame);
     av_frame_free(&ost->tmp_frame);
+    sws_freeContext(ost->sws_ctx);
 }
 
 /**************************************************************/



More information about the ffmpeg-cvslog mailing list