changeset 36753:b3afe52119c4

vf_screenshot: Switch to avcodec_encode_video2 API.
author reimar
date Sun, 16 Feb 2014 14:47:32 +0000
parents af146b0d33f0
children 122de2aa583b
files libmpcodecs/vf_screenshot.c
diffstat 1 files changed, 21 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/libmpcodecs/vf_screenshot.c	Sun Feb 16 14:01:23 2014 +0000
+++ b/libmpcodecs/vf_screenshot.c	Sun Feb 16 14:47:32 2014 +0000
@@ -50,8 +50,8 @@
     /// 1: take single screenshot, reset to 0 afterwards
     /// 2: take screenshots of each frame
     int shot, store_slices;
-    int dw, dh, stride;
-    uint8_t *buffer;
+    int dw, dh;
+    AVFrame *pic;
     struct SwsContext *ctx;
     AVCodecContext *avctx;
     uint8_t *outbuffer;
@@ -64,11 +64,7 @@
                        int* stride, int w,int h, int x, int y)
 {
     if (vf->priv->store_slices) {
-        uint8_t *dst[MP_MAX_PLANES] = {NULL};
-        int dst_stride[MP_MAX_PLANES] = {0};
-        dst_stride[0] = vf->priv->stride;
-        dst[0] = vf->priv->buffer;
-        sws_scale(vf->priv->ctx, src, stride, y, h, dst, dst_stride);
+        sws_scale(vf->priv->ctx, src, stride, y, h, vf->priv->pic->data, vf->priv->pic->linesize);
     }
     vf_next_draw_slice(vf,src,stride,w,h,x,y);
 }
@@ -88,10 +84,9 @@
     vf->priv->avctx->compression_level = 0;
     vf->priv->dw = d_width;
     vf->priv->dh = d_height;
-    vf->priv->stride = (3*vf->priv->dw+15)&~15;
+    vf->priv->pic->linesize[0] = (3*vf->priv->dw+15)&~15;
 
-    free(vf->priv->buffer); // probably reconfigured
-    vf->priv->buffer = NULL;
+    av_freep(&vf->priv->pic->data[0]); // probably reconfigured
 
     res = vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
     // Our draw_slice only works properly if the
@@ -104,8 +99,8 @@
 {
     char *fname = priv->fname;
     FILE * fp;
-    AVFrame pic;
-    int size;
+    AVPacket pkt;
+    int res, got_pkt;
 
     fp = fopen (fname, "wb");
     if (fp == NULL) {
@@ -113,11 +108,12 @@
         return;
     }
 
-    pic.data[0] = priv->buffer;
-    pic.linesize[0] = priv->stride;
-    size = avcodec_encode_video(priv->avctx, priv->outbuffer, priv->outbuffer_size, &pic);
-    if (size > 0)
-        fwrite(priv->outbuffer, size, 1, fp);
+    av_init_packet(&pkt);
+    pkt.data = priv->outbuffer;
+    pkt.size = priv->outbuffer_size;
+    res = avcodec_encode_video2(priv->avctx, &pkt, priv->pic, &got_pkt);
+    if (res >= 0 && got_pkt && pkt.size > 0)
+        fwrite(priv->outbuffer, pkt.size, 1, fp);
 
     fclose (fp);
 }
@@ -144,15 +140,10 @@
 
 static void scale_image(struct vf_priv_s* priv, mp_image_t *mpi)
 {
-    uint8_t *dst[MP_MAX_PLANES] = {NULL};
-    int dst_stride[MP_MAX_PLANES] = {0};
+    if (!priv->pic->data[0])
+        priv->pic->data[0] = av_malloc(priv->pic->linesize[0]*priv->dh);
 
-    dst_stride[0] = priv->stride;
-    if (!priv->buffer)
-        priv->buffer = av_malloc(dst_stride[0]*priv->dh);
-
-    dst[0] = priv->buffer;
-    sws_scale(priv->ctx, mpi->planes, mpi->stride, 0, priv->dh, dst, dst_stride);
+    sws_scale(priv->ctx, mpi->planes, mpi->stride, 0, priv->dh, priv->pic->data, priv->pic->linesize);
 }
 
 static void start_slice(struct vf_instance *vf, mp_image_t *mpi)
@@ -162,8 +153,8 @@
         mpi->type, mpi->flags, mpi->width, mpi->height);
     if (vf->priv->shot) {
         vf->priv->store_slices = 1;
-        if (!vf->priv->buffer)
-            vf->priv->buffer = av_malloc(vf->priv->stride*vf->priv->dh);
+        if (!vf->priv->pic->data[0])
+            vf->priv->pic->data[0] = av_malloc(vf->priv->pic->linesize[0]*vf->priv->dh);
     }
 
 }
@@ -281,7 +272,8 @@
     avcodec_close(vf->priv->avctx);
     av_freep(&vf->priv->avctx);
     if(vf->priv->ctx) sws_freeContext(vf->priv->ctx);
-    av_free(vf->priv->buffer);
+    av_freep(&vf->priv->pic->data[0]);
+    av_frame_free(&vf->priv->pic);
     free(vf->priv->outbuffer);
     free(vf->priv->prefix);
     free(vf->priv);
@@ -298,11 +290,11 @@
     vf->get_image=get_image;
     vf->uninit=uninit;
     vf->priv=malloc(sizeof(struct vf_priv_s));
+    vf->priv->pic = av_frame_alloc();
     vf->priv->prefix = strdup(args ? args : "shot");
     vf->priv->frameno=0;
     vf->priv->shot=0;
     vf->priv->store_slices=0;
-    vf->priv->buffer=0;
     vf->priv->outbuffer=0;
     vf->priv->ctx=0;
     vf->priv->avctx = avcodec_alloc_context3(NULL);