LCOV - code coverage report
Current view: top level - libavfilter - vf_pp.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 46 65 70.8 %
Date: 2017-12-13 10:57:33 Functions: 5 6 83.3 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2002 A'rpi
       3             :  * Copyright (C) 2012 Clément Bœsch
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or modify
       8             :  * it under the terms of the GNU General Public License as published by
       9             :  * the Free Software Foundation; either version 2 of the License, or
      10             :  * (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :  * GNU General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU General Public License along
      18             :  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
      19             :  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      20             :  */
      21             : 
      22             : /**
      23             :  * @file
      24             :  * libpostproc filter, ported from MPlayer.
      25             :  */
      26             : 
      27             : #include "libavutil/avassert.h"
      28             : #include "libavutil/opt.h"
      29             : #include "internal.h"
      30             : 
      31             : #include "libpostproc/postprocess.h"
      32             : 
      33             : typedef struct PPFilterContext {
      34             :     const AVClass *class;
      35             :     char *subfilters;
      36             :     int mode_id;
      37             :     pp_mode *modes[PP_QUALITY_MAX + 1];
      38             :     void *pp_ctx;
      39             : } PPFilterContext;
      40             : 
      41             : #define OFFSET(x) offsetof(PPFilterContext, x)
      42             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
      43             : static const AVOption pp_options[] = {
      44             :     { "subfilters", "set postprocess subfilters", OFFSET(subfilters), AV_OPT_TYPE_STRING, {.str="de"}, .flags = FLAGS },
      45             :     { NULL }
      46             : };
      47             : 
      48             : AVFILTER_DEFINE_CLASS(pp);
      49             : 
      50           8 : static av_cold int pp_init(AVFilterContext *ctx)
      51             : {
      52             :     int i;
      53           8 :     PPFilterContext *pp = ctx->priv;
      54             : 
      55          64 :     for (i = 0; i <= PP_QUALITY_MAX; i++) {
      56          56 :         pp->modes[i] = pp_get_mode_by_name_and_quality(pp->subfilters, i);
      57          56 :         if (!pp->modes[i])
      58           0 :             return AVERROR_EXTERNAL;
      59             :     }
      60           8 :     pp->mode_id = PP_QUALITY_MAX;
      61           8 :     return 0;
      62             : }
      63             : 
      64           0 : static int pp_process_command(AVFilterContext *ctx, const char *cmd, const char *args,
      65             :                               char *res, int res_len, int flags)
      66             : {
      67           0 :     PPFilterContext *pp = ctx->priv;
      68             : 
      69           0 :     if (!strcmp(cmd, "quality")) {
      70           0 :         pp->mode_id = av_clip(strtol(args, NULL, 10), 0, PP_QUALITY_MAX);
      71           0 :         return 0;
      72             :     }
      73           0 :     return AVERROR(ENOSYS);
      74             : }
      75             : 
      76           8 : static int pp_query_formats(AVFilterContext *ctx)
      77             : {
      78             :     static const enum AVPixelFormat pix_fmts[] = {
      79             :         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
      80             :         AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
      81             :         AV_PIX_FMT_YUV411P,
      82             :         AV_PIX_FMT_GBRP,
      83             :         AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
      84             :         AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
      85             :         AV_PIX_FMT_GRAY8,
      86             :         AV_PIX_FMT_NONE
      87             :     };
      88             : 
      89           8 :     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
      90           8 :     if (!fmts_list)
      91           0 :         return AVERROR(ENOMEM);
      92           8 :     return ff_set_common_formats(ctx, fmts_list);
      93             : }
      94             : 
      95           8 : static int pp_config_props(AVFilterLink *inlink)
      96             : {
      97           8 :     int flags = PP_CPU_CAPS_AUTO;
      98           8 :     PPFilterContext *pp = inlink->dst->priv;
      99             : 
     100           8 :     switch (inlink->format) {
     101           8 :     case AV_PIX_FMT_GRAY8:
     102             :     case AV_PIX_FMT_YUVJ420P:
     103           8 :     case AV_PIX_FMT_YUV420P: flags |= PP_FORMAT_420; break;
     104           0 :     case AV_PIX_FMT_YUVJ422P:
     105           0 :     case AV_PIX_FMT_YUV422P: flags |= PP_FORMAT_422; break;
     106           0 :     case AV_PIX_FMT_YUV411P: flags |= PP_FORMAT_411; break;
     107           0 :     case AV_PIX_FMT_GBRP:
     108             :     case AV_PIX_FMT_YUVJ444P:
     109           0 :     case AV_PIX_FMT_YUV444P: flags |= PP_FORMAT_444; break;
     110           0 :     case AV_PIX_FMT_YUVJ440P:
     111           0 :     case AV_PIX_FMT_YUV440P: flags |= PP_FORMAT_440; break;
     112           0 :     default: av_assert0(0);
     113             :     }
     114             : 
     115           8 :     pp->pp_ctx = pp_get_context(inlink->w, inlink->h, flags);
     116           8 :     if (!pp->pp_ctx)
     117           0 :         return AVERROR(ENOMEM);
     118           8 :     return 0;
     119             : }
     120             : 
     121          41 : static int pp_filter_frame(AVFilterLink *inlink, AVFrame *inbuf)
     122             : {
     123          41 :     AVFilterContext *ctx = inlink->dst;
     124          41 :     PPFilterContext *pp = ctx->priv;
     125          41 :     AVFilterLink *outlink = ctx->outputs[0];
     126          41 :     const int aligned_w = FFALIGN(outlink->w, 8);
     127          41 :     const int aligned_h = FFALIGN(outlink->h, 8);
     128             :     AVFrame *outbuf;
     129             :     int qstride, qp_type;
     130             :     int8_t *qp_table ;
     131             : 
     132          41 :     outbuf = ff_get_video_buffer(outlink, aligned_w, aligned_h);
     133          41 :     if (!outbuf) {
     134           0 :         av_frame_free(&inbuf);
     135           0 :         return AVERROR(ENOMEM);
     136             :     }
     137          41 :     av_frame_copy_props(outbuf, inbuf);
     138          41 :     outbuf->width  = inbuf->width;
     139          41 :     outbuf->height = inbuf->height;
     140          41 :     qp_table = av_frame_get_qp_table(inbuf, &qstride, &qp_type);
     141             : 
     142         123 :     pp_postprocess((const uint8_t **)inbuf->data, inbuf->linesize,
     143          41 :                    outbuf->data,                 outbuf->linesize,
     144             :                    aligned_w, outlink->h,
     145             :                    qp_table,
     146             :                    qstride,
     147          41 :                    pp->modes[pp->mode_id],
     148          41 :                    pp->pp_ctx,
     149          41 :                    outbuf->pict_type | (qp_type ? PP_PICT_TYPE_QP2 : 0));
     150             : 
     151          41 :     av_frame_free(&inbuf);
     152          41 :     return ff_filter_frame(outlink, outbuf);
     153             : }
     154             : 
     155           8 : static av_cold void pp_uninit(AVFilterContext *ctx)
     156             : {
     157             :     int i;
     158           8 :     PPFilterContext *pp = ctx->priv;
     159             : 
     160          64 :     for (i = 0; i <= PP_QUALITY_MAX; i++)
     161          56 :         pp_free_mode(pp->modes[i]);
     162           8 :     if (pp->pp_ctx)
     163           8 :         pp_free_context(pp->pp_ctx);
     164           8 : }
     165             : 
     166             : static const AVFilterPad pp_inputs[] = {
     167             :     {
     168             :         .name         = "default",
     169             :         .type         = AVMEDIA_TYPE_VIDEO,
     170             :         .config_props = pp_config_props,
     171             :         .filter_frame = pp_filter_frame,
     172             :     },
     173             :     { NULL }
     174             : };
     175             : 
     176             : static const AVFilterPad pp_outputs[] = {
     177             :     {
     178             :         .name = "default",
     179             :         .type = AVMEDIA_TYPE_VIDEO,
     180             :     },
     181             :     { NULL }
     182             : };
     183             : 
     184             : AVFilter ff_vf_pp = {
     185             :     .name            = "pp",
     186             :     .description     = NULL_IF_CONFIG_SMALL("Filter video using libpostproc."),
     187             :     .priv_size       = sizeof(PPFilterContext),
     188             :     .init            = pp_init,
     189             :     .uninit          = pp_uninit,
     190             :     .query_formats   = pp_query_formats,
     191             :     .inputs          = pp_inputs,
     192             :     .outputs         = pp_outputs,
     193             :     .process_command = pp_process_command,
     194             :     .priv_class      = &pp_class,
     195             :     .flags           = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
     196             : };

Generated by: LCOV version 1.13