libavfilter/vf_hflip.c
a1e171df
 /*
  * Copyright (c) 2007 Benoit Fouet
  * Copyright (c) 2010 Stefano Sabatini
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
7fe82bd7
  * @file
a1e171df
  * horizontal flip filter
  */
 
1d9c2dc8
 #include <string.h>
 
a1e171df
 #include "avfilter.h"
b74a1da4
 #include "formats.h"
9d0bfc50
 #include "internal.h"
803391f7
 #include "video.h"
a1e171df
 #include "libavutil/pixdesc.h"
1d9c2dc8
 #include "libavutil/internal.h"
a1e171df
 #include "libavutil/intreadwrite.h"
7ffe76e5
 #include "libavutil/imgutils.h"
a1e171df
 
 typedef struct {
     int max_step[4];    ///< max pixel step for each plane, expressed as a number of bytes
2a6a3d43
     int planewidth[4];  ///< width of each plane
     int planeheight[4]; ///< height of each plane
a1e171df
 } FlipContext;
 
 static int query_formats(AVFilterContext *ctx)
 {
e0ccb5fa
     AVFilterFormats *pix_fmts = NULL;
     int fmt;
a1e171df
 
e0ccb5fa
     for (fmt = 0; fmt < AV_PIX_FMT_NB; fmt++) {
         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
c7c71f95
         if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
               desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ||
e0ccb5fa
               (desc->log2_chroma_w != desc->log2_chroma_h &&
                desc->comp[0].plane == desc->comp[1].plane)))
             ff_add_format(&pix_fmts, fmt);
     }
 
     ff_set_common_formats(ctx, pix_fmts);
a1e171df
     return 0;
 }
 
 static int config_props(AVFilterLink *inlink)
 {
7af5ae2d
     FlipContext *s = inlink->dst->priv;
59ee9f78
     const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
2a6a3d43
     const int hsub = pix_desc->log2_chroma_w;
     const int vsub = pix_desc->log2_chroma_h;
a1e171df
 
7af5ae2d
     av_image_fill_max_pixsteps(s->max_step, NULL, pix_desc);
2a6a3d43
     s->planewidth[0]  = s->planewidth[3]  = inlink->w;
     s->planewidth[1]  = s->planewidth[2]  = FF_CEIL_RSHIFT(inlink->w, hsub);
     s->planeheight[0] = s->planeheight[3] = inlink->h;
     s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, vsub);
a1e171df
 
     return 0;
 }
 
0a4aea6d
 typedef struct ThreadData {
     AVFrame *in, *out;
 } ThreadData;
 
 static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
ad12e9e8
 {
0a4aea6d
     FlipContext *s = ctx->priv;
     ThreadData *td = arg;
     AVFrame *in = td->in;
     AVFrame *out = td->out;
eb7e7e82
     uint8_t *inrow, *outrow;
50e66726
     int i, j, plane, step;
ad12e9e8
 
e43a0a23
     for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++) {
2a6a3d43
         const int width  = s->planewidth[plane];
         const int height = s->planeheight[plane];
0a4aea6d
         const int start = (height *  job   ) / nb_jobs;
         const int end   = (height * (job+1)) / nb_jobs;
 
7af5ae2d
         step = s->max_step[plane];
a1e171df
 
0a4aea6d
         outrow = out->data[plane] + start * out->linesize[plane];
         inrow  = in ->data[plane] + start * in->linesize[plane] + (width - 1) * step;
         for (i = start; i < end; i++) {
a1e171df
             switch (step) {
             case 1:
50e66726
                 for (j = 0; j < width; j++)
a1e171df
                     outrow[j] = inrow[-j];
             break;
 
             case 2:
             {
                 uint16_t *outrow16 = (uint16_t *)outrow;
                 uint16_t * inrow16 = (uint16_t *) inrow;
50e66726
                 for (j = 0; j < width; j++)
a1e171df
                     outrow16[j] = inrow16[-j];
             }
             break;
 
             case 3:
             {
                 uint8_t *in  =  inrow;
                 uint8_t *out = outrow;
50e66726
                 for (j = 0; j < width; j++, out += 3, in -= 3) {
a1e171df
                     int32_t v = AV_RB24(in);
                     AV_WB24(out, v);
                 }
             }
             break;
 
             case 4:
             {
                 uint32_t *outrow32 = (uint32_t *)outrow;
                 uint32_t * inrow32 = (uint32_t *) inrow;
50e66726
                 for (j = 0; j < width; j++)
a1e171df
                     outrow32[j] = inrow32[-j];
             }
             break;
 
             default:
50e66726
                 for (j = 0; j < width; j++)
a1e171df
                     memcpy(outrow + j*step, inrow - j*step, step);
             }
 
eb7e7e82
             inrow  += in ->linesize[plane];
             outrow += out->linesize[plane];
a1e171df
         }
     }
 
0a4aea6d
     return 0;
 }
 
 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 {
     AVFilterContext *ctx  = inlink->dst;
     AVFilterLink *outlink = ctx->outputs[0];
     ThreadData td;
     AVFrame *out;
 
     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     if (!out) {
         av_frame_free(&in);
         return AVERROR(ENOMEM);
     }
     av_frame_copy_props(out, in);
 
     /* copy palette if required */
     if (av_pix_fmt_desc_get(inlink->format)->flags & AV_PIX_FMT_FLAG_PAL)
         memcpy(out->data[1], in->data[1], AVPALETTE_SIZE);
 
     td.in = in, td.out = out;
     ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outlink->h, ctx->graph->nb_threads));
 
7e350379
     av_frame_free(&in);
eb7e7e82
     return ff_filter_frame(outlink, out);
a1e171df
 }
 
568c70e7
 static const AVFilterPad avfilter_vf_hflip_inputs[] = {
     {
         .name         = "default",
         .type         = AVMEDIA_TYPE_VIDEO,
eb7e7e82
         .filter_frame = filter_frame,
568c70e7
         .config_props = config_props,
     },
     { NULL }
 };
 
 static const AVFilterPad avfilter_vf_hflip_outputs[] = {
     {
         .name = "default",
         .type = AVMEDIA_TYPE_VIDEO,
     },
     { NULL }
 };
 
cd43ca04
 AVFilter ff_vf_hflip = {
b211607b
     .name          = "hflip",
     .description   = NULL_IF_CONFIG_SMALL("Horizontally flip the input video."),
     .priv_size     = sizeof(FlipContext),
a1e171df
     .query_formats = query_formats,
b211607b
     .inputs        = avfilter_vf_hflip_inputs,
     .outputs       = avfilter_vf_hflip_outputs,
0a4aea6d
     .flags         = AVFILTER_FLAG_SLICE_THREADS,
a1e171df
 };