Merge commit '09cb75cdeba420d680bcb165a1ef668d8f381fed'
authorMichael Niedermayer <michaelni@gmx.at>
Mon, 9 Dec 2013 21:04:29 +0000 (22:04 +0100)
committerMichael Niedermayer <michaelni@gmx.at>
Mon, 9 Dec 2013 21:04:29 +0000 (22:04 +0100)
* commit '09cb75cdeba420d680bcb165a1ef668d8f381fed':
  libx264: set frame packing information when relevant information is found

Conflicts:
libavcodec/libx264.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>
1  2 
libavcodec/libx264.c

diff --combined libavcodec/libx264.c
@@@ -2,20 -2,20 +2,20 @@@
   * H.264 encoding using the x264 library
   * Copyright (C) 2005  Mans Rullgard <mans@mansr.com>
   *
 - * 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
   */
  
@@@ -23,6 -23,7 +23,7 @@@
  #include "libavutil/opt.h"
  #include "libavutil/mem.h"
  #include "libavutil/pixdesc.h"
+ #include "libavutil/stereo3d.h"
  #include "avcodec.h"
  #include "internal.h"
  
@@@ -47,10 -48,7 +48,10 @@@ typedef struct X264Context 
      char *preset;
      char *tune;
      char *profile;
 +    char *level;
      int fastfirstpass;
 +    char *wpredp;
 +    char *x264opts;
      float crf;
      float crf_max;
      int cqp;
