Statistics
| Branch: | Revision:

ffmpeg / libavfilter / vf_pad.c @ 2912e87a

History | View | Annotate | Download (11.4 KB)

1
/*
2
 * Copyright (c) 2008 vmrsss
3
 * Copyright (c) 2009 Stefano Sabatini
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * Libav is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with Libav; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
/**
23
 * @file
24
 * video padding filter and color source
25
 */
26

    
27
#include "avfilter.h"
28
#include "libavutil/pixdesc.h"
29
#include "libavutil/colorspace.h"
30
#include "libavutil/avassert.h"
31
#include "libavutil/imgutils.h"
32
#include "libavutil/parseutils.h"
33
#include "drawutils.h"
34

    
35
static int query_formats(AVFilterContext *ctx)
36
{
37
    static const enum PixelFormat pix_fmts[] = {
38
        PIX_FMT_ARGB,         PIX_FMT_RGBA,
39
        PIX_FMT_ABGR,         PIX_FMT_BGRA,
40
        PIX_FMT_RGB24,        PIX_FMT_BGR24,
41

    
42
        PIX_FMT_YUV444P,      PIX_FMT_YUV422P,
43
        PIX_FMT_YUV420P,      PIX_FMT_YUV411P,
44
        PIX_FMT_YUV410P,      PIX_FMT_YUV440P,
45
        PIX_FMT_YUVJ444P,     PIX_FMT_YUVJ422P,
46
        PIX_FMT_YUVJ420P,     PIX_FMT_YUVJ440P,
47
        PIX_FMT_YUVA420P,
48

    
49
        PIX_FMT_NONE
50
    };
51

    
52
    avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
53
    return 0;
54
}
55

    
56
typedef struct {
57
    int w, h;               ///< output dimensions, a value of 0 will result in the input size
58
    int x, y;               ///< offsets of the input area with respect to the padded area
59
    int in_w, in_h;         ///< width and height for the padded input video, which has to be aligned to the chroma values in order to avoid chroma issues
60

    
61
    uint8_t color[4];       ///< color expressed either in YUVA or RGBA colorspace for the padding area
62
    uint8_t *line[4];
63
    int      line_step[4];
64
    int hsub, vsub;         ///< chroma subsampling values
65
    int needs_copy;
66
} PadContext;
67

    
68
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
69
{
70
    PadContext *pad = ctx->priv;
71
    char color_string[128] = "black";
72

    
73
    if (args)
74
        sscanf(args, "%d:%d:%d:%d:%s", &pad->w, &pad->h, &pad->x, &pad->y, color_string);
75

    
76
    if (av_parse_color(pad->color, color_string, -1, ctx) < 0)
77
        return AVERROR(EINVAL);
78

    
79
    /* sanity check params */
80
    if (pad->w < 0 || pad->h < 0) {
81
        av_log(ctx, AV_LOG_ERROR, "Negative size values are not acceptable.\n");
82
        return AVERROR(EINVAL);
83
    }
84

    
85
    return 0;
86
}
87

    
88
static av_cold void uninit(AVFilterContext *ctx)
89
{
90
    PadContext *pad = ctx->priv;
91
    int i;
92

    
93
    for (i = 0; i < 4; i++) {
94
        av_freep(&pad->line[i]);
95
        pad->line_step[i] = 0;
96
    }
97
}
98

    
99
static int config_input(AVFilterLink *inlink)
100
{
101
    AVFilterContext *ctx = inlink->dst;
102
    PadContext *pad = ctx->priv;
103
    const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
104
    uint8_t rgba_color[4];
105
    int is_packed_rgba;
106

    
107
    pad->hsub = pix_desc->log2_chroma_w;
108
    pad->vsub = pix_desc->log2_chroma_h;
109

    
110
    if (!pad->w)
111
        pad->w = inlink->w;
112
    if (!pad->h)
113
        pad->h = inlink->h;
114

    
115
    pad->w &= ~((1 << pad->hsub) - 1);
116
    pad->h &= ~((1 << pad->vsub) - 1);
117
    pad->x &= ~((1 << pad->hsub) - 1);
118
    pad->y &= ~((1 << pad->vsub) - 1);
119

    
120
    pad->in_w = inlink->w & ~((1 << pad->hsub) - 1);
121
    pad->in_h = inlink->h & ~((1 << pad->vsub) - 1);
122

    
123
    memcpy(rgba_color, pad->color, sizeof(rgba_color));
124
    ff_fill_line_with_color(pad->line, pad->line_step, pad->w, pad->color,
125
                            inlink->format, rgba_color, &is_packed_rgba, NULL);
126

    
127
    av_log(ctx, AV_LOG_INFO, "w:%d h:%d -> w:%d h:%d x:%d y:%d color:0x%02X%02X%02X%02X[%s]\n",
128
           inlink->w, inlink->h, pad->w, pad->h, pad->x, pad->y,
129
           pad->color[0], pad->color[1], pad->color[2], pad->color[3],
130
           is_packed_rgba ? "rgba" : "yuva");
131

    
132
    if (pad->x <  0 || pad->y <  0                      ||
133
        pad->w <= 0 || pad->h <= 0                      ||
134
        (unsigned)pad->x + (unsigned)inlink->w > pad->w ||
135
        (unsigned)pad->y + (unsigned)inlink->h > pad->h) {
136
        av_log(ctx, AV_LOG_ERROR,
137
               "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
138
               pad->x, pad->y, pad->x + inlink->w, pad->y + inlink->h, pad->w, pad->h);
139
        return AVERROR(EINVAL);
140
    }
141

    
142
    return 0;
143
}
144

    
145
static int config_output(AVFilterLink *outlink)
146
{
147
    PadContext *pad = outlink->src->priv;
148

    
149
    outlink->w = pad->w;
150
    outlink->h = pad->h;
151
    return 0;
152
}
153

    
154
static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
155
{
156
    PadContext *pad = inlink->dst->priv;
157

    
158
    AVFilterBufferRef *picref = avfilter_get_video_buffer(inlink->dst->outputs[0], perms,
159
                                                       w + (pad->w - pad->in_w),
160
                                                       h + (pad->h - pad->in_h));
161
    int plane;
162

    
163
    picref->video->w = w;
164
    picref->video->h = h;
165

    
166
    for (plane = 0; plane < 4 && picref->data[plane]; plane++) {
167
        int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
168
        int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
169

    
170
        picref->data[plane] += (pad->x >> hsub) * pad->line_step[plane] +
171
            (pad->y >> vsub) * picref->linesize[plane];
172
    }
173

    
174
    return picref;
175
}
176

    
177
static int does_clip(PadContext *pad, AVFilterBufferRef *outpicref, int plane, int hsub, int vsub, int x, int y)
178
{
179
    int64_t x_in_buf, y_in_buf;
180

    
181
    x_in_buf =  outpicref->data[plane] - outpicref->buf->data[plane]
182
             +  (x >> hsub) * pad      ->line_step[plane]
183
             +  (y >> vsub) * outpicref->linesize [plane];
184

    
185
    if(x_in_buf < 0 || x_in_buf % pad->line_step[plane])
186
        return 1;
187
    x_in_buf /= pad->line_step[plane];
188

    
189
    av_assert0(outpicref->buf->linesize[plane]>0); //while reference can use negative linesize the main buffer should not
190

    
191
    y_in_buf = x_in_buf / outpicref->buf->linesize[plane];
192
    x_in_buf %= outpicref->buf->linesize[plane];
193

    
194
    if(   y_in_buf<<vsub >= outpicref->buf->h
195
       || x_in_buf<<hsub >= outpicref->buf->w)
196
        return 1;
197
    return 0;
198
}
199

    
200
static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
201
{
202
    PadContext *pad = inlink->dst->priv;
203
    AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
204
    int plane;
205

    
206
    for (plane = 0; plane < 4 && outpicref->data[plane]; plane++) {
207
        int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
208
        int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
209

    
210
        av_assert0(outpicref->buf->w>0 && outpicref->buf->h>0);
211

    
212
        if(outpicref->format != outpicref->buf->format) //unsupported currently
213
            break;
214

    
215
        outpicref->data[plane] -=   (pad->x  >> hsub) * pad      ->line_step[plane]
216
                                  + (pad->y  >> vsub) * outpicref->linesize [plane];
217

    
218
        if(   does_clip(pad, outpicref, plane, hsub, vsub, 0, 0)
219
           || does_clip(pad, outpicref, plane, hsub, vsub, 0, pad->h-1)
220
           || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, 0)
221
           || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, pad->h-1)
222
          )
