Revision e3331706 libavfilter/vf_pad.c

View differences:

libavfilter/vf_pad.c
27 27
#include "avfilter.h"
28 28
#include "libavutil/pixdesc.h"
29 29
#include "libavutil/colorspace.h"
30
#include "libavutil/avassert.h"
30 31
#include "libavcore/imgutils.h"
31 32
#include "libavcore/parseutils.h"
32 33

  
......
100 101
    }
101 102
}
102 103

  
104
static void copy_rectangle(AVFilterBufferRef *outpic,uint8_t *line[4], int line_step[4], int linesize[4],
105
                           int hsub, int vsub, int x, int y, int y2, int w, int h)
106
{
107
    int i, plane;
108
    uint8_t *p;
109

  
110
    for (plane = 0; plane < 4 && outpic->data[plane]; plane++) {
111
        int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
112
        int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
113

  
114
        p = outpic->data[plane] + (y >> vsub1) * outpic->linesize[plane];
115
        for (i = 0; i < (h >> vsub1); i++) {
116
            memcpy(p + (x >> hsub1) * line_step[plane], line[plane] + linesize[plane]*(i+(y2>>vsub1)), (w >> hsub1) * line_step[plane]);
117
            p += outpic->linesize[plane];
118
        }
119
    }
120
}
121

  
103 122
static int query_formats(AVFilterContext *ctx)
104 123
{
105 124
    static const enum PixelFormat pix_fmts[] = {
......
132 151
    uint8_t *line[4];
133 152
    int      line_step[4];
134 153
    int hsub, vsub;         ///< chroma subsampling values
154
    int needs_copy;
135 155
} PadContext;
136 156

  
137 157
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
......
243 263
    return picref;
244 264
}
245 265

  
266
static int does_clip(PadContext *pad, AVFilterBufferRef *outpicref, int plane, int hsub, int vsub, int x, int y)
267
{
268
    int64_t x_in_buf, y_in_buf;
269

  
270
    x_in_buf =  outpicref->data[plane] - outpicref->buf->data[plane]
271
             +  (x >> hsub) * pad      ->line_step[plane]
272
             +  (y >> vsub) * outpicref->linesize [plane];
273

  
274
    if(x_in_buf < 0 || x_in_buf % pad->line_step[plane])
275
        return 1;
276
    x_in_buf /= pad->line_step[plane];
277

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

  
280
    y_in_buf = x_in_buf / outpicref->buf->linesize[plane];
281
    x_in_buf %= outpicref->buf->linesize[plane];
282

  
283
    if(   y_in_buf<<vsub >= outpicref->buf->h
284
       || x_in_buf<<hsub >= outpicref->buf->w)
285
        return 1;
286
    return 0;
287
}
288

  
246 289
static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
247 290
{
248 291
    PadContext *pad = inlink->dst->priv;
249 292
    AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
250 293
    int plane;
251 294

  
252
    inlink->dst->outputs[0]->out_buf = outpicref;
253

  
254 295
    for (plane = 0; plane < 4 && outpicref->data[plane]; plane++) {
255 296
        int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
256 297
        int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
257 298

  
258
        outpicref->data[plane] -= (pad->x >> hsub) * pad->line_step[plane] +
259
            (pad->y >> vsub) * outpicref->linesize[plane];
299
        av_assert0(outpicref->buf->w>0 && outpicref->buf->h>0);
300

  
301
        if(outpicref->format != outpicref->buf->format) //unsupported currently
302
            break;
303

  
304
        outpicref->data[plane] -=   (pad->x  >> hsub) * pad      ->line_step[plane]
305
                                  + (pad->y  >> vsub) * outpicref->linesize [plane];
306

  
307
        if(   does_clip(pad, outpicref, plane, hsub, vsub, 0, 0)
308
           || does_clip(pad, outpicref, plane, hsub, vsub, 0, pad->h-1)
309
           || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, 0)
310
           || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, pad->h-1)
311
          )
312
            break;
260 313
    }
314
    pad->needs_copy= plane < 4 && outpicref->data[plane];
315
    if(pad->needs_copy){
316
        av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
317
        avfilter_unref_buffer(outpicref);
318
        outpicref = avfilter_get_video_buffer(inlink->dst->outputs[0], AV_PERM_WRITE | AV_PERM_NEG_LINESIZES,
319
                                                       FFMAX(inlink->w, pad->w),
320
                                                       FFMAX(inlink->h, pad->h));
321
        avfilter_copy_buffer_ref_props(outpicref, inpicref);
322
    }
323

  
324
    inlink->dst->outputs[0]->out_buf = outpicref;
261 325

  
262 326
    outpicref->video->w = pad->w;
263 327
    outpicref->video->h = pad->h;
......
298 362
{
299 363
    PadContext *pad = link->dst->priv;
300 364
    AVFilterBufferRef *outpic = link->dst->outputs[0]->out_buf;
365
    AVFilterBufferRef *inpic = link->cur_buf;
301 366

  
302 367
    y += pad->y;
303 368

  
......
311 376
    /* left border */
312 377
    draw_rectangle(outpic, pad->line, pad->line_step, pad->hsub, pad->vsub,
313 378
                   0, y, pad->x, h);
379

  
380
    if(pad->needs_copy){
381
        copy_rectangle(outpic,
382
                       inpic->data, pad->line_step, inpic->linesize, pad->hsub, pad->vsub,
383
                       pad->x, y, y-pad->y, inpic->video->w, h);
384
    }
385

  
314 386
    /* right border */
315 387
    draw_rectangle(outpic, pad->line, pad->line_step, pad->hsub, pad->vsub,
316 388
                   pad->x + pad->in_w, y, pad->w - pad->x - pad->in_w, h);

Also available in: Unified diff