Merge commit 'c9943f00cfa2471d1b8a3a9ddc7a21049a71090e'
authorHendrik Leppkes <h.leppkes@gmail.com>
Tue, 29 Sep 2015 13:44:14 +0000 (15:44 +0200)
committerHendrik Leppkes <h.leppkes@gmail.com>
Tue, 29 Sep 2015 13:44:14 +0000 (15:44 +0200)
* commit 'c9943f00cfa2471d1b8a3a9ddc7a21049a71090e':
  vf_framepack: Use av_image_copy() where appropriate

Merged-by: Hendrik Leppkes <h.leppkes@gmail.com>
1  2 
libavfilter/vf_framepack.c

@@@ -1,20 -1,20 +1,20 @@@
  /*
   * Copyright (c) 2013 Vittorio Giovara
   *
 - * This file is part of Libav.
 + * This file is part of FFmpeg.
   *
 - * Libav is free software; you can redistribute it and/or
 + * 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.
   *
 - * Libav is distributed in the hope that it will be useful,
 + * 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 Libav; if not, write to the Free Software
 + * License along with FFmpeg; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
   */
  
@@@ -61,10 -61,8 +61,10 @@@ static const enum AVPixelFormat formats
  static int query_formats(AVFilterContext *ctx)
  {
      // this will ensure that formats are the same on all pads
 -    ff_set_common_formats(ctx, ff_make_format_list(formats_supported));
 -    return 0;
 +    AVFilterFormats *fmts_list = ff_make_format_list(formats_supported);
 +    if (!fmts_list)
 +        return AVERROR(ENOMEM);
 +    return ff_set_common_formats(ctx, fmts_list);
  }
  
  static av_cold void framepack_uninit(AVFilterContext *ctx)
