LCOV - code coverage report
Current view: top level - libavfilter - vf_hflip.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 99 104 95.2 %
Date: 2017-12-18 06:23:41 Functions: 11 11 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2007 Benoit Fouet
       3             :  * Copyright (c) 2010 Stefano Sabatini
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (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 GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : /**
      23             :  * @file
      24             :  * horizontal flip filter
      25             :  */
      26             : 
      27             : #include <string.h>
      28             : 
      29             : #include "libavutil/opt.h"
      30             : #include "avfilter.h"
      31             : #include "formats.h"
      32             : #include "hflip.h"
      33             : #include "internal.h"
      34             : #include "video.h"
      35             : #include "libavutil/pixdesc.h"
      36             : #include "libavutil/internal.h"
      37             : #include "libavutil/intreadwrite.h"
      38             : #include "libavutil/imgutils.h"
      39             : 
      40             : static const AVOption hflip_options[] = {
      41             :     { NULL }
      42             : };
      43             : 
      44             : AVFILTER_DEFINE_CLASS(hflip);
      45             : 
      46         137 : static int query_formats(AVFilterContext *ctx)
      47             : {
      48         137 :     AVFilterFormats *pix_fmts = NULL;
      49             :     int fmt, ret;
      50             : 
      51       25208 :     for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
      52       25071 :         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
      53       70555 :         if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
      54       23016 :               desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ||
      55       27126 :               (desc->log2_chroma_w != desc->log2_chroma_h &&
      56       26578 :                desc->comp[0].plane == desc->comp[1].plane)) &&
      57       21920 :             (ret = ff_add_format(&pix_fmts, fmt)) < 0)
      58           0 :             return ret;
      59             :     }
      60             : 
      61         137 :     return ff_set_common_formats(ctx, pix_fmts);
      62             : }
      63             : 
      64       16173 : static void hflip_byte_c(const uint8_t *src, uint8_t *dst, int w)
      65             : {
      66             :     int j;
      67             : 
      68     4739373 :     for (j = 0; j < w; j++)
      69     4723200 :         dst[j] = src[-j];
      70       16173 : }
      71             : 
      72       67197 : static void hflip_short_c(const uint8_t *ssrc, uint8_t *ddst, int w)
      73             : {
      74       67197 :     const uint16_t *src = (const uint16_t *)ssrc;
      75       67197 :     uint16_t *dst = (uint16_t *)ddst;
      76             :     int j;
      77             : 
      78    21127101 :     for (j = 0; j < w; j++)
      79    21059904 :         dst[j] = src[-j];
      80       67197 : }
      81             : 
      82        2592 : static void hflip_dword_c(const uint8_t *ssrc, uint8_t *ddst, int w)
      83             : {
      84        2592 :     const uint32_t *src = (const uint32_t *)ssrc;
      85        2592 :     uint32_t *dst = (uint32_t *)ddst;
      86             :     int j;
      87             : 
      88      864288 :     for (j = 0; j < w; j++)
      89      861696 :         dst[j] = src[-j];
      90        2592 : }
      91             : 
      92         576 : static void hflip_b24_c(const uint8_t *src, uint8_t *dst, int w)
      93             : {
      94         576 :     const uint8_t *in  = src;
      95         576 :     uint8_t *out = dst;
      96             :     int j;
      97             : 
      98      203328 :     for (j = 0; j < w; j++, out += 3, in -= 3) {
      99      202752 :         int32_t v = AV_RB24(in);
     100             : 
     101      202752 :         AV_WB24(out, v);
     102             :     }
     103         576 : }
     104             : 
     105        1728 : static void hflip_b48_c(const uint8_t *src, uint8_t *dst, int w)
     106             : {
     107        1728 :     const uint8_t *in  = src;
     108        1728 :     uint8_t *out = dst;
     109             :     int j;
     110             : 
     111      609984 :     for (j = 0; j < w; j++, out += 6, in -= 6) {
     112      608256 :         int64_t v = AV_RB48(in);
     113             : 
     114      608256 :         AV_WB48(out, v);
     115             :     }
     116        1728 : }
     117             : 
     118        1440 : static void hflip_qword_c(const uint8_t *ssrc, uint8_t *ddst, int w)
     119             : {
     120        1440 :     const uint64_t *src = (const uint64_t *)ssrc;
     121        1440 :     uint64_t *dst = (uint64_t *)ddst;
     122             :     int j;
     123             : 
     124      508320 :     for (j = 0; j < w; j++)
     125      506880 :         dst[j] = src[-j];
     126        1440 : }
     127             : 
     128         136 : static int config_props(AVFilterLink *inlink)
     129             : {
     130         136 :     FlipContext *s = inlink->dst->priv;
     131         136 :     const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
     132         136 :     const int hsub = pix_desc->log2_chroma_w;
     133         136 :     const int vsub = pix_desc->log2_chroma_h;
     134             :     int nb_planes;
     135             : 
     136         136 :     av_image_fill_max_pixsteps(s->max_step, NULL, pix_desc);
     137         136 :     s->planewidth[0]  = s->planewidth[3]  = inlink->w;
     138         136 :     s->planewidth[1]  = s->planewidth[2]  = AV_CEIL_RSHIFT(inlink->w, hsub);
     139         136 :     s->planeheight[0] = s->planeheight[3] = inlink->h;
     140         136 :     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
     141             : 
     142         136 :     nb_planes = av_pix_fmt_count_planes(inlink->format);
     143             : 
     144         136 :     return ff_hflip_init(s, s->max_step, nb_planes);
     145             : }
     146             : 
     147         162 : int ff_hflip_init(FlipContext *s, int step[4], int nb_planes)
     148             : {
     149             :     int i;
     150             : 
     151         602 :     for (i = 0; i < nb_planes; i++) {
     152         440 :         switch (step[i]) {
     153         112 :         case 1: s->flip_line[i] = hflip_byte_c;  break;
     154         305 :         case 2: s->flip_line[i] = hflip_short_c; break;
     155           2 :         case 3: s->flip_line[i] = hflip_b24_c;   break;
     156          10 :         case 4: s->flip_line[i] = hflip_dword_c; break;
     157           6 :         case 6: s->flip_line[i] = hflip_b48_c;   break;
     158           5 :         case 8: s->flip_line[i] = hflip_qword_c; break;
     159           0 :         default:
     160           0 :             return AVERROR_BUG;
     161             :         }
     162             :     }
     163             :     if (ARCH_X86)
     164         162 :         ff_hflip_init_x86(s, step, nb_planes);
     165             : 
     166         162 :     return 0;
     167             : }
     168             : 
     169             : typedef struct ThreadData {
     170             :     AVFrame *in, *out;
     171             : } ThreadData;
     172             : 
     173         136 : static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
     174             : {
     175         136 :     FlipContext *s = ctx->priv;
     176         136 :     ThreadData *td = arg;
     177         136 :     AVFrame *in = td->in;
     178         136 :     AVFrame *out = td->out;
     179             :     uint8_t *inrow, *outrow;
     180             :     int i, plane, step;
     181             : 
     182         472 :     for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++) {
     183         336 :         const int width  = s->planewidth[plane];
     184         336 :         const int height = s->planeheight[plane];
     185         336 :         const int start = (height *  job   ) / nb_jobs;
     186         336 :         const int end   = (height * (job+1)) / nb_jobs;
     187             : 
     188         336 :         step = s->max_step[plane];
     189             : 
     190         336 :         outrow = out->data[plane] + start * out->linesize[plane];
     191         336 :         inrow  = in ->data[plane] + start * in->linesize[plane] + (width - 1) * step;
     192       88896 :         for (i = start; i < end; i++) {
     193       88560 :             s->flip_line[plane](inrow, outrow, width);
     194             : 
     195       88560 :             inrow  += in ->linesize[plane];
     196       88560 :             outrow += out->linesize[plane];
     197             :         }
     198             :     }
     199             : 
     200         136 :     return 0;
     201             : }
     202             : 
     203         136 : static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     204             : {
     205         136 :     AVFilterContext *ctx  = inlink->dst;
     206         136 :     AVFilterLink *outlink = ctx->outputs[0];
     207             :     ThreadData td;
     208             :     AVFrame *out;
     209             : 
     210         136 :     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     211         136 :     if (!out) {
     212           0 :         av_frame_free(&in);
     213           0 :         return AVERROR(ENOMEM);
     214             :     }
     215         136 :     av_frame_copy_props(out, in);
     216             : 
     217             :     /* copy palette if required */
     218         136 :     if (av_pix_fmt_desc_get(inlink->format)->flags & AV_PIX_FMT_FLAG_PAL)
     219           1 :         memcpy(out->data[1], in->data[1], AVPALETTE_SIZE);
     220             : 
     221         136 :     td.in = in, td.out = out;
     222         136 :     ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
     223             : 
     224         136 :     av_frame_free(&in);
     225         136 :     return ff_filter_frame(outlink, out);
     226             : }
     227             : 
     228             : static const AVFilterPad avfilter_vf_hflip_inputs[] = {
     229             :     {
     230             :         .name         = "default",
     231             :         .type         = AVMEDIA_TYPE_VIDEO,
     232             :         .filter_frame = filter_frame,
     233             :         .config_props = config_props,
     234             :     },
     235             :     { NULL }
     236             : };
     237             : 
     238             : static const AVFilterPad avfilter_vf_hflip_outputs[] = {
     239             :     {
     240             :         .name = "default",
     241             :         .type = AVMEDIA_TYPE_VIDEO,
     242             :     },
     243             :     { NULL }
     244             : };
     245             : 
     246             : AVFilter ff_vf_hflip = {
     247             :     .name          = "hflip",
     248             :     .description   = NULL_IF_CONFIG_SMALL("Horizontally flip the input video."),
     249             :     .priv_size     = sizeof(FlipContext),
     250             :     .priv_class    = &hflip_class,
     251             :     .query_formats = query_formats,
     252             :     .inputs        = avfilter_vf_hflip_inputs,
     253             :     .outputs       = avfilter_vf_hflip_outputs,
     254             :     .flags         = AVFILTER_FLAG_SLICE_THREADS | AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
     255             : };

Generated by: LCOV version 1.13