]> git.sesse.net Git - ffmpeg/blob - libavcodec/libx264.c
Fix libx264 profile listing
[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
447     if (x4->fastfirstpass)
448         x264_param_apply_fastfirstpass(&x4->params);
449
450     if (x4->profile)
451         if (x264_param_apply_profile(&x4->params, x4->profile) < 0) {
452             int i;
453             av_log(avctx, AV_LOG_ERROR, "Error setting profile %s.\n", x4->profile);
454             av_log(avctx, AV_LOG_INFO, "Possible profiles:");
455             for (i = 0; x264_profile_names[i]; i++)
456                 av_log(avctx, AV_LOG_INFO, " %s", x264_profile_names[i]);
457             av_log(avctx, AV_LOG_INFO, "\n");
458             return AVERROR(EINVAL);
459         }
460
461     x4->params.i_width          = avctx->width;
462     x4->params.i_height         = avctx->height;
463     av_reduce(&sw, &sh, avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 4096);
464     x4->params.vui.i_sar_width  = sw;
465     x4->params.vui.i_sar_height = sh;
466     x4->params.i_fps_num = x4->params.i_timebase_den = avctx->time_base.den;
467     x4->params.i_fps_den = x4->params.i_timebase_num = avctx->time_base.num;
468
469     x4->params.analyse.b_psnr = avctx->flags & CODEC_FLAG_PSNR;
470
471     x4->params.i_threads      = avctx->thread_count;
472     if (avctx->thread_type)
473         x4->params.b_sliced_threads = avctx->thread_type == FF_THREAD_SLICE;
474
475     x4->params.b_interlaced   = avctx->flags & CODEC_FLAG_INTERLACED_DCT;
476
477 //    x4->params.b_open_gop     = !(avctx->flags & CODEC_FLAG_CLOSED_GOP);
478
479     x4->params.i_slice_count  = avctx->slices;
480
481     x4->params.vui.b_fullrange = avctx->pix_fmt == PIX_FMT_YUVJ420P;
482
483     if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER)
484         x4->params.b_repeat_headers = 0;
485
486     // update AVCodecContext with x264 parameters
487     avctx->has_b_frames = x4->params.i_bframe ?
488         x4->params.i_bframe_pyramid ? 2 : 1 : 0;
489     if (avctx->max_b_frames < 0)
490         avctx->max_b_frames = 0;
491
492     avctx->bit_rate = x4->params.rc.i_bitrate*1000;
493
494     x4->enc = x264_encoder_open(&x4->params);
495     if (!x4->enc)
496         return -1;
497
498     avctx->coded_frame = &x4->out_pic;
499
500     if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
501         x264_nal_t *nal;
502         uint8_t *p;
503         int nnal, s, i;
504
505         s = x264_encoder_headers(x4->enc, &nal, &nnal);
506         avctx->extradata = p = av_malloc(s);
507
508         for (i = 0; i < nnal; i++) {
509             /* Don't put the SEI in extradata. */
510             if (nal[i].i_type == NAL_SEI) {
511                 av_log(avctx, AV_LOG_INFO, "%s\n", nal[i].p_payload+25);
512                 x4->sei_size = nal[i].i_payload;
513                 x4->sei      = av_malloc(x4->sei_size);
514                 memcpy(x4->sei, nal[i].p_payload, nal[i].i_payload);
515                 continue;
516             }
517             memcpy(p, nal[i].p_payload, nal[i].i_payload);
518             p += nal[i].i_payload;
519         }
520         avctx->extradata_size = p - avctx->extradata;
521     }
522
523     return 0;
524 }
525
526 static const enum PixelFormat pix_fmts_8bit[] = {
527     PIX_FMT_YUV420P,
528     PIX_FMT_YUVJ420P,
529     PIX_FMT_YUV422P,
530     PIX_FMT_YUV444P,
531     PIX_FMT_NONE
532 };
533 static const enum PixelFormat pix_fmts_9bit[] = {
534     PIX_FMT_YUV420P9,
535     PIX_FMT_YUV444P9,
536     PIX_FMT_NONE
537 };
538 static const enum PixelFormat pix_fmts_10bit[] = {
539     PIX_FMT_YUV420P10,
540     PIX_FMT_YUV422P10,
541     PIX_FMT_YUV444P10,
542     PIX_FMT_NONE
543 };
544 static const enum PixelFormat pix_fmts_8bit_rgb[] = {
545 #ifdef X264_CSP_BGR
546     PIX_FMT_BGR24,
547     PIX_FMT_RGB24,
548 #endif
549     PIX_FMT_NONE
550 };
551
552 static av_cold void X264_init_static(AVCodec *codec)
553 {
554     if (x264_bit_depth == 8)
555         codec->pix_fmts = pix_fmts_8bit;
556     else if (x264_bit_depth == 9)
557         codec->pix_fmts = pix_fmts_9bit;
558     else if (x264_bit_depth == 10)
559         codec->pix_fmts = pix_fmts_10bit;
560 }
561
562 #define OFFSET(x) offsetof(X264Context, x)
563 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
564 static const AVOption options[] = {
565     { "preset",        "Set the encoding preset (cf. x264 --fullhelp)",   OFFSET(preset),        AV_OPT_TYPE_STRING, { .str = "medium" }, 0, 0, VE},
566     { "tune",          "Tune the encoding params (cf. x264 --fullhelp)",  OFFSET(tune),          AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
567     { "profile",       "Set profile restrictions (cf. x264 --fullhelp) ", OFFSET(profile),       AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
568     { "fastfirstpass", "Use fast settings when encoding first pass",      OFFSET(fastfirstpass), AV_OPT_TYPE_INT,    { 1 }, 0, 1, VE},
569     {"level", "Specify level (as defined by Annex A)", OFFSET(level), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
570     {"passlogfile", "Filename for 2 pass stats", OFFSET(stats), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
571     {"wpredp", "Weighted prediction for P-frames", OFFSET(wpredp), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
572     {"x264opts", "x264 options", OFFSET(x264opts), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
573     { "crf",           "Select the quality for constant quality mode",    OFFSET(crf),           AV_OPT_TYPE_FLOAT,  {-1 }, -1, FLT_MAX, VE },
574     { "crf_max",       "In CRF mode, prevents VBV from lowering quality beyond this point.",OFFSET(crf_max), AV_OPT_TYPE_FLOAT, {-1 }, -1, FLT_MAX, VE },
575     { "qp",            "Constant quantization parameter rate control method",OFFSET(cqp),        AV_OPT_TYPE_INT,    {-1 }, -1, INT_MAX, VE },
576     { "aq-mode",       "AQ method",                                       OFFSET(aq_mode),       AV_OPT_TYPE_INT,    {-1 }, -1, INT_MAX, VE, "aq_mode"},
577     { "none",          NULL,                              0, AV_OPT_TYPE_CONST, {X264_AQ_NONE},         INT_MIN, INT_MAX, VE, "aq_mode" },
578     { "variance",      "Variance AQ (complexity mask)",   0, AV_OPT_TYPE_CONST, {X264_AQ_VARIANCE},     INT_MIN, INT_MAX, VE, "aq_mode" },
579     { "autovariance",  "Auto-variance AQ (experimental)", 0, AV_OPT_TYPE_CONST, {X264_AQ_AUTOVARIANCE}, INT_MIN, INT_MAX, VE, "aq_mode" },
580     { "aq-strength",   "AQ strength. Reduces blocking and blurring in flat and textured areas.", OFFSET(aq_strength), AV_OPT_TYPE_FLOAT, {-1}, -1, FLT_MAX, VE},
581     { "psy",           "Use psychovisual optimizations.",                 OFFSET(psy),           AV_OPT_TYPE_INT,    {-1 }, -1, 1, VE },
582     { "psy-rd",        "Strength of psychovisual optimization, in <psy-rd>:<psy-trellis> format.", OFFSET(psy_rd), AV_OPT_TYPE_STRING,  {0 }, 0, 0, VE},
583     { "rc-lookahead",  "Number of frames to look ahead for frametype and ratecontrol", OFFSET(rc_lookahead), AV_OPT_TYPE_INT, {-1 }, -1, INT_MAX, VE },
584     { "weightb",       "Weighted prediction for B-frames.",               OFFSET(weightb),       AV_OPT_TYPE_INT,    {-1 }, -1, 1, VE },
585     { "weightp",       "Weighted prediction analysis method.",            OFFSET(weightp),       AV_OPT_TYPE_INT,    {-1 }, -1, INT_MAX, VE, "weightp" },
586     { "none",          NULL, 0, AV_OPT_TYPE_CONST, {X264_WEIGHTP_NONE},   INT_MIN, INT_MAX, VE, "weightp" },
587     { "simple",        NULL, 0, AV_OPT_TYPE_CONST, {X264_WEIGHTP_SIMPLE}, INT_MIN, INT_MAX, VE, "weightp" },
588     { "smart",         NULL, 0, AV_OPT_TYPE_CONST, {X264_WEIGHTP_SMART},  INT_MIN, INT_MAX, VE, "weightp" },
589     { "ssim",          "Calculate and print SSIM stats.",                 OFFSET(ssim),          AV_OPT_TYPE_INT,    {-1 }, -1, 1, VE },
590     { "intra-refresh", "Use Periodic Intra Refresh instead of IDR frames.",OFFSET(intra_refresh),AV_OPT_TYPE_INT,    {-1 }, -1, 1, VE },
591     { "b-bias",        "Influences how often B-frames are used",          OFFSET(b_bias),        AV_OPT_TYPE_INT,    {INT_MIN}, INT_MIN, INT_MAX, VE },
592     { "b-pyramid",     "Keep some B-frames as references.",               OFFSET(b_pyramid),     AV_OPT_TYPE_INT,    {-1 }, -1, INT_MAX, VE, "b_pyramid" },
593     { "none",          NULL,                                  0, AV_OPT_TYPE_CONST, {X264_B_PYRAMID_NONE},   INT_MIN, INT_MAX, VE, "b_pyramid" },
594     { "strict",        "Strictly hierarchical pyramid",       0, AV_OPT_TYPE_CONST, {X264_B_PYRAMID_STRICT}, INT_MIN, INT_MAX, VE, "b_pyramid" },
595     { "normal",        "Non-strict (not Blu-ray compatible)", 0, AV_OPT_TYPE_CONST, {X264_B_PYRAMID_NORMAL}, INT_MIN, INT_MAX, VE, "b_pyramid" },
596     { "mixed-refs",    "One reference per partition, as opposed to one reference per macroblock", OFFSET(mixed_refs), AV_OPT_TYPE_INT, {-1}, -1, 1, VE },
597     { "8x8dct",        "High profile 8x8 transform.",                     OFFSET(dct8x8),        AV_OPT_TYPE_INT,    {-1 }, -1, 1, VE},
598     { "fast-pskip",    NULL,                                              OFFSET(fast_pskip),    AV_OPT_TYPE_INT,    {-1 }, -1, 1, VE},
599     { "aud",           "Use access unit delimiters.",                     OFFSET(aud),           AV_OPT_TYPE_INT,    {-1 }, -1, 1, VE},
600     { "mbtree",        "Use macroblock tree ratecontrol.",                OFFSET(mbtree),        AV_OPT_TYPE_INT,    {-1 }, -1, 1, VE},
601     { "deblock",       "Loop filter parameters, in <alpha:beta> form.",   OFFSET(deblock),       AV_OPT_TYPE_STRING, { 0 },  0, 0, VE},
602     { "cplxblur",      "Reduce fluctuations in QP (before curve compression)", OFFSET(cplxblur), AV_OPT_TYPE_FLOAT,  {-1 }, -1, FLT_MAX, VE},
603     { "partitions",    "A comma-separated list of partitions to consider. "
604                        "Possible values: p8x8, p4x4, b8x8, i8x8, i4x4, none, all", OFFSET(partitions), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
605     { "direct-pred",   "Direct MV prediction mode",                       OFFSET(direct_pred),   AV_OPT_TYPE_INT,    {-1 }, -1, INT_MAX, VE, "direct-pred" },
606     { "none",          NULL,      0,    AV_OPT_TYPE_CONST, { X264_DIRECT_PRED_NONE },     0, 0, VE, "direct-pred" },
607     { "spatial",       NULL,      0,    AV_OPT_TYPE_CONST, { X264_DIRECT_PRED_SPATIAL },  0, 0, VE, "direct-pred" },
608     { "temporal",      NULL,      0,    AV_OPT_TYPE_CONST, { X264_DIRECT_PRED_TEMPORAL }, 0, 0, VE, "direct-pred" },
609     { "auto",          NULL,      0,    AV_OPT_TYPE_CONST, { X264_DIRECT_PRED_AUTO },     0, 0, VE, "direct-pred" },
610     { "slice-max-size","Limit the size of each slice in bytes",           OFFSET(slice_max_size),AV_OPT_TYPE_INT,    {-1 }, -1, INT_MAX, VE },
611     { "stats",         "Filename for 2 pass stats",                       OFFSET(stats),         AV_OPT_TYPE_STRING, { 0 },  0,       0, VE },
612     { NULL },
613 };
614
615 static const AVClass class = {
616     .class_name = "libx264",
617     .item_name  = av_default_item_name,
618     .option     = options,
619     .version    = LIBAVUTIL_VERSION_INT,
620 };
621
622 static const AVClass rgbclass = {
623     .class_name = "libx264rgb",
624     .item_name  = av_default_item_name,
625     .option     = options,
626     .version    = LIBAVUTIL_VERSION_INT,
627 };
628
629 static const AVCodecDefault x264_defaults[] = {
630     { "b",                "0" },
631     { "bf",               "-1" },
632     { "flags2",           "0" },
633     { "g",                "-1" },
634     { "qmin",             "-1" },
635     { "qmax",             "-1" },
636     { "qdiff",            "-1" },
637     { "qblur",            "-1" },
638     { "qcomp",            "-1" },
639 //     { "rc_lookahead",     "-1" },
640     { "refs",             "-1" },
641     { "sc_threshold",     "-1" },
642     { "trellis",          "-1" },
643     { "nr",               "-1" },
644     { "me_range",         "-1" },
645     { "me_method",        "-1" },
646     { "subq",             "-1" },
647     { "b_strategy",       "-1" },
648     { "keyint_min",       "-1" },
649     { "coder",            "-1" },
650     { "cmp",              "-1" },
651     { "threads",          AV_STRINGIFY(X264_THREADS_AUTO) },
652     { "thread_type",      "0" },
653     { NULL },
654 };
655
656 AVCodec ff_libx264_encoder = {
657     .name             = "libx264",
658     .type             = AVMEDIA_TYPE_VIDEO,
659     .id               = AV_CODEC_ID_H264,
660     .priv_data_size   = sizeof(X264Context),
661     .init             = X264_init,
662     .encode2          = X264_frame,
663     .close            = X264_close,
664     .capabilities     = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS,
665     .long_name        = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
666     .priv_class       = &class,
667     .defaults         = x264_defaults,
668     .init_static_data = X264_init_static,
669 };
670
671 AVCodec ff_libx264rgb_encoder = {
672     .name           = "libx264rgb",
673     .type           = AVMEDIA_TYPE_VIDEO,
674     .id             = AV_CODEC_ID_H264,
675     .priv_data_size = sizeof(X264Context),
676     .init           = X264_init,
677     .encode2        = X264_frame,
678     .close          = X264_close,
679     .capabilities   = CODEC_CAP_DELAY,
680     .long_name      = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 RGB"),
681     .priv_class     = &rgbclass,
682     .defaults       = x264_defaults,
683     .pix_fmts       = pix_fmts_8bit_rgb,
684 };