223
            break;
224
    }
225
    pad->needs_copy= plane < 4 && outpicref->data[plane];
226
    if(pad->needs_copy){
227
        av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
228
        avfilter_unref_buffer(outpicref);
229
        outpicref = avfilter_get_video_buffer(inlink->dst->outputs[0], AV_PERM_WRITE | AV_PERM_NEG_LINESIZES,
230
                                                       FFMAX(inlink->w, pad->w),
231
                                                       FFMAX(inlink->h, pad->h));
232
        avfilter_copy_buffer_ref_props(outpicref, inpicref);
233
    }
234

    
235
    inlink->dst->outputs[0]->out_buf = outpicref;
236

    
237
    outpicref->video->w = pad->w;
238
    outpicref->video->h = pad->h;
239

    
240
    avfilter_start_frame(inlink->dst->outputs[0], outpicref);
241
}
242

    
243
static void end_frame(AVFilterLink *link)
244
{
245
    avfilter_end_frame(link->dst->outputs[0]);
246
    avfilter_unref_buffer(link->cur_buf);
247
}
248

    
249
static void draw_send_bar_slice(AVFilterLink *link, int y, int h, int slice_dir, int before_slice)
250
{
251
    PadContext *pad = link->dst->priv;
252
    int bar_y, bar_h = 0;
253

    
254
    if        (slice_dir * before_slice ==  1 && y == pad->y) {
255
        /* top bar */
256
        bar_y = 0;
257
        bar_h = pad->y;
258
    } else if (slice_dir * before_slice == -1 && (y + h) == (pad->y + pad->in_h)) {
259
        /* bottom bar */
260
        bar_y = pad->y + pad->in_h;
261
        bar_h = pad->h - pad->in_h - pad->y;
262
    }
263

    
264
    if (bar_h) {
265
        ff_draw_rectangle(link->dst->outputs[0]->out_buf->data,
266
                          link->dst->outputs[0]->out_buf->linesize,
267
                          pad->line, pad->line_step, pad->hsub, pad->vsub,
268
                          0, bar_y, pad->w, bar_h);
269
        avfilter_draw_slice(link->dst->outputs[0], bar_y, bar_h, slice_dir);
270
    }
271
}
272

    
273
static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
274
{
275
    PadContext *pad = link->dst->priv;
276
    AVFilterBufferRef *outpic = link->dst->outputs[0]->out_buf;
277
    AVFilterBufferRef *inpic = link->cur_buf;
278

    
279
    y += pad->y;
280

    
281
    y &= ~((1 << pad->vsub) - 1);
282
    h &= ~((1 << pad->vsub) - 1);
283

    
284
    if (!h)
285
        return;
286
    draw_send_bar_slice(link, y, h, slice_dir, 1);
287

    
288
    /* left border */
289
    ff_draw_rectangle(outpic->data, outpic->linesize, pad->line, pad->line_step,
290
                      pad->hsub, pad->vsub, 0, y, pad->x, h);
291

    
292
    if(pad->needs_copy){
293
        ff_copy_rectangle(outpic->data, outpic->linesize,
294
                          inpic->data, inpic->linesize, pad->line_step,
295
                          pad->hsub, pad->vsub,
296
                          pad->x, y, y-pad->y, inpic->video->w, h);
297
    }
298

    
299
    /* right border */
300
    ff_draw_rectangle(outpic->data, outpic->linesize,
301
                      pad->line, pad->line_step, pad->hsub, pad->vsub,
302
                      pad->x + pad->in_w, y, pad->w - pad->x - pad->in_w, h);
303
    avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
304

    
305
    draw_send_bar_slice(link, y, h, slice_dir, -1);
306
}
307

    
308
AVFilter avfilter_vf_pad = {
309
    .name          = "pad",
310
    .description   = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
311

    
312
    .priv_size     = sizeof(PadContext),
313
    .init          = init,
314
    .uninit        = uninit,
315
    .query_formats = query_formats,
316

    
317
    .inputs    = (AVFilterPad[]) {{ .name             = "default",
318
                                    .type             = AVMEDIA_TYPE_VIDEO,
319
                                    .config_props     = config_input,
320
                                    .get_video_buffer = get_video_buffer,
321
                                    .start_frame      = start_frame,
322
                                    .draw_slice       = draw_slice,
323
                                    .end_frame        = end_frame, },
324
                                  { .name = NULL}},
325

    
326
    .outputs   = (AVFilterPad[]) {{ .name             = "default",
327
                                    .type             = AVMEDIA_TYPE_VIDEO,
328
                                    .config_props     = config_output, },
329
                                  { .name = NULL}},
330
};