~ubuntu-branches/ubuntu/lucid/ffmpeg-extra/lucid

« back to all changes in this revision

Viewing changes to debian/patches/ffmpeg-x264-backport.patch

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2010-02-17 08:37:17 UTC
  • Revision ID: james.westby@ubuntu.com-20100217083717-y6umxyc3httgaicb
Tags: 4:0.5+svn20090706-5ubuntu4
* tighten build dependency on new x264 package
* add libx264 wrapper backport for ffmpeg 0.5
* install presets in 'libavcodec package' instead of 'ffmpeg' binary,
  see git history for rationale of this change

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
--- a/libavcodec/options.c
 
2
+++ b/libavcodec/options.c
 
3
@@ -122,6 +122,7 @@ static const AVOption options[]={
 
4
 {"b_qfactor", "qp factor between p and b frames", OFFSET(b_quant_factor), FF_OPT_TYPE_FLOAT, 1.25, -FLT_MAX, FLT_MAX, V|E},
 
5
 {"rc_strategy", "ratecontrol method", OFFSET(rc_strategy), FF_OPT_TYPE_INT, DEFAULT, INT_MIN, INT_MAX, V|E},
 
6
 {"b_strategy", "strategy to choose between I/P/B-frames", OFFSET(b_frame_strategy), FF_OPT_TYPE_INT, 0, INT_MIN, INT_MAX, V|E},
 
7
+{"wpredp", "weighted prediction analysis method", OFFSET(weighted_p_pred), FF_OPT_TYPE_INT, 0, INT_MIN, INT_MAX, V|E},
 
8
 {"hurry_up", NULL, OFFSET(hurry_up), FF_OPT_TYPE_INT, DEFAULT, INT_MIN, INT_MAX, V|D},
 
9
 {"ps", "rtp payload size in bits", OFFSET(rtp_payload_size), FF_OPT_TYPE_INT, DEFAULT, INT_MIN, INT_MAX, V|E},
 
10
 {"mv_bits", NULL, OFFSET(mv_bits), FF_OPT_TYPE_INT, DEFAULT, INT_MIN, INT_MAX},
 
11
@@ -388,6 +389,7 @@ static const AVOption options[]={
 
12
 {"request_channels", "set desired number of audio channels", OFFSET(request_channels), FF_OPT_TYPE_INT, DEFAULT, 0, INT_MAX, A|D},
 
13
 {"drc_scale", "percentage of dynamic range compression to apply", OFFSET(drc_scale), FF_OPT_TYPE_FLOAT, 1.0, 0.0, 1.0, A|D},
 
14
 {"reservoir", "use bit reservoir", 0, FF_OPT_TYPE_CONST, CODEC_FLAG2_BIT_RESERVOIR, INT_MIN, INT_MAX, A|E, "flags2"},
 
15
+{"mbtree", "use macroblock tree ratecontrol (x264 only)", 0, FF_OPT_TYPE_CONST, CODEC_FLAG2_MBTREE, INT_MIN, INT_MAX, V|E, "flags2"},
 
16
 {"bits_per_raw_sample", NULL, OFFSET(bits_per_raw_sample), FF_OPT_TYPE_INT, DEFAULT, INT_MIN, INT_MAX},
 
17
 {"channel_layout", NULL, OFFSET(channel_layout), FF_OPT_TYPE_INT64, DEFAULT, 0, INT64_MAX, A|E|D, "channel_layout"},
 
18
 {"request_channel_layout", NULL, OFFSET(request_channel_layout), FF_OPT_TYPE_INT64, DEFAULT, 0, INT64_MAX, A|D, "request_channel_layout"},
 
19
--- a/libavcodec/libx264.c
 
20
+++ b/libavcodec/libx264.c
 
21
@@ -27,14 +27,15 @@
 
22
 #include <string.h>
 
23
 
 
24
 typedef struct X264Context {
 
25
-    x264_param_t params;
 
26
-    x264_t *enc;
 
27
-    x264_picture_t pic;
 
28
-    AVFrame out_pic;
 
29
+    x264_param_t    params;
 
30
+    x264_t         *enc;
 
31
+    x264_picture_t  pic;
 
32
+    uint8_t        *sei;
 
33
+    int             sei_size;
 
34
+    AVFrame         out_pic;
 
35
 } X264Context;
 
36
 
 
37
-static void
 
38
-X264_log(void *p, int level, const char *fmt, va_list args)
 
39
+static void X264_log(void *p, int level, const char *fmt, va_list args)
 
40
 {
 
41
     static const int level_map[] = {
 
42
         [X264_LOG_ERROR]   = AV_LOG_ERROR,
 
43
@@ -43,31 +44,75 @@ X264_log(void *p, int level, const char 
 
44
         [X264_LOG_DEBUG]   = AV_LOG_DEBUG
 
45
     };
 
46
 
 
47
-    if(level < 0 || level > X264_LOG_DEBUG)
 
48
+    if (level < 0 || level > X264_LOG_DEBUG)
 
49
         return;
 
50
 
 
51
     av_vlog(p, level_map[level], fmt, args);
 
52
 }
 
53
 
 
54
-
 
55
-static int
 
56
-encode_nals(uint8_t *buf, int size, x264_nal_t *nals, int nnal)
 
57
+#if X264_BUILD >= 76
 
58
+static int encode_nals(AVCodecContext *ctx, uint8_t *buf, int size,
 
59
+                       x264_nal_t *nals, int nnal, int skip_sei)
 
60
 {
 
61
+    X264Context *x4 = ctx->priv_data;
 
62
     uint8_t *p = buf;
 
63
     int i;
 
64
 
 
65
-    for(i = 0; i < nnal; i++){
 
66
-        int s = x264_nal_encode(p, &size, 1, nals + i);
 
67
-        if(s < 0)
 
68
+    /* Write the SEI as part of the first frame. */
 
69
+    if (x4->sei_size > 0 && nnal > 0) {
 
70
+        memcpy(p, x4->sei, x4->sei_size);
 
71
+        p += x4->sei_size;
 
72
+        x4->sei_size = 0;
 
73
+    }
 
74
+
 
75
+    for (i = 0; i < nnal; i++){
 
76
+        /* Don't put the SEI in extradata. */
 
77
+        if (skip_sei && nals[i].i_type == NAL_SEI) {
 
78
+            x4->sei_size = nals[i].i_payload;
 
79
+            x4->sei      = av_malloc(x4->sei_size);
 
80
+            memcpy(x4->sei, nals[i].p_payload, nals[i].i_payload);
 
81
+            continue;
 
82
+        }
 
83
+        memcpy(p, nals[i].p_payload, nals[i].i_payload);
 
84
+        p += nals[i].i_payload;
 
85
+    }
 
86
+
 
87
+    return p - buf;
 
88
+}
 
89
+#else
 
90
+static int encode_nals(AVCodecContext *ctx, uint8_t *buf, int size, x264_nal_t *nals, int nnal, int skip_sei)
 
91
+{
 
92
+    X264Context *x4 = ctx->priv_data;
 
93
+    uint8_t *p = buf;
 
94
+    int i, s;
 
95
+
 
96
+    /* Write the SEI as part of the first frame. */
 
97
+    if (x4->sei_size > 0 && nnal > 0) {
 
98
+        memcpy(p, x4->sei, x4->sei_size);
 
99
+        p += x4->sei_size;
 
100
+        x4->sei_size = 0;
 
101
+    }
 
102
+
 
103
+    for (i = 0; i < nnal; i++) {
 
104
+        /* Don't put the SEI in extradata. */
 
105
+        if (skip_sei && nals[i].i_type == NAL_SEI) {
 
106
+            x4->sei = av_malloc( 5 + nals[i].i_payload * 4 / 3 );
 
107
+            if(x264_nal_encode(x4->sei, &x4->sei_size, 1, nals + i) < 0)
 
108
+                return -1;
 
109
+            continue;
 
110
+        }
 
111
+        s = x264_nal_encode(p, &size, 1, nals + i);
 
112
+        if (s < 0)
 
113
             return -1;
 
114
         p += s;
 
115
     }
 
116
 
 
117
     return p - buf;
 
118
 }
 
119
+#endif
 
120
 
 
121
-static int
 
122
-X264_frame(AVCodecContext *ctx, uint8_t *buf, int bufsize, void *data)
 
123
+static int X264_frame(AVCodecContext *ctx, uint8_t *buf,
 
124
+                      int bufsize, void *data)
 
125
 {
 
126
     X264Context *x4 = ctx->priv_data;
 
127
     AVFrame *frame = data;
 
128
@@ -75,31 +120,30 @@ X264_frame(AVCodecContext *ctx, uint8_t 
 
129
     int nnal, i;
 
130
     x264_picture_t pic_out;
 
131
 
 
132
-    x4->pic.img.i_csp = X264_CSP_I420;
 
133
+    x4->pic.img.i_csp   = X264_CSP_I420;
 
134
     x4->pic.img.i_plane = 3;
 
135
 
 
136
     if (frame) {
 
137
-        for(i = 0; i < 3; i++){
 
138
-            x4->pic.img.plane[i] = frame->data[i];
 
139
+        for (i = 0; i < 3; i++) {
 
140
+            x4->pic.img.plane[i]    = frame->data[i];
 
141
             x4->pic.img.i_stride[i] = frame->linesize[i];
 
142
         }
 
143
 
 
144
-        x4->pic.i_pts = frame->pts;
 
145
+        x4->pic.i_pts  = frame->pts;
 
146
         x4->pic.i_type = X264_TYPE_AUTO;
 
147
     }
 
148
 
 
149
-    if(x264_encoder_encode(x4->enc, &nal, &nnal, frame? &x4->pic: NULL,
 
150
-                           &pic_out))
 
151
+    if (x264_encoder_encode(x4->enc, &nal, &nnal, frame? &x4->pic: NULL, &pic_out) < 0)
 
152
         return -1;
 
153
 
 
154
-    bufsize = encode_nals(buf, bufsize, nal, nnal);
 
155
-    if(bufsize < 0)
 
156
+    bufsize = encode_nals(ctx, buf, bufsize, nal, nnal, 0);
 
157
+    if (bufsize < 0)
 
158
         return -1;
 
159
 
 
160
-    /* FIXME: dts */
 
161
+    /* FIXME: libx264 now provides DTS, but AVFrame doesn't have a field for it. */
 
162
     x4->out_pic.pts = pic_out.i_pts;
 
163
 
 
164
-    switch(pic_out.i_type){
 
165
+    switch (pic_out.i_type) {
 
166
     case X264_TYPE_IDR:
 
167
     case X264_TYPE_I:
 
168
         x4->out_pic.pict_type = FF_I_TYPE;
 
169
@@ -113,166 +157,194 @@ X264_frame(AVCodecContext *ctx, uint8_t 
 
170
         break;
 
171
     }
 
172
 
 
173
+#if X264_BUILD < 82
 
174
     x4->out_pic.key_frame = pic_out.i_type == X264_TYPE_IDR;
 
175
-    x4->out_pic.quality = (pic_out.i_qpplus1 - 1) * FF_QP2LAMBDA;
 
176
+#else
 
177
+    x4->out_pic.key_frame = pic_out.b_keyframe;
 
178
+#endif
 
179
+    x4->out_pic.quality   = (pic_out.i_qpplus1 - 1) * FF_QP2LAMBDA;
 
180
 
 
181
     return bufsize;
 
182
 }
 
183
 
 
184
-static av_cold int
 
185
-X264_close(AVCodecContext *avctx)
 
186
+static av_cold int X264_close(AVCodecContext *avctx)
 
187
 {
 
188
     X264Context *x4 = avctx->priv_data;
 
189
 
 
190
     av_freep(&avctx->extradata);
 
191
+    av_free(x4->sei);
 
192
 
 
193
-    if(x4->enc)
 
194
+    if (x4->enc)
 
195
         x264_encoder_close(x4->enc);
 
196
 
 
197
     return 0;
 
198
 }
 
199
 
 
200
-static av_cold int
 
201
-X264_init(AVCodecContext *avctx)
 
202
+static av_cold int X264_init(AVCodecContext *avctx)
 
203
 {
 
204
     X264Context *x4 = avctx->priv_data;
 
205
 
 
206
+    x4->sei_size = 0;
 
207
     x264_param_default(&x4->params);
 
208
 
 
209
-    x4->params.pf_log = X264_log;
 
210
-    x4->params.p_log_private = avctx;
 
211
+    x4->params.pf_log               = X264_log;
 
212
+    x4->params.p_log_private        = avctx;
 
213
 
 
214
-    x4->params.i_keyint_max = avctx->gop_size;
 
215
-    x4->params.rc.i_bitrate = avctx->bit_rate / 1000;
 
216
+    x4->params.i_keyint_max         = avctx->gop_size;
 
217
+    x4->params.rc.i_bitrate         = avctx->bit_rate       / 1000;
 
218
     x4->params.rc.i_vbv_buffer_size = avctx->rc_buffer_size / 1000;
 
219
-    x4->params.rc.i_vbv_max_bitrate = avctx->rc_max_rate / 1000;
 
220
-    x4->params.rc.b_stat_write = avctx->flags & CODEC_FLAG_PASS1;
 
221
-    if(avctx->flags & CODEC_FLAG_PASS2) x4->params.rc.b_stat_read = 1;
 
222
-    else{
 
223
-        if(avctx->crf){
 
224
-            x4->params.rc.i_rc_method = X264_RC_CRF;
 
225
+    x4->params.rc.i_vbv_max_bitrate = avctx->rc_max_rate    / 1000;
 
226
+    x4->params.rc.b_stat_write      = avctx->flags & CODEC_FLAG_PASS1;
 
227
+    if (avctx->flags & CODEC_FLAG_PASS2) {
 
228
+        x4->params.rc.b_stat_read = 1;
 
229
+    } else {
 
230
+        if (avctx->crf) {
 
231
+            x4->params.rc.i_rc_method   = X264_RC_CRF;
 
232
             x4->params.rc.f_rf_constant = avctx->crf;
 
233
-        }else if(avctx->cqp > -1){
 
234
-            x4->params.rc.i_rc_method = X264_RC_CQP;
 
235
+        } else if (avctx->cqp > -1) {
 
236
+            x4->params.rc.i_rc_method   = X264_RC_CQP;
 
237
             x4->params.rc.i_qp_constant = avctx->cqp;
 
238
         }
 
239
     }
 
240
 
 
241
     // if neither crf nor cqp modes are selected we have to enable the RC
 
242
     // we do it this way because we cannot check if the bitrate has been set
 
243
-    if(!(avctx->crf || (avctx->cqp > -1))) x4->params.rc.i_rc_method = X264_RC_ABR;
 
244
+    if (!(avctx->crf || (avctx->cqp > -1)))
 
245
+        x4->params.rc.i_rc_method = X264_RC_ABR;
 
246
 
 
247
-    x4->params.i_bframe = avctx->max_b_frames;
 
248
-    x4->params.b_cabac = avctx->coder_type == FF_CODER_TYPE_AC;
 
249
+    x4->params.i_bframe          = avctx->max_b_frames;
 
250
+    x4->params.b_cabac           = avctx->coder_type == FF_CODER_TYPE_AC;
 
251
     x4->params.i_bframe_adaptive = avctx->b_frame_strategy;
 
252
-    x4->params.i_bframe_bias = avctx->bframebias;
 
253
-    x4->params.b_bframe_pyramid = avctx->flags2 & CODEC_FLAG2_BPYRAMID;
 
254
-    avctx->has_b_frames= avctx->flags2 & CODEC_FLAG2_BPYRAMID ? 2 : !!avctx->max_b_frames;
 
255
+    x4->params.i_bframe_bias     = avctx->bframebias;
 
256
+#if X264_BUILD >= 78
 
257
+    x4->params.i_bframe_pyramid  = avctx->flags2 & CODEC_FLAG2_BPYRAMID ? X264_B_PYRAMID_NORMAL : X264_B_PYRAMID_NONE;
 
258
+#else
 
259
+    x4->params.b_bframe_pyramid  = avctx->flags2 & CODEC_FLAG2_BPYRAMID;
 
260
+#endif
 
261
+    avctx->has_b_frames          = avctx->flags2 & CODEC_FLAG2_BPYRAMID ? 2 : !!avctx->max_b_frames;
 
262
 
 
263
     x4->params.i_keyint_min = avctx->keyint_min;
 
264
-    if(x4->params.i_keyint_min > x4->params.i_keyint_max)
 
265
+    if (x4->params.i_keyint_min > x4->params.i_keyint_max)
 
266
         x4->params.i_keyint_min = x4->params.i_keyint_max;
 
267
 
 
268
-    x4->params.i_scenecut_threshold = avctx->scenechange_threshold;
 
269
+    x4->params.i_scenecut_threshold        = avctx->scenechange_threshold;
 
270
 
 
271
-    x4->params.b_deblocking_filter = avctx->flags & CODEC_FLAG_LOOP_FILTER;
 
272
+    x4->params.b_deblocking_filter         = avctx->flags & CODEC_FLAG_LOOP_FILTER;
 
273
     x4->params.i_deblocking_filter_alphac0 = avctx->deblockalpha;
 
274
-    x4->params.i_deblocking_filter_beta = avctx->deblockbeta;
 
275
+    x4->params.i_deblocking_filter_beta    = avctx->deblockbeta;
 
276
 
 
277
-    x4->params.rc.i_qp_min = avctx->qmin;
 
278
-    x4->params.rc.i_qp_max = avctx->qmax;
 
279
-    x4->params.rc.i_qp_step = avctx->max_qdiff;
 
280
+    x4->params.rc.i_qp_min                 = avctx->qmin;
 
281
+    x4->params.rc.i_qp_max                 = avctx->qmax;
 
282
+    x4->params.rc.i_qp_step                = avctx->max_qdiff;
 
283
 
 
284
-    x4->params.rc.f_qcompress = avctx->qcompress;  /* 0.0 => cbr, 1.0 => constant qp */
 
285
-    x4->params.rc.f_qblur = avctx->qblur;        /* temporally blur quants */
 
286
+    x4->params.rc.f_qcompress       = avctx->qcompress; /* 0.0 => cbr, 1.0 => constant qp */
 
287
+    x4->params.rc.f_qblur           = avctx->qblur;     /* temporally blur quants */
 
288
     x4->params.rc.f_complexity_blur = avctx->complexityblur;
 
289
 
 
290
-    x4->params.i_frame_reference = avctx->refs;
 
291
+    x4->params.i_frame_reference    = avctx->refs;
 
292
 
 
293
-    x4->params.i_width = avctx->width;
 
294
-    x4->params.i_height = avctx->height;
 
295
-    x4->params.vui.i_sar_width = avctx->sample_aspect_ratio.num;
 
296
-    x4->params.vui.i_sar_height = avctx->sample_aspect_ratio.den;
 
297
-    x4->params.i_fps_num = avctx->time_base.den;
 
298
-    x4->params.i_fps_den = avctx->time_base.num;
 
299
-
 
300
-    x4->params.analyse.inter = 0;
 
301
-    if(avctx->partitions){
 
302
-        if(avctx->partitions & X264_PART_I4X4)
 
303
+    x4->params.i_width              = avctx->width;
 
304
+    x4->params.i_height             = avctx->height;
 
305
+    x4->params.vui.i_sar_width      = avctx->sample_aspect_ratio.num;
 
306
+    x4->params.vui.i_sar_height     = avctx->sample_aspect_ratio.den;
 
307
+#if X264_BUILD >= 81
 
308
+    x4->params.i_fps_num = x4->params.i_timebase_den = avctx->time_base.den;
 
309
+    x4->params.i_fps_den = x4->params.i_timebase_num = avctx->time_base.num;
 
310
+#endif
 
311
+
 
312
+    x4->params.analyse.inter    = 0;
 
313
+    if (avctx->partitions) {
 
314
+        if (avctx->partitions & X264_PART_I4X4)
 
315
             x4->params.analyse.inter |= X264_ANALYSE_I4x4;
 
316
-        if(avctx->partitions & X264_PART_I8X8)
 
317
+        if (avctx->partitions & X264_PART_I8X8)
 
318
             x4->params.analyse.inter |= X264_ANALYSE_I8x8;
 
319
-        if(avctx->partitions & X264_PART_P8X8)
 
320
+        if (avctx->partitions & X264_PART_P8X8)
 
321
             x4->params.analyse.inter |= X264_ANALYSE_PSUB16x16;
 
322
-        if(avctx->partitions & X264_PART_P4X4)
 
323
+        if (avctx->partitions & X264_PART_P4X4)
 
324
             x4->params.analyse.inter |= X264_ANALYSE_PSUB8x8;
 
325
-        if(avctx->partitions & X264_PART_B8X8)
 
326
+        if (avctx->partitions & X264_PART_B8X8)
 
327
             x4->params.analyse.inter |= X264_ANALYSE_BSUB16x16;
 
328
     }
 
329
 
 
330
-    x4->params.analyse.i_direct_mv_pred = avctx->directpred;
 
331
+    x4->params.analyse.i_direct_mv_pred  = avctx->directpred;
 
332
 
 
333
     x4->params.analyse.b_weighted_bipred = avctx->flags2 & CODEC_FLAG2_WPRED;
 
334
+#if X264_BUILD >= 79
 
335
+    x4->params.analyse.i_weighted_pred = avctx->weighted_p_pred;
 
336
+#endif
 
337
 
 
338
-    if(avctx->me_method == ME_EPZS)
 
339
+    if (avctx->me_method == ME_EPZS)
 
340
         x4->params.analyse.i_me_method = X264_ME_DIA;
 
341
-    else if(avctx->me_method == ME_HEX)
 
342
+    else if (avctx->me_method == ME_HEX)
 
343
         x4->params.analyse.i_me_method = X264_ME_HEX;
 
344
-    else if(avctx->me_method == ME_UMH)
 
345
+    else if (avctx->me_method == ME_UMH)
 
346
         x4->params.analyse.i_me_method = X264_ME_UMH;
 
347
-    else if(avctx->me_method == ME_FULL)
 
348
+    else if (avctx->me_method == ME_FULL)
 
349
         x4->params.analyse.i_me_method = X264_ME_ESA;
 
350
-    else if(avctx->me_method == ME_TESA)
 
351
+    else if (avctx->me_method == ME_TESA)
 
352
         x4->params.analyse.i_me_method = X264_ME_TESA;
 
353
     else x4->params.analyse.i_me_method = X264_ME_HEX;
 
354
 
 
355
-    x4->params.analyse.i_me_range = avctx->me_range;
 
356
-    x4->params.analyse.i_subpel_refine = avctx->me_subpel_quality;
 
357
+    x4->params.analyse.i_me_range         = avctx->me_range;
 
358
+    x4->params.analyse.i_subpel_refine    = avctx->me_subpel_quality;
 
359
 
 
360
-    x4->params.analyse.b_mixed_references =
 
361
-        avctx->flags2 & CODEC_FLAG2_MIXED_REFS;
 
362
-    x4->params.analyse.b_chroma_me = avctx->me_cmp & FF_CMP_CHROMA;
 
363
-    x4->params.analyse.b_transform_8x8 = avctx->flags2 & CODEC_FLAG2_8X8DCT;
 
364
-    x4->params.analyse.b_fast_pskip = avctx->flags2 & CODEC_FLAG2_FASTPSKIP;
 
365
+    x4->params.analyse.b_mixed_references = avctx->flags2 & CODEC_FLAG2_MIXED_REFS;
 
366
+    x4->params.analyse.b_chroma_me        = avctx->me_cmp & FF_CMP_CHROMA;
 
367
+    x4->params.analyse.b_transform_8x8    = avctx->flags2 & CODEC_FLAG2_8X8DCT;
 
368
+    x4->params.analyse.b_fast_pskip       = avctx->flags2 & CODEC_FLAG2_FASTPSKIP;
 
369
 
 
370
-    x4->params.analyse.i_trellis = avctx->trellis;
 
371
-    x4->params.analyse.i_noise_reduction = avctx->noise_reduction;
 
372
+    x4->params.analyse.i_trellis          = avctx->trellis;
 
373
+    x4->params.analyse.i_noise_reduction  = avctx->noise_reduction;
 
374
 
 
375
-    if(avctx->level > 0) x4->params.i_level_idc = avctx->level;
 
376
+    if (avctx->level > 0)
 
377
+        x4->params.i_level_idc = avctx->level;
 
378
 
 
379
     x4->params.rc.f_rate_tolerance =
 
380
         (float)avctx->bit_rate_tolerance/avctx->bit_rate;
 
381
 
 
382
-    if((avctx->rc_buffer_size != 0) &&
 
383
-            (avctx->rc_initial_buffer_occupancy <= avctx->rc_buffer_size)){
 
384
+    if ((avctx->rc_buffer_size != 0) &&
 
385
+        (avctx->rc_initial_buffer_occupancy <= avctx->rc_buffer_size)) {
 
386
         x4->params.rc.f_vbv_buffer_init =
 
387
-            (float)avctx->rc_initial_buffer_occupancy/avctx->rc_buffer_size;
 
388
-    }
 
389
-    else x4->params.rc.f_vbv_buffer_init = 0.9;
 
390
-
 
391
-    x4->params.rc.f_ip_factor = 1/fabs(avctx->i_quant_factor);
 
392
-    x4->params.rc.f_pb_factor = avctx->b_quant_factor;
 
393
+            (float)avctx->rc_initial_buffer_occupancy / avctx->rc_buffer_size;
 
394
+    } else
 
395
+        x4->params.rc.f_vbv_buffer_init = 0.9;
 
396
+
 
397
+#if X264_BUILD >= 69
 
398
+    x4->params.rc.b_mb_tree               = !!(avctx->flags2 & CODEC_FLAG2_MBTREE);
 
399
+#endif
 
400
+    x4->params.rc.f_ip_factor             = 1 / fabs(avctx->i_quant_factor);
 
401
+    x4->params.rc.f_pb_factor             = avctx->b_quant_factor;
 
402
     x4->params.analyse.i_chroma_qp_offset = avctx->chromaoffset;
 
403
 
 
404
     x4->params.analyse.b_psnr = avctx->flags & CODEC_FLAG_PSNR;
 
405
-    x4->params.i_log_level = X264_LOG_DEBUG;
 
406
+    x4->params.i_log_level    = X264_LOG_DEBUG;
 
407
 
 
408
-    x4->params.b_aud = avctx->flags2 & CODEC_FLAG2_AUD;
 
409
+    x4->params.b_aud          = avctx->flags2 & CODEC_FLAG2_AUD;
 
410
 
 
411
-    x4->params.i_threads = avctx->thread_count;
 
412
+    x4->params.i_threads      = avctx->thread_count;
 
413
 
 
414
-    x4->params.b_interlaced = avctx->flags & CODEC_FLAG_INTERLACED_DCT;
 
415
+    x4->params.b_interlaced   = avctx->flags & CODEC_FLAG_INTERLACED_DCT;
 
416
 
 
417
-    if(avctx->flags & CODEC_FLAG_GLOBAL_HEADER){
 
418
+    if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER)
 
419
         x4->params.b_repeat_headers = 0;
 
420
-    }
 
421
 
 
422
     x4->enc = x264_encoder_open(&x4->params);
 
423
-    if(!x4->enc)
 
424
+    if (!x4->enc)
 
425
         return -1;
 
426
 
 
427
     avctx->coded_frame = &x4->out_pic;
 
428
 
 
429
+#if X264_BUILD >= 76
 
430
+    if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
 
431
+        x264_nal_t *nal;
 
432
+        int nnal, s;
 
433
+
 
434
+        s = x264_encoder_headers(x4->enc, &nal, &nnal);
 
435
+
 
436
+        avctx->extradata      = av_malloc(s);
 
437
+        avctx->extradata_size = encode_nals(avctx, avctx->extradata, s, nal, nnal, 1);
 
438
+    }
 
439
+#else
 
440
     if(avctx->flags & CODEC_FLAG_GLOBAL_HEADER){
 
441
         x264_nal_t *nal;
 
442
         int nnal, i, s = 0;
 
443
@@ -284,21 +356,22 @@ X264_init(AVCodecContext *avctx)
 
444
             s += 5 + nal[i].i_payload * 4 / 3;
 
445
 
 
446
         avctx->extradata = av_malloc(s);
 
447
-        avctx->extradata_size = encode_nals(avctx->extradata, s, nal, nnal);
 
448
+        avctx->extradata_size = encode_nals(avctx, avctx->extradata, s, nal, nnal, 1);
 
449
     }
 
450
+#endif
 
451
 
 
452
     return 0;
 
453
 }
 
454
 
 
455
 AVCodec libx264_encoder = {
 
456
-    .name = "libx264",
 
457
-    .type = CODEC_TYPE_VIDEO,
 
458
-    .id = CODEC_ID_H264,
 
459
+    .name           = "libx264",
 
460
+    .type           = CODEC_TYPE_VIDEO,
 
461
+    .id             = CODEC_ID_H264,
 
462
     .priv_data_size = sizeof(X264Context),
 
463
-    .init = X264_init,
 
464
-    .encode = X264_frame,
 
465
-    .close = X264_close,
 
466
-    .capabilities = CODEC_CAP_DELAY,
 
467
-    .pix_fmts = (enum PixelFormat[]) { PIX_FMT_YUV420P, PIX_FMT_NONE },
 
468
-    .long_name = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
 
469
+    .init           = X264_init,
 
470
+    .encode         = X264_frame,
 
471
+    .close          = X264_close,
 
472
+    .capabilities   = CODEC_CAP_DELAY,
 
473
+    .pix_fmts       = (const enum PixelFormat[]) { PIX_FMT_YUV420P, PIX_FMT_NONE },
 
474
+    .long_name      = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
 
475
 };
 
476
--- a/libavcodec/avcodec.h
 
477
+++ b/libavcodec/avcodec.h
 
478
@@ -501,6 +501,7 @@ typedef struct RcOverride{
 
479
 #define CODEC_FLAG2_CHUNKS        0x00008000 ///< Input bitstream might be truncated at a packet boundaries instead of only at frame boundaries.
 
480
 #define CODEC_FLAG2_NON_LINEAR_QUANT 0x00010000 ///< Use MPEG-2 nonlinear quantizer.
 
481
 #define CODEC_FLAG2_BIT_RESERVOIR 0x00020000 ///< Use a bit reservoir when encoding if possible
 
482
+#define CODEC_FLAG2_MBTREE        0x00040000 ///< Use macroblock tree ratecontrol (x264 only)
 
483
 
 
484
 /* Unsupported options :
 
485
  *              Syntax Arithmetic coding (SAC)
 
486
@@ -2332,6 +2333,16 @@ typedef struct AVCodecContext {
 
487
      * Set to time_base ticks per frame. Default 1, e.g., H.264/MPEG-2 set it to 2.
 
488
      */
 
489
     int ticks_per_frame;
 
490
+
 
491
+    /**
 
492
+     * explicit P-frame weighted prediction analysis method
 
493
+     * 0: off
 
494
+     * 1: fast blind weighting (one reference duplicate with -1 offset)
 
495
+     * 2: smart weighting (full fade detection analysis)
 
496
+     * - encoding: Set by user.
 
497
+     * - decoding: unused
 
498
+     */
 
499
+    int weighted_p_pred;
 
500
 } AVCodecContext;
 
501
 
 
502
 /**
 
503
--- a/ffpresets/libx264-lossless_slow.ffpreset
 
504
+++ b/ffpresets/libx264-lossless_slow.ffpreset
 
505
@@ -16,5 +16,6 @@ qmax=51
 
506
 qdiff=4
 
507
 refs=2
 
508
 directpred=1
 
509
-flags2=+dct8x8+fastpskip
 
510
+flags2=+dct8x8+fastpskip+mbtree
 
511
 cqp=0
 
512
+wpredp=2
 
513
--- a/ffpresets/libx264-lossless_max.ffpreset
 
514
+++ b/ffpresets/libx264-lossless_max.ffpreset
 
515
@@ -16,5 +16,6 @@ qmax=51
 
516
 qdiff=4
 
517
 refs=16
 
518
 directpred=1
 
519
-flags2=+mixed_refs+dct8x8+fastpskip
 
520
+flags2=+mixed_refs+dct8x8+fastpskip+mbtree
 
521
 cqp=0
 
522
+wpredp=2
 
523
--- a/ffpresets/libx264-ipod320.ffpreset
 
524
+++ b/ffpresets/libx264-ipod320.ffpreset
 
525
@@ -1,6 +1,7 @@
 
526
 coder=0
 
527
 bf=0
 
528
-flags2=-wpred-dct8x8
 
529
+flags2=-wpred-dct8x8+mbtree
 
530
 level=13
 
531
 maxrate=768000
 
532
 bufsize=3000000
 
533
+wpredp=0
 
534
--- a/ffpresets/libx264-baseline.ffpreset
 
535
+++ b/ffpresets/libx264-baseline.ffpreset
 
536
@@ -1,3 +1,4 @@
 
537
 coder=0
 
538
 bf=0
 
539
-flags2=-wpred-dct8x8
 
540
+flags2=-wpred-dct8x8+mbtree
 
541
+wpredp=0
 
542
--- a/ffpresets/libx264-slowfirstpass.ffpreset
 
543
+++ b/ffpresets/libx264-slowfirstpass.ffpreset
 
544
@@ -14,8 +14,9 @@ qcomp=0.6
 
545
 qmin=10
 
546
 qmax=51
 
547
 qdiff=4
 
548
-bf=4
 
549
+bf=3
 
550
 refs=1
 
551
 directpred=3
 
552
 trellis=0
 
553
-flags2=+bpyramid+wpred+dct8x8+fastpskip
 
554
+flags2=+wpred+dct8x8+fastpskip+mbtree
 
555
+wpredp=2
 
556
--- a/ffpresets/libx264-default.ffpreset
 
557
+++ b/ffpresets/libx264-default.ffpreset
 
558
@@ -3,7 +3,7 @@ flags=+loop
 
559
 cmp=+chroma
 
560
 partitions=+parti8x8+parti4x4+partp8x8+partb8x8
 
561
 me_method=hex
 
562
-subq=6
 
563
+subq=7
 
564
 me_range=16
 
565
 g=250
 
566
 keyint_min=25
 
567
@@ -14,5 +14,9 @@ qcomp=0.6
 
568
 qmin=10
 
569
 qmax=51
 
570
 qdiff=4
 
571
+bf=3
 
572
+refs=3
 
573
 directpred=1
 
574
-flags2=+fastpskip
 
575
+trellis=1
 
576
+flags2=+mixed_refs+wpred+dct8x8+fastpskip+mbtree
 
577
+wpredp=2
 
578
--- a/ffpresets/libx264-lossless_fast.ffpreset
 
579
+++ b/ffpresets/libx264-lossless_fast.ffpreset
 
580
@@ -15,5 +15,6 @@ qmin=10
 
581
 qmax=51
 
582
 qdiff=4
 
583
 directpred=1
 
584
-flags2=+fastpskip
 
585
+flags2=+fastpskip+mbtree
 
586
 cqp=0
 
587
+wpredp=0
 
588
--- a/ffpresets/libx264-main.ffpreset
 
589
+++ b/ffpresets/libx264-main.ffpreset
 
590
@@ -1 +1 @@
 
591
-flags2=-dct8x8
 
592
+flags2=-dct8x8+mbtree
 
593
--- a/ffpresets/libx264-ipod640.ffpreset
 
594
+++ b/ffpresets/libx264-ipod640.ffpreset
 
595
@@ -1,7 +1,8 @@
 
596
 coder=0
 
597
 bf=0
 
598
 refs=1
 
599
-flags2=-wpred-dct8x8
 
600
+flags2=-wpred-dct8x8+mbtree
 
601
 level=30
 
602
 maxrate=10000000
 
603
 bufsize=10000000
 
604
+wpredp=0
 
605
--- a/ffpresets/libx264-max.ffpreset
 
606
+++ b/ffpresets/libx264-max.ffpreset
 
607
@@ -3,8 +3,8 @@ flags=+loop
 
608
 cmp=+chroma
 
609
 partitions=+parti8x8+parti4x4+partp8x8+partp4x4+partb8x8
 
610
 me_method=tesa
 
611
-subq=9
 
612
-me_range=32
 
613
+subq=10
 
614
+me_range=24
 
615
 g=250
 
616
 keyint_min=25
 
617
 sc_threshold=40
 
618
@@ -14,8 +14,9 @@ qcomp=0.6
 
619
 qmin=10
 
620
 qmax=51
 
621
 qdiff=4
 
622
-bf=4
 
623
+bf=3
 
624
 refs=16
 
625
 directpred=3
 
626
 trellis=2
 
627
-flags2=+bpyramid+wpred+mixed_refs+dct8x8-fastpskip
 
628
+flags2=+wpred+mixed_refs+dct8x8-fastpskip+mbtree
 
629
+wpredp=2
 
630
--- a/ffpresets/libx264-normal.ffpreset
 
631
+++ b/ffpresets/libx264-normal.ffpreset
 
632
@@ -14,8 +14,9 @@ qcomp=0.6
 
633
 qmin=10
 
634
 qmax=51
 
635
 qdiff=4
 
636
-bf=4
 
637
+bf=3
 
638
 refs=2
 
639
 directpred=3
 
640
 trellis=0
 
641
-flags2=+bpyramid+wpred+dct8x8+fastpskip
 
642
+flags2=+wpred+dct8x8+fastpskip+mbtree
 
643
+wpredp=2
 
644
--- a/ffpresets/libx264-fastfirstpass.ffpreset
 
645
+++ b/ffpresets/libx264-fastfirstpass.ffpreset
 
646
@@ -3,7 +3,7 @@ flags=+loop
 
647
 cmp=+chroma
 
648
 partitions=-parti8x8-parti4x4-partp8x8-partp4x4-partb8x8
 
649
 me_method=dia
 
650
-subq=1
 
651
+subq=2
 
652
 me_range=16
 
653
 g=250
 
654
 keyint_min=25
 
655
@@ -14,8 +14,9 @@ qcomp=0.6
 
656
 qmin=10
 
657
 qmax=51
 
658
 qdiff=4
 
659
-bf=4
 
660
+bf=3
 
661
 refs=1
 
662
 directpred=3
 
663
 trellis=0
 
664
-flags2=-bpyramid-wpred-mixed_refs-dct8x8+fastpskip
 
665
+flags2=-bpyramid-wpred-mixed_refs-dct8x8+fastpskip+mbtree
 
666
+wpredp=2
 
667
--- a/ffpresets/libx264-lossless_ultrafast.ffpreset
 
668
+++ b/ffpresets/libx264-lossless_ultrafast.ffpreset
 
669
@@ -15,5 +15,5 @@ qmin=10
 
670
 qmax=51
 
671
 qdiff=4
 
672
 directpred=1
 
673
-flags2=+fastpskip
 
674
+flags2=+fastpskip+mbtree
 
675
 cqp=0
 
676
--- a/ffpresets/libx264-lossless_slower.ffpreset
 
677
+++ b/ffpresets/libx264-lossless_slower.ffpreset
 
678
@@ -16,5 +16,6 @@ qmax=51
 
679
 qdiff=4
 
680
 refs=4
 
681
 directpred=1
 
682
-flags2=+mixed_refs+dct8x8+fastpskip
 
683
+flags2=+mixed_refs+dct8x8+fastpskip+mbtree
 
684
 cqp=0
 
685
+wpredp=2
 
686
--- a/ffpresets/libx264-lossless_medium.ffpreset
 
687
+++ b/ffpresets/libx264-lossless_medium.ffpreset
 
688
@@ -15,5 +15,6 @@ qmin=10
 
689
 qmax=51
 
690
 qdiff=4
 
691
 directpred=1
 
692
-flags2=+fastpskip
 
693
+flags2=+fastpskip+mbtree
 
694
 cqp=0
 
695
+wpredp=2
 
696
--- a/ffpresets/libx264-hq.ffpreset
 
697
+++ b/ffpresets/libx264-hq.ffpreset
 
698
@@ -14,8 +14,9 @@ qcomp=0.6
 
699
 qmin=10
 
700
 qmax=51
 
701
 qdiff=4
 
702
-bf=4
 
703
+bf=3
 
704
 refs=4
 
705
 directpred=3
 
706
 trellis=1
 
707
-flags2=+bpyramid+wpred+mixed_refs+dct8x8+fastpskip
 
708
+flags2=+wpred+mixed_refs+dct8x8+fastpskip+mbtree
 
709
+wpredp=2
 
710
--- a/configure
 
711
+++ b/configure
 
712
@@ -2011,7 +2011,7 @@ enabled libschroedinger && add_cflags $(
 
713
 enabled libspeex   && require  libspeex speex/speex.h speex_decoder_init -lspeex
 
714
 enabled libtheora  && require  libtheora theora/theora.h theora_info_init -ltheora -logg
 
715
 enabled libvorbis  && require  libvorbis vorbis/vorbisenc.h vorbis_info_init -lvorbisenc -lvorbis -logg
 
716
-enabled libx264    && require  libx264 x264.h x264_encoder_open -lx264 -lm &&
 
717
+enabled libx264    && require  libx264 x264.h x264_encoder_encode -lx264 -lm &&
 
718
                       { check_cpp_condition x264.h "X264_BUILD >= 65" ||
 
719
                         die "ERROR: libx264 version must be >= 0.65."; }
 
720
 enabled libxvid    && require  libxvid xvid.h xvid_global -lxvidcore