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
29 #if defined(_MSC_VER)
30 #define X264_API_IMPORTS 1
31 #endif
32
33 #include <x264.h>
34 #include <float.h>
35 #include <math.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
83
85 {
86 static const int level_map[] = {
91 };
92
93 if (level < 0 || level > X264_LOG_DEBUG)
94 return;
95
96 av_vlog(p, level_map[level], fmt, args);
97 }
98
99
101 x264_nal_t *nals, int nnal)
102 {
106
107 if (!nnal)
108 return 0;
109
110 for (i = 0; i < nnal; i++)
111 size += nals[i].i_payload;
112
115
117
118 /* Write the SEI as part of the first frame. */
122 return -1;
123 }
128 }
129
130 for (i = 0; i < nnal; i++){
131 memcpy(p, nals[i].p_payload, nals[i].i_payload);
132 p += nals[i].i_payload;
133 }
134
135 return 1;
136 }
137
139 {
140 switch (avfmt) {
146 return 3;
147
150 return 1;
151
152 default:
153 return 3;
154 }
155 }
156
158 int *got_packet)
159 {
161 x264_nal_t *nal;
163 x264_picture_t pic_out = {0};
164
165 x264_picture_init( &x4->
pic );
167 if (x264_bit_depth > 8)
168 x4->
pic.img.i_csp |= X264_CSP_HIGH_DEPTH;
170
171 if (frame) {
172 for (i = 0; i < x4->
pic.img.i_plane; i++) {
173 x4->
pic.img.plane[i] = frame->
data[i];
175 }
176
177 x4->
pic.i_pts = frame->
pts;
182 X264_TYPE_AUTO;
185 x264_encoder_reconfig(x4->
enc, &x4->
params);
186 }
191 x264_encoder_reconfig(x4->
enc, &x4->
params);
192 }
193 }
194
195 do {
196 if (x264_encoder_encode(x4->
enc, &nal, &nnal, frame? &x4->
pic: NULL, &pic_out) < 0)
197 return -1;
198
200 if (ret < 0)
201 return -1;
202 }
while (!ret && !frame && x264_encoder_delayed_frames(x4->
enc));
203
204 pkt->
pts = pic_out.i_pts;
205 pkt->
dts = pic_out.i_dts;
206
207 switch (pic_out.i_type) {
208 case X264_TYPE_IDR:
209 case X264_TYPE_I:
211 break;
212 case X264_TYPE_P:
214 break;
215 case X264_TYPE_B:
216 case X264_TYPE_BREF:
218 break;
219 }
220
222 if (ret)
224
226 return 0;
227 }
228
230 {
232
235
237 x264_encoder_close(x4->
enc);
238
240
241 return 0;
242 }
243
244 #define OPT_STR(opt, param) \
245 do { \
246 int ret; \
247 if (param!=NULL && (ret = x264_param_parse(&x4->params, opt, param)) < 0) { \
248 if(ret == X264_PARAM_BAD_NAME) \
249 av_log(avctx, AV_LOG_ERROR, \
250 "bad option '%s': '%s'\n", opt, param); \
251 else \
252 av_log(avctx, AV_LOG_ERROR, \
253 "bad value for '%s': '%s'\n", opt, param); \
254 return -1; \
255 } \
256 } while (0)
257
259 {
260 switch (pix_fmt) {
272 #ifdef X264_CSP_BGR
274 return X264_CSP_BGR;
275
277 return X264_CSP_RGB;
278 #endif
282 };
283 return 0;
284 }
285
286 #define PARSE_X264_OPT(name, var)\
287 if (x4->var && x264_param_parse(&x4->params, name, x4->var) < 0) {\
288 av_log(avctx, AV_LOG_ERROR, "Error parsing option '%s' with value '%s'.\n", name, x4->var);\
289 return AVERROR(EINVAL);\
290 }
291
293 {
295 int sw,sh;
296
297 x264_param_default(&x4->
params);
298
300
305 int i;
308 for (i = 0; x264_preset_names[i]; i++)
312 for (i = 0; x264_tune_names[i]; i++)
316 }
317
318 if (avctx->
level > 0)
320
322 x4->
params.p_log_private = avctx;
323 x4->
params.i_log_level = X264_LOG_DEBUG;
325
327
330 x4->
params.rc.i_rc_method = X264_RC_ABR;
331 }
336 x4->
params.rc.b_stat_read = 1;
337 } else {
339 x4->
params.rc.i_rc_method = X264_RC_CRF;
341 }
else if (x4->
cqp >= 0) {
342 x4->
params.rc.i_rc_method = X264_RC_CQP;
344 }
345
348 }
349
352 x4->
params.rc.f_vbv_buffer_init =
354 }
355
357
360 while(p){
361 char param[256]={0},
val[256]={0};
362 if(sscanf(p,
"%255[^:=]=%255[^:]", param,
val) == 1){
364 }else
366 p= strchr(p, ':');
367 p+=!!p;
368 }
369 }
370
373
375 x4->
params.analyse.i_me_method = X264_ME_DIA;
377 x4->
params.analyse.i_me_method = X264_ME_HEX;
379 x4->
params.analyse.i_me_method = X264_ME_UMH;
381 x4->
params.analyse.i_me_method = X264_ME_ESA;
383 x4->
params.analyse.i_me_method = X264_ME_TESA;
384
391 if (avctx->
qmin >= 0)
393 if (avctx->
qmax >= 0)
397 if (avctx->
qblur >= 0)
398 x4->
params.rc.f_qblur = avctx->
qblur;
/* temporally blur quants */
400 x4->
params.rc.f_qcompress = avctx->
qcompress;
/* 0.0 => cbr, 1.0 => constant qp */
401 if (avctx->
refs >= 0)
419
438
445 x4->
params.b_vfr_input = 0;
446 }
447 if (x4->
b_bias != INT_MIN)
463
466 else {
467 /*
468 * Allow x264 to be instructed through AVCodecContext about the maximum
469 * size of the RTP payload. For example, this enables the production of
470 * payload suitable for the H.264 RTP packetization-mode 0 i.e. single
471 * NAL unit per RTP packet.
472 */
475 }
476
478 x264_param_apply_fastfirstpass(&x4->
params);
479
480 /* Allow specifying the x264 profile through AVCodecContext. */
485 break;
488 break;
491 break;
494 break;
497 break;
500 break;
501 default:
502 break;
503 }
504
507
509 if (x264_param_apply_profile(&x4->
params, x4->
profile) < 0) {
510 int i;
513 for (i = 0; x264_profile_names[i]; i++)
517 }
518
522 x4->
params.vui.i_sar_width = sw;
523 x4->
params.vui.i_sar_height = sh;
526
528
532
534
536
538
542
544 x4->
params.b_repeat_headers = 0;
545
549
554 "Error parsing option '%s = %s'.\n",
556 }
557
559 }
560 }
561
562 // update AVCodecContext with x264 parameters
564 x4->
params.i_bframe_pyramid ? 2 : 1 : 0;
567
569
570 x4->
enc = x264_encoder_open(&x4->
params);
572 return -1;
573
577
579 x264_nal_t *nal;
582
583 s = x264_encoder_headers(x4->
enc, &nal, &nnal);
585
586 for (i = 0; i < nnal; i++) {
587 /* Don't put the SEI in extradata. */
588 if (nal[i].i_type ==
NAL_SEI) {
592 memcpy(x4->
sei, nal[i].p_payload, nal[i].i_payload);
593 continue;
594 }
595 memcpy(p, nal[i].p_payload, nal[i].i_payload);
596 p += nal[i].i_payload;
597 }
599 }
600
601 return 0;
602 }
603
614 };
619 };
626 };
628 #ifdef X264_CSP_BGR
631 #endif
633 };
634
636 {
637 if (x264_bit_depth == 8)
639 else if (x264_bit_depth == 9)
641 else if (x264_bit_depth == 10)
643 }
644
645 #define OFFSET(x) offsetof(X264Context, x)
646 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
651 {
"fastfirstpass",
"Use fast settings when encoding first pass",
OFFSET(fastfirstpass),
AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1,
VE},
656 {
"crf",
"Select the quality for constant quality mode",
OFFSET(crf),
AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX,
VE },
657 {
"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 },
658 {
"qp",
"Constant quantization parameter rate control method",
OFFSET(cqp),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE },
660 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_AQ_NONE}, INT_MIN, INT_MAX,
VE,
"aq_mode" },
661 {
"variance",
"Variance AQ (complexity mask)", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_AQ_VARIANCE}, INT_MIN, INT_MAX,
VE,
"aq_mode" },
662 {
"autovariance",
"Auto-variance AQ (experimental)", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_AQ_AUTOVARIANCE}, INT_MIN, INT_MAX,
VE,
"aq_mode" },
663 {
"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},
665 {
"psy-rd",
"Strength of psychovisual optimization, in <psy-rd>:<psy-trellis> format.",
OFFSET(psy_rd),
AV_OPT_TYPE_STRING, {0 }, 0, 0,
VE},
666 {
"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 },
667 {
"weightb",
"Weighted prediction for B-frames.",
OFFSET(weightb),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1,
VE },
668 {
"weightp",
"Weighted prediction analysis method.",
OFFSET(weightp),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE,
"weightp" },
669 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_NONE}, INT_MIN, INT_MAX,
VE,
"weightp" },
670 {
"simple", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_SIMPLE}, INT_MIN, INT_MAX,
VE,
"weightp" },
671 {
"smart", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_SMART}, INT_MIN, INT_MAX,
VE,
"weightp" },
673 {
"intra-refresh",
"Use Periodic Intra Refresh instead of IDR frames.",
OFFSET(intra_refresh),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1,
VE },
674 {
"bluray-compat",
"Bluray compatibility workarounds.",
OFFSET(bluray_compat) ,
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1,
VE },
675 {
"b-bias",
"Influences how often B-frames are used",
OFFSET(b_bias),
AV_OPT_TYPE_INT, { .i64 = INT_MIN}, INT_MIN, INT_MAX,
VE },
676 {
"b-pyramid",
"Keep some B-frames as references.",
OFFSET(b_pyramid),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE,
"b_pyramid" },
677 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_NONE}, INT_MIN, INT_MAX,
VE,
"b_pyramid" },
678 {
"strict",
"Strictly hierarchical pyramid", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_STRICT}, INT_MIN, INT_MAX,
VE,
"b_pyramid" },
679 {
"normal",
"Non-strict (not Blu-ray compatible)", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_NORMAL}, INT_MIN, INT_MAX,
VE,
"b_pyramid" },
680 {
"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 },
686 {
"cplxblur",
"Reduce fluctuations in QP (before curve compression)",
OFFSET(cplxblur),
AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX,
VE},
687 { "partitions", "A comma-separated list of partitions to consider. "
688 "Possible values: p8x8, p4x4, b8x8, i8x8, i4x4, none, all",
OFFSET(partitions),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
VE},
689 {
"direct-pred",
"Direct MV prediction mode",
OFFSET(direct_pred),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE,
"direct-pred" },
690 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_NONE }, 0, 0,
VE,
"direct-pred" },
691 {
"spatial", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_SPATIAL }, 0, 0,
VE,
"direct-pred" },
692 {
"temporal", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_TEMPORAL }, 0, 0,
VE,
"direct-pred" },
693 {
"auto", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_AUTO }, 0, 0,
VE,
"direct-pred" },
694 {
"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 },
696 { "nal-hrd", "Signal HRD information (requires vbv-bufsize; "
698 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_NONE}, INT_MIN, INT_MAX,
VE,
"nal-hrd" },
699 {
"vbr", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_VBR}, INT_MIN, INT_MAX,
VE,
"nal-hrd" },
700 {
"cbr", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_CBR}, INT_MIN, INT_MAX,
VE,
"nal-hrd" },
701 {
"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 },
702 { NULL },
703 };
704
710 };
711
717 };
718
720 { "b", "0" },
721 { "bf", "-1" },
722 { "flags2", "0" },
723 { "g", "-1" },
724 { "i_qfactor", "-1" },
725 { "qmin", "-1" },
726 { "qmax", "-1" },
727 { "qdiff", "-1" },
728 { "qblur", "-1" },
729 { "qcomp", "-1" },
730 // { "rc_lookahead", "-1" },
731 { "refs", "-1" },
732 { "sc_threshold", "-1" },
733 { "trellis", "-1" },
734 { "nr", "-1" },
735 { "me_range", "-1" },
736 { "me_method", "-1" },
737 { "subq", "-1" },
738 { "b_strategy", "-1" },
739 { "keyint_min", "-1" },
740 { "coder", "-1" },
741 { "cmp", "-1" },
743 { "thread_type", "0" },
744 { "flags", "+cgop" },
745 { "rc_init_occupancy","-1" },
746 { NULL },
747 };
748
759 .priv_class = &x264_class,
762 };
763
765 .
name =
"libx264rgb",
774 .priv_class = &rgbclass,
777 };