summaryrefslogtreecommitdiffstats
path: root/video/filter/vf_screenshot.c
blob: 693d871e5f2137cc520b3e71049d7ce5b7da2630 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * This file is part of MPlayer.
 *
 * MPlayer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * MPlayer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

#include "mp_msg.h"
#include "img_format.h"
#include "mp_image.h"
#include "vf.h"
#include "libmpcodecs/sws_utils.h"
#include "fmt-conversion.h"
#include "libvo/fastmemcpy.h"

#include <libswscale/swscale.h>

struct vf_priv_s {
    mp_image_t *image;
    void (*image_callback)(void *, mp_image_t *);
    void *image_callback_ctx;
    int shot, store_slices;
};

//===========================================================================//

static int config(struct vf_instance *vf,
                  int width, int height, int d_width, int d_height,
                  unsigned int flags, unsigned int outfmt)
{
    free_mp_image(vf->priv->image);
    vf->priv->image = new_mp_image(width, height);
    mp_image_setfmt(vf->priv->image, outfmt);
    vf->priv->image->w = d_width;
    vf->priv->image->h = d_height;
    return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
}

static void start_slice(struct vf_instance *vf, mp_image_t *mpi)
{
    mpi->priv=
    vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
        mpi->type, mpi->flags, mpi->width, mpi->height);
    if (vf->priv->shot) {
        vf->priv->store_slices = 1;
        if (!(vf->priv->image->flags & MP_IMGFLAG_ALLOCATED))
            mp_image_alloc_planes(vf->priv->image);
    }

}

static void memcpy_pic_slice(unsigned char *dst, unsigned char *src,
                             int bytesPerLine, int y, int h,
                             int dstStride, int srcStride)
{
    memcpy_pic(dst + h * dstStride, src + h * srcStride, bytesPerLine,
               h, dstStride, srcStride);
}

static void draw_slice(struct vf_instance *vf, unsigned char** src,
                       int* stride, int w,int h, int x, int y)
{
    if (vf->priv->store_slices) {
        mp_image_t *dst = vf->priv->image;
        int bp = (dst->bpp + 7) / 8;

        if (dst->flags & MP_IMGFLAG_PLANAR) {
            int bytes_per_line[3] = { w * bp, dst->chroma_width, dst->chroma_width };
            for (int n = 0; n < 3; n++) {
                memcpy_pic_slice(dst->planes[n], src[n], bytes_per_line[n],
                                 y, h, dst->stride[n], stride[n]);
            }
        } else {
            memcpy_pic_slice(dst->planes[0], src[0], dst->w*bp, y, dst->h,
                             dst->stride[0], stride[0]);
        }
    }
    vf_next_draw_slice(vf,src,stride,w,h,x,y);
}

static void get_image(struct vf_instance *vf, mp_image_t *mpi)
{
    // FIXME: should vf.c really call get_image when using slices??
    if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
      return;
    vf->dmpi= vf_get_image(vf->next, mpi->imgfmt,
                           mpi->type, mpi->flags/* | MP_IMGFLAG_READABLE*/, mpi->width, mpi->height);

    for (int i = 0; i < MP_MAX_PLANES; i++) {
        mpi->planes[i]=vf->dmpi->planes[i];
        mpi->stride[i]=vf->dmpi->stride[i];
    }
    mpi->width=vf->dmpi->width;

    mpi->flags|=MP_IMGFLAG_DIRECT;

    mpi->priv=(void*)vf->dmpi;
}

static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
{
    mp_image_t *dmpi = (mp_image_t *)mpi->priv;

    if(!(mpi->flags&(MP_IMGFLAG_DIRECT|MP_IMGFLAG_DRAW_CALLBACK))){
        dmpi=vf_get_image(vf->next,mpi->imgfmt,
                                    MP_IMGTYPE_EXPORT, 0,
                                    mpi->width, mpi->height);
        vf_clone_mpi_attributes(dmpi, mpi);
        for (int i = 0; i < MP_MAX_PLANES; i++) {
            dmpi->planes[i]=mpi->planes[i];
            dmpi->stride[i]=mpi->stride[i];
        }
        dmpi->width=mpi->width;
        dmpi->height=mpi->height;
    }

    if(vf->priv->shot) {
        vf->priv->shot=0;
        mp_image_t image;
        if (!vf->priv->store_slices)
            image = *dmpi;
        else
            image = *vf->priv->image;
        image.w = vf->priv->image->w;
        image.h = vf->priv->image->h;
        vf_clone_mpi_attributes(&image, mpi);
        vf->priv->image_callback(vf->priv->image_callback_ctx, &image);
        vf->priv->store_slices = 0;
    }

    return vf_next_put_image(vf, dmpi, pts);
}

static int control (vf_instance_t *vf, int request, void *data)
{
    if(request==VFCTRL_SCREENSHOT) {
        struct vf_ctrl_screenshot *cmd = (struct vf_ctrl_screenshot *)data;
        vf->priv->image_callback = cmd->image_callback;
        vf->priv->image_callback_ctx = cmd->image_callback_ctx;
        vf->priv->shot=1;
        return CONTROL_TRUE;
    }
    return vf_next_control (vf, request, data);
}


//===========================================================================//

static int query_format(struct vf_instance *vf, unsigned int fmt)
{
    enum PixelFormat av_format = imgfmt2pixfmt(fmt);

    if (av_format != PIX_FMT_NONE && sws_isSupportedInput(av_format))
        return vf_next_query_format(vf, fmt);
    return 0;
}

static void uninit(vf_instance_t *vf)
{
    free_mp_image(vf->priv->image);
    free(vf->priv);
}

static int vf_open(vf_instance_t *vf, char *args)
{
    vf->config=config;
    vf->control=control;
    vf->put_image=put_image;
    vf->query_format=query_format;
    vf->start_slice=start_slice;
    vf->draw_slice=draw_slice;
    vf->get_image=get_image;
    vf->uninit=uninit;
    vf->priv=malloc(sizeof(struct vf_priv_s));
    vf->priv->shot=0;
    vf->priv->store_slices=0;
    vf->priv->image=NULL;
    return 1;
}

const vf_info_t vf_info_screenshot = {
    "screenshot to file",
    "screenshot",
    "A'rpi, Jindrich Makovicka",
    "",
    vf_open,
    NULL
};

// screenshot.c will look for a filter named "screenshot_force", and not use
// the VO based screenshot code if it's in the filter chain.
const vf_info_t vf_info_screenshot_force = {
    "screenshot to file (override VO based screenshot code)",
    "screenshot_force",
    "A'rpi, Jindrich Makovicka",
    "",
    vf_open,
    NULL
};

//===========================================================================//