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
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
78
80 {
81 static const int level_map[] = {
86 };
87
88 if (level < 0 || level > X264_LOG_DEBUG)
89 return;
90
91 av_vlog(p, level_map[level], fmt, args);
92 }
93
94
96 x264_nal_t *nals, int nnal)
97 {
101
102 if (!nnal)
103 return 0;
104
105 for (i = 0; i < nnal; i++)
106 size += nals[i].i_payload;
107
110
112
113 /* Write the SEI as part of the first frame. */
117 return -1;
118 }
123 }
124
125 for (i = 0; i < nnal; i++){
126 memcpy(p, nals[i].p_payload, nals[i].i_payload);
127 p += nals[i].i_payload;
128 }
129
130 return 1;
131 }
132
134 {
135 switch (avfmt) {
141 return 3;
142
145 return 1;
146
147 default:
148 return 3;
149 }
150 }
151
153 int *got_packet)
154 {
156 x264_nal_t *nal;
158 x264_picture_t pic_out = {0};
159
160 x264_picture_init( &x4->
pic );
162 if (x264_bit_depth > 8)
163 x4->
pic.img.i_csp |= X264_CSP_HIGH_DEPTH;
165
166 if (frame) {
167 for (i = 0; i < x4->
pic.img.i_plane; i++) {
168 x4->
pic.img.plane[i] = frame->
data[i];
170 }
171
172 x4->
pic.i_pts = frame->
pts;
177 X264_TYPE_AUTO;
180 x264_encoder_reconfig(x4->
enc, &x4->
params);
181 }
186 x264_encoder_reconfig(x4->
enc, &x4->
params);
187 }
188 }
189
190 do {
191 if (x264_encoder_encode(x4->
enc, &nal, &nnal, frame? &x4->
pic: NULL, &pic_out) < 0)
192 return -1;
193
195 if (ret < 0)
196 return -1;
197 }
while (!ret && !frame && x264_encoder_delayed_frames(x4->
enc));
198
199 pkt->
pts = pic_out.i_pts;
200 pkt->
dts = pic_out.i_dts;
201
202 switch (pic_out.i_type) {
203 case X264_TYPE_IDR:
204 case X264_TYPE_I:
206 break;
207 case X264_TYPE_P:
209 break;
210 case X264_TYPE_B:
211 case X264_TYPE_BREF:
213 break;
214 }
215
217 if (ret)
219
221 return 0;
222 }
223
225 {
227
230
232 x264_encoder_close(x4->
enc);
233
234 return 0;
235 }
236
237 #define OPT_STR(opt, param) \
238 do { \
239 int ret; \
240 if (param!=NULL && (ret = x264_param_parse(&x4->params, opt, param)) < 0) { \
241 if(ret == X264_PARAM_BAD_NAME) \
242 av_log(avctx, AV_LOG_ERROR, \
243 "bad option '%s': '%s'\n", opt, param); \
244 else \
245 av_log(avctx, AV_LOG_ERROR, \
246 "bad value for '%s': '%s'\n", opt, param); \
247 return -1; \
248 } \
249 } while (0)
250
252 {
253 switch (pix_fmt) {
263 #ifdef X264_CSP_BGR
265 return X264_CSP_BGR;
266
268 return X264_CSP_RGB;
269 #endif
270 };
271 return 0;
272 }
273
274 #define PARSE_X264_OPT(name, var)\
275 if (x4->var && x264_param_parse(&x4->params, name, x4->var) < 0) {\
276 av_log(avctx, AV_LOG_ERROR, "Error parsing option '%s' with value '%s'.\n", name, x4->var);\
277 return AVERROR(EINVAL);\
278 }
279
281 {
283 int sw,sh;
284
285 x264_param_default(&x4->
params);
286
288
293 int i;
296 for (i = 0; x264_preset_names[i]; i++)
300 for (i = 0; x264_tune_names[i]; i++)
304 }
305
306 if (avctx->
level > 0)
308
310 x4->
params.p_log_private = avctx;
311 x4->
params.i_log_level = X264_LOG_DEBUG;
313
315
318 x4->
params.rc.i_rc_method = X264_RC_ABR;
319 }
324 x4->
params.rc.b_stat_read = 1;
325 } else {
327 x4->
params.rc.i_rc_method = X264_RC_CRF;
329 }
else if (x4->
cqp >= 0) {
330 x4->
params.rc.i_rc_method = X264_RC_CQP;
332 }
333
336 }
337
340 x4->
params.rc.f_vbv_buffer_init =
342 }
343
345
348 while(p){
349 char param[256]={0},
val[256]={0};
350 if(sscanf(p,
"%255[^:=]=%255[^:]", param,
val) == 1){
352 }else
354 p= strchr(p, ':');
355 p+=!!p;
356 }
357 }
358
361
363 x4->
params.analyse.i_me_method = X264_ME_DIA;
365 x4->
params.analyse.i_me_method = X264_ME_HEX;
367 x4->
params.analyse.i_me_method = X264_ME_UMH;
369 x4->
params.analyse.i_me_method = X264_ME_ESA;
371 x4->
params.analyse.i_me_method = X264_ME_TESA;
372
379 if (avctx->
qmin >= 0)
381 if (avctx->
qmax >= 0)
385 if (avctx->
qblur >= 0)
386 x4->
params.rc.f_qblur = avctx->
qblur;
/* temporally blur quants */
388 x4->
params.rc.f_qcompress = avctx->
qcompress;
/* 0.0 => cbr, 1.0 => constant qp */
389 if (avctx->
refs >= 0)
407
426
431 if (x4->
b_bias != INT_MIN)
447
450 else {
451 /*
452 * Allow x264 to be instructed through AVCodecContext about the maximum
453 * size of the RTP payload. For example, this enables the production of
454 * payload suitable for the H.264 RTP packetization-mode 0 i.e. single
455 * NAL unit per RTP packet.
456 */
459 }
460
462 x264_param_apply_fastfirstpass(&x4->
params);
463
464 /* Allow specifying the x264 profile through AVCodecContext. */
469 break;
472 break;
475 break;
478 break;
481 break;
484 break;
485 default:
486 break;
487 }
488
491
493 if (x264_param_apply_profile(&x4->
params, x4->
profile) < 0) {
494 int i;
497 for (i = 0; x264_profile_names[i]; i++)
501 }
502
506 x4->
params.vui.i_sar_width = sw;
507 x4->
params.vui.i_sar_height = sh;
510
512
516
518
520
522
524
526 x4->
params.b_repeat_headers = 0;
527
531
536 "Error parsing option '%s = %s'.\n",
538 }
539
541 }
542 }
543
544 // update AVCodecContext with x264 parameters
546 x4->
params.i_bframe_pyramid ? 2 : 1 : 0;
549
551
552 x4->
enc = x264_encoder_open(&x4->
params);
554 return -1;
555
557
559 x264_nal_t *nal;
562
563 s = x264_encoder_headers(x4->
enc, &nal, &nnal);
565
566 for (i = 0; i < nnal; i++) {
567 /* Don't put the SEI in extradata. */
568 if (nal[i].i_type ==
NAL_SEI) {
572 memcpy(x4->
sei, nal[i].p_payload, nal[i].i_payload);
573 continue;
574 }
575 memcpy(p, nal[i].p_payload, nal[i].i_payload);
576 p += nal[i].i_payload;
577 }
579 }
580
581 return 0;
582 }
583
590 };
595 };
601 };
603 #ifdef X264_CSP_BGR
606 #endif
608 };
609
611 {
612 if (x264_bit_depth == 8)
614 else if (x264_bit_depth == 9)
616 else if (x264_bit_depth == 10)
618 }
619
620 #define OFFSET(x) offsetof(X264Context, x)
621 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
626 {
"fastfirstpass",
"Use fast settings when encoding first pass",
OFFSET(fastfirstpass),
AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1,
VE},
631 {
"crf",
"Select the quality for constant quality mode",
OFFSET(crf),
AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX,
VE },
632 {
"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 },
633 {
"qp",
"Constant quantization parameter rate control method",
OFFSET(cqp),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE },
635 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_AQ_NONE}, INT_MIN, INT_MAX,
VE,
"aq_mode" },
636 {
"variance",
"Variance AQ (complexity mask)", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_AQ_VARIANCE}, INT_MIN, INT_MAX,
VE,
"aq_mode" },
637 {
"autovariance",
"Auto-variance AQ (experimental)", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_AQ_AUTOVARIANCE}, INT_MIN, INT_MAX,
VE,
"aq_mode" },
638 {
"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},
640 {
"psy-rd",
"Strength of psychovisual optimization, in <psy-rd>:<psy-trellis> format.",
OFFSET(psy_rd),
AV_OPT_TYPE_STRING, {0 }, 0, 0,
VE},
641 {
"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 },
642 {
"weightb",
"Weighted prediction for B-frames.",
OFFSET(weightb),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1,
VE },
643 {
"weightp",
"Weighted prediction analysis method.",
OFFSET(weightp),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE,
"weightp" },
644 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_NONE}, INT_MIN, INT_MAX,
VE,
"weightp" },
645 {
"simple", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_SIMPLE}, INT_MIN, INT_MAX,
VE,
"weightp" },
646 {
"smart", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_SMART}, INT_MIN, INT_MAX,
VE,
"weightp" },
648 {
"intra-refresh",
"Use Periodic Intra Refresh instead of IDR frames.",
OFFSET(intra_refresh),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1,
VE },
649 {
"b-bias",
"Influences how often B-frames are used",
OFFSET(b_bias),
AV_OPT_TYPE_INT, { .i64 = INT_MIN}, INT_MIN, INT_MAX,
VE },
650 {
"b-pyramid",
"Keep some B-frames as references.",
OFFSET(b_pyramid),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE,
"b_pyramid" },
651 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_NONE}, INT_MIN, INT_MAX,
VE,
"b_pyramid" },
652 {
"strict",
"Strictly hierarchical pyramid", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_STRICT}, INT_MIN, INT_MAX,
VE,
"b_pyramid" },
653 {
"normal",
"Non-strict (not Blu-ray compatible)", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_NORMAL}, INT_MIN, INT_MAX,
VE,
"b_pyramid" },
654 {
"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 },
660 {
"cplxblur",
"Reduce fluctuations in QP (before curve compression)",
OFFSET(cplxblur),
AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX,
VE},
661 { "partitions", "A comma-separated list of partitions to consider. "
662 "Possible values: p8x8, p4x4, b8x8, i8x8, i4x4, none, all",
OFFSET(partitions),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
VE},
663 {
"direct-pred",
"Direct MV prediction mode",
OFFSET(direct_pred),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE,
"direct-pred" },
664 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_NONE }, 0, 0,
VE,
"direct-pred" },
665 {
"spatial", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_SPATIAL }, 0, 0,
VE,
"direct-pred" },
666 {
"temporal", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_TEMPORAL }, 0, 0,
VE,
"direct-pred" },
667 {
"auto", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_AUTO }, 0, 0,
VE,
"direct-pred" },
668 {
"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 },
670 { "nal-hrd", "Signal HRD information (requires vbv-bufsize; "
672 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_NONE}, INT_MIN, INT_MAX,
VE,
"nal-hrd" },
673 {
"vbr", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_VBR}, INT_MIN, INT_MAX,
VE,
"nal-hrd" },
674 {
"cbr", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_CBR}, INT_MIN, INT_MAX,
VE,
"nal-hrd" },
675 {
"x264-params",
"Override the x264 configuration using a :-separated list of key=value parameters",
OFFSET(x264_params),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
VE },
676 { NULL },
677 };
678
684 };
685
691 };
692
694 { "b", "0" },
695 { "bf", "-1" },
696 { "flags2", "0" },
697 { "g", "-1" },
698 { "i_qfactor", "-1" },
699 { "qmin", "-1" },
700 { "qmax", "-1" },
701 { "qdiff", "-1" },
702 { "qblur", "-1" },
703 { "qcomp", "-1" },
704 // { "rc_lookahead", "-1" },
705 { "refs", "-1" },
706 { "sc_threshold", "-1" },
707 { "trellis", "-1" },
708 { "nr", "-1" },
709 { "me_range", "-1" },
710 { "me_method", "-1" },
711 { "subq", "-1" },
712 { "b_strategy", "-1" },
713 { "keyint_min", "-1" },
714 { "coder", "-1" },
715 { "cmp", "-1" },
717 { "thread_type", "0" },
718 { "flags", "+cgop" },
719 { "rc_init_occupancy","-1" },
720 { NULL },
721 };
722
736 };
737
739 .
name =
"libx264rgb",
751 };