@@@ -110,21 -108,16 +111,21 @@@ static int encode_nals(AVCodecContext *
      for (i = 0; i < nnal; i++)
          size += nals[i].i_payload;
  
 -    if ((ret = ff_alloc_packet(pkt, size)) < 0)
 +    if ((ret = ff_alloc_packet2(ctx, pkt, size)) < 0)
          return ret;
  
      p = pkt->data;
  
      /* Write the SEI as part of the first frame. */
      if (x4->sei_size > 0 && nnal > 0) {
 +        if (x4->sei_size > size) {
 +            av_log(ctx, AV_LOG_ERROR, "Error: nal buffer is too small\n");
 +            return -1;
 +        }
          memcpy(p, x4->sei, x4->sei_size);
          p += x4->sei_size;
          x4->sei_size = 0;
 +        av_freep(&x4->sei);
      }
  
      for (i = 0; i < nnal; i++){
      return 1;
  }
  
 +static int avfmt2_num_planes(int avfmt)
 +{
 +    switch (avfmt) {
 +    case AV_PIX_FMT_YUV420P:
 +    case AV_PIX_FMT_YUVJ420P:
 +    case AV_PIX_FMT_YUV420P9:
 +    case AV_PIX_FMT_YUV420P10:
 +    case AV_PIX_FMT_YUV444P:
 +        return 3;
 +
 +    case AV_PIX_FMT_BGR24:
 +    case AV_PIX_FMT_RGB24:
 +        return 1;
 +
 +    default:
 +        return 3;
 +    }
 +}
 +
  static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame,
                        int *got_packet)
  {
      X264Context *x4 = ctx->priv_data;
      x264_nal_t *nal;
      int nnal, i, ret;
 -    x264_picture_t pic_out;
 +    x264_picture_t pic_out = {0};
+     AVFrameSideData *side_data;
  
      x264_picture_init( &x4->pic );
      x4->pic.img.i_csp   = x4->params.i_csp;
      if (x264_bit_depth > 8)
          x4->pic.img.i_csp |= X264_CSP_HIGH_DEPTH;
 -    x4->pic.img.i_plane = 3;
 +    x4->pic.img.i_plane = avfmt2_num_planes(ctx->pix_fmt);
  
      if (frame) {
 -        for (i = 0; i < 3; i++) {
 +        for (i = 0; i < x4->pic.img.i_plane; i++) {
              x4->pic.img.plane[i]    = frame->data[i];
              x4->pic.img.i_stride[i] = frame->linesize[i];
          }
              frame->pict_type == AV_PICTURE_TYPE_P ? X264_TYPE_P :
              frame->pict_type == AV_PICTURE_TYPE_B ? X264_TYPE_B :
                                              X264_TYPE_AUTO;
 -        if (x4->params.b_tff != frame->top_field_first) {
 +        if (x4->params.b_interlaced && x4->params.b_tff != frame->top_field_first) {
              x4->params.b_tff = frame->top_field_first;
              x264_encoder_reconfig(x4->enc, &x4->params);
          }
              x4->params.vui.i_sar_width  = ctx->sample_aspect_ratio.num;
              x264_encoder_reconfig(x4->enc, &x4->params);
          }
-     }
  
+         side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_STEREO3D);
+         if (side_data) {
+             AVStereo3D *stereo = (AVStereo3D *)side_data->data;
+             int fpa_type;
+             switch (stereo->type) {
+             case AV_STEREO3D_CHECKERBOARD:
+                 fpa_type = 0;
+                 break;
+             case AV_STEREO3D_LINES:
+                 fpa_type = 1;
+                 break;
+             case AV_STEREO3D_COLUMNS:
+                 fpa_type = 2;
+                 break;
+             case AV_STEREO3D_SIDEBYSIDE:
+                 fpa_type = 3;
+                 break;
+             case AV_STEREO3D_TOPBOTTOM:
+                 fpa_type = 4;
+                 break;
+             case AV_STEREO3D_FRAMESEQUENCE:
+                 fpa_type = 5;
+                 break;
+             default:
+                 fpa_type = -1;
+                 break;
+             }
+             if (fpa_type != x4->params.i_frame_packing) {
+                 x4->params.i_frame_packing = fpa_type;
+                 x264_encoder_reconfig(x4->enc, &x4->params);
+             }
+         }
+     }
      do {
          if (x264_encoder_encode(x4->enc, &nal, &nnal, frame? &x4->pic: NULL, &pic_out) < 0)
              return -1;
@@@ -241,20 -250,6 +277,20 @@@ static av_cold int X264_close(AVCodecCo
      return 0;
  }
  
 +#define OPT_STR(opt, param)                                                   \
 +    do {                                                                      \
 +        int ret;                                                              \
 +        if (param!=NULL && (ret = x264_param_parse(&x4->params, opt, param)) < 0) { \
 +            if(ret == X264_PARAM_BAD_NAME)                                    \
 +                av_log(avctx, AV_LOG_ERROR,                                   \
 +                        "bad option '%s': '%s'\n", opt, param);               \
 +            else                                                              \
 +                av_log(avctx, AV_LOG_ERROR,                                   \
 +                        "bad value for '%s': '%s'\n", opt, param);            \
 +            return -1;                                                        \
 +        }                                                                     \
 +    } while (0)
 +
  static int convert_pix_fmt(enum AVPixelFormat pix_fmt)
  {
      switch (pix_fmt) {
      case AV_PIX_FMT_YUV420P9:
      case AV_PIX_FMT_YUV420P10: return X264_CSP_I420;
      case AV_PIX_FMT_YUV422P:
 +    case AV_PIX_FMT_YUVJ422P:
      case AV_PIX_FMT_YUV422P10: return X264_CSP_I422;
      case AV_PIX_FMT_YUV444P:
 +    case AV_PIX_FMT_YUVJ444P:
      case AV_PIX_FMT_YUV444P9:
      case AV_PIX_FMT_YUV444P10: return X264_CSP_I444;
 +#ifdef X264_CSP_BGR
 +    case AV_PIX_FMT_BGR24:
 +        return X264_CSP_BGR;
 +
 +    case AV_PIX_FMT_RGB24:
 +        return X264_CSP_RGB;
 +#endif
      case AV_PIX_FMT_NV12:      return X264_CSP_NV12;
      case AV_PIX_FMT_NV16:
      case AV_PIX_FMT_NV20:      return X264_CSP_NV16;
  static av_cold int X264_init(AVCodecContext *avctx)
  {
      X264Context *x4 = avctx->priv_data;
 +    int sw,sh;
  
      x264_param_default(&x4->params);
  
      x4->params.b_deblocking_filter         = avctx->flags & CODEC_FLAG_LOOP_FILTER;
  
 +    x4->params.rc.f_pb_factor             = avctx->b_quant_factor;
 +    x4->params.analyse.i_chroma_qp_offset = avctx->chromaoffset;
      if (x4->preset || x4->tune)
          if (x264_param_default_preset(&x4->params, x4->preset, x4->tune) < 0) {
 +            int i;
              av_log(avctx, AV_LOG_ERROR, "Error setting preset/tune %s/%s.\n", x4->preset, x4->tune);
 +            av_log(avctx, AV_LOG_INFO, "Possible presets:");
 +            for (i = 0; x264_preset_names[i]; i++)
 +                av_log(avctx, AV_LOG_INFO, " %s", x264_preset_names[i]);
 +            av_log(avctx, AV_LOG_INFO, "\n");
 +            av_log(avctx, AV_LOG_INFO, "Possible tunes:");
 +            for (i = 0; x264_tune_names[i]; i++)
 +                av_log(avctx, AV_LOG_INFO, " %s", x264_tune_names[i]);
 +            av_log(avctx, AV_LOG_INFO, "\n");
              return AVERROR(EINVAL);
          }
  
      x4->params.i_log_level          = X264_LOG_DEBUG;
      x4->params.i_csp                = convert_pix_fmt(avctx->pix_fmt);
  
 +    OPT_STR("weightp", x4->wpredp);
 +
      if (avctx->bit_rate) {
          x4->params.rc.i_bitrate   = avctx->bit_rate / 1000;
          x4->params.rc.i_rc_method = X264_RC_ABR;
              (float)avctx->rc_initial_buffer_occupancy / avctx->rc_buffer_size;
      }
  
 +    OPT_STR("level", x4->level);
 +
 +    if(x4->x264opts){
 +        const char *p= x4->x264opts;
 +        while(p){
 +            char param[256]={0}, val[256]={0};
 +            if(sscanf(p, "%255[^:=]=%255[^:]", param, val) == 1){
 +                OPT_STR(param, "1");
 +            }else
 +                OPT_STR(param, val);
 +            p= strchr(p, ':');
 +            p+=!!p;
 +        }
 +    }
 +
      if (avctx->i_quant_factor > 0)
          x4->params.rc.f_ip_factor         = 1 / fabs(avctx->i_quant_factor);
 -    x4->params.rc.f_pb_factor             = avctx->b_quant_factor;
 -    x4->params.analyse.i_chroma_qp_offset = avctx->chromaoffset;
  
      if (avctx->me_method == ME_EPZS)
          x4->params.analyse.i_me_method = X264_ME_DIA;
  
      if (x4->slice_max_size >= 0)
          x4->params.i_slice_max_size =  x4->slice_max_size;
 +    else {
 +        /*
 +         * Allow x264 to be instructed through AVCodecContext about the maximum
 +         * size of the RTP payload. For example, this enables the production of
 +         * payload suitable for the H.264 RTP packetization-mode 0 i.e. single
 +         * NAL unit per RTP packet.
 +         */
 +        if (avctx->rtp_payload_size)
 +            x4->params.i_slice_max_size = avctx->rtp_payload_size;
 +    }
  
      if (x4->fastfirstpass)
          x264_param_apply_fastfirstpass(&x4->params);
  
 +    /* Allow specifying the x264 profile through AVCodecContext. */
 +    if (!x4->profile)
 +        switch (avctx->profile) {
 +        case FF_PROFILE_H264_BASELINE:
 +            x4->profile = av_strdup("baseline");
 +            break;
 +        case FF_PROFILE_H264_HIGH:
 +            x4->profile = av_strdup("high");
 +            break;
 +        case FF_PROFILE_H264_HIGH_10:
 +            x4->profile = av_strdup("high10");
 +            break;
 +        case FF_PROFILE_H264_HIGH_422:
 +            x4->profile = av_strdup("high422");
 +            break;
 +        case FF_PROFILE_H264_HIGH_444:
 +            x4->profile = av_strdup("high444");
 +            break;
 +        case FF_PROFILE_H264_MAIN:
 +            x4->profile = av_strdup("main");
 +            break;
 +        default:
 +            break;
 +        }
 +
      if (x4->nal_hrd >= 0)
          x4->params.i_nal_hrd = x4->nal_hrd;
  
      if (x4->profile)
          if (x264_param_apply_profile(&x4->params, x4->profile) < 0) {
 +            int i;
              av_log(avctx, AV_LOG_ERROR, "Error setting profile %s.\n", x4->profile);
 +            av_log(avctx, AV_LOG_INFO, "Possible profiles:");
 +            for (i = 0; x264_profile_names[i]; i++)
 +                av_log(avctx, AV_LOG_INFO, " %s", x264_profile_names[i]);
 +            av_log(avctx, AV_LOG_INFO, "\n");
              return AVERROR(EINVAL);
          }
  
      x4->params.i_width          = avctx->width;
      x4->params.i_height         = avctx->height;
 -    x4->params.vui.i_sar_width  = avctx->sample_aspect_ratio.num;
 -    x4->params.vui.i_sar_height = avctx->sample_aspect_ratio.den;
 +    av_reduce(&sw, &sh, avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 4096);
 +    x4->params.vui.i_sar_width  = sw;
 +    x4->params.vui.i_sar_height = sh;
      x4->params.i_fps_num = x4->params.i_timebase_den = avctx->time_base.den;
      x4->params.i_fps_den = x4->params.i_timebase_num = avctx->time_base.num;
  
  
      x4->params.i_slice_count  = avctx->slices;
  
 -    x4->params.vui.b_fullrange = avctx->pix_fmt == AV_PIX_FMT_YUVJ420P;
 +    x4->params.vui.b_fullrange = avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
 +                                 avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
 +                                 avctx->pix_fmt == AV_PIX_FMT_YUVJ444P;
  
      if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER)
          x4->params.b_repeat_headers = 0;
@@@ -605,9 -521,7 +641,9 @@@ static const enum AVPixelFormat pix_fmt
      AV_PIX_FMT_YUV420P,
      AV_PIX_FMT_YUVJ420P,
      AV_PIX_FMT_YUV422P,
 +    AV_PIX_FMT_YUVJ422P,
      AV_PIX_FMT_YUV444P,
 +    AV_PIX_FMT_YUVJ444P,
      AV_PIX_FMT_NV12,
      AV_PIX_FMT_NV16,
      AV_PIX_FMT_NONE
@@@ -624,13 -538,6 +660,13 @@@ static const enum AVPixelFormat pix_fmt
      AV_PIX_FMT_NV20,
      AV_PIX_FMT_NONE
  };
 +static const enum AVPixelFormat pix_fmts_8bit_rgb[] = {
 +#ifdef X264_CSP_BGR
 +    AV_PIX_FMT_BGR24,
 +    AV_PIX_FMT_RGB24,
 +#endif
 +    AV_PIX_FMT_NONE
 +};
  
  static av_cold void X264_init_static(AVCodec *codec)
  {
@@@ -649,10 -556,6 +685,10 @@@ static const AVOption options[] = 
      { "tune",          "Tune the encoding params (cf. x264 --fullhelp)",  OFFSET(tune),          AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
      { "profile",       "Set profile restrictions (cf. x264 --fullhelp) ", OFFSET(profile),       AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
      { "fastfirstpass", "Use fast settings when encoding first pass",      OFFSET(fastfirstpass), AV_OPT_TYPE_INT,    { .i64 = 1 }, 0, 1, VE},
 +    {"level", "Specify level (as defined by Annex A)", OFFSET(level), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
 +    {"passlogfile", "Filename for 2 pass stats", OFFSET(stats), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
 +    {"wpredp", "Weighted prediction for P-frames", OFFSET(wpredp), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
 +    {"x264opts", "x264 options", OFFSET(x264opts), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
      { "crf",           "Select the quality for constant quality mode",    OFFSET(crf),           AV_OPT_TYPE_FLOAT,  {.dbl = -1 }, -1, FLT_MAX, VE },
      { "crf_max",       "In CRF mode, prevents VBV from lowering quality beyond this point.",OFFSET(crf_max), AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX, VE },
      { "qp",            "Constant quantization parameter rate control method",OFFSET(cqp),        AV_OPT_TYPE_INT,    { .i64 = -1 }, -1, INT_MAX, VE },
      { NULL },
  };
  
 -static const AVClass class = {
 +static const AVClass x264_class = {
      .class_name = "libx264",
      .item_name  = av_default_item_name,
      .option     = options,
      .version    = LIBAVUTIL_VERSION_INT,
  };
  
 +static const AVClass rgbclass = {
 +    .class_name = "libx264rgb",
 +    .item_name  = av_default_item_name,
 +    .option     = options,
 +    .version    = LIBAVUTIL_VERSION_INT,
 +};
 +
  static const AVCodecDefault x264_defaults[] = {
      { "b",                "0" },
      { "bf",               "-1" },
 +    { "flags2",           "0" },
      { "g",                "-1" },
      { "i_qfactor",        "-1" },
      { "qmin",             "-1" },
      { "qdiff",            "-1" },
      { "qblur",            "-1" },
      { "qcomp",            "-1" },
 +//     { "rc_lookahead",     "-1" },
      { "refs",             "-1" },
      { "sc_threshold",     "-1" },
      { "trellis",          "-1" },
@@@ -756,22 -650,7 +792,22 @@@ AVCodec ff_libx264_encoder = 
      .encode2          = X264_frame,
      .close            = X264_close,
      .capabilities     = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS,
 -    .priv_class       = &class,
 +    .priv_class       = &x264_class,
      .defaults         = x264_defaults,
      .init_static_data = X264_init_static,
  };
 +
 +AVCodec ff_libx264rgb_encoder = {
 +    .name           = "libx264rgb",
 +    .long_name      = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 RGB"),
 +    .type           = AVMEDIA_TYPE_VIDEO,
 +    .id             = AV_CODEC_ID_H264,
 +    .priv_data_size = sizeof(X264Context),
 +    .init           = X264_init,
 +    .encode2        = X264_frame,
 +    .close          = X264_close,
 +    .capabilities   = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS,
 +    .priv_class     = &rgbclass,
 +    .defaults       = x264_defaults,
 +    .pix_fmts       = pix_fmts_8bit_rgb,
 +};