]> git.sesse.net Git - ffmpeg/blob - libavcodec/libx264.c
libx264: connect AVCodecContext.rtp_payload_size to x264_params_t.i_slice_max_size
[ffmpeg] / libavcodec / libx264.c
1 /*
2  * H.264 encoding using the x264 library
3  * Copyright (C) 2005  Mans Rullgard <mans@mansr.com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/internal.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/mem.h"
25 #include "libavutil/pixdesc.h"
26 #include "avcodec.h"
27 #include "internal.h"
28 #include <x264.h>
29 #include <float.h>
30 #include <math.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 typedef struct X264Context {
36     AVClass        *class;
37     x264_param_t    params;
38     x264_t         *enc;
39     x264_picture_t  pic;
40     uint8_t        *sei;
41     int             sei_size;
42     AVFrame         out_pic;
43     char *preset;
44     char *tune;
45     char *profile;
46     char *level;
47     int fastfirstpass;
48     char *wpredp;
49     char *x264opts;
50     float crf;
51     float crf_max;
52     int cqp;
53     int aq_mode;
54     float aq_strength;
55     char *psy_rd;
56     int psy;
57     int rc_lookahead;
58     int weightp;
59     int weightb;
60     int ssim;
61     int intra_refresh;
62     int b_bias;
63     int b_pyramid;
64     int mixed_refs;
65     int dct8x8;
66     int fast_pskip;
67     int aud;
68     int mbtree;
69     char *deblock;
70     float cplxblur;
71     char *partitions;
72     int direct_pred;
73     int slice_max_size;
74     char *stats;
75 } X264Context;
76
77 static void X264_log(void *p, int level, const char *fmt, va_list args)
78 {
79     static const int level_map[] = {
80         [X264_LOG_ERROR]   = AV_LOG_ERROR,
81         [X264_LOG_WARNING] = AV_LOG_WARNING,
82         [X264_LOG_INFO]    = AV_LOG_INFO,
83         [X264_LOG_DEBUG]   = AV_LOG_DEBUG
84     };
85
86     if (level < 0 || level > X264_LOG_DEBUG)
87         return;
88
89     av_vlog(p, level_map[level], fmt, args);
90 }
91
92
93 static int encode_nals(AVCodecContext *ctx, AVPacket *pkt,
94                        x264_nal_t *nals, int nnal)
95 {
96     X264Context *x4 = ctx->priv_data;
97     uint8_t *p;
98     int i, size = x4->sei_size, ret;
99
100     if (!nnal)
101         return 0;
102
103     for (i = 0; i < nnal; i++)
104         size += nals[i].i_payload;
105
106     if ((ret = ff_alloc_packet2(ctx, pkt, size)) < 0)
107         return ret;
108
109     p = pkt->data;
110
111     /* Write the SEI as part of the first frame. */
112     if (x4->sei_size > 0 && nnal > 0) {
113         if (x4->sei_size > size) {
114             av_log(ctx, AV_LOG_ERROR, "Error: nal buffer is too small\n");
115             return -1;
116         }
117         memcpy(p, x4->sei, x4->sei_size);
118         p += x4->sei_size;
119         x4->sei_size = 0;
120         av_freep(&x4->sei);
121     }
122
123     for (i = 0; i < nnal; i++){
124         memcpy(p, nals[i].p_payload, nals[i].i_payload);
125         p += nals[i].i_payload;
126     }
127
128     return 1;
129 }
130
131 static int avfmt2_num_planes(int avfmt)
132 {
133     switch (avfmt) {
134     case PIX_FMT_YUV420P:
135     case PIX_FMT_YUVJ420P:
136     case PIX_FMT_YUV420P9:
137     case PIX_FMT_YUV420P10:
138     case PIX_FMT_YUV444P:
139         return 3;
140
141     case PIX_FMT_BGR24:
142     case PIX_FMT_RGB24:
143         return 1;
144
145     default:
146         return 3;
147     }
148 }
149
150 static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame,
151                       int *got_packet)
152 {
153     X264Context *x4 = ctx->priv_data;
154     x264_nal_t *nal;
155     int nnal, i, ret;
156     x264_picture_t pic_out;
157
158     x264_picture_init( &x4->pic );
159     x4->pic.img.i_csp   = x4->params.i_csp;
160     if (x264_bit_depth > 8)
161         x4->pic.img.i_csp |= X264_CSP_HIGH_DEPTH;
162     x4->pic.img.i_plane = avfmt2_num_planes(ctx->pix_fmt);
163
164     if (frame) {
165         for (i = 0; i < x4->pic.img.i_plane; i++) {
166             x4->pic.img.plane[i]    = frame->data[i];
167             x4->pic.img.i_stride[i] = frame->linesize[i];
168         }
169
170         x4->pic.i_pts  = frame->pts;
171         x4->pic.i_type =
172             frame->pict_type == AV_PICTURE_TYPE_I ? X264_TYPE_KEYFRAME :
173             frame->pict_type == AV_PICTURE_TYPE_P ? X264_TYPE_P :
174             frame->pict_type == AV_PICTURE_TYPE_B ? X264_TYPE_B :
175                                             X264_TYPE_AUTO;
176         if (x4->params.b_tff != frame->top_field_first) {
177             x4->params.b_tff = frame->top_field_first;
178             x264_encoder_reconfig(x4->enc, &x4->params);
179         }
180         if (x4->params.vui.i_sar_height != ctx->sample_aspect_ratio.den ||
181             x4->params.vui.i_sar_width  != ctx->sample_aspect_ratio.num) {
182             x4->params.vui.i_sar_height = ctx->sample_aspect_ratio.den;
183             x4->params.vui.i_sar_width  = ctx->sample_aspect_ratio.num;
184             x264_encoder_reconfig(x4->enc, &x4->params);
185         }
186     }
187
188     do {
189         if (x264_encoder_encode(x4->enc, &nal, &nnal, frame? &x4->pic: NULL, &pic_out) < 0)
190             return -1;
191
192         ret = encode_nals(ctx, pkt, nal, nnal);
193         if (ret < 0)
194             return -1;
195     } while (!ret && !frame && x264_encoder_delayed_frames(x4->enc));
196
197     pkt->pts = pic_out.i_pts;
198     pkt->dts = pic_out.i_dts;
199
200     switch (pic_out.i_type) {
201     case X264_TYPE_IDR:
202     case X264_TYPE_I:
203         x4->out_pic.pict_type = AV_PICTURE_TYPE_I;
204         break;
205     case X264_TYPE_P:
206         x4->out_pic.pict_type = AV_PICTURE_TYPE_P;
207         break;
208     case X264_TYPE_B:
209     case X264_TYPE_BREF:
210         x4->out_pic.pict_type = AV_PICTURE_TYPE_B;
211         break;
212     }
213
214     pkt->flags |= AV_PKT_FLAG_KEY*pic_out.b_keyframe;
215     if (ret)
216         x4->out_pic.quality = (pic_out.i_qpplus1 - 1) * FF_QP2LAMBDA;
217
218     *got_packet = ret;
219     return 0;
220 }
221
222 static av_cold int X264_close(AVCodecContext *avctx)
223 {
224     X264Context *x4 = avctx->priv_data;
225
226     av_freep(&avctx->extradata);
227     av_free(x4->sei);
228
229     if (x4->enc)
230         x264_encoder_close(x4->enc);
231
232     return 0;
233 }
234
235 #define OPT_STR(opt, param)                                                   \
236     do {                                                                      \
237         int ret;                                                              \
238         if (param && (ret = x264_param_parse(&x4->params, opt, param)) < 0) { \
239             if(ret == X264_PARAM_BAD_NAME)                                    \
240                 av_log(avctx, AV_LOG_ERROR,                                   \
241                         "bad option '%s': '%s'\n", opt, param);               \
242             else                                                              \
243                 av_log(avctx, AV_LOG_ERROR,                                   \
244                         "bad value for '%s': '%s'\n", opt, param);            \
245             return -1;                                                        \
246         }                                                                     \
247     } while (0)
248
249 static int convert_pix_fmt(enum PixelFormat pix_fmt)
250 {
251     switch (pix_fmt) {
252     case PIX_FMT_YUV420P:
253     case PIX_FMT_YUVJ420P:
254     case PIX_FMT_YUV420P9:
255     case PIX_FMT_YUV420P10: return X264_CSP_I420;
256     case PIX_FMT_YUV422P:
257     case PIX_FMT_YUV422P10: return X264_CSP_I422;
258     case PIX_FMT_YUV444P:
259     case PIX_FMT_YUV444P9:
260     case PIX_FMT_YUV444P10: return X264_CSP_I444;
261 #ifdef X264_CSP_BGR
262     case PIX_FMT_BGR24:
263         return X264_CSP_BGR;
264
265     case PIX_FMT_RGB24:
266         return X264_CSP_RGB;
267 #endif
268     };
269     return 0;
270 }
271
272 #define PARSE_X264_OPT(name, var)\
273     if (x4->var && x264_param_parse(&x4->params, name, x4->var) < 0) {\
274         av_log(avctx, AV_LOG_ERROR, "Error parsing option '%s' with value '%s'.\n", name, x4->var);\
275         return AVERROR(EINVAL);\
276     }
277
278 static av_cold int X264_init(AVCodecContext *avctx)
279 {
280     X264Context *x4 = avctx->priv_data;
281     int sw,sh;
282
283     x264_param_default(&x4->params);
284
285     x4->params.b_deblocking_filter         = avctx->flags & CODEC_FLAG_LOOP_FILTER;
286
287     x4->params.rc.f_ip_factor             = 1 / fabs(avctx->i_quant_factor);
288     x4->params.rc.f_pb_factor             = avctx->b_quant_factor;
289     x4->params.analyse.i_chroma_qp_offset = avctx->chromaoffset;
290     if (x4->preset || x4->tune)
291         if (x264_param_default_preset(&x4->params, x4->preset, x4->tune) < 0) {
292             int i;
293             av_log(avctx, AV_LOG_ERROR, "Error setting preset/tune %s/%s.\n", x4->preset, x4->tune);
294             av_log(avctx, AV_LOG_INFO, "Possible presets:");
295             for (i = 0; x264_preset_names[i]; i++)
296                 av_log(avctx, AV_LOG_INFO, " %s", x264_preset_names[i]);
297             av_log(avctx, AV_LOG_INFO, "\n");
298             av_log(avctx, AV_LOG_INFO, "Possible tunes:");
299             for (i = 0; x264_tune_names[i]; i++)
300                 av_log(avctx, AV_LOG_INFO, " %s", x264_tune_names[i]);
301             av_log(avctx, AV_LOG_INFO, "\n");
302             return AVERROR(EINVAL);
303         }
304
305     if (avctx->level > 0)
306         x4->params.i_level_idc = avctx->level;
307
308     x4->params.pf_log               = X264_log;
309     x4->params.p_log_private        = avctx;
310     x4->params.i_log_level          = X264_LOG_DEBUG;
311     x4->params.i_csp                = convert_pix_fmt(avctx->pix_fmt);
312
313     OPT_STR("weightp", x4->wpredp);
314
315     if (avctx->bit_rate) {
316         x4->params.rc.i_bitrate   = avctx->bit_rate / 1000;
317         x4->params.rc.i_rc_method = X264_RC_ABR;
318     }
319     x4->params.rc.i_vbv_buffer_size = avctx->rc_buffer_size / 1000;
320     x4->params.rc.i_vbv_max_bitrate = avctx->rc_max_rate    / 1000;
321     x4->params.rc.b_stat_write      = avctx->flags & CODEC_FLAG_PASS1;
322     if (avctx->flags & CODEC_FLAG_PASS2) {
323         x4->params.rc.b_stat_read = 1;
324     } else {
325         if (x4->crf >= 0) {
326             x4->params.rc.i_rc_method   = X264_RC_CRF;
327             x4->params.rc.f_rf_constant = x4->crf;
328         } else if (x4->cqp >= 0) {
329             x4->params.rc.i_rc_method   = X264_RC_CQP;
330             x4->params.rc.i_qp_constant = x4->cqp;
331         }
332
333         if (x4->crf_max >= 0)
334             x4->params.rc.f_rf_constant_max = x4->crf_max;
335     }
336
337     if (avctx->rc_buffer_size && avctx->rc_initial_buffer_occupancy &&
338         (avctx->rc_initial_buffer_occupancy <= avctx->rc_buffer_size)) {
339         x4->params.rc.f_vbv_buffer_init =
340             (float)avctx->rc_initial_buffer_occupancy / avctx->rc_buffer_size;
341     }
342
343     OPT_STR("level", x4->level);
344
345     if(x4->x264opts){
346         const char *p= x4->x264opts;
347         while(p){
348             char param[256]={0}, val[256]={0};
349             if(sscanf(p, "%255[^:=]=%255[^:]", param, val) == 1){
350                 OPT_STR(param, "1");
351             }else
352                 OPT_STR(param, val);
353             p= strchr(p, ':');
354             p+=!!p;
355         }
356     }
357
358     if (avctx->me_method == ME_EPZS)
359         x4->params.analyse.i_me_method = X264_ME_DIA;
360     else if (avctx->me_method == ME_HEX)
361         x4->params.analyse.i_me_method = X264_ME_HEX;
362     else if (avctx->me_method == ME_UMH)
363         x4->params.analyse.i_me_method = X264_ME_UMH;
364     else if (avctx->me_method == ME_FULL)
365         x4->params.analyse.i_me_method = X264_ME_ESA;
366     else if (avctx->me_method == ME_TESA)
367         x4->params.analyse.i_me_method = X264_ME_TESA;
368
369     if (avctx->gop_size >= 0)
370         x4->params.i_keyint_max         = avctx->gop_size;
371     if (avctx->max_b_frames >= 0)
372         x4->params.i_bframe             = avctx->max_b_frames;
373     if (avctx->scenechange_threshold >= 0)
374         x4->params.i_scenecut_threshold = avctx->scenechange_threshold;
375     if (avctx->qmin >= 0)
376         x4->params.rc.i_qp_min          = avctx->qmin;
377     if (avctx->qmax >= 0)
378         x4->params.rc.i_qp_max          = avctx->qmax;
379     if (avctx->max_qdiff >= 0)
380         x4->params.rc.i_qp_step         = avctx->max_qdiff;
381     if (avctx->qblur >= 0)
382         x4->params.rc.f_qblur           = avctx->qblur;     /* temporally blur quants */
383     if (avctx->qcompress >= 0)
384         x4->params.rc.f_qcompress       = avctx->qcompress; /* 0.0 => cbr, 1.0 => constant qp */
385     if (avctx->refs >= 0)
386         x4->params.i_frame_reference    = avctx->refs;
387     if (avctx->trellis >= 0)
388         x4->params.analyse.i_trellis    = avctx->trellis;
389     if (avctx->me_range >= 0)
390         x4->params.analyse.i_me_range   = avctx->me_range;
391     if (avctx->noise_reduction >= 0)
392         x4->params.analyse.i_noise_reduction = avctx->noise_reduction;
393     if (avctx->me_subpel_quality >= 0)
394         x4->params.analyse.i_subpel_refine   = avctx->me_subpel_quality;
395     if (avctx->b_frame_strategy >= 0)
396         x4->params.i_bframe_adaptive = avctx->b_frame_strategy;
397     if (avctx->keyint_min >= 0)
398         x4->params.i_keyint_min = avctx->keyint_min;
399     if (avctx->coder_type >= 0)
400         x4->params.b_cabac = avctx->coder_type == FF_CODER_TYPE_AC;
401     if (avctx->me_cmp >= 0)
402         x4->params.analyse.b_chroma_me = avctx->me_cmp & FF_CMP_CHROMA;
403
404     if (x4->aq_mode >= 0)
405         x4->params.rc.i_aq_mode = x4->aq_mode;
406     if (x4->aq_strength >= 0)
407         x4->params.rc.f_aq_strength = x4->aq_strength;
408     PARSE_X264_OPT("psy-rd", psy_rd);
409     PARSE_X264_OPT("deblock", deblock);
410     PARSE_X264_OPT("partitions", partitions);
411     PARSE_X264_OPT("stats", stats);
412     if (x4->psy >= 0)
413         x4->params.analyse.b_psy  = x4->psy;
414     if (x4->rc_lookahead >= 0)
415         x4->params.rc.i_lookahead = x4->rc_lookahead;
416     if (x4->weightp >= 0)
417         x4->params.analyse.i_weighted_pred = x4->weightp;
418     if (x4->weightb >= 0)
419         x4->params.analyse.b_weighted_bipred = x4->weightb;
420     if (x4->cplxblur >= 0)
421         x4->params.rc.f_complexity_blur = x4->cplxblur;
422
423     if (x4->ssim >= 0)
424         x4->params.analyse.b_ssim = x4->ssim;
425     if (x4->intra_refresh >= 0)
426         x4->params.b_intra_refresh = x4->intra_refresh;
427     if (x4->b_bias != INT_MIN)
428         x4->params.i_bframe_bias              = x4->b_bias;
429     if (x4->b_pyramid >= 0)
430         x4->params.i_bframe_pyramid = x4->b_pyramid;
431     if (x4->mixed_refs >= 0)
432         x4->params.analyse.b_mixed_references = x4->mixed_refs;
433     if (x4->dct8x8 >= 0)
434         x4->params.analyse.b_transform_8x8    = x4->dct8x8;
435     if (x4->fast_pskip >= 0)
436         x4->params.analyse.b_fast_pskip       = x4->fast_pskip;
437     if (x4->aud >= 0)
438         x4->params.b_aud                      = x4->aud;
439     if (x4->mbtree >= 0)
440         x4->params.rc.b_mb_tree               = x4->mbtree;
441     if (x4->direct_pred >= 0)
442         x4->params.analyse.i_direct_mv_pred   = x4->direct_pred;
443
444     if (x4->slice_max_size >= 0)
445         x4->params.i_slice_max_size =  x4->slice_max_size;
446     else {
447         /*
448          * Allow x264 to be instructed through AVCodecContext about the maximum
449          * size of the RTP payload. For example, this enables the production of
450          * payload suitable for the H.264 RTP packetization-mode 0 i.e. single
451          * NAL unit per RTP packet.
452          */
453         if (avctx->rtp_payload_size)
454             x4->params.i_slice_max_size = avctx->rtp_payload_size;
455     }
456
457     if (x4->fastfirstpass)
458         x264_param_apply_fastfirstpass(&x4->params);
459
460     if (x4->profile)
461         if (x264_param_apply_profile(&x4->params, x4->profile) < 0) {
462             int i;
463             av_log(avctx, AV_LOG_ERROR, "Error setting profile %s.\n", x4->profile);
464             av_log(avctx, AV_LOG_INFO, "Possible profiles:");
465             for (i = 0; x264_profile_names[i]; i++)
466                 av_log(avctx, AV_LOG_INFO, " %s", x264_profile_names[i]);
467             av_log(avctx, AV_LOG_INFO, "\n");
468             return AVERROR(EINVAL);
469         }
470
471     x4->params.i_width          = avctx->width;
472     x4->params.i_height         = avctx->height;
473     av_reduce(&sw, &sh, avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 4096);
474     x4->params.vui.i_sar_width  = sw;
475     x4->params.vui.i_sar_height = sh;
476     x4->params.i_fps_num = x4->params.i_timebase_den = avctx->time_base.den;
477     x4->params.i_fps_den = x4->params.i_timebase_num = avctx->time_base.num;
478
479     x4->params.analyse.b_psnr = avctx->flags & CODEC_FLAG_PSNR;
480
481     x4->params.i_threads      = avctx->thread_count;
482     if (avctx->thread_type)
483         x4->params.b_sliced_threads = avctx->thread_type == FF_THREAD_SLICE;
484
485     x4->params.b_interlaced   = avctx->flags & CODEC_FLAG_INTERLACED_DCT;
486
487 //    x4->params.b_open_gop     = !(avctx->flags & CODEC_FLAG_CLOSED_GOP);
488
489     x4->params.i_slice_count  = avctx->slices;
490
491     x4->params.vui.b_fullrange = avctx->pix_fmt == PIX_FMT_YUVJ420P;
492
493     if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER)
494         x4->params.b_repeat_headers = 0;
495
496     // update AVCodecContext with x264 parameters
497     avctx->has_b_frames = x4->params.i_bframe ?
498         x4->params.i_bframe_pyramid ? 2 : 1 : 0;
499     if (avctx->max_b_frames < 0)
500         avctx->max_b_frames = 0;
501
502     avctx->bit_rate = x4->params.rc.i_bitrate*1000;
503
504     x4->enc = x264_encoder_open(&x4->params);
505     if (!x4->enc)
506         return -1;
507
508     avctx->coded_frame = &x4->out_pic;
509
510     if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
511         x264_nal_t *nal;
512         uint8_t *p;
513         int nnal, s, i;
514
515         s = x264_encoder_headers(x4->enc, &nal, &nnal);
516         avctx->extradata = p = av_malloc(s);
517
518         for (i = 0; i < nnal; i++) {
519             /* Don't put the SEI in extradata. */
520             if (nal[i].i_type == NAL_SEI) {
521                 av_log(avctx, AV_LOG_INFO, "%s\n", nal[i].p_payload+25);
522                 x4->sei_size = nal[i].i_payload;
523                 x4->sei      = av_malloc(x4->sei_size);
524                 memcpy(x4->sei, nal[i].p_payload, nal[i].i_payload);
525                 continue;
526             }
527             memcpy(p, nal[i].p_payload, nal[i].i_payload);
528             p += nal[i].i_payload;
529         }
530         avctx->extradata_size = p - avctx->extradata;
531     }
532
533     return 0;
534 }
535
536 static const enum PixelFormat pix_fmts_8bit[] = {
537     PIX_FMT_YUV420P,
538     PIX_FMT_YUVJ420P,
539     PIX_FMT_YUV422P,
540     PIX_FMT_YUV444P,
541     PIX_FMT_NONE
542 };
543 static const enum PixelFormat pix_fmts_9bit[] = {
544     PIX_FMT_YUV420P9,
545     PIX_FMT_YUV444P9,
546     PIX_FMT_NONE
547 };
548 static const enum PixelFormat pix_fmts_10bit[] = {
549     PIX_FMT_YUV420P10,
550     PIX_FMT_YUV422P10,
551     PIX_FMT_YUV444P10,
552     PIX_FMT_NONE
553 };
554 static const enum PixelFormat pix_fmts_8bit_rgb[] = {
555 #ifdef X264_CSP_BGR
556     PIX_FMT_BGR24,
557     PIX_FMT_RGB24,
558 #endif
559     PIX_FMT_NONE
560 };
561
562 static av_cold void X264_init_static(AVCodec *codec)
563 {
564     if (x264_bit_depth == 8)
565         codec->pix_fmts = pix_fmts_8bit;
566     else if (x264_bit_depth == 9)
567         codec->pix_fmts = pix_fmts_9bit;
568     else if (x264_bit_depth == 10)
569         codec->pix_fmts = pix_fmts_10bit;
570 }
571
572 #define OFFSET(x) offsetof(X264Context, x)
573 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
574 static const AVOption options[] = {
575     { "preset",        "Set the encoding preset (cf. x264 --fullhelp)",   OFFSET(preset),        AV_OPT_TYPE_STRING, { .str = "medium" }, 0, 0, VE},
576     { "tune",          "Tune the encoding params (cf. x264 --fullhelp)",  OFFSET(tune),          AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
577     { "profile",       "Set profile restrictions (cf. x264 --fullhelp) ", OFFSET(profile),       AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
578     { "fastfirstpass", "Use fast settings when encoding first pass",      OFFSET(fastfirstpass), AV_OPT_TYPE_INT,    { .i64 = 1 }, 0, 1, VE},
579     {"level", "Specify level (as defined by Annex A)", OFFSET(level), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
580     {"passlogfile", "Filename for 2 pass stats", OFFSET(stats), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
581     {"wpredp", "Weighted prediction for P-frames", OFFSET(wpredp), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
582     {"x264opts", "x264 options", OFFSET(x264opts), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
583     { "crf",           "Select the quality for constant quality mode",    OFFSET(crf),           AV_OPT_TYPE_FLOAT,  {.dbl = -1 }, -1, FLT_MAX, VE },
584     { "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 },
585     { "qp",            "Constant quantization parameter rate control method",OFFSET(cqp),        AV_OPT_TYPE_INT,    { .i64 = -1 }, -1, INT_MAX, VE },
586     { "aq-mode",       "AQ method",                                       OFFSET(aq_mode),       AV_OPT_TYPE_INT,    { .i64 = -1 }, -1, INT_MAX, VE, "aq_mode"},
587     { "none",          NULL,                              0, AV_OPT_TYPE_CONST, {.i64 = X264_AQ_NONE},         INT_MIN, INT_MAX, VE, "aq_mode" },
588     { "variance",      "Variance AQ (complexity mask)",   0, AV_OPT_TYPE_CONST, {.i64 = X264_AQ_VARIANCE},     INT_MIN, INT_MAX, VE, "aq_mode" },
589     { "autovariance",  "Auto-variance AQ (experimental)", 0, AV_OPT_TYPE_CONST, {.i64 = X264_AQ_AUTOVARIANCE}, INT_MIN, INT_MAX, VE, "aq_mode" },
590     { "aq-strength",   "AQ strength. Reduces blocking and blurring in flat and textured areas.", OFFSET(aq_strength), AV_OPT_TYPE_FLOAT, {.dbl = -1}, -1, FLT_MAX, VE},
591     { "psy",           "Use psychovisual optimizations.",                 OFFSET(psy),           AV_OPT_TYPE_INT,    { .i64 = -1 }, -1, 1, VE },
592     { "psy-rd",        "Strength of psychovisual optimization, in <psy-rd>:<psy-trellis> format.", OFFSET(psy_rd), AV_OPT_TYPE_STRING,  {0 }, 0, 0, VE},
593     { "rc-lookahead",  "Number of frames to look ahead for frametype and ratecontrol", OFFSET(rc_lookahead), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE },
594     { "weightb",       "Weighted prediction for B-frames.",               OFFSET(weightb),       AV_OPT_TYPE_INT,    { .i64 = -1 }, -1, 1, VE },
595     { "weightp",       "Weighted prediction analysis method.",            OFFSET(weightp),       AV_OPT_TYPE_INT,    { .i64 = -1 }, -1, INT_MAX, VE, "weightp" },
596     { "none",          NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_NONE},   INT_MIN, INT_MAX, VE, "weightp" },
597     { "simple",        NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_SIMPLE}, INT_MIN, INT_MAX, VE, "weightp" },
598     { "smart",         NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_SMART},  INT_MIN, INT_MAX, VE, "weightp" },
599     { "ssim",          "Calculate and print SSIM stats.",                 OFFSET(ssim),          AV_OPT_TYPE_INT,    { .i64 = -1 }, -1, 1, VE },
600     { "intra-refresh", "Use Periodic Intra Refresh instead of IDR frames.",OFFSET(intra_refresh),AV_OPT_TYPE_INT,    { .i64 = -1 }, -1, 1, VE },
601     { "b-bias",        "Influences how often B-frames are used",          OFFSET(b_bias),        AV_OPT_TYPE_INT,    { .i64 = INT_MIN}, INT_MIN, INT_MAX, VE },
602     { "b-pyramid",     "Keep some B-frames as references.",               OFFSET(b_pyramid),     AV_OPT_TYPE_INT,    { .i64 = -1 }, -1, INT_MAX, VE, "b_pyramid" },
603     { "none",          NULL,                                  0, AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_NONE},   INT_MIN, INT_MAX, VE, "b_pyramid" },
604     { "strict",        "Strictly hierarchical pyramid",       0, AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_STRICT}, INT_MIN, INT_MAX, VE, "b_pyramid" },
605     { "normal",        "Non-strict (not Blu-ray compatible)", 0, AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_NORMAL}, INT_MIN, INT_MAX, VE, "b_pyramid" },
606     { "mixed-refs",    "One reference per partition, as opposed to one reference per macroblock", OFFSET(mixed_refs), AV_OPT_TYPE_INT, { .i64 = -1}, -1, 1, VE },
607     { "8x8dct",        "High profile 8x8 transform.",                     OFFSET(dct8x8),        AV_OPT_TYPE_INT,    { .i64 = -1 }, -1, 1, VE},
608     { "fast-pskip",    NULL,                                              OFFSET(fast_pskip),    AV_OPT_TYPE_INT,    { .i64 = -1 }, -1, 1, VE},
609     { "aud",           "Use access unit delimiters.",                     OFFSET(aud),           AV_OPT_TYPE_INT,    { .i64 = -1 }, -1, 1, VE},
610     { "mbtree",        "Use macroblock tree ratecontrol.",                OFFSET(mbtree),        AV_OPT_TYPE_INT,    { .i64 = -1 }, -1, 1, VE},
611     { "deblock",       "Loop filter parameters, in <alpha:beta> form.",   OFFSET(deblock),       AV_OPT_TYPE_STRING, { 0 },  0, 0, VE},
612     { "cplxblur",      "Reduce fluctuations in QP (before curve compression)", OFFSET(cplxblur), AV_OPT_TYPE_FLOAT,  {.dbl = -1 }, -1, FLT_MAX, VE},
613     { "partitions",    "A comma-separated list of partitions to consider. "
614                        "Possible values: p8x8, p4x4, b8x8, i8x8, i4x4, none, all", OFFSET(partitions), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
615     { "direct-pred",   "Direct MV prediction mode",                       OFFSET(direct_pred),   AV_OPT_TYPE_INT,    { .i64 = -1 }, -1, INT_MAX, VE, "direct-pred" },
616     { "none",          NULL,      0,    AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_NONE },     0, 0, VE, "direct-pred" },
617     { "spatial",       NULL,      0,    AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_SPATIAL },  0, 0, VE, "direct-pred" },
618     { "temporal",      NULL,      0,    AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_TEMPORAL }, 0, 0, VE, "direct-pred" },
619     { "auto",          NULL,      0,    AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_AUTO },     0, 0, VE, "direct-pred" },
620     { "slice-max-size","Limit the size of each slice in bytes",           OFFSET(slice_max_size),AV_OPT_TYPE_INT,    { .i64 = -1 }, -1, INT_MAX, VE },
621     { "stats",         "Filename for 2 pass stats",                       OFFSET(stats),         AV_OPT_TYPE_STRING, { 0 },  0,       0, VE },
622     { NULL },
623 };
624
625 static const AVClass class = {
626     .class_name = "libx264",
627     .item_name  = av_default_item_name,
628     .option     = options,
629     .version    = LIBAVUTIL_VERSION_INT,
630 };
631
632 static const AVClass rgbclass = {
633     .class_name = "libx264rgb",
634     .item_name  = av_default_item_name,
635     .option     = options,
636     .version    = LIBAVUTIL_VERSION_INT,
637 };
638
639 static const AVCodecDefault x264_defaults[] = {
640     { "b",                "0" },
641     { "bf",               "-1" },
642     { "flags2",           "0" },
643     { "g",                "-1" },
644     { "qmin",             "-1" },
645     { "qmax",             "-1" },
646     { "qdiff",            "-1" },
647     { "qblur",            "-1" },
648     { "qcomp",            "-1" },
649 //     { "rc_lookahead",     "-1" },
650     { "refs",             "-1" },
651     { "sc_threshold",     "-1" },
652     { "trellis",          "-1" },
653     { "nr",               "-1" },
654     { "me_range",         "-1" },
655     { "me_method",        "-1" },
656     { "subq",             "-1" },
657     { "b_strategy",       "-1" },
658     { "keyint_min",       "-1" },
659     { "coder",            "-1" },
660     { "cmp",              "-1" },
661     { "threads",          AV_STRINGIFY(X264_THREADS_AUTO) },
662     { "thread_type",      "0" },
663     { NULL },
664 };
665
666 AVCodec ff_libx264_encoder = {
667     .name             = "libx264",
668     .type             = AVMEDIA_TYPE_VIDEO,
669     .id               = AV_CODEC_ID_H264,
670     .priv_data_size   = sizeof(X264Context),
671     .init             = X264_init,
672     .encode2          = X264_frame,
673     .close            = X264_close,
674     .capabilities     = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS,
675     .long_name        = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
676     .priv_class       = &class,
677     .defaults         = x264_defaults,
678     .init_static_data = X264_init_static,
679 };
680
681 AVCodec ff_libx264rgb_encoder = {
682     .name           = "libx264rgb",
683     .type           = AVMEDIA_TYPE_VIDEO,
684     .id             = AV_CODEC_ID_H264,
685     .priv_data_size = sizeof(X264Context),
686     .init           = X264_init,
687     .encode2        = X264_frame,
688     .close          = X264_close,
689     .capabilities   = CODEC_CAP_DELAY,
690     .long_name      = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 RGB"),
691     .priv_class     = &rgbclass,
692     .defaults       = x264_defaults,
693     .pix_fmts       = pix_fmts_8bit_rgb,
694 };