]> git.sesse.net Git - ffmpeg/blobdiff - output_example.c
Move the encoding of the image data to its own function.
[ffmpeg] / output_example.c
index d1cb754a0ba5b6a2f39cb59de9b5d1ae84545236..ca12b783f1ae360ddea349f28cd2e85370cef846 100644 (file)
 #include <math.h>
 
 #ifndef M_PI
-#define M_PI 3.1415926535897931
+#define M_PI 3.14159265358979323846
 #endif
 
 #include "avformat.h"
+#include "swscale.h"
 
 /* 5 seconds stream duration */
 #define STREAM_DURATION   5.0
@@ -39,6 +40,8 @@
 #define STREAM_NB_FRAMES  ((int)(STREAM_DURATION * STREAM_FRAME_RATE))
 #define STREAM_PIX_FMT PIX_FMT_YUV420P /* default pix_fmt */
 
+static int sws_flags = SWS_BICUBIC;
+
 /**************************************************************/
 /* audio output */
 
@@ -51,7 +54,7 @@ int audio_input_frame_size;
 /*
  * add an audio output stream
  */
-AVStream *add_audio_stream(AVFormatContext *oc, int codec_id)
+static AVStream *add_audio_stream(AVFormatContext *oc, int codec_id)
 {
     AVCodecContext *c;
     AVStream *st;
@@ -73,7 +76,7 @@ AVStream *add_audio_stream(AVFormatContext *oc, int codec_id)
     return st;
 }
 
-void open_audio(AVFormatContext *oc, AVStream *st)
+static void open_audio(AVFormatContext *oc, AVStream *st)
 {
     AVCodecContext *c;
     AVCodec *codec;
@@ -100,7 +103,7 @@ void open_audio(AVFormatContext *oc, AVStream *st)
     tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;
 
     audio_outbuf_size = 10000;
-    audio_outbuf = malloc(audio_outbuf_size);
+    audio_outbuf = av_malloc(audio_outbuf_size);
 
     /* ugly hack for PCM codecs (will be removed ASAP with new PCM
        support to compute the input frame size in samples */
@@ -119,12 +122,12 @@ void open_audio(AVFormatContext *oc, AVStream *st)
     } else {
         audio_input_frame_size = c->frame_size;
     }
-    samples = malloc(audio_input_frame_size * 2 * c->channels);
+    samples = av_malloc(audio_input_frame_size * 2 * c->channels);
 }
 
 /* prepare a 16 bit dummy audio frame of 'frame_size' samples and
    'nb_channels' channels */
-void get_audio_frame(int16_t *samples, int frame_size, int nb_channels)
+static void get_audio_frame(int16_t *samples, int frame_size, int nb_channels)
 {
     int j, i, v;
     int16_t *q;
@@ -139,7 +142,7 @@ void get_audio_frame(int16_t *samples, int frame_size, int nb_channels)
     }
 }
 
-void write_audio_frame(AVFormatContext *oc, AVStream *st)
+static void write_audio_frame(AVFormatContext *oc, AVStream *st)
 {
     AVCodecContext *c;
     AVPacket pkt;
@@ -163,7 +166,7 @@ void write_audio_frame(AVFormatContext *oc, AVStream *st)
     }
 }
 
-void close_audio(AVFormatContext *oc, AVStream *st)
+static void close_audio(AVFormatContext *oc, AVStream *st)
 {
     avcodec_close(st->codec);
 
@@ -179,7 +182,7 @@ uint8_t *video_outbuf;
 int frame_count, video_outbuf_size;
 
 /* add a video output stream */
-AVStream *add_video_stream(AVFormatContext *oc, int codec_id)
+static AVStream *add_video_stream(AVFormatContext *oc, int codec_id)
 {
     AVCodecContext *c;
     AVStream *st;
@@ -217,14 +220,14 @@ AVStream *add_video_stream(AVFormatContext *oc, int codec_id)
            motion of the chroma plane doesnt match the luma plane */
         c->mb_decision=2;
     }
-    // some formats want stream headers to be seperate
+    // some formats want stream headers to be separate
     if(!strcmp(oc->oformat->name, "mp4") || !strcmp(oc->oformat->name, "mov") || !strcmp(oc->oformat->name, "3gp"))
         c->flags |= CODEC_FLAG_GLOBAL_HEADER;
 
     return st;
 }
 