@@@ -84,7 -82,6 +84,7 @@@ static int config_output(AVFilterLink *
      int width            = ctx->inputs[LEFT]->w;
      int height           = ctx->inputs[LEFT]->h;
      AVRational time_base = ctx->inputs[LEFT]->time_base;
 +    AVRational frame_rate = ctx->inputs[LEFT]->frame_rate;
  
      // check size and fps match on the other input
      if (width  != ctx->inputs[RIGHT]->w ||
          return AVERROR_INVALIDDATA;
      } else if (av_cmp_q(time_base, ctx->inputs[RIGHT]->time_base) != 0) {
          av_log(ctx, AV_LOG_ERROR,
 -               "Left and right framerates differ (%d/%d vs %d/%d).\n",
 +               "Left and right time bases differ (%d/%d vs %d/%d).\n",
                 time_base.num, time_base.den,
                 ctx->inputs[RIGHT]->time_base.num,
                 ctx->inputs[RIGHT]->time_base.den);
          return AVERROR_INVALIDDATA;
 +    } else if (av_cmp_q(frame_rate, ctx->inputs[RIGHT]->frame_rate) != 0) {
 +        av_log(ctx, AV_LOG_ERROR,
 +               "Left and right framerates differ (%d/%d vs %d/%d).\n",
 +               frame_rate.num, frame_rate.den,
 +               ctx->inputs[RIGHT]->frame_rate.num,
 +               ctx->inputs[RIGHT]->frame_rate.den);
 +        return AVERROR_INVALIDDATA;
      }
  
      s->pix_desc = av_pix_fmt_desc_get(outlink->format);
      switch (s->format) {
      case AV_STEREO3D_FRAMESEQUENCE:
          time_base.den *= 2;
 +        frame_rate.num *= 2;
 +
          s->double_pts = AV_NOPTS_VALUE;
          break;
      case AV_STEREO3D_COLUMNS:
      outlink->w         = width;
      outlink->h         = height;
      outlink->time_base = time_base;
 +    outlink->frame_rate= frame_rate;
  
      return 0;
  }
  
- static void horizontal_frame_pack(FramepackContext *s,
-                                   AVFrame *dst,
+ static void horizontal_frame_pack(AVFilterLink *outlink,
+                                   AVFrame *out,
                                    int interleaved)
  {
-     int plane, i;
-     int length = dst->width / 2;
-     int lines  = dst->height;
-     for (plane = 0; plane < s->pix_desc->nb_components; plane++) {
-         const uint8_t *leftp  = s->input_views[LEFT]->data[plane];
-         const uint8_t *rightp = s->input_views[RIGHT]->data[plane];
-         uint8_t *dstp         = dst->data[plane];
-         if (plane == 1 || plane == 2) {
-             length = FF_CEIL_RSHIFT(dst->width / 2, s->pix_desc->log2_chroma_w);
-             lines  = FF_CEIL_RSHIFT(dst->height,    s->pix_desc->log2_chroma_h);
-         }
-         if (interleaved) {
+     AVFilterContext *ctx = outlink->src;
+     FramepackContext *s = ctx->priv;
+     int i, plane;
+     if (interleaved) {
+         const uint8_t *leftp  = s->input_views[LEFT]->data[0];
+         const uint8_t *rightp = s->input_views[RIGHT]->data[0];
+         uint8_t *dstp         = out->data[0];
+         int length = out->width / 2;
+         int lines  = out->height;
+         for (plane = 0; plane < s->pix_desc->nb_components; plane++) {
+             if (plane == 1 || plane == 2) {
 -                length = -(-(out->width / 2) >> s->pix_desc->log2_chroma_w);
 -                lines  = -(-(out->height)    >> s->pix_desc->log2_chroma_h);
++                length = FF_CEIL_RSHIFT(out->width / 2, s->pix_desc->log2_chroma_w);
++                lines  = FF_CEIL_RSHIFT(out->height,    s->pix_desc->log2_chroma_h);
+             }
              for (i = 0; i < lines; i++) {
                  int j;
-                 int k = 0;
+                 leftp  = s->input_views[LEFT]->data[plane] +
+                          s->input_views[LEFT]->linesize[plane] * i;
+                 rightp = s->input_views[RIGHT]->data[plane] +
+                          s->input_views[RIGHT]->linesize[plane] * i;
+                 dstp   = out->data[plane] + out->linesize[plane] * i;
                  for (j = 0; j < length; j++) {
-                     dstp[k++] = leftp[j];
-                     dstp[k++] = rightp[j];
+                     // interpolate chroma as necessary
+                     if ((s->pix_desc->log2_chroma_w ||
+                          s->pix_desc->log2_chroma_h) &&
+                         (plane == 1 || plane == 2)) {
+                         *dstp++ = (*leftp + *rightp) / 2;
+                         *dstp++ = (*leftp + *rightp) / 2;
+                     } else {
+                         *dstp++ = *leftp;
+                         *dstp++ = *rightp;
+                     }
+                     leftp += 1;
+                     rightp += 1;
                  }
-                 dstp   += dst->linesize[plane];
-                 leftp  += s->input_views[LEFT]->linesize[plane];
-                 rightp += s->input_views[RIGHT]->linesize[plane];
              }
-         } else {
-             av_image_copy_plane(dst->data[plane], dst->linesize[plane],
-                                 leftp, s->input_views[LEFT]->linesize[plane],
-                                 length, lines);
-             av_image_copy_plane(dst->data[plane] + length, dst->linesize[plane],
-                                 rightp, s->input_views[RIGHT]->linesize[plane],
-                                 length, lines);
+         }
+     } else {
+         for (i = 0; i < 2; i++) {
+             const uint8_t *src[4];
+             uint8_t *dst[4];
+             int sub_w = s->input_views[i]->width >> s->pix_desc->log2_chroma_w;
+             src[0] = s->input_views[i]->data[0];
+             src[1] = s->input_views[i]->data[1];
+             src[2] = s->input_views[i]->data[2];
+             dst[0] = out->data[0] + i * s->input_views[i]->width;
+             dst[1] = out->data[1] + i * sub_w;
+             dst[2] = out->data[2] + i * sub_w;
+             av_image_copy(dst, out->linesize, src, s->input_views[i]->linesize,
+                           s->input_views[i]->format,
+                           s->input_views[i]->width,
+                           s->input_views[i]->height);
          }
      }
  }
  
- static void vertical_frame_pack(FramepackContext *s,
-                                 AVFrame *dst,
+ static void vertical_frame_pack(AVFilterLink *outlink,
+                                 AVFrame *out,
                                  int interleaved)
  {
-     int plane, offset;
-     int length = dst->width;
-     int lines  = dst->height / 2;
-     for (plane = 0; plane < s->pix_desc->nb_components; plane++) {
-         if (plane == 1 || plane == 2) {
-             length = -(-(dst->width)      >> s->pix_desc->log2_chroma_w);
-             lines  = -(-(dst->height / 2) >> s->pix_desc->log2_chroma_h);
-         }
+     AVFilterContext *ctx = outlink->src;
+     FramepackContext *s = ctx->priv;
+     int i;
  
-         offset = interleaved ? dst->linesize[plane] : dst->linesize[plane] * lines;
-         av_image_copy_plane(dst->data[plane],
-                             dst->linesize[plane] << interleaved,
-                             s->input_views[LEFT]->data[plane],
-                             s->input_views[LEFT]->linesize[plane],
-                             length, lines);
-         av_image_copy_plane(dst->data[plane] + offset,
-                             dst->linesize[plane] << interleaved,
-                             s->input_views[RIGHT]->data[plane],
-                             s->input_views[RIGHT]->linesize[plane],
-                             length, lines);
+     for (i = 0; i < 2; i++) {
+         const uint8_t *src[4];
+         uint8_t *dst[4];
+         int linesizes[4];
+         int sub_h = s->input_views[i]->height >> s->pix_desc->log2_chroma_h;
+         src[0] = s->input_views[i]->data[0];
+         src[1] = s->input_views[i]->data[1];
+         src[2] = s->input_views[i]->data[2];
+         dst[0] = out->data[0] + i * out->linesize[0] *
+                  (interleaved + s->input_views[i]->height * (1 - interleaved));
+         dst[1] = out->data[1] + i * out->linesize[1] *
+                  (interleaved + sub_h * (1 - interleaved));
+         dst[2] = out->data[2] + i * out->linesize[2] *
+                  (interleaved + sub_h * (1 - interleaved));
+         linesizes[0] = out->linesize[0] +
+                        interleaved * out->linesize[0];
+         linesizes[1] = out->linesize[1] +
+                        interleaved * out->linesize[1];
+         linesizes[2] = out->linesize[2] +
+                        interleaved * out->linesize[2];
+         av_image_copy(dst, linesizes, src, s->input_views[i]->linesize,
+                       s->input_views[i]->format,
+                       s->input_views[i]->width,
+                       s->input_views[i]->height);
      }
  }
  
- static av_always_inline void spatial_frame_pack(FramepackContext *s, AVFrame *dst)
+ static av_always_inline void spatial_frame_pack(AVFilterLink *outlink,
+                                                 AVFrame *dst)
  {
+     AVFilterContext *ctx = outlink->src;
+     FramepackContext *s = ctx->priv;
      switch (s->format) {
      case AV_STEREO3D_SIDEBYSIDE:
-         horizontal_frame_pack(s, dst, 0);
+         horizontal_frame_pack(outlink, dst, 0);
          break;
      case AV_STEREO3D_COLUMNS:
-         horizontal_frame_pack(s, dst, 1);
+         horizontal_frame_pack(outlink, dst, 1);
          break;
      case AV_STEREO3D_TOPBOTTOM:
-         vertical_frame_pack(s, dst, 0);
+         vertical_frame_pack(outlink, dst, 0);
          break;
      case AV_STEREO3D_LINES:
-         vertical_frame_pack(s, dst, 1);
+         vertical_frame_pack(outlink, dst, 1);
          break;
      }
  }
@@@ -289,7 -311,7 +324,7 @@@ static int request_frame(AVFilterLink *
          if (!dst)
              return AVERROR(ENOMEM);
  
-         spatial_frame_pack(s, dst);
+         spatial_frame_pack(outlink, dst);
  
          // get any property from the original frame
          ret = av_frame_copy_props(dst, s->input_views[LEFT]);
  
  #define OFFSET(x) offsetof(FramepackContext, x)
  #define V AV_OPT_FLAG_VIDEO_PARAM
 -static const AVOption options[] = {
 +static const AVOption framepack_options[] = {
      { "format", "Frame pack output format", OFFSET(format), AV_OPT_TYPE_INT,
          { .i64 = AV_STEREO3D_SIDEBYSIDE }, 0, INT_MAX, .flags = V, .unit = "format" },
      { "sbs", "Views are packed next to each other", 0, AV_OPT_TYPE_CONST,
      { NULL },
  };
  
 -static const AVClass framepack_class = {
 -    .class_name = "framepack",
 -    .item_name  = av_default_item_name,
 -    .option     = options,
 -    .version    = LIBAVUTIL_VERSION_INT,
 -};
 +AVFILTER_DEFINE_CLASS(framepack);
  
  static const AVFilterPad framepack_inputs[] = {
      {