]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_fieldhint.c
avfilter/vf_waveform: add 12bit depth support
[ffmpeg] / libavfilter / vf_fieldhint.c
1 /*
2  * Copyright (c) 2016 Paul B Mahol
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 #include "libavutil/imgutils.h"
22 #include "libavutil/internal.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/pixdesc.h"
25 #include "avfilter.h"
26 #include "internal.h"
27 #include "video.h"
28
29 typedef struct FieldHintContext {
30     const AVClass *class;
31
32     char *hint_file_str;
33     FILE *hint;
34     int mode;
35
36     AVFrame *frame[3];
37
38     int64_t line;
39     int nb_planes;
40     int eof;
41     int planewidth[4];
42     int planeheight[4];
43 } FieldHintContext;
44
45 #define OFFSET(x) offsetof(FieldHintContext, x)
46 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
47
48 static const AVOption fieldhint_options[] = {
49     { "hint", "set hint file", OFFSET(hint_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
50     { "mode", "set hint mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "mode" },
51     {   "absolute", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode" },
52     {   "relative", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
53     { NULL }
54 };
55
56 AVFILTER_DEFINE_CLASS(fieldhint);
57
58 static av_cold int init(AVFilterContext *ctx)
59 {
60     FieldHintContext *s = ctx->priv;
61     int ret;
62
63     if (!s->hint_file_str) {
64         av_log(ctx, AV_LOG_ERROR, "Hint file must be set.\n");
65         return AVERROR(EINVAL);
66     }
67     s->hint = fopen(s->hint_file_str, "r");
68     if (!s->hint) {
69         ret = AVERROR(errno);
70         av_log(ctx, AV_LOG_ERROR, "%s: %s\n", s->hint_file_str, av_err2str(ret));
71         return ret;
72     }
73
74     return 0;
75 }
76
77 static int query_formats(AVFilterContext *ctx)
78 {
79     AVFilterFormats *pix_fmts = NULL;
80     int fmt, ret;
81
82     for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
83         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
84         if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
85               desc->flags & AV_PIX_FMT_FLAG_PAL     ||
86               desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) &&
87             (ret = ff_add_format(&pix_fmts, fmt)) < 0)
88             return ret;
89     }
90
91     return ff_set_common_formats(ctx, pix_fmts);
92 }
93
94 static int config_input(AVFilterLink *inlink)
95 {
96     FieldHintContext *s = inlink->dst->priv;
97     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
98     int ret;
99
100     if ((ret = av_image_fill_linesizes(s->planewidth, inlink->format, inlink->w)) < 0)
101         return ret;
102
103     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
104     s->planeheight[0] = s->planeheight[3] = inlink->h;
105
106     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
107
108     return 0;
109 }
110
111 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
112 {
113     AVFilterContext *ctx = inlink->dst;
114     AVFilterLink *outlink = ctx->outputs[0];
115     FieldHintContext *s = ctx->priv;
116     AVFrame *out, *top, *bottom;
117     char buf[1024] = { 0 };
118     int64_t tf, bf;
119     char hint = '=';
120     int p;
121
122     av_frame_free(&s->frame[0]);
123     s->frame[0] = s->frame[1];
124     s->frame[1] = s->frame[2];
125     s->frame[2] = in;
126     if (!s->frame[1])
127         return 0;
128     else if (!s->frame[0]) {
129         s->frame[0] = av_frame_clone(s->frame[1]);
130         if (!s->frame[0])
131             return AVERROR(ENOMEM);
132     }
133
134     while (1) {
135         if (fgets(buf, sizeof(buf)-1, s->hint)) {
136             s->line++;
137             if (buf[0] == '#' || buf[0] == ';') {
138                 continue;
139             } else if (sscanf(buf, "%"PRId64",%"PRId64" %c", &tf, &bf, &hint) == 3) {
140                 ;
141             } else if (sscanf(buf, "%"PRId64",%"PRId64"", &tf, &bf) == 2) {
142                 ;
143             } else {
144                 av_log(ctx, AV_LOG_ERROR, "Invalid entry at line %"PRId64".\n", s->line);
145                 return AVERROR_INVALIDDATA;
146             }
147             switch (s->mode) {
148             case 0:
149                 if (tf > outlink->frame_count + 1 || tf < FFMAX(0, outlink->frame_count - 1) ||
150                     bf > outlink->frame_count + 1 || bf < FFMAX(0, outlink->frame_count - 1)) {
151                     av_log(ctx, AV_LOG_ERROR, "Out of range frames %"PRId64" and/or %"PRId64" on line %"PRId64" for %"PRId64". input frame.\n", tf, bf, s->line, inlink->frame_count);
152                     return AVERROR_INVALIDDATA;
153                 }
154                 break;
155             case 1:
156                 if (tf > 1 || tf < -1 ||
157                     bf > 1 || bf < -1) {
158                     av_log(ctx, AV_LOG_ERROR, "Out of range %"PRId64" and/or %"PRId64" on line %"PRId64" for %"PRId64". input frame.\n", tf, bf, s->line, inlink->frame_count);
159                     return AVERROR_INVALIDDATA;
160                 }
161             };
162             break;
163         } else {
164             av_log(ctx, AV_LOG_ERROR, "Missing entry for %"PRId64". input frame.\n", inlink->frame_count);
165             return AVERROR_INVALIDDATA;
166         }
167     }
168
169     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
170     if (!out)
171         return AVERROR(ENOMEM);
172     av_frame_copy_props(out, s->frame[1]);
173
174     switch (s->mode) {
175     case 0:
176         top    = s->frame[1 + tf - outlink->frame_count];
177         bottom = s->frame[1 + bf - outlink->frame_count];
178         break;
179     case 1:
180         top    = s->frame[1 + tf];
181         bottom = s->frame[1 + bf];
182         break;
183     }
184
185     switch (hint) {
186     case '+':
187         out->interlaced_frame = 1;
188         break;
189     case '-':
190         out->interlaced_frame = 0;
191         break;
192     case '=':
193         break;
194     default:
195         av_log(ctx, AV_LOG_ERROR, "Invalid hint: %c.\n", hint);
196         return AVERROR(EINVAL);
197     }
198
199     for (p = 0; p < s->nb_planes; p++) {
200         av_image_copy_plane(out->data[p],
201                             out->linesize[p] * 2,
202                             top->data[p],
203                             top->linesize[p] * 2,
204                             s->planewidth[p],
205                             (s->planeheight[p] + 1) / 2);
206         av_image_copy_plane(out->data[p] + out->linesize[p],
207                             out->linesize[p] * 2,
208                             bottom->data[p] + bottom->linesize[p],
209                             bottom->linesize[p] * 2,
210                             s->planewidth[p],
211                             (s->planeheight[p] + 1) / 2);
212     }
213
214     return ff_filter_frame(outlink, out);
215 }
216
217 static int request_frame(AVFilterLink *outlink)
218 {
219     AVFilterContext *ctx = outlink->src;
220     FieldHintContext *s = ctx->priv;
221     int ret;
222
223     if (s->eof)
224         return AVERROR_EOF;
225
226     ret = ff_request_frame(ctx->inputs[0]);
227     if (ret == AVERROR_EOF && s->frame[2]) {
228         AVFrame *next = av_frame_clone(s->frame[2]);
229         if (!next)
230             return AVERROR(ENOMEM);
231         ret = filter_frame(ctx->inputs[0], next);
232         s->eof = 1;
233     }
234
235     return ret;
236 }
237
238 static av_cold void uninit(AVFilterContext *ctx)
239 {
240     FieldHintContext *s = ctx->priv;
241
242     if (s->hint)
243         fclose(s->hint);
244     s->hint = NULL;
245
246     av_frame_free(&s->frame[0]);
247     av_frame_free(&s->frame[1]);
248     av_frame_free(&s->frame[2]);
249 }
250
251 static const AVFilterPad inputs[] = {
252     {
253         .name         = "default",
254         .type         = AVMEDIA_TYPE_VIDEO,
255         .config_props = config_input,
256         .filter_frame = filter_frame,
257     },
258     { NULL }
259 };
260
261 static const AVFilterPad outputs[] = {
262     {
263         .name          = "default",
264         .type          = AVMEDIA_TYPE_VIDEO,
265         .request_frame = request_frame,
266     },
267     { NULL }
268 };
269
270 AVFilter ff_vf_fieldhint = {
271     .name          = "fieldhint",
272     .description   = NULL_IF_CONFIG_SMALL("Field matching using hints."),
273     .priv_size     = sizeof(FieldHintContext),
274     .priv_class    = &fieldhint_class,
275     .init          = init,
276     .uninit        = uninit,
277     .query_formats = query_formats,
278     .inputs        = inputs,
279     .outputs       = outputs,
280 };