Statistics
| Branch: | Revision:

ffmpeg / libavfilter / vf_slicify.c @ e74bf3f3

History | View | Annotate | Download (3.9 KB)

1 bc37ec92 Stefano Sabatini
/*
2 3fa77bde Stefano Sabatini
 * Copyright (c) 2007 Bobby Bingham
3 bc37ec92 Stefano Sabatini
 *
4 2912e87a Mans Rullgard
 * This file is part of Libav.
5 bc37ec92 Stefano Sabatini
 *
6 2912e87a Mans Rullgard
 * Libav is free software; you can redistribute it and/or
7 bc37ec92 Stefano Sabatini
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11 2912e87a Mans Rullgard
 * Libav is distributed in the hope that it will be useful,
12 bc37ec92 Stefano Sabatini
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17 2912e87a Mans Rullgard
 * License along with Libav; if not, write to the Free Software
18 bc37ec92 Stefano Sabatini
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
/**
22 ba87f080 Diego Biurrun
 * @file
23 bc37ec92 Stefano Sabatini
 * video slicing filter
24
 */
25
26
#include "avfilter.h"
27 b4b66456 Stefano Sabatini
#include "libavutil/pixdesc.h"
28 bc37ec92 Stefano Sabatini
29
typedef struct {
30
    int h;          ///< output slice height
31
    int vshift;     ///< vertical chroma subsampling shift
32 ec7ab610 Stefano Sabatini
    uint32_t lcg_state; ///< LCG state used to compute random slice height
33
    int use_random_h;   ///< enable the use of random slice height values
34 bc37ec92 Stefano Sabatini
} SliceContext;
35
36
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
37
{
38
    SliceContext *slice = ctx->priv;
39
40
    slice->h = 16;
41 ec7ab610 Stefano Sabatini
    if (args) {
42
        if (!strcmp(args, "random")) {
43
            slice->use_random_h = 1;
44
        } else {
45
            sscanf(args, "%d", &slice->h);
46
        }
47
    }
48 bc37ec92 Stefano Sabatini
    return 0;
49
}
50
51
static int config_props(AVFilterLink *link)
52
{
53
    SliceContext *slice = link->dst->priv;
54
55 b4b66456 Stefano Sabatini
    slice->vshift = av_pix_fmt_descriptors[link->format].log2_chroma_h;
56 bc37ec92 Stefano Sabatini
57
    return 0;
58
}
59
60 ecc8dada S.N. Hemanth Meenakshisundaram
static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
61 bc37ec92 Stefano Sabatini
{
62 ec7ab610 Stefano Sabatini
    SliceContext *slice = link->dst->priv;
63
64
    if (slice->use_random_h) {
65
        slice->lcg_state = slice->lcg_state * 1664525 + 1013904223;
66
        slice->h = 8 + (uint64_t)slice->lcg_state * 25 / UINT32_MAX;
67
    }
68
69
    /* ensure that slices play nice with chroma subsampling, and enforce
70
     * a reasonable minimum size for the slices */
71
    slice->h = FFMAX(8, slice->h & (-1 << slice->vshift));
72
73
    av_log(link->dst, AV_LOG_DEBUG, "h:%d\n", slice->h);
74
75 bc37ec92 Stefano Sabatini
    avfilter_start_frame(link->dst->outputs[0], picref);
76
}
77
78 a13a5437 Stefano Sabatini
static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
79 bc37ec92 Stefano Sabatini
{
80
    SliceContext *slice = link->dst->priv;
81
    int y2;
82
83 bf972d5e Stefano Sabatini
    if (slice_dir == 1) {
84 3a1acfd7 Stefano Sabatini
        for (y2 = y; y2 + slice->h <= y + h; y2 += slice->h)
85
            avfilter_draw_slice(link->dst->outputs[0], y2, slice->h, slice_dir);
86 bc37ec92 Stefano Sabatini
87 3a1acfd7 Stefano Sabatini
        if (y2 < y + h)
88
            avfilter_draw_slice(link->dst->outputs[0], y2, y + h - y2, slice_dir);
89 bf972d5e Stefano Sabatini
    } else if (slice_dir == -1) {
90
        for (y2 = y + h; y2 - slice->h >= y; y2 -= slice->h)
91
            avfilter_draw_slice(link->dst->outputs[0], y2 - slice->h, slice->h, slice_dir);
92
93
        if (y2 > y)
94
            avfilter_draw_slice(link->dst->outputs[0], y, y2 - y, slice_dir);
95
    }
96 bc37ec92 Stefano Sabatini
}
97
98
AVFilter avfilter_vf_slicify = {
99
    .name      = "slicify",
100 a6881c6a Stefano Sabatini
    .description = NULL_IF_CONFIG_SMALL("Pass the images of input video on to next video filter as multiple slices."),
101 bc37ec92 Stefano Sabatini
102
    .init      = init,
103
104
    .priv_size = sizeof(SliceContext),
105
106
    .inputs    = (AVFilterPad[]) {{ .name             = "default",
107 72415b2a Stefano Sabatini
                                    .type             = AVMEDIA_TYPE_VIDEO,
108 d313e17a Bobby Bingham
                                    .get_video_buffer = avfilter_null_get_video_buffer,
109 bc37ec92 Stefano Sabatini
                                    .start_frame      = start_frame,
110
                                    .draw_slice       = draw_slice,
111
                                    .config_props     = config_props,
112 d313e17a Bobby Bingham
                                    .end_frame        = avfilter_null_end_frame, },
113 bc37ec92 Stefano Sabatini
                                  { .name = NULL}},
114
    .outputs   = (AVFilterPad[]) {{ .name            = "default",
115 72415b2a Stefano Sabatini
                                    .type            = AVMEDIA_TYPE_VIDEO, },
116 bc37ec92 Stefano Sabatini
                                  { .name = NULL}},
117
};