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