]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_pixdesctest.c
lavfi: move color filter to testsrc, factorize
[ffmpeg] / libavfilter / vf_pixdesctest.c
1 /*
2  * Copyright (c) 2009 Stefano Sabatini
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * 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  * FFmpeg is distributed in the hope that it will be useful,
12  * 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  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * pixdesc test filter
24  */
25
26 #include "libavutil/pixdesc.h"
27 #include "avfilter.h"
28 #include "internal.h"
29 #include "video.h"
30
31 typedef struct {
32     const AVPixFmtDescriptor *pix_desc;
33     uint16_t *line;
34 } PixdescTestContext;
35
36 static av_cold void uninit(AVFilterContext *ctx)
37 {
38     PixdescTestContext *priv = ctx->priv;
39     av_freep(&priv->line);
40 }
41
42 static int config_props(AVFilterLink *inlink)
43 {
44     PixdescTestContext *priv = inlink->dst->priv;
45
46     priv->pix_desc = &av_pix_fmt_descriptors[inlink->format];
47
48     if (!(priv->line = av_malloc(sizeof(*priv->line) * inlink->w)))
49         return AVERROR(ENOMEM);
50
51     return 0;
52 }
53
54 static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
55 {
56     PixdescTestContext *priv = inlink->dst->priv;
57     AVFilterLink *outlink    = inlink->dst->outputs[0];
58     AVFilterBufferRef *outpicref, *for_next_filter;
59     int i, ret = 0;
60
61     outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE,
62                                     outlink->w, outlink->h);
63     if (!outpicref)
64         return AVERROR(ENOMEM);
65
66     avfilter_copy_buffer_ref_props(outpicref, picref);
67
68     for (i = 0; i < 4; i++) {
69         int h = outlink->h;
70         h = i == 1 || i == 2 ? h>>priv->pix_desc->log2_chroma_h : h;
71         if (outpicref->data[i]) {
72             uint8_t *data = outpicref->data[i] +
73                 (outpicref->linesize[i] > 0 ? 0 : outpicref->linesize[i] * (h-1));
74             memset(data, 0, FFABS(outpicref->linesize[i]) * h);
75         }
76     }
77
78     /* copy palette */
79     if (priv->pix_desc->flags & PIX_FMT_PAL ||
80         priv->pix_desc->flags & PIX_FMT_PSEUDOPAL)
81         memcpy(outpicref->data[1], picref->data[1], AVPALETTE_SIZE);
82
83     for_next_filter = avfilter_ref_buffer(outpicref, ~0);
84     if (for_next_filter)
85         ret = ff_start_frame(outlink, for_next_filter);
86     else
87         ret = AVERROR(ENOMEM);
88
89     if (ret < 0) {
90         avfilter_unref_bufferp(&outpicref);
91         return ret;
92     }
93
94     outlink->out_buf = outpicref;
95     return 0;
96 }
97
98 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
99 {
100     PixdescTestContext *priv = inlink->dst->priv;
101     AVFilterBufferRef *inpic    = inlink->cur_buf;
102     AVFilterBufferRef *outpic   = inlink->dst->outputs[0]->out_buf;
103     int i, c, w = inlink->w;
104
105     for (c = 0; c < priv->pix_desc->nb_components; c++) {
106         int w1 = c == 1 || c == 2 ? w>>priv->pix_desc->log2_chroma_w : w;
107         int h1 = c == 1 || c == 2 ? h>>priv->pix_desc->log2_chroma_h : h;
108         int y1 = c == 1 || c == 2 ? y>>priv->pix_desc->log2_chroma_h : y;
109
110         for (i = y1; i < y1 + h1; i++) {
111             av_read_image_line(priv->line,
112                                (void*)inpic->data,
113                                inpic->linesize,
114                                priv->pix_desc,
115                                0, i, c, w1, 0);
116
117             av_write_image_line(priv->line,
118                                 outpic->data,
119                                 outpic->linesize,
120                                 priv->pix_desc,
121                                 0, i, c, w1);
122         }
123     }
124
125     return ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
126 }
127
128 AVFilter avfilter_vf_pixdesctest = {
129     .name        = "pixdesctest",
130     .description = NULL_IF_CONFIG_SMALL("Test pixel format definitions."),
131
132     .priv_size = sizeof(PixdescTestContext),
133     .uninit    = uninit,
134
135     .inputs    = (const AVFilterPad[]) {{ .name            = "default",
136                                           .type            = AVMEDIA_TYPE_VIDEO,
137                                           .start_frame     = start_frame,
138                                           .draw_slice      = draw_slice,
139                                           .config_props    = config_props,
140                                           .min_perms       = AV_PERM_READ, },
141                                         { .name = NULL}},
142
143     .outputs   = (const AVFilterPad[]) {{ .name            = "default",
144                                           .type            = AVMEDIA_TYPE_VIDEO, },
145                                         { .name = NULL}},
146 };