Onryo

FFmpeg patch for Webm updated. By Onryo aka Erik Adler

May 23rd, 2011
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 10.18 KB | None | 0 0
  1. # Remove this before patching. This is a new patch what will patch the trunk code of FFmpeg with googles #webM. Just put this in the source code of FFmpeg and patch -P1 < patchname
  2.  
  3. diff --git a/ffpresets/libvpx-1080p.ffpreset b/ffpresets/libvpx-1080p.ffpreset
  4. new file mode 100644
  5. index 0000000..19ec3ff
  6. --- /dev/null
  7. +++ b/ffpresets/libvpx-1080p.ffpreset
  8. @@ -0,0 +1,18 @@
  9. +vcodec=libvpx
  10. +g=120
  11. +rc_lookahead=16
  12. +quality=good
  13. +speed=0
  14. +profile=1
  15. +qmax=51
  16. +qmin=11
  17. +slices=4
  18. +vb=2M
  19. +
  20. +#ignored unless using -pass 2
  21. +maxrate=24M
  22. +minrate=100k
  23. +vp8flags=+altref
  24. +arnr_max_frames=7
  25. +arnr_strength=5
  26. +arnr_type=3
  27. diff --git a/ffpresets/libvpx-1080p50_60.ffpreset b/ffpresets/libvpx-1080p50_60.ffpreset
  28. new file mode 100644
  29. index 0000000..9cff038
  30. --- /dev/null
  31. +++ b/ffpresets/libvpx-1080p50_60.ffpreset
  32. @@ -0,0 +1,18 @@
  33. +vcodec=libvpx
  34. +g=120
  35. +rc_lookahead=25
  36. +quality=good
  37. +speed=0
  38. +profile=1
  39. +qmax=51
  40. +qmin=11
  41. +slices=4
  42. +vb=2M
  43. +
  44. +#ignored unless using -pass 2
  45. +maxrate=24M
  46. +minrate=100k
  47. +vp8flags=+altref
  48. +arnr_max_frames=7
  49. +arnr_strength=5
  50. +arnr_type=3
  51. diff --git a/ffpresets/libvpx-360p.ffpreset b/ffpresets/libvpx-360p.ffpreset
  52. new file mode 100644
  53. index 0000000..d72c66d
  54. --- /dev/null
  55. +++ b/ffpresets/libvpx-360p.ffpreset
  56. @@ -0,0 +1,17 @@
  57. +vcodec=libvpx
  58. +g=120
  59. +rc_lookahead=16
  60. +quality=good
  61. +speed=0
  62. +profile=0
  63. +qmax=63
  64. +qmin=0
  65. +vb=768k
  66. +
  67. +#ignored unless using -pass 2
  68. +maxrate=1.5M
  69. +minrate=40k
  70. +vp8flags=+altref
  71. +arnr_max_frames=7
  72. +arnr_strength=5
  73. +arnr_type=3
  74. diff --git a/ffpresets/libvpx-720p.ffpreset b/ffpresets/libvpx-720p.ffpreset
  75. new file mode 100644
  76. index 0000000..3414539
  77. --- /dev/null
  78. +++ b/ffpresets/libvpx-720p.ffpreset
  79. @@ -0,0 +1,18 @@
  80. +vcodec=libvpx
  81. +g=120
  82. +rc_lookahead=16
  83. +quality=good
  84. +speed=0
  85. +profile=0
  86. +qmax=51
  87. +qmin=11
  88. +slices=4
  89. +vb=2M
  90. +
  91. +#ignored unless using -pass 2
  92. +maxrate=24M
  93. +minrate=100k
  94. +vp8flags=+altref
  95. +arnr_max_frames=7
  96. +arnr_strength=5
  97. +arnr_type=3
  98. diff --git a/ffpresets/libvpx-720p50_60.ffpreset b/ffpresets/libvpx-720p50_60.ffpreset
  99. new file mode 100644
  100. index 0000000..a0a4321
  101. --- /dev/null
  102. +++ b/ffpresets/libvpx-720p50_60.ffpreset
  103. @@ -0,0 +1,18 @@
  104. +vcodec=libvpx
  105. +g=120
  106. +rc_lookahead=25
  107. +quality=good
  108. +speed=0
  109. +profile=0
  110. +qmax=51
  111. +qmin=11
  112. +slices=4
  113. +vb=2M
  114. +
  115. +#ignored unless using -pass 2
  116. +maxrate=24M
  117. +minrate=100k
  118. +vp8flags=+altref
  119. +arnr_max_frames=7
  120. +arnr_strength=5
  121. +arnr_type=3
  122. diff --git a/libavcodec/libvpxenc.c b/libavcodec/libvpxenc.c
  123. index ef85b30..b55e755 100644
  124. --- a/libavcodec/libvpxenc.c
  125. +++ b/libavcodec/libvpxenc.c
  126. @@ -30,6 +30,7 @@
  127.  
  128.  #include "avcodec.h"
  129.  #include "libavutil/base64.h"
  130. +#include "libavutil/opt.h"
  131.  
  132.  /**
  133.   * Portion of struct vpx_codec_cx_pkt from vpx_encoder.h.
  134. @@ -50,10 +51,45 @@ typedef struct VP8EncoderContext {
  135.      struct vpx_codec_ctx encoder;
  136.      struct vpx_image rawimg;
  137.      struct vpx_fixed_buf twopass_stats;
  138. -    unsigned long deadline; //i.e., RT/GOOD/BEST
  139. +    int deadline; //i.e., RT/GOOD/BEST
  140.      struct FrameListData *coded_frame_list;
  141. +
  142. +    int cpuused;
  143. +
  144. +    /**
  145. +     * VP8 specific flags, see VP8F_* below.
  146. +     */
  147. +    int flags;
  148. +#define VP8F_ERROR_RESILIENT 0x00000001 ///< Enable measures appropriate for streaming over lossy links
  149. +#define VP8F_AUTO_ALT_REF    0x00000002 ///< Enable automatic alternate reference frame generation
  150. +
  151. +    int arnr_max_frames;
  152. +    int arnr_strength;
  153. +    int arnr_type;
  154.  } VP8Context;
  155.  
  156. +#define V AV_OPT_FLAG_VIDEO_PARAM
  157. +#define E AV_OPT_FLAG_ENCODING_PARAM
  158. +
  159. +static const AVOption options[]={
  160. +{"speed", "", offsetof(VP8Context, cpuused), FF_OPT_TYPE_INT, 3, -16, 16, V|E},
  161. +{"quality", "", offsetof(VP8Context, deadline), FF_OPT_TYPE_INT, VPX_DL_GOOD_QUALITY, INT_MIN, INT_MAX, V|E, "quality"},
  162. +{"best", NULL, 0, FF_OPT_TYPE_CONST, VPX_DL_BEST_QUALITY, INT_MIN, INT_MAX, V|E, "quality"},
  163. +{"good", NULL, 0, FF_OPT_TYPE_CONST, VPX_DL_GOOD_QUALITY, INT_MIN, INT_MAX, V|E, "quality"},
  164. +{"realtime", NULL, 0, FF_OPT_TYPE_CONST, VPX_DL_REALTIME, INT_MIN, INT_MAX, V|E, "quality"},
  165. +{"vp8flags", "", offsetof(VP8Context, flags), FF_OPT_TYPE_FLAGS, 0, 0, UINT_MAX, V|E, "flags"},
  166. +{"error_resilient", "enable error resilience", 0, FF_OPT_TYPE_CONST, VP8F_ERROR_RESILIENT, INT_MIN, INT_MAX, V|E, "flags"},
  167. +{"altref", "enable use of alternate reference frames (VP8/2-pass only)", 0, FF_OPT_TYPE_CONST, VP8F_AUTO_ALT_REF, INT_MIN, INT_MAX, V|E, "flags"},
  168. +{"arnr_max_frames", "altref noise reduction max frame count", offsetof(VP8Context, arnr_max_frames), FF_OPT_TYPE_INT, 0, 0, 15, V|E},
  169. +{"arnr_strength", "altref noise reduction filter strength", offsetof(VP8Context, arnr_strength), FF_OPT_TYPE_INT, 3, 0, 6, V|E},
  170. +{"arnr_type", "altref noise reduction filter type", offsetof(VP8Context, arnr_type), FF_OPT_TYPE_INT, 3, 1, 3, V|E},
  171. +{NULL}
  172. +};
  173. +static const AVClass class = { "libvpx", av_default_item_name, options, LIBAVUTIL_VERSION_INT };
  174. +
  175. +#undef V
  176. +#undef E
  177. +
  178.  /** String mappings for enum vp8e_enc_control_id */
  179.  static const char *ctlidstr[] = {
  180.      [VP8E_UPD_ENTROPY]           = "VP8E_UPD_ENTROPY",
  181. @@ -205,7 +241,6 @@ static av_cold int vp8_init(AVCodecContext *avctx)
  182.  {
  183.      VP8Context *ctx = avctx->priv_data;
  184.      const struct vpx_codec_iface *iface = &vpx_codec_vp8_cx_algo;
  185. -    int cpuused = 3;
  186.      struct vpx_codec_enc_cfg enccfg;
  187.      int res;
  188.  
  189. @@ -224,6 +259,7 @@ static av_cold int vp8_init(AVCodecContext *avctx)
  190.      enccfg.g_timebase.num = avctx->time_base.num;
  191.      enccfg.g_timebase.den = avctx->time_base.den;
  192.      enccfg.g_threads      = avctx->thread_count;
  193. +    enccfg.g_lag_in_frames= FFMIN(avctx->rc_lookahead, 25);  //0-25, avoids init failure
  194.  
  195.      if (avctx->flags & CODEC_FLAG_PASS1)
  196.          enccfg.g_pass = VPX_RC_FIRST_PASS;
  197. @@ -259,6 +295,7 @@ static av_cold int vp8_init(AVCodecContext *avctx)
  198.          enccfg.rc_buf_initial_sz =
  199.              avctx->rc_initial_buffer_occupancy * 1000LL / avctx->bit_rate;
  200.      enccfg.rc_buf_optimal_sz     = enccfg.rc_buf_sz * 5 / 6;
  201. +    enccfg.rc_undershoot_pct     = round(avctx->rc_buffer_aggressivity * 100);
  202.  
  203.      //_enc_init() will balk if kf_min_dist differs from max w/VPX_KF_AUTO
  204.      if (avctx->keyint_min == avctx->gop_size)
  205. @@ -294,13 +331,14 @@ static av_cold int vp8_init(AVCodecContext *avctx)
  206.          enccfg.rc_twopass_stats_in = ctx->twopass_stats;
  207.      }
  208.  
  209. -    ctx->deadline = VPX_DL_GOOD_QUALITY;
  210.      /* 0-3: For non-zero values the encoder increasingly optimizes for reduced
  211.         complexity playback on low powered devices at the expense of encode
  212.         quality. */
  213.     if (avctx->profile != FF_PROFILE_UNKNOWN)
  214.         enccfg.g_profile = avctx->profile;
  215.  
  216. +    enccfg.g_error_resilient = ctx->flags & VP8F_ERROR_RESILIENT;
  217. +
  218.      dump_enc_cfg(avctx, &enccfg);
  219.      /* Construct Encoder Context */
  220.      res = vpx_codec_enc_init(&ctx->encoder, iface, &enccfg, 0);
  221. @@ -311,11 +349,17 @@ static av_cold int vp8_init(AVCodecContext *avctx)
  222.  
  223.      //codec control failures are currently treated only as warnings
  224.      av_log(avctx, AV_LOG_DEBUG, "vpx_codec_control\n");
  225. -    codecctl_int(avctx, VP8E_SET_CPUUSED,           cpuused);
  226. +    codecctl_int(avctx, VP8E_SET_CPUUSED,           ctx->cpuused);
  227.      codecctl_int(avctx, VP8E_SET_NOISE_SENSITIVITY, avctx->noise_reduction);
  228.      codecctl_int(avctx, VP8E_SET_TOKEN_PARTITIONS,  av_log2(avctx->slices));
  229.      codecctl_int(avctx, VP8E_SET_STATIC_THRESHOLD,  avctx->mb_threshold);
  230.      codecctl_int(avctx, VP8E_SET_CQ_LEVEL,          (int)avctx->crf);
  231. +    codecctl_int(avctx, VP8E_SET_ENABLEAUTOALTREF,  !!(ctx->flags & VP8F_AUTO_ALT_REF));
  232. +    codecctl_int(avctx, VP8E_SET_ARNR_MAXFRAMES,    ctx->arnr_max_frames);
  233. +    codecctl_int(avctx, VP8E_SET_ARNR_STRENGTH,     ctx->arnr_strength);
  234. +    codecctl_int(avctx, VP8E_SET_ARNR_TYPE,         ctx->arnr_type);
  235. +
  236. +    av_log(avctx, AV_LOG_DEBUG, "Using deadline: %d\n", ctx->deadline);
  237.  
  238.      //provide dummy value to initialize wrapper, values will be updated each _encode()
  239.      vpx_img_wrap(&ctx->rawimg, VPX_IMG_FMT_I420, avctx->width, avctx->height, 1,
  240. @@ -511,4 +555,5 @@ AVCodec ff_libvpx_encoder = {
  241.      CODEC_CAP_DELAY,
  242.      .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
  243.      .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
  244. +    .priv_class= &class,
  245.  };
  246. diff --git a/libavformat/avformat.h b/libavformat/avformat.h
  247. index 991b0a4..8d9ed4e 100644
  248. --- a/libavformat/avformat.h
  249. +++ b/libavformat/avformat.h
  250. @@ -258,6 +258,9 @@ typedef struct AVFormatParameters {
  251.  #define AVFMT_NODIMENSIONS  0x0800 /**< Format does not need width/height */
  252.  #define AVFMT_NOSTREAMS     0x1000 /**< Format does not require any streams */
  253.  #define AVFMT_NOBINSEARCH   0x2000 /**< Format does not allow to fallback to binary search via read_timestamp */
  254. +#define AVFMT_TS_NONSTRICT  0x2000 /**< Format does not require strictly
  255. +                                        increasing timestamps, but they must
  256. +                                        still be monotonic */
  257.  #define AVFMT_NOGENSEARCH   0x4000 /**< Format does not allow to fallback to generic search */
  258.  
  259.  typedef struct AVOutputFormat {
  260. diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
  261. index 387cead..3260c4b 100644
  262. --- a/libavformat/matroskaenc.c
  263. +++ b/libavformat/matroskaenc.c
  264. @@ -1199,7 +1199,7 @@ AVOutputFormat ff_webm_muxer = {
  265.      mkv_write_header,
  266.      mkv_write_packet,
  267.      mkv_write_trailer,
  268. -    .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
  269. +    .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT,
  270.  };
  271.  #endif
  272.  
  273. diff --git a/libavformat/utils.c b/libavformat/utils.c
  274. index c5e5700..9642b38 100644
  275. --- a/libavformat/utils.c
  276. +++ b/libavformat/utils.c
  277. @@ -2972,7 +2972,7 @@ static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt){
  278.          pkt->dts= st->pts_buffer[0];
  279.      }
  280.  
  281. -    if(st->cur_dts && st->cur_dts != AV_NOPTS_VALUE && st->cur_dts >= pkt->dts){
  282. +    if(st->cur_dts && st->cur_dts != AV_NOPTS_VALUE && ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) && st->cur_dts >= pkt->dts) || st->cur_dts > pkt->dts)){
  283.          av_log(s, AV_LOG_ERROR,
  284.                 "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %"PRId64" >= %"PRId64"\n",
  285.                 st->index, st->cur_dts, pkt->dts);
Advertisement
Add Comment
Please, Sign In to add comment