-AVFrame *alloc_picture(int pix_fmt, int width, int height)
+static AVFrame *alloc_picture(int pix_fmt, int width, int height)
 {
     AVFrame *picture;
     uint8_t *picture_buf;
@@ -234,7 +237,7 @@ AVFrame *alloc_picture(int pix_fmt, int width, int height)
     if (!picture)
         return NULL;
     size = avpicture_get_size(pix_fmt, width, height);
-    picture_buf = malloc(size);
+    picture_buf = av_malloc(size);
     if (!picture_buf) {
         av_free(picture);
         return NULL;
@@ -244,7 +247,7 @@ AVFrame *alloc_picture(int pix_fmt, int width, int height)
     return picture;
 }
 
-void open_video(AVFormatContext *oc, AVStream *st)
+static void open_video(AVFormatContext *oc, AVStream *st)
 {
     AVCodec *codec;
     AVCodecContext *c;
@@ -268,8 +271,12 @@ void open_video(AVFormatContext *oc, AVStream *st)
     if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) {
         /* allocate output buffer */
         /* XXX: API change will be done */
+        /* buffers passed into lav* can be allocated any way you prefer,
+           as long as they're aligned enough for the architecture, and
+           they're freed appropriately (such as using av_free for buffers
+           allocated with av_malloc) */
         video_outbuf_size = 200000;
-        video_outbuf = malloc(video_outbuf_size);
+        video_outbuf = av_malloc(video_outbuf_size);
     }
 
     /* allocate the encoded raw picture */
@@ -293,7 +300,7 @@ void open_video(AVFormatContext *oc, AVStream *st)
 }
 
 /* prepare a dummy image */
-void fill_yuv_image(AVFrame *pict, int frame_index, int width, int height)
+static void fill_yuv_image(AVFrame *pict, int frame_index, int width, int height)
 {
     int x, y, i;
 
@@ -315,10 +322,11 @@ void fill_yuv_image(AVFrame *pict, int frame_index, int width, int height)
     }
 }
 
-void write_video_frame(AVFormatContext *oc, AVStream *st)
+static void write_video_frame(AVFormatContext *oc, AVStream *st)
 {
     int out_size, ret;
     AVCodecContext *c;
+    static struct SwsContext *img_convert_ctx;
 
     c = st->codec;
 
@@ -330,10 +338,20 @@ void write_video_frame(AVFormatContext *oc, AVStream *st)
         if (c->pix_fmt != 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,
+                                                 PIX_FMT_YUV420P,
+                                                 c->width, c->height,
+                                                 c->pix_fmt,
+                                                 sws_flags, NULL, NULL, NULL);
+                if (img_convert_ctx == NULL) {
+                    fprintf(stderr, "Cannot initialize the conversion context\n");
+                    exit(1);
+                }
+            }
             fill_yuv_image(tmp_picture, frame_count, c->width, c->height);
-            img_convert((AVPicture *)picture, c->pix_fmt,
-                        (AVPicture *)tmp_picture, PIX_FMT_YUV420P,
-                        c->width, c->height);
+            sws_scale(img_convert_ctx, tmp_picture->data, tmp_picture->linesize,
+                      0, c->height, picture->data, picture->linesize);
         } else {
             fill_yuv_image(picture, frame_count, c->width, c->height);
         }
@@ -380,7 +398,7 @@ void write_video_frame(AVFormatContext *oc, AVStream *st)
     frame_count++;
 }
 
-void close_video(AVFormatContext *oc, AVStream *st)
+static void close_video(AVFormatContext *oc, AVStream *st)
 {
     avcodec_close(st->codec);
     av_free(picture->data[0]);
@@ -512,6 +530,7 @@ int main(int argc, char **argv)
 
     /* free the streams */
     for(i = 0; i < oc->nb_streams; i++) {
+        av_freep(&oc->streams[i]->codec);
         av_freep(&oc->streams[i]);
     }