]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/output-example.c
Replace all instances of avcodec_alloc_frame() with av_frame_alloc().
[ffmpeg] / libavformat / output-example.c
index e4babb49a5a6164e78e824d9090d035c9b929129..eb8cb7d8e32818337061f3c37be8894f022d7772 100644 (file)
@@ -38,8 +38,6 @@
 #include "libavformat/avformat.h"
 #include "libswscale/swscale.h"
 
-#undef exit
-
 /* 5 seconds stream duration */
 #define STREAM_DURATION   5.0
 #define STREAM_FRAME_RATE 25 /* 25 images/s */
@@ -140,7 +138,7 @@ static void write_audio_frame(AVFormatContext *oc, AVStream *st)
 {
     AVCodecContext *c;
     AVPacket pkt = { 0 }; // data and size must be 0;
-    AVFrame *frame = avcodec_alloc_frame();
+    AVFrame *frame = av_frame_alloc();
     int got_packet;
 
     av_init_packet(&pkt);
@@ -179,8 +177,7 @@ static void close_audio(AVFormatContext *oc, AVStream *st)
 /* video output */
 
 static AVFrame *picture, *tmp_picture;
-static uint8_t *video_outbuf;
-static int frame_count, video_outbuf_size;
+static int frame_count;
 
 /* Add a video output stream. */
 static AVStream *add_video_stream(AVFormatContext *oc, enum AVCodecID codec_id)
@@ -240,7 +237,7 @@ static AVFrame *alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
     uint8_t *picture_buf;
     int size;
 
-    picture = avcodec_alloc_frame();
+    picture = av_frame_alloc();
     if (!picture)
         return NULL;
     size        = avpicture_get_size(pix_fmt, width, height);
@@ -266,18 +263,6 @@ static void open_video(AVFormatContext *oc, AVStream *st)
         exit(1);
     }
 
-    video_outbuf = NULL;
-    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      = av_malloc(video_outbuf_size);
-    }
-
     /* Allocate the encoded raw picture. */
     picture = alloc_picture(c->pix_fmt, c->width, c->height);
     if (!picture) {
@@ -322,7 +307,7 @@ static void fill_yuv_image(AVFrame *pict, int frame_index,
 
 static void write_video_frame(AVFormatContext *oc, AVStream *st)
 {
-    int out_size, ret;
+    int ret;
     AVCodecContext *c;
     static struct SwsContext *img_convert_ctx;
 
@@ -369,22 +354,23 @@ static void write_video_frame(AVFormatContext *oc, AVStream *st)
 
         ret = av_interleaved_write_frame(oc, &pkt);
     } else {
+        AVPacket pkt = { 0 };
+        int got_packet;
+        av_init_packet(&pkt);
+
         /* encode the image */
-        out_size = avcodec_encode_video(c, video_outbuf,
-                                        video_outbuf_size, picture);
+        ret = avcodec_encode_video2(c, &pkt, picture, &got_packet);
         /* If size is zero, it means the image was buffered. */
-        if (out_size > 0) {
-            AVPacket pkt;
-            av_init_packet(&pkt);
-
-            if (c->coded_frame->pts != AV_NOPTS_VALUE)
-                pkt.pts = av_rescale_q(c->coded_frame->pts,
+        if (!ret && got_packet && pkt.size) {
+            if (pkt.pts != AV_NOPTS_VALUE) {
+                pkt.pts = av_rescale_q(pkt.pts,
+                                       c->time_base, st->time_base);
+            }
+            if (pkt.dts != AV_NOPTS_VALUE) {
+                pkt.dts = av_rescale_q(pkt.dts,
                                        c->time_base, st->time_base);
-            if (c->coded_frame->key_frame)
-                pkt.flags |= AV_PKT_FLAG_KEY;
+            }
             pkt.stream_index = st->index;
-            pkt.data         = video_outbuf;
-            pkt.size         = out_size;
 
             /* Write the compressed frame to the media file. */
             ret = av_interleaved_write_frame(oc, &pkt);
@@ -408,7 +394,6 @@ static void close_video(AVFormatContext *oc, AVStream *st)
         av_free(tmp_picture->data[0]);
         av_free(tmp_picture);
     }
-    av_free(video_outbuf);
 }
 
 /**************************************************************/