| Line | Branch | Exec | Source |
|---|---|---|---|
| 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/avassert.h" | ||
| 22 | #include "libavutil/file_open.h" | ||
| 23 | #include "libavutil/imgutils.h" | ||
| 24 | #include "libavutil/internal.h" | ||
| 25 | #include "libavutil/opt.h" | ||
| 26 | #include "libavutil/pixdesc.h" | ||
| 27 | #include "avfilter.h" | ||
| 28 | #include "filters.h" | ||
| 29 | #include "formats.h" | ||
| 30 | #include "video.h" | ||
| 31 | |||
| 32 | enum HintModes { | ||
| 33 | ABSOLUTE_HINT, | ||
| 34 | RELATIVE_HINT, | ||
| 35 | PATTERN_HINT, | ||
| 36 | NB_HINTS, | ||
| 37 | }; | ||
| 38 | |||
| 39 | typedef struct FieldHintContext { | ||
| 40 | const AVClass *class; | ||
| 41 | |||
| 42 | char *hint_file_str; | ||
| 43 | FILE *hint; | ||
| 44 | int mode; | ||
| 45 | |||
| 46 | AVFrame *frame[3]; | ||
| 47 | |||
| 48 | int64_t line; | ||
| 49 | int nb_planes; | ||
| 50 | int eof; | ||
| 51 | int planewidth[4]; | ||
| 52 | int planeheight[4]; | ||
| 53 | } FieldHintContext; | ||
| 54 | |||
| 55 | #define OFFSET(x) offsetof(FieldHintContext, x) | ||
| 56 | #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM | ||
| 57 | |||
| 58 | static const AVOption fieldhint_options[] = { | ||
| 59 | { "hint", "set hint file", OFFSET(hint_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS }, | ||
| 60 | { "mode", "set hint mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_HINTS-1, FLAGS, .unit = "mode" }, | ||
| 61 | { "absolute", 0, 0, AV_OPT_TYPE_CONST, {.i64=ABSOLUTE_HINT}, 0, 0, FLAGS, .unit = "mode" }, | ||
| 62 | { "relative", 0, 0, AV_OPT_TYPE_CONST, {.i64=RELATIVE_HINT}, 0, 0, FLAGS, .unit = "mode" }, | ||
| 63 | { "pattern", 0, 0, AV_OPT_TYPE_CONST, {.i64=PATTERN_HINT}, 0, 0, FLAGS, .unit = "mode" }, | ||
| 64 | { NULL } | ||
| 65 | }; | ||
| 66 | |||
| 67 | AVFILTER_DEFINE_CLASS(fieldhint); | ||
| 68 | |||
| 69 | ✗ | static av_cold int init(AVFilterContext *ctx) | |
| 70 | { | ||
| 71 | ✗ | FieldHintContext *s = ctx->priv; | |
| 72 | int ret; | ||
| 73 | |||
| 74 | ✗ | if (!s->hint_file_str) { | |
| 75 | ✗ | av_log(ctx, AV_LOG_ERROR, "Hint file must be set.\n"); | |
| 76 | ✗ | return AVERROR(EINVAL); | |
| 77 | } | ||
| 78 | ✗ | s->hint = avpriv_fopen_utf8(s->hint_file_str, "r"); | |
| 79 | ✗ | if (!s->hint) { | |
| 80 | ✗ | ret = AVERROR(errno); | |
| 81 | ✗ | av_log(ctx, AV_LOG_ERROR, "%s: %s\n", s->hint_file_str, av_err2str(ret)); | |
| 82 | ✗ | return ret; | |
| 83 | } | ||
| 84 | |||
| 85 | ✗ | return 0; | |
| 86 | } | ||
| 87 | |||
| 88 | ✗ | static int query_formats(const AVFilterContext *ctx, | |
| 89 | AVFilterFormatsConfig **cfg_in, | ||
| 90 | AVFilterFormatsConfig **cfg_out) | ||
| 91 | { | ||
| 92 | ✗ | int reject_flags = AV_PIX_FMT_FLAG_BITSTREAM | | |
| 93 | AV_PIX_FMT_FLAG_HWACCEL | | ||
| 94 | AV_PIX_FMT_FLAG_PAL; | ||
| 95 | |||
| 96 | ✗ | return ff_set_common_formats2(ctx, cfg_in, cfg_out, | |
| 97 | ff_formats_pixdesc_filter(0, reject_flags)); | ||
| 98 | } | ||
| 99 | |||
| 100 | ✗ | static int config_input(AVFilterLink *inlink) | |
| 101 | { | ||
| 102 | ✗ | FieldHintContext *s = inlink->dst->priv; | |
| 103 | ✗ | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); | |
| 104 | int ret; | ||
| 105 | |||
| 106 | ✗ | if ((ret = av_image_fill_linesizes(s->planewidth, inlink->format, inlink->w)) < 0) | |
| 107 | ✗ | return ret; | |
| 108 | |||
| 109 | ✗ | s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); | |
| 110 | ✗ | s->planeheight[0] = s->planeheight[3] = inlink->h; | |
| 111 | |||
| 112 | ✗ | s->nb_planes = av_pix_fmt_count_planes(inlink->format); | |
| 113 | |||
| 114 | ✗ | return 0; | |
| 115 | } | ||
| 116 | |||
| 117 | ✗ | static int filter_frame(AVFilterLink *inlink, AVFrame *in) | |
| 118 | { | ||
| 119 | ✗ | FilterLink *inl = ff_filter_link(inlink); | |
| 120 | ✗ | AVFilterContext *ctx = inlink->dst; | |
| 121 | ✗ | AVFilterLink *outlink = ctx->outputs[0]; | |
| 122 | ✗ | FilterLink *outl = ff_filter_link(outlink); | |
| 123 | ✗ | FieldHintContext *s = ctx->priv; | |
| 124 | AVFrame *out, *top, *bottom; | ||
| 125 | ✗ | char buf[1024] = { 0 }; | |
| 126 | int64_t tf, bf; | ||
| 127 | ✗ | int tfactor = 0, bfactor = 1; | |
| 128 | ✗ | char hint = '=', field = '='; | |
| 129 | int p; | ||
| 130 | |||
| 131 | ✗ | av_frame_free(&s->frame[0]); | |
| 132 | ✗ | s->frame[0] = s->frame[1]; | |
| 133 | ✗ | s->frame[1] = s->frame[2]; | |
| 134 | ✗ | s->frame[2] = in; | |
| 135 | ✗ | if (!s->frame[1]) | |
| 136 | ✗ | return 0; | |
| 137 | ✗ | else if (!s->frame[0]) { | |
| 138 | ✗ | s->frame[0] = av_frame_clone(s->frame[1]); | |
| 139 | ✗ | if (!s->frame[0]) | |
| 140 | ✗ | return AVERROR(ENOMEM); | |
| 141 | } | ||
| 142 | |||
| 143 | while (1) { | ||
| 144 | ✗ | if (fgets(buf, sizeof(buf)-1, s->hint)) { | |
| 145 | ✗ | s->line++; | |
| 146 | ✗ | if (buf[0] == '#' || buf[0] == ';') { | |
| 147 | ✗ | continue; | |
| 148 | ✗ | } else if (sscanf(buf, "%"PRId64",%"PRId64" %c %c", &tf, &bf, &hint, &field) == 4) { | |
| 149 | ; | ||
| 150 | ✗ | } else if (sscanf(buf, "%"PRId64",%"PRId64" %c", &tf, &bf, &hint) == 3) { | |
| 151 | ; | ||
| 152 | ✗ | } else if (sscanf(buf, "%"PRId64",%"PRId64"", &tf, &bf) == 2) { | |
| 153 | ; | ||
| 154 | } else { | ||
| 155 | ✗ | av_log(ctx, AV_LOG_ERROR, "Invalid entry at line %"PRId64".\n", s->line); | |
| 156 | ✗ | return AVERROR_INVALIDDATA; | |
| 157 | } | ||
| 158 | ✗ | switch (s->mode) { | |
| 159 | ✗ | case ABSOLUTE_HINT: | |
| 160 | ✗ | if (tf > outl->frame_count_in + 1 || tf < FFMAX(0, outl->frame_count_in - 1) || | |
| 161 | ✗ | bf > outl->frame_count_in + 1 || bf < FFMAX(0, outl->frame_count_in - 1)) { | |
| 162 | ✗ | 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, inl->frame_count_out); | |
| 163 | ✗ | return AVERROR_INVALIDDATA; | |
| 164 | } | ||
| 165 | ✗ | break; | |
| 166 | ✗ | case PATTERN_HINT: | |
| 167 | case RELATIVE_HINT: | ||
| 168 | ✗ | if (tf > 1 || tf < -1 || | |
| 169 | ✗ | bf > 1 || bf < -1) { | |
| 170 | ✗ | 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, inl->frame_count_out); | |
| 171 | ✗ | return AVERROR_INVALIDDATA; | |
| 172 | } | ||
| 173 | ✗ | break; | |
| 174 | ✗ | default: | |
| 175 | ✗ | return AVERROR_BUG; | |
| 176 | }; | ||
| 177 | ✗ | break; | |
| 178 | } else { | ||
| 179 | ✗ | if (s->mode == PATTERN_HINT) { | |
| 180 | ✗ | fseek(s->hint, 0, SEEK_SET); | |
| 181 | ✗ | continue; | |
| 182 | } | ||
| 183 | ✗ | av_log(ctx, AV_LOG_ERROR, "Missing entry for %"PRId64". input frame.\n", inl->frame_count_out); | |
| 184 | ✗ | return AVERROR_INVALIDDATA; | |
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | ✗ | out = ff_get_video_buffer(outlink, outlink->w, outlink->h); | |
| 189 | ✗ | if (!out) | |
| 190 | ✗ | return AVERROR(ENOMEM); | |
| 191 | ✗ | av_frame_copy_props(out, s->frame[1]); | |
| 192 | |||
| 193 | ✗ | switch (s->mode) { | |
| 194 | ✗ | case ABSOLUTE_HINT: | |
| 195 | ✗ | top = s->frame[tf - outl->frame_count_in + 1]; | |
| 196 | ✗ | bottom = s->frame[bf - outl->frame_count_in + 1]; | |
| 197 | ✗ | break; | |
| 198 | ✗ | case PATTERN_HINT: | |
| 199 | case RELATIVE_HINT: | ||
| 200 | ✗ | top = s->frame[1 + tf]; | |
| 201 | ✗ | bottom = s->frame[1 + bf]; | |
| 202 | ✗ | break; | |
| 203 | ✗ | default: | |
| 204 | ✗ | av_assert0(0); | |
| 205 | } | ||
| 206 | |||
| 207 | ✗ | switch (field) { | |
| 208 | ✗ | case 'b': | |
| 209 | ✗ | tfactor = 1; | |
| 210 | ✗ | top = bottom; | |
| 211 | ✗ | break; | |
| 212 | ✗ | case 't': | |
| 213 | ✗ | bfactor = 0; | |
| 214 | ✗ | bottom = top; | |
| 215 | ✗ | break; | |
| 216 | ✗ | case '=': | |
| 217 | ✗ | break; | |
| 218 | ✗ | default: | |
| 219 | ✗ | av_log(ctx, AV_LOG_ERROR, "Invalid field: %c.\n", field); | |
| 220 | ✗ | av_frame_free(&out); | |
| 221 | ✗ | return AVERROR(EINVAL); | |
| 222 | } | ||
| 223 | |||
| 224 | ✗ | switch (hint) { | |
| 225 | ✗ | case '+': | |
| 226 | ✗ | out->flags |= AV_FRAME_FLAG_INTERLACED; | |
| 227 | ✗ | break; | |
| 228 | ✗ | case '-': | |
| 229 | ✗ | out->flags &= ~AV_FRAME_FLAG_INTERLACED; | |
| 230 | ✗ | break; | |
| 231 | ✗ | case '=': | |
| 232 | ✗ | break; | |
| 233 | ✗ | case 'b': | |
| 234 | ✗ | tfactor = 1; | |
| 235 | ✗ | top = bottom; | |
| 236 | ✗ | break; | |
| 237 | ✗ | case 't': | |
| 238 | ✗ | bfactor = 0; | |
| 239 | ✗ | bottom = top; | |
| 240 | ✗ | break; | |
| 241 | ✗ | default: | |
| 242 | ✗ | av_log(ctx, AV_LOG_ERROR, "Invalid hint: %c.\n", hint); | |
| 243 | ✗ | av_frame_free(&out); | |
| 244 | ✗ | return AVERROR(EINVAL); | |
| 245 | } | ||
| 246 | |||
| 247 | ✗ | for (p = 0; p < s->nb_planes; p++) { | |
| 248 | ✗ | av_image_copy_plane(out->data[p], | |
| 249 | ✗ | out->linesize[p] * 2, | |
| 250 | ✗ | top->data[p] + tfactor * top->linesize[p], | |
| 251 | ✗ | top->linesize[p] * 2, | |
| 252 | s->planewidth[p], | ||
| 253 | ✗ | (s->planeheight[p] + 1) / 2); | |
| 254 | ✗ | av_image_copy_plane(out->data[p] + out->linesize[p], | |
| 255 | ✗ | out->linesize[p] * 2, | |
| 256 | ✗ | bottom->data[p] + bfactor * bottom->linesize[p], | |
| 257 | ✗ | bottom->linesize[p] * 2, | |
| 258 | s->planewidth[p], | ||
| 259 | ✗ | (s->planeheight[p] + 1) / 2); | |
| 260 | } | ||
| 261 | |||
| 262 | ✗ | return ff_filter_frame(outlink, out); | |
| 263 | } | ||
| 264 | |||
| 265 | ✗ | static int request_frame(AVFilterLink *outlink) | |
| 266 | { | ||
| 267 | ✗ | AVFilterContext *ctx = outlink->src; | |
| 268 | ✗ | FieldHintContext *s = ctx->priv; | |
| 269 | int ret; | ||
| 270 | |||
| 271 | ✗ | if (s->eof) | |
| 272 | ✗ | return AVERROR_EOF; | |
| 273 | |||
| 274 | ✗ | ret = ff_request_frame(ctx->inputs[0]); | |
| 275 | ✗ | if (ret == AVERROR_EOF && s->frame[2]) { | |
| 276 | ✗ | AVFrame *next = av_frame_clone(s->frame[2]); | |
| 277 | ✗ | if (!next) | |
| 278 | ✗ | return AVERROR(ENOMEM); | |
| 279 | ✗ | ret = filter_frame(ctx->inputs[0], next); | |
| 280 | ✗ | s->eof = 1; | |
| 281 | } | ||
| 282 | |||
| 283 | ✗ | return ret; | |
| 284 | } | ||
| 285 | |||
| 286 | ✗ | static av_cold void uninit(AVFilterContext *ctx) | |
| 287 | { | ||
| 288 | ✗ | FieldHintContext *s = ctx->priv; | |
| 289 | |||
| 290 | ✗ | if (s->hint) | |
| 291 | ✗ | fclose(s->hint); | |
| 292 | ✗ | s->hint = NULL; | |
| 293 | |||
| 294 | ✗ | av_frame_free(&s->frame[0]); | |
| 295 | ✗ | av_frame_free(&s->frame[1]); | |
| 296 | ✗ | av_frame_free(&s->frame[2]); | |
| 297 | ✗ | } | |
| 298 | |||
| 299 | static const AVFilterPad inputs[] = { | ||
| 300 | { | ||
| 301 | .name = "default", | ||
| 302 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 303 | .config_props = config_input, | ||
| 304 | .filter_frame = filter_frame, | ||
| 305 | }, | ||
| 306 | }; | ||
| 307 | |||
| 308 | static const AVFilterPad outputs[] = { | ||
| 309 | { | ||
| 310 | .name = "default", | ||
| 311 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 312 | .request_frame = request_frame, | ||
| 313 | }, | ||
| 314 | }; | ||
| 315 | |||
| 316 | const FFFilter ff_vf_fieldhint = { | ||
| 317 | .p.name = "fieldhint", | ||
| 318 | .p.description = NULL_IF_CONFIG_SMALL("Field matching using hints."), | ||
| 319 | .p.priv_class = &fieldhint_class, | ||
| 320 | .priv_size = sizeof(FieldHintContext), | ||
| 321 | .init = init, | ||
| 322 | .uninit = uninit, | ||
| 323 | FILTER_INPUTS(inputs), | ||
| 324 | FILTER_OUTPUTS(outputs), | ||
| 325 | FILTER_QUERY_FUNC2(query_formats), | ||
| 326 | }; | ||
| 327 |