LCOV - code coverage report
Current view: top level - libavfilter - vf_fieldhint.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 131 0.0 %
Date: 2017-12-10 21:22:29 Functions: 0 6 0.0 %

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

Generated by: LCOV version 1.13