1 /* encavcodec.c
2
3 Copyright (c) 2003-2021 HandBrake Team
4 This file is part of the HandBrake source code
5 Homepage: <http://handbrake.fr/>.
6 It may be used under the terms of the GNU General Public License v2.
7 For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
8 */
9
10 #include "handbrake/handbrake.h"
11 #include "handbrake/hb_dict.h"
12 #include "handbrake/hbffmpeg.h"
13 #include "handbrake/h264_common.h"
14 #include "handbrake/h265_common.h"
15 #include "handbrake/nal_units.h"
16
17 #if HB_PROJECT_FEATURE_NVENC
18 #include "handbrake/nvenc_common.h"
19 #endif
20
21
22 /*
23 * The frame info struct remembers information about each frame across calls
24 * to avcodec_encode_video. Since frames are uniquely identified by their
25 * frame number, we use this as an index.
26 *
27 * The size of the array is chosen so that two frames can't use the same
28 * slot during the encoder's max frame delay (set by the standard as 16
29 * frames) and so that, up to some minimum frame rate, frames are guaranteed
30 * to map to * different slots.
31 */
32 #define FRAME_INFO_SIZE 32
33 #define FRAME_INFO_MASK (FRAME_INFO_SIZE - 1)
34
35 struct hb_work_private_s
36 {
37 hb_job_t * job;
38 AVCodecContext * context;
39 AVPacket * pkt;
40 FILE * file;
41
42 int frameno_in;
43 int frameno_out;
44 hb_buffer_list_t delay_list;
45
46 int64_t dts_delay;
47
48 struct {
49 int64_t start;
50 int64_t duration;
51 } frame_info[FRAME_INFO_SIZE];
52
53 hb_chapter_queue_t * chapter_queue;
54 };
55
56 int encavcodecInit( hb_work_object_t *, hb_job_t * );
57 int encavcodecWork( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
58 void encavcodecClose( hb_work_object_t * );
59
60 static int apply_encoder_preset(int vcodec, AVDictionary ** av_opts,
61 const char * preset);
62
63 hb_work_object_t hb_encavcodec =
64 {
65 WORK_ENCAVCODEC,
66 "FFMPEG encoder (libavcodec)",
67 encavcodecInit,
68 encavcodecWork,
69 encavcodecClose
70 };
71
72 static const char * const vpx_preset_names[] =
73 {
74 "veryfast", "faster", "fast", "medium", "slow", "slower", "veryslow", NULL
75 };
76
77 static const char * const h26x_nvenc_preset_names[] =
78 {
79 "fastest", "faster", "fast", "medium", "slow", "slower", "slowest", NULL
80 };
81
82 static const char * const h264_nvenc_profile_names[] =
83 {
84 "auto", "baseline", "main", "high", NULL // "high444p" not supported.
85 };
86
87 static const char * const h265_nvenc_profile_names[] =
88 {
89 "auto", "main", NULL // "main10", "rext" We do not currently support 10bit encodes with this encoder.
90 };
91
92 static const char * const h26x_vt_preset_name[] =
93 {
94 "default", NULL
95 };
96
97 static const char * const h264_vt_profile_name[] =
98 {
99 "auto", "baseline", "main", "high", NULL
100 };
101
102 static const char * const h26x_mf_preset_name[] =
103 {
104 "default", NULL
105 };
106
107 static const char * const h264_mf_profile_name[] =
108 {
109 "auto", "baseline", "main", "high", NULL
110 };
111
112 static const char * const h265_mf_profile_name[] =
113 {
114 "auto", "main", NULL
115 };
116
117 static const enum AVPixelFormat pix_fmts[] =
118 {
119 AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE
120 };
121
122 static const enum AVPixelFormat h26x_vt_pix_fmts[] =
123 {
124 AV_PIX_FMT_NV12, AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE
125 };
126
127 static const enum AVPixelFormat h265_10bit_vt_pix_fmts[] =
128 {
129 AV_PIX_FMT_P010LE, AV_PIX_FMT_NONE
130 };
131
132 static const enum AVPixelFormat h26x_mf_pix_fmts[] =
133 {
134 AV_PIX_FMT_NV12, AV_PIX_FMT_NONE
135 };
136
encavcodecInit(hb_work_object_t * w,hb_job_t * job)137 int encavcodecInit( hb_work_object_t * w, hb_job_t * job )
138 {
139 int ret = 0;
140 char reason[80];
141 char * codec_name = NULL;
142 AVCodec * codec = NULL;
143 AVCodecContext * context;
144 AVRational fps;
145
146 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
147 w->private_data = pv;
148 pv->job = job;
149 pv->chapter_queue = hb_chapter_queue_init();
150 pv->pkt = av_packet_alloc();
151
152 if (pv->pkt == NULL)
153 {
154 hb_log("encavcodecInit: av_packet_alloc failed");
155 ret = 1;
156 goto done;
157 }
158
159 hb_buffer_list_clear(&pv->delay_list);
160
161 int clock_min, clock_max, clock;
162 hb_video_framerate_get_limits(&clock_min, &clock_max, &clock);
163
164 switch ( w->codec_param )
165 {
166 case AV_CODEC_ID_MPEG4:
167 {
168 hb_log("encavcodecInit: MPEG-4 ASP encoder");
169 codec_name = "mpeg4";
170 } break;
171 case AV_CODEC_ID_MPEG2VIDEO:
172 {
173 hb_log("encavcodecInit: MPEG-2 encoder");
174 codec_name = "mpeg2video";
175 } break;
176 case AV_CODEC_ID_VP8:
177 {
178 hb_log("encavcodecInit: VP8 encoder");
179 codec_name = "libvpx";
180 } break;
181 case AV_CODEC_ID_VP9:
182 {
183 hb_log("encavcodecInit: VP9 encoder");
184 codec_name = "libvpx-vp9";
185 } break;
186 case AV_CODEC_ID_H264:
187 {
188 switch (job->vcodec) {
189 case HB_VCODEC_FFMPEG_NVENC_H264:
190 hb_log("encavcodecInit: H.264 (Nvidia NVENC)");
191 codec_name = "h264_nvenc";
192 break;
193 case HB_VCODEC_FFMPEG_VCE_H264:
194 hb_log("encavcodecInit: H.264 (AMD VCE)");
195 codec_name = "h264_amf";
196 break;
197 case HB_VCODEC_FFMPEG_VT_H264:
198 hb_log("encavcodecInit: H.264 (VideoToolbox)");
199 codec_name = "h264_videotoolbox";
200 break;
201 case HB_VCODEC_FFMPEG_MF_H264:
202 hb_log("encavcodecInit: H.264 (MediaFoundation)");
203 codec_name = "h264_mf";
204 break;
205 }
206 }break;
207 case AV_CODEC_ID_HEVC:
208 {
209 switch (job->vcodec) {
210 case HB_VCODEC_FFMPEG_NVENC_H265:
211 hb_log("encavcodecInit: H.265 (Nvidia NVENC)");
212 codec_name = "hevc_nvenc";
213 break;
214 case HB_VCODEC_FFMPEG_VCE_H265:
215 hb_log("encavcodecInit: H.265 (AMD VCE)");
216 codec_name = "hevc_amf";
217 break;
218 case HB_VCODEC_FFMPEG_VT_H265:
219 hb_log("encavcodecInit: H.265 (VideoToolbox)");
220 codec_name = "hevc_videotoolbox";
221 break;
222 case HB_VCODEC_FFMPEG_VT_H265_10BIT:
223 hb_log("encavcodecInit: H.265 10 bit (VideoToolbox)");
224 codec_name = "hevc_videotoolbox";
225 break;
226 case HB_VCODEC_FFMPEG_MF_H265:
227 hb_log("encavcodecInit: H.265 (MediaFoundation)");
228 codec_name = "hevc_mf";
229 break;
230 }
231 }break;
232 }
233
234 if (codec_name == NULL)
235 {
236 // Catch all when the switch above fails
237 hb_log( "encavcodecInit: Unable to determine codec_name "
238 "from hb_work_object_t.codec_param=%d and "
239 "hb_job_t.vcodec=%x", w->codec_param,
240 job->vcodec );
241 ret = 1;
242 goto done;
243 }
244
245 codec = avcodec_find_encoder_by_name(codec_name);
246 if( !codec )
247 {
248 hb_log( "encavcodecInit: avcodec_find_encoder_by_name(%s) "
249 "failed", codec_name );
250 ret = 1;
251 goto done;
252 }
253 context = avcodec_alloc_context3( codec );
254
255 // Set things in context that we will allow the user to
256 // override with advanced settings.
257 fps.den = job->vrate.den;
258 fps.num = job->vrate.num;
259
260 // If the fps.num is the internal clock rate, there's a good chance
261 // this is a standard rate that we have in our hb_video_rates table.
262 // Because of rounding errors and approximations made while
263 // measuring framerate, the actual value may not be exact. So
264 // we look for rates that are "close" and make an adjustment
265 // to fps.den.
266 if (fps.num == clock)
267 {
268 const hb_rate_t *video_framerate = NULL;
269 while ((video_framerate = hb_video_framerate_get_next(video_framerate)) != NULL)
270 {
271 if (abs(fps.den - video_framerate->rate) < 10)
272 {
273 fps.den = video_framerate->rate;
274 break;
275 }
276 }
277 }
278 hb_reduce(&fps.den, &fps.num, fps.den, fps.num);
279
280 // Check that the framerate is supported. If not, pick the closest.
281 // The mpeg2 codec only supports a specific list of frame rates.
282 if (codec->supported_framerates)
283 {
284 AVRational supported_fps;
285 supported_fps = codec->supported_framerates[av_find_nearest_q_idx(fps, codec->supported_framerates)];
286 if (supported_fps.num != fps.num || supported_fps.den != fps.den)
287 {
288 hb_log( "encavcodec: framerate %d / %d is not supported. Using %d / %d.",
289 fps.num, fps.den, supported_fps.num, supported_fps.den );
290 fps = supported_fps;
291 }
292 }
293 else if ((fps.num & ~0xFFFF) || (fps.den & ~0xFFFF))
294 {
295 // This may only be required for mpeg4 video. But since
296 // our only supported options are mpeg2 and mpeg4, there is
297 // no need to check codec type.
298 hb_log( "encavcodec: truncating framerate %d / %d",
299 fps.num, fps.den );
300 while ((fps.num & ~0xFFFF) || (fps.den & ~0xFFFF))
301 {
302 fps.num >>= 1;
303 fps.den >>= 1;
304 }
305 }
306
307 context->time_base.den = fps.num;
308 context->time_base.num = fps.den;
309 context->framerate = fps;
310 context->gop_size = ((double)job->orig_vrate.num / job->orig_vrate.den +
311 0.5) * 10;
312 if ((job->vcodec == HB_VCODEC_FFMPEG_VCE_H264) || (job->vcodec == HB_VCODEC_FFMPEG_VCE_H265))
313 {
314 // Set encoder preset
315 context->profile = FF_PROFILE_UNKNOWN;
316 if (job->encoder_preset != NULL && *job->encoder_preset)
317 {
318 if ((!strcasecmp(job->encoder_preset, "balanced"))
319 || (!strcasecmp(job->encoder_preset, "speed"))
320 || (!strcasecmp(job->encoder_preset, "quality")))
321 {
322 av_opt_set(context, "quality", job->encoder_preset, AV_OPT_SEARCH_CHILDREN);
323 }
324 }
325 }
326
327 /* place job->encoder_options in an hb_dict_t for convenience */
328 hb_dict_t * lavc_opts = NULL;
329 if (job->encoder_options != NULL && *job->encoder_options)
330 {
331 lavc_opts = hb_encopts_to_dict(job->encoder_options, job->vcodec);
332 }
333
334 AVDictionary * av_opts = NULL;
335 if (apply_encoder_preset(job->vcodec, &av_opts, job->encoder_preset))
336 {
337 av_free( context );
338 av_dict_free( &av_opts );
339 ret = 1;
340 goto done;
341 }
342
343 /* iterate through lavc_opts and have avutil parse the options for us */
344 hb_dict_iter_t iter;
345 for (iter = hb_dict_iter_init(lavc_opts);
346 iter != HB_DICT_ITER_DONE;
347 iter = hb_dict_iter_next(lavc_opts, iter))
348 {
349 const char *key = hb_dict_iter_key(iter);
350 hb_value_t *value = hb_dict_iter_value(iter);
351 char *str = hb_value_get_string_xform(value);
352
353 /* Here's where the strings are passed to avutil for parsing. */
354 av_dict_set( &av_opts, key, str, 0 );
355 free(str);
356 }
357 hb_dict_free( &lavc_opts );
358
359 // Now set the things in context that we don't want to allow
360 // the user to override.
361 if (job->vquality <= HB_INVALID_VIDEO_QUALITY)
362 {
363 /* Average bitrate */
364 context->bit_rate = 1000 * job->vbitrate;
365 // ffmpeg's mpeg2 encoder requires that the bit_rate_tolerance be >=
366 // bitrate * fps
367 context->bit_rate_tolerance = context->bit_rate * av_q2d(fps) + 1;
368
369 if ( job->vcodec == HB_VCODEC_FFMPEG_NVENC_H264 ||
370 job->vcodec == HB_VCODEC_FFMPEG_NVENC_H265 ) {
371 av_dict_set( &av_opts, "rc", "vbr", 0 );
372 av_dict_set( &av_opts, "multipass", "fullres", 0 );
373 hb_log( "encavcodec: encoding at rc=vbr, multipass=fullres, Bitrate %d", job->vbitrate );
374 }
375
376 if ( job->vcodec == HB_VCODEC_FFMPEG_VCE_H264 || job->vcodec == HB_VCODEC_FFMPEG_VCE_H265 )
377 {
378 av_dict_set( &av_opts, "rc", "vbr_peak", 0 );
379
380 // since we do not have scene change detection, set a
381 // relatively short gop size to help avoid stale references
382 context->gop_size = (int)(FFMIN(av_q2d(fps) * 2, 120));
383
384 //Work around an ffmpeg issue mentioned in issue #3447
385 if (job->vcodec == HB_VCODEC_FFMPEG_VCE_H265)
386 {
387 av_dict_set( &av_opts, "qmin", "0", 0 );
388 av_dict_set( &av_opts, "qmax", "51", 0 );
389 }
390 hb_log( "encavcodec: encoding at rc=vbr_peak Bitrate %d", job->vbitrate );
391 }
392
393 if (job->vcodec == HB_VCODEC_FFMPEG_MF_H264 ||
394 job->vcodec == HB_VCODEC_FFMPEG_MF_H265) {
395 av_dict_set(&av_opts, "rate_control", "u_vbr", 0); // options are cbr, pc_vbr, u_vbr, ld_vbr, g_vbr, gld_vbr
396
397 // On Qualcomm encoders, the VBR modes can easily drop frames if
398 // the rate control feels like it needs it (in certain
399 // configurations), unless scenario is set to camera_record.
400 av_dict_set(&av_opts, "scenario", "camera_record", 0);
401 }
402 }
403 else
404 {
405 /* Constant quantizer */
406
407 //Set constant quality for libvpx
408 if ( w->codec_param == AV_CODEC_ID_VP8 ||
409 w->codec_param == AV_CODEC_ID_VP9 )
410 {
411 // These settings produce better image quality than
412 // what was previously used
413 context->flags |= AV_CODEC_FLAG_QSCALE;
414 context->global_quality = FF_QP2LAMBDA * job->vquality + 0.5;
415
416 char quality[7];
417 snprintf(quality, 7, "%.2f", job->vquality);
418 av_dict_set( &av_opts, "crf", quality, 0 );
419 //This value was chosen to make the bitrate high enough
420 //for libvpx to "turn off" the maximum bitrate feature
421 //that is normally applied to constant quality.
422 context->bit_rate = (int64_t)job->width * job->height *
423 fps.num / fps.den;
424 hb_log( "encavcodec: encoding at CQ %.2f", job->vquality );
425 }
426 //Set constant quality for nvenc
427 else if ( job->vcodec == HB_VCODEC_FFMPEG_NVENC_H264 ||
428 job->vcodec == HB_VCODEC_FFMPEG_NVENC_H265 )
429 {
430 char qualityI[7];
431 char quality[7];
432 char qualityB[7];
433
434 double adjustedQualityI = job->vquality - 2;
435 double adjustedQualityB = job->vquality + 2;
436 if (adjustedQualityB > 51) {
437 adjustedQualityB = 51;
438 }
439
440 if (adjustedQualityI < 0){
441 adjustedQualityI = 0;
442 }
443
444 snprintf(quality, 7, "%.2f", job->vquality);
445 snprintf(qualityI, 7, "%.2f", adjustedQualityI);
446 snprintf(qualityB, 7, "%.2f", adjustedQualityB);
447
448 context->bit_rate = 0;
449
450 av_dict_set( &av_opts, "rc", "vbr", 0 );
451 av_dict_set( &av_opts, "cq", quality, 0 );
452 av_dict_set( &av_opts, "multipass", "fullres", 0 );
453
454 // further Advanced Quality Settings in Constant Quality Mode
455 av_dict_set( &av_opts, "init_qpP", quality, 0 );
456 av_dict_set( &av_opts, "init_qpB", qualityB, 0 );
457 av_dict_set( &av_opts, "init_qpI", qualityI, 0 );
458 hb_log( "encavcodec: encoding at rc=vbr, multipass=fullres, %.2f", job->vquality );
459 }
460 else if ( job->vcodec == HB_VCODEC_FFMPEG_VCE_H264 )
461 {
462 // since we do not have scene change detection, set a
463 // relatively short gop size to help avoid stale references
464 context->gop_size = (int)(FFMIN(av_q2d(fps) * 2, 120));
465
466 char quality[7];
467 char qualityB[7];
468 double adjustedQualityB = job->vquality + 2;
469
470 snprintf(quality, 7, "%.2f", job->vquality);
471 snprintf(qualityB, 7, "%.2f", adjustedQualityB);
472
473 if (adjustedQualityB > 51) {
474 adjustedQualityB = 51;
475 }
476
477 av_dict_set( &av_opts, "rc", "cqp", 0 );
478
479 av_dict_set( &av_opts, "qp_i", quality, 0 );
480 av_dict_set( &av_opts, "qp_p", quality, 0 );
481 av_dict_set( &av_opts, "qp_b", qualityB, 0 );
482 hb_log( "encavcodec: encoding at QP %.2f", job->vquality );
483 }
484 else if ( job->vcodec == HB_VCODEC_FFMPEG_VCE_H265 )
485 {
486 char *vce_h265_max_au_size_char,
487 vce_h265_q_char[4];
488 int vce_h265_cq_step,
489 vce_h265_max_au_size,
490 vce_h265_max_au_size_length,
491 vce_h265_qmin,
492 vce_h265_qmax,
493 vce_h265_qmin_p,
494 vce_h265_qmax_p,
495 vce_h265_threshold;
496 double vce_h265_bit_rate,
497 vce_h265_buffer_size,
498 vce_h265_comp_factor,
499 vce_h265_exp_scale,
500 vce_h265_max_rate = 0;
501
502 /*
503 constant quality tuning for peak constrained vbr rate control
504
505 summary of settings:
506 - set a relatively short gop size to help avoid stale references, since we do not have scene detection
507 - constrain qmin and qmax to ensure consistent visual quality window regardless of complexity in detail and/or motion
508 - limit max rate to 1.3x (effectively ~2x) target bit rate to allow adequate variance while avoiding extreme peaks
509 - calculate hrd buffer size based on max rate to manage short term data rate in conjunction with max au size
510 - set max au size to 1/4 hrd buffer size to improve intra-gop bit allocation and minimize refresh/recovery effects at gop transitions
511
512 additional settings for low quality / bit rates:
513 - increase gop size slightly to save bits by reducing total keyframe count
514 - increase qmin to save bits by minimizing overallocation to static scenes
515 - increase max rate to give the encoder flexibility in reallocating saved bits
516 - increase qmax as a last resort to avoid overshooting data rate
517 */
518
519 // set gop size to two seconds in frames
520 context->gop_size = (int)(FFMIN(av_q2d(fps) * 2, 120));
521
522 // the fun part
523 if (job->vquality > 0 && job->vquality < 51)
524 {
525 // set rc mode to peak constrained vbr
526 av_dict_set( &av_opts, "rc", "vbr_peak", 0 );
527
528 /*
529 // calculate CQ 33 bit rate, which is the basis for all other CQ bit rates
530 vce_h265_bit_rate = ((sqrt(sqrt(job->width * job->height) * job->width * job->height / 1000) * 1.2) + 150) * 1000;
531
532 // initial compounding factor for calculating bit rates for other CQ values
533 vce_h265_comp_factor = 1.16;
534
535 // calculate CQ 39 bit rate, which is the low bit rate quality threshold
536 vce_h265_threshold = vce_h265_bit_rate * pow(1.0L / vce_h265_comp_factor, 5);
537
538 // calculate bit rate for user specified CQ value
539 if (job->vquality < 33)
540 {
541 for (vce_h265_cq_step = 32; vce_h265_cq_step >= job->vquality; vce_h265_cq_step--)
542 {
543 if (vce_h265_cq_step < 18)
544 {
545 vce_h265_comp_factor = 1.14; // vce_h265_comp_factor * 0.9827586207;
546 }
547 if (vce_h265_cq_step < 15)
548 {
549 vce_h265_comp_factor = 1.12; // vce_h265_comp_factor * 0.9824561404;
550 }
551 vce_h265_bit_rate = vce_h265_bit_rate * vce_h265_comp_factor;
552 }
553 }
554 else if (job->vquality > 33)
555 {
556 for (vce_h265_cq_step = 34; vce_h265_cq_step <= job->vquality; vce_h265_cq_step++)
557 {
558 if (vce_h265_cq_step > 39)
559 {
560 vce_h265_comp_factor = 1.15; // vce_h265_comp_factor * 0.9913793103;
561 }
562 if (vce_h265_cq_step > 49)
563 {
564 vce_h265_comp_factor = 1.25; // vce_h265_comp_factor * 1.0869565217;
565 }
566 vce_h265_bit_rate = vce_h265_bit_rate / vce_h265_comp_factor;
567 }
568 }
569 context->bit_rate = (int)(vce_h265_bit_rate);
570 */
571
572 // calculate CQ 30 bit rate, which is the basis for all other CQ bit rates
573 vce_h265_bit_rate = sqrt(job->width * job->height * pow(sqrt(job->width * job->height) / 1000, 2.5) + 400000) * 1000;
574
575 // initial compounding factor for calculating bit rates for other CQ values
576 vce_h265_comp_factor = 1.15;
577
578 // calculate CQ 39 bit rate, which is the low bit rate quality threshold
579 vce_h265_threshold = vce_h265_bit_rate * pow(1.0L / vce_h265_comp_factor, 8);
580
581 // calculate bit rate for user specified CQ value
582 if (job->vquality < 30)
583 {
584 vce_h265_comp_factor = 1.18;
585 for (vce_h265_cq_step = 29; vce_h265_cq_step >= job->vquality; vce_h265_cq_step--)
586 {
587 // reticulate splines
588 if (vce_h265_cq_step < 21)
589 {
590 vce_h265_comp_factor = 1.15;
591 }
592 if (vce_h265_cq_step < 15)
593 {
594 vce_h265_comp_factor = 1.12;
595 }
596 if (vce_h265_cq_step < 8)
597 {
598 vce_h265_comp_factor = 1.1;
599 }
600 if (vce_h265_cq_step < 3)
601 {
602 vce_h265_comp_factor = 1.08;
603 }
604 vce_h265_bit_rate = vce_h265_bit_rate * vce_h265_comp_factor;
605 }
606 }
607 else
608 {
609 for (vce_h265_cq_step = 31; vce_h265_cq_step <= job->vquality; vce_h265_cq_step++)
610 {
611 vce_h265_bit_rate = vce_h265_bit_rate / vce_h265_comp_factor;
612 }
613 }
614 context->bit_rate = (int)(vce_h265_bit_rate);
615
616 // QP 1-19
617 // constrain qmax to ensure bits are not underallocated to motion
618 vce_h265_qmin = 0;
619 vce_h265_qmax = (int)(sqrt(job->vquality - 0.75) * 8);
620 vce_h265_qmin_p = 0;
621 vce_h265_qmax_p = vce_h265_qmax + 2;
622
623 if (vce_h265_bit_rate < vce_h265_threshold * 12)
624 {
625 // CQ 20-22
626 // constrain qmin to ensure bits are not overallocated to low motion, static scenes
627 vce_h265_qmin = 4;
628 vce_h265_qmax = 34;
629 vce_h265_qmin_p = 8;
630 vce_h265_qmax_p = 38;
631
632 if (vce_h265_bit_rate < vce_h265_threshold * 8)
633 {
634 // CQ 23-27
635 vce_h265_qmin = 8;
636 vce_h265_qmax = 36;
637 vce_h265_qmin_p = 12;
638 vce_h265_qmax_p = 40;
639
640 if (vce_h265_bit_rate < vce_h265_threshold * 4)
641 {
642 // CQ 28-32
643 vce_h265_qmin = 16;
644 vce_h265_qmax = 38;
645 vce_h265_qmin_p = 19;
646 vce_h265_qmax_p = 42;
647
648 if (vce_h265_bit_rate < vce_h265_threshold * 2)
649 {
650 // CQ 33-38
651 // bit rate is at or just above the starvation threshold
652 // increase qmax to baseline for decent references (I) and minimal motion trails, recovery effects (P)
653 vce_h265_qmin = 19;
654 vce_h265_qmax = 39;
655 vce_h265_qmin_p = 22;
656 vce_h265_qmax_p = 44;
657
658 if (vce_h265_bit_rate <= vce_h265_threshold)
659 {
660 // CQ 39-40
661 // bit rate is at or just below the starvation threshold
662 // increase gop size to save bits by reducing total keyframe count
663 // increase qmin to continue saving bits by minimizing overallocation to static scenes
664 // increase qmax beyond baseline for decent references (I) and minimal motion trails, recovery effects (P)
665 // increase max rate to allow higher relative peaks in short bursts
666 context->gop_size = (int)(FFMIN(av_q2d(fps) * 3, 180));
667 vce_h265_qmin = 22;
668 vce_h265_qmax = 40;
669 vce_h265_qmin_p = 24;
670 vce_h265_qmax_p = 45;
671 vce_h265_max_rate = vce_h265_bit_rate * 1.5;
672
673 if (vce_h265_bit_rate < vce_h265_threshold * 0.85)
674 {
675 // CQ 41
676 vce_h265_qmin = 24;
677 vce_h265_qmax = 41;
678 vce_h265_qmin_p = 27;
679 vce_h265_qmax_p = 46;
680 vce_h265_max_rate = vce_h265_bit_rate * 2.5;
681
682 if (vce_h265_bit_rate < vce_h265_threshold * 0.7)
683 {
684 // CQ 42
685 vce_h265_qmin = 27;
686 vce_h265_qmax = 42;
687 vce_h265_qmin_p = 30;
688 vce_h265_qmax_p = 47;
689 vce_h265_max_rate = vce_h265_bit_rate * 6.5;
690
691 if (vce_h265_bit_rate < vce_h265_threshold * 0.6)
692 {
693 // CQ 43
694 vce_h265_qmin = 31;
695 vce_h265_qmax = 44;
696 vce_h265_qmin_p = 34;
697 vce_h265_qmax_p = 48;
698 vce_h265_max_rate = vce_h265_bit_rate * 15;
699
700 if (vce_h265_bit_rate < vce_h265_threshold * 0.51)
701 {
702 // CQ 44-45
703 vce_h265_qmin = 35;
704 vce_h265_qmax = 46;
705 vce_h265_qmin_p = 38;
706 vce_h265_qmax_p = 49;
707 vce_h265_max_rate = vce_h265_bit_rate * 19;
708
709 if (vce_h265_bit_rate < vce_h265_threshold * 0.42)
710 {
711 // CQ 46-47
712 // bit rate is insufficient for any motion
713 vce_h265_qmin = 39;
714 vce_h265_qmax = 48;
715 vce_h265_qmin_p = 42;
716 vce_h265_qmax_p = 50;
717 vce_h265_max_rate = vce_h265_bit_rate * 22;
718
719 if (vce_h265_bit_rate < vce_h265_threshold * 0.32)
720 {
721 // CQ 48-49
722 // bit rate is entirely insufficient
723 vce_h265_qmin = 43;
724 vce_h265_qmax = 49;
725 vce_h265_qmin_p = 46;
726 vce_h265_qmax_p = 50;
727 vce_h265_max_rate = vce_h265_bit_rate * 24;
728
729 if (vce_h265_bit_rate < vce_h265_threshold * 0.24)
730 {
731 // CQ 50
732 // there are no bits
733 vce_h265_qmin = 45;
734 vce_h265_qmax = 49;
735 vce_h265_qmin_p = 49;
736 vce_h265_qmax_p = 51;
737 vce_h265_max_rate = vce_h265_bit_rate * 10;
738 }
739
740 }
741 }
742 }
743 }
744 }
745 }
746 }
747 }
748 }
749 }
750 }
751
752 // factor for calculating max rate and buffer size
753 vce_h265_exp_scale = FFMIN(1.0L * 1000000 / vce_h265_bit_rate, 1.0L);
754
755 // ideal max rate is ~1.3x target bit rate, diminishing on a curve as target bit rate increases
756 // this allows allows the actual bit rate to vary as needed to ensure consistent visual quality,
757 // while limiting potentially exteme one-second peaks to approximately double the target bit rate
758 if (vce_h265_max_rate == 0)
759 {
760 vce_h265_max_rate = vce_h265_bit_rate * ((vce_h265_exp_scale * 10) + 1);
761 vce_h265_max_rate = FFMAX(vce_h265_bit_rate * 1.05L, FFMIN(vce_h265_max_rate, vce_h265_bit_rate * 1.3L));
762 }
763 context->rc_max_rate = (int)(vce_h265_max_rate);
764
765 // ideal hrd buffer size is the calculated max rate, diminishing on a curve as target bit rate increases
766 // minimum 1/3 target bit rate ensures the buffer size is not too constrained at higher target bit rates
767 vce_h265_buffer_size = FFMAX(vce_h265_bit_rate / 3, vce_h265_max_rate - (vce_h265_max_rate / FFMAX(vce_h265_exp_scale * 150.0L, 1.5L)));
768 if (vce_h265_buffer_size / vce_h265_max_rate > 0.98L)
769 {
770 // buffer size is nearly identical to max rate, make them equal
771 vce_h265_buffer_size = vce_h265_max_rate;
772 }
773 context->rc_buffer_size = (int)(vce_h265_buffer_size);
774 context->rc_initial_buffer_occupancy = context->rc_buffer_size;
775
776 // ideal max au size (frame size + overhead) is 1/4 the hrd buffer size
777 // this improves bit allocation by preventing the encoder from spending too many bits early in the gop
778 // or during periods of low motion, leaving too few bits for remaining frames and objects in motion
779 // better intra-gop quality consistency also helps minimize refresh/recovery effects at gop transitions
780 // if max_au_size is set, libavcodec will also set enforce_hrd for us
781 vce_h265_max_au_size = (int)(vce_h265_buffer_size * 0.25);
782 vce_h265_max_au_size_length = snprintf(NULL, 0, "%d", vce_h265_max_au_size);
783 vce_h265_max_au_size_char = malloc(vce_h265_max_au_size_length + 1);
784 if( !vce_h265_max_au_size_char )
785 {
786 hb_log( "encavcodecInit: malloc of size %d "
787 "failed", vce_h265_max_au_size_length );
788 ret = 1;
789 goto done;
790 }
791 snprintf(vce_h265_max_au_size_char, vce_h265_max_au_size_length + 1, "%d", vce_h265_max_au_size);
792 av_dict_set( &av_opts, "max_au_size", vce_h265_max_au_size_char, 0 );
793 free(vce_h265_max_au_size_char);
794 }
795 else
796 {
797 // set rc mode to cqp
798 av_dict_set( &av_opts, "rc", "cqp", 0 );
799
800 // set relatively long gop size for CQ 0
801 // does not affect quality; only IDR frequency and thus file size
802 context->gop_size = job->vquality < 1 ? 250 : (int)(FFMIN(av_q2d(fps) * 3, 180));
803
804 // CQ 0 == CQP 0
805 // CQ 51 == CQP 51
806 vce_h265_qmin = job->vquality < 1 ? 0 : 51;
807 vce_h265_qmax = vce_h265_qmin;
808 vce_h265_qmin_p = vce_h265_qmin;
809 vce_h265_qmax_p = vce_h265_qmin;
810 snprintf(vce_h265_q_char, 4, "%d", vce_h265_qmin);
811 av_dict_set( &av_opts, "qp_i", vce_h265_q_char, 0 );
812 av_dict_set( &av_opts, "qp_p", vce_h265_q_char, 0 );
813 }
814
815 context->qmin = vce_h265_qmin;
816 context->qmax = vce_h265_qmax;
817 snprintf(vce_h265_q_char, 4, "%d", vce_h265_qmin_p);
818 av_dict_set( &av_opts, "min_qp_p", vce_h265_q_char, 0 );
819 snprintf(vce_h265_q_char, 4, "%d", vce_h265_qmax_p);
820 av_dict_set( &av_opts, "max_qp_p", vce_h265_q_char, 0 );
821
822 hb_log( "encavcodec: encoding at constant quality %d", (int)(job->vquality) );
823 hb_log( "encavcodec: QP (I) %d-%d", vce_h265_qmin, vce_h265_qmax );
824 hb_log( "encavcodec: QP (P) %d-%d", vce_h265_qmin_p, vce_h265_qmax_p );
825 hb_log( "encavcodec: GOP Size %d", context->gop_size );
826 if (vce_h265_max_rate > 0)
827 {
828 hb_log( "encavcodec: Max Rate %"PRId64"", context->rc_max_rate/1000 );
829 hb_log( "encavcodec: Buffer Size %d", context->rc_buffer_size/1000 );
830 hb_log( "encavcodec: Max AU Size %d", vce_h265_max_au_size/1000 );
831 }
832 }
833 else if ( job->vcodec == HB_VCODEC_FFMPEG_VT_H264 || job->vcodec == HB_VCODEC_FFMPEG_VT_H265 || job->vcodec == HB_VCODEC_FFMPEG_VT_H265_10BIT)
834 {
835 context->flags |= AV_CODEC_FLAG_QSCALE;
836 context->global_quality = FF_QP2LAMBDA * job->vquality + 0.5;
837
838 hb_log( "encavcodec: encoding at constant quality %d",
839 (int)job->vquality );
840 }
841 else if (job->vcodec == HB_VCODEC_FFMPEG_MF_H264 ||
842 job->vcodec == HB_VCODEC_FFMPEG_MF_H265)
843 {
844 char quality[7];
845 snprintf(quality, 7, "%d", (int)job->vquality);
846 av_dict_set(&av_opts, "rate_control", "quality", 0);
847 av_dict_set(&av_opts, "quality", quality, 0);
848 }
849 else
850 {
851 // These settings produce better image quality than
852 // what was previously used
853 context->flags |= AV_CODEC_FLAG_QSCALE;
854 context->global_quality = FF_QP2LAMBDA * job->vquality + 0.5;
855
856 hb_log( "encavcodec: encoding at constant quantizer %d",
857 context->global_quality );
858 }
859 }
860 context->width = job->width;
861 context->height = job->height;
862 context->pix_fmt = job->output_pix_fmt;
863
864 context->sample_aspect_ratio.num = job->par.num;
865 context->sample_aspect_ratio.den = job->par.den;
866 if (job->vcodec == HB_VCODEC_FFMPEG_MPEG4)
867 {
868 // MPEG-4 Part 2 stores the PAR num/den as unsigned 8-bit fields,
869 // and libavcodec's encoder fails to initialize if we don't
870 // reduce it to fit 8-bits.
871 hb_limit_rational(&context->sample_aspect_ratio.num,
872 &context->sample_aspect_ratio.den,
873 context->sample_aspect_ratio.num,
874 context->sample_aspect_ratio.den, 255);
875 }
876
877 hb_log( "encavcodec: encoding with stored aspect %d/%d",
878 job->par.num, job->par.den );
879
880 // set colorimetry
881 context->color_primaries = hb_output_color_prim(job);
882 context->color_trc = hb_output_color_transfer(job);
883 context->colorspace = hb_output_color_matrix(job);
884
885 if (!job->inline_parameter_sets)
886 {
887 context->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
888 }
889 if( job->grayscale )
890 {
891 context->flags |= AV_CODEC_FLAG_GRAY;
892 }
893
894 if (job->vcodec == HB_VCODEC_FFMPEG_VT_H264)
895 {
896 // Set profile and level
897 if (job->encoder_profile != NULL && *job->encoder_profile)
898 {
899 if (!strcasecmp(job->encoder_profile, "baseline"))
900 av_dict_set(&av_opts, "profile", "baseline", 0);
901 else if (!strcasecmp(job->encoder_profile, "main"))
902 av_dict_set(&av_opts, "profile", "main", 0);
903 else if (!strcasecmp(job->encoder_profile, "high"))
904 av_dict_set(&av_opts, "profile", "high", 0);
905 }
906
907 if (job->encoder_level != NULL && *job->encoder_level)
908 {
909 int i = 1;
910 while (hb_h264_level_names[i] != NULL)
911 {
912 if (!strcasecmp(job->encoder_level, hb_h264_level_names[i]))
913 av_dict_set(&av_opts, "level", job->encoder_level, 0);
914 ++i;
915 }
916 }
917
918 context->max_b_frames = 16;
919 }
920
921 if (job->vcodec == HB_VCODEC_FFMPEG_VT_H265)
922 {
923 av_dict_set(&av_opts, "profile", "main", 0);
924 context->max_b_frames = 16;
925 }
926
927 if (job->vcodec == HB_VCODEC_FFMPEG_VT_H265_10BIT)
928 {
929 av_dict_set(&av_opts, "profile", "main10", 0);
930 context->max_b_frames = 16;
931 }
932
933 if (job->vcodec == HB_VCODEC_FFMPEG_VCE_H264)
934 {
935 // Set profile and level
936 context->profile = FF_PROFILE_UNKNOWN;
937 if (job->encoder_profile != NULL && *job->encoder_profile)
938 {
939 if (!strcasecmp(job->encoder_profile, "baseline"))
940 context->profile = FF_PROFILE_H264_BASELINE;
941 else if (!strcasecmp(job->encoder_profile, "main"))
942 context->profile = FF_PROFILE_H264_MAIN;
943 else if (!strcasecmp(job->encoder_profile, "high"))
944 context->profile = FF_PROFILE_H264_HIGH;
945 }
946 context->level = FF_LEVEL_UNKNOWN;
947 if (job->encoder_level != NULL && *job->encoder_level)
948 {
949 int i = 1;
950 while (hb_h264_level_names[i] != NULL)
951 {
952 if (!strcasecmp(job->encoder_level, hb_h264_level_names[i]))
953 context->level = hb_h264_level_values[i];
954 ++i;
955 }
956 }
957 }
958
959 if (job->vcodec == HB_VCODEC_FFMPEG_VCE_H265)
960 {
961 // Set profile and level
962 context->profile = FF_PROFILE_UNKNOWN;
963 if (job->encoder_profile != NULL && *job->encoder_profile)
964 {
965 if (!strcasecmp(job->encoder_profile, "main"))
966 context->profile = FF_PROFILE_HEVC_MAIN;
967 }
968 context->level = FF_LEVEL_UNKNOWN;
969 if (job->encoder_level != NULL && *job->encoder_level)
970 {
971 int i = 1;
972 while (hb_h265_level_names[i] != NULL)
973 {
974 if (!strcasecmp(job->encoder_level, hb_h265_level_names[i]))
975 context->level = hb_h265_level_values[i];
976 ++i;
977 }
978 }
979 // FIXME
980 //context->tier = FF_TIER_UNKNOWN;
981 }
982
983 if (job->vcodec == HB_VCODEC_FFMPEG_NVENC_H264 ||
984 job->vcodec == HB_VCODEC_FFMPEG_NVENC_H265)
985 {
986 // Force IDR frames when we force a new keyframe for chapters
987 av_dict_set( &av_opts, "forced-idr", "1", 0 );
988
989 // Set profile and level
990 if (job->encoder_profile != NULL && *job->encoder_profile)
991 {
992 if (!strcasecmp(job->encoder_profile, "baseline"))
993 av_dict_set(&av_opts, "profile", "baseline", 0);
994 else if (!strcasecmp(job->encoder_profile, "main"))
995 av_dict_set(&av_opts, "profile", "main", 0);
996 else if (!strcasecmp(job->encoder_profile, "high"))
997 av_dict_set(&av_opts, "profile", "high", 0);
998 }
999
1000 if (job->encoder_level != NULL && *job->encoder_level)
1001 {
1002 int i = 1;
1003 while (hb_h264_level_names[i] != NULL)
1004 {
1005 if (!strcasecmp(job->encoder_level, hb_h264_level_names[i]))
1006 av_dict_set(&av_opts, "level", job->encoder_level, 0);
1007 ++i;
1008 }
1009 }
1010 }
1011
1012 // Make VCE h.265 encoder emit an IDR for every GOP
1013 if (job->vcodec == HB_VCODEC_FFMPEG_VCE_H265)
1014 {
1015 av_dict_set(&av_opts, "gops_per_idr", "1", 0);
1016 }
1017
1018 if (job->vcodec == HB_VCODEC_FFMPEG_MF_H264)
1019 {
1020 context->profile = FF_PROFILE_UNKNOWN;
1021 if (job->encoder_profile != NULL && *job->encoder_profile)
1022 {
1023 if (!strcasecmp(job->encoder_profile, "baseline"))
1024 context->profile = FF_PROFILE_H264_BASELINE;
1025 else if (!strcasecmp(job->encoder_profile, "main"))
1026 context->profile = FF_PROFILE_H264_MAIN;
1027 else if (!strcasecmp(job->encoder_profile, "high"))
1028 context->profile = FF_PROFILE_H264_HIGH;
1029 }
1030
1031 }
1032
1033 if (job->vcodec == HB_VCODEC_FFMPEG_MF_H264 ||
1034 job->vcodec == HB_VCODEC_FFMPEG_MF_H265)
1035 {
1036 av_dict_set(&av_opts, "hw_encoding", "1", 0);
1037 }
1038
1039 if (job->vcodec == HB_VCODEC_FFMPEG_MF_H265)
1040 {
1041 // Qualcomm's HEVC encoder does support b-frames. Some chipsets
1042 // support setting this to either 1 or 2, while others only support
1043 // setting it to 1.
1044 context->max_b_frames = 1;
1045 }
1046
1047 if( job->pass_id == HB_PASS_ENCODE_1ST ||
1048 job->pass_id == HB_PASS_ENCODE_2ND )
1049 {
1050 char * filename = hb_get_temporary_filename("ffmpeg.log");
1051
1052 if( job->pass_id == HB_PASS_ENCODE_1ST )
1053 {
1054 pv->file = hb_fopen(filename, "wb");
1055 if (!pv->file)
1056 {
1057 if (strerror_r(errno, reason, 79) != 0)
1058 strcpy(reason, "unknown -- strerror_r() failed");
1059
1060 hb_error("encavcodecInit: Failed to open %s (reason: %s)", filename, reason);
1061 free(filename);
1062 ret = 1;
1063 goto done;
1064 }
1065 context->flags |= AV_CODEC_FLAG_PASS1;
1066 }
1067 else
1068 {
1069 int size;
1070 char * log;
1071
1072 pv->file = hb_fopen(filename, "rb");
1073 if (!pv->file) {
1074 if (strerror_r(errno, reason, 79) != 0)
1075 strcpy(reason, "unknown -- strerror_r() failed");
1076
1077 hb_error("encavcodecInit: Failed to open %s (reason: %s)", filename, reason);
1078 free(filename);
1079 ret = 1;
1080 goto done;
1081 }
1082 fseek( pv->file, 0, SEEK_END );
1083 size = ftell( pv->file );
1084 fseek( pv->file, 0, SEEK_SET );
1085 log = malloc( size + 1 );
1086 log[size] = '\0';
1087 if (size > 0 &&
1088 fread( log, size, 1, pv->file ) < size)
1089 {
1090 if (ferror(pv->file))
1091 {
1092 if (strerror_r(errno, reason, 79) != 0)
1093 strcpy(reason, "unknown -- strerror_r() failed");
1094
1095 hb_error( "encavcodecInit: Failed to read %s (reason: %s)" , filename, reason);
1096 free(filename);
1097 ret = 1;
1098 fclose( pv->file );
1099 pv->file = NULL;
1100 goto done;
1101 }
1102 }
1103 fclose( pv->file );
1104 pv->file = NULL;
1105
1106 context->flags |= AV_CODEC_FLAG_PASS2;
1107 context->stats_in = log;
1108 }
1109 free(filename);
1110 }
1111
1112 if (hb_avcodec_open(context, codec, &av_opts, HB_FFMPEG_THREADS_AUTO))
1113 {
1114 hb_log( "encavcodecInit: avcodec_open failed" );
1115 ret = 1;
1116 goto done;
1117 }
1118
1119 /*
1120 * Reload colorimetry settings in case custom
1121 * values were set in the encoder_options string.
1122 */
1123 job->color_prim_override = context->color_primaries;
1124 job->color_transfer_override = context->color_trc;
1125 job->color_matrix_override = context->colorspace;
1126
1127 if (job->pass_id == HB_PASS_ENCODE_1ST &&
1128 context->stats_out != NULL)
1129 {
1130 // Some encoders may write stats during init in avcodec_open
1131 fprintf(pv->file, "%s", context->stats_out);
1132 }
1133
1134 // avcodec_open populates the opts dictionary with the
1135 // things it didn't recognize.
1136 AVDictionaryEntry *t = NULL;
1137 while( ( t = av_dict_get( av_opts, "", t, AV_DICT_IGNORE_SUFFIX ) ) )
1138 {
1139 hb_log( "encavcodecInit: Unknown avcodec option %s", t->key );
1140 }
1141 av_dict_free( &av_opts );
1142
1143 pv->context = context;
1144
1145 job->areBframes = 0;
1146 if (context->has_b_frames > 0)
1147 {
1148 if (job->vcodec == HB_VCODEC_FFMPEG_VT_H265 || job->vcodec == HB_VCODEC_FFMPEG_VT_H265_10BIT)
1149 {
1150 // VT appears to enable b-pyramid by default and there
1151 // is no documented way of modifying this behaviour or
1152 // querying if it is enabled.
1153 job->areBframes = 2;
1154 }
1155 else
1156 {
1157 job->areBframes = context->has_b_frames;
1158 }
1159 }
1160
1161 if (context->extradata != NULL)
1162 {
1163 memcpy(w->config->extradata.bytes, context->extradata,
1164 context->extradata_size);
1165 w->config->extradata.length = context->extradata_size;
1166 }
1167
1168 done:
1169 return ret;
1170 }
1171
1172 /***********************************************************************
1173 * Close
1174 ***********************************************************************
1175 *
1176 **********************************************************************/
encavcodecClose(hb_work_object_t * w)1177 void encavcodecClose( hb_work_object_t * w )
1178 {
1179 hb_work_private_t * pv = w->private_data;
1180
1181 if (pv == NULL)
1182 {
1183 return;
1184 }
1185 av_packet_free(&pv->pkt);
1186 hb_chapter_queue_close(&pv->chapter_queue);
1187 if( pv->context )
1188 {
1189 hb_deep_log( 2, "encavcodec: closing libavcodec" );
1190 if( pv->context->codec ) {
1191 avcodec_flush_buffers( pv->context );
1192 }
1193 hb_avcodec_free_context(&pv->context);
1194 }
1195 if( pv->file )
1196 {
1197 fclose( pv->file );
1198 }
1199 free( pv );
1200 w->private_data = NULL;
1201 }
1202
1203 /*
1204 * see comments in definition of 'frame_info' in pv struct for description
1205 * of what these routines are doing.
1206 */
save_frame_info(hb_work_private_t * pv,hb_buffer_t * in)1207 static void save_frame_info( hb_work_private_t * pv, hb_buffer_t * in )
1208 {
1209 int i = pv->frameno_in & FRAME_INFO_MASK;
1210 pv->frame_info[i].start = in->s.start;
1211 pv->frame_info[i].duration = in->s.stop - in->s.start;
1212 }
1213
get_frame_start(hb_work_private_t * pv,int64_t frameno)1214 static int64_t get_frame_start( hb_work_private_t * pv, int64_t frameno )
1215 {
1216 int i = frameno & FRAME_INFO_MASK;
1217 return pv->frame_info[i].start;
1218 }
1219
get_frame_duration(hb_work_private_t * pv,int64_t frameno)1220 static int64_t get_frame_duration( hb_work_private_t * pv, int64_t frameno )
1221 {
1222 int i = frameno & FRAME_INFO_MASK;
1223 return pv->frame_info[i].duration;
1224 }
1225
compute_dts_offset(hb_work_private_t * pv,hb_buffer_t * buf)1226 static void compute_dts_offset( hb_work_private_t * pv, hb_buffer_t * buf )
1227 {
1228 if ( pv->job->areBframes )
1229 {
1230 if ( ( pv->frameno_in ) == pv->job->areBframes )
1231 {
1232 pv->dts_delay = buf->s.start;
1233 pv->job->config.init_delay = pv->dts_delay;
1234 }
1235 }
1236 }
1237
1238 // Generate DTS by rearranging PTS in this sequence:
1239 // pts0 - delay, pts1 - delay, pts2 - delay, pts1, pts2, pts3...
1240 //
1241 // Where pts0 - ptsN are in decoded monotonically increasing presentation
1242 // order and delay == pts1 (1 being the number of frames the decoder must
1243 // delay before it has sufficient information to decode). The number of
1244 // frames to delay is set by job->areBframes, so it is configurable.
1245 // This guarantees that DTS <= PTS for any frame.
1246 //
1247 // This is similar to how x264 generates DTS
process_delay_list(hb_work_private_t * pv,hb_buffer_t * buf)1248 static hb_buffer_t * process_delay_list( hb_work_private_t * pv, hb_buffer_t * buf )
1249 {
1250 if (pv->job->areBframes)
1251 {
1252 // Has dts_delay been set yet?
1253 hb_buffer_list_append(&pv->delay_list, buf);
1254 if (pv->frameno_in <= pv->job->areBframes)
1255 {
1256 // dts_delay not yet set. queue up buffers till it is set.
1257 return NULL;
1258 }
1259
1260 // We have dts_delay. Apply it to any queued buffers renderOffset
1261 // and return all queued buffers.
1262 buf = hb_buffer_list_head(&pv->delay_list);
1263 while (buf != NULL)
1264 {
1265 // Use the cached frame info to get the start time of Nth frame
1266 // Note that start Nth frame != start time this buffer since the
1267 // output buffers have rearranged start times.
1268 if (pv->frameno_out < pv->job->areBframes)
1269 {
1270 int64_t start = get_frame_start( pv, pv->frameno_out );
1271 buf->s.renderOffset = start - pv->dts_delay;
1272 }
1273 else
1274 {
1275 buf->s.renderOffset = get_frame_start(pv,
1276 pv->frameno_out - pv->job->areBframes);
1277 }
1278 buf = buf->next;
1279 pv->frameno_out++;
1280 }
1281 buf = hb_buffer_list_clear(&pv->delay_list);
1282 return buf;
1283 }
1284 else if (buf != NULL)
1285 {
1286 buf->s.renderOffset = buf->s.start;
1287 return buf;
1288 }
1289 return NULL;
1290 }
1291
get_packets(hb_work_object_t * w,hb_buffer_list_t * list)1292 static void get_packets( hb_work_object_t * w, hb_buffer_list_t * list )
1293 {
1294 hb_work_private_t * pv = w->private_data;
1295
1296 while (1)
1297 {
1298 int ret;
1299 hb_buffer_t * out;
1300
1301 ret = avcodec_receive_packet(pv->context, pv->pkt);
1302 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
1303 {
1304 break;
1305 }
1306 if (ret < 0)
1307 {
1308 hb_log("encavcodec: avcodec_receive_packet failed");
1309 }
1310
1311 out = hb_buffer_init(pv->pkt->size);
1312 memcpy(out->data, pv->pkt->data, out->size);
1313
1314 int64_t frameno = pv->pkt->pts;
1315 out->size = pv->pkt->size;
1316 out->s.start = get_frame_start(pv, frameno);
1317 out->s.duration = get_frame_duration(pv, frameno);
1318 out->s.stop = out->s.stop + out->s.duration;
1319 // libav 12 deprecated context->coded_frame, so we can't determine
1320 // the exact frame type any more. So until I can completely
1321 // wire up ffmpeg with AV_PKT_DISPOSABLE_FRAME, all frames
1322 // must be considered to potentially be reference frames
1323 out->s.flags = HB_FLAG_FRAMETYPE_REF;
1324 out->s.frametype = 0;
1325 if (pv->pkt->flags & AV_PKT_FLAG_KEY)
1326 {
1327 out->s.flags |= HB_FLAG_FRAMETYPE_KEY;
1328 hb_chapter_dequeue(pv->chapter_queue, out);
1329 }
1330 out = process_delay_list(pv, out);
1331
1332 hb_buffer_list_append(list, out);
1333 av_packet_unref(pv->pkt);
1334 }
1335 }
1336
Encode(hb_work_object_t * w,hb_buffer_t * in,hb_buffer_list_t * list)1337 static void Encode( hb_work_object_t *w, hb_buffer_t *in,
1338 hb_buffer_list_t *list )
1339 {
1340 hb_work_private_t * pv = w->private_data;
1341 AVFrame frame = {{0}};
1342 int ret;
1343
1344 frame.width = in->f.width;
1345 frame.height = in->f.height;
1346 frame.format = in->f.fmt;
1347 frame.data[0] = in->plane[0].data;
1348 frame.data[1] = in->plane[1].data;
1349 frame.data[2] = in->plane[2].data;
1350 frame.linesize[0] = in->plane[0].stride;
1351 frame.linesize[1] = in->plane[1].stride;
1352 frame.linesize[2] = in->plane[2].stride;
1353
1354 if (in->s.new_chap > 0 && pv->job->chapter_markers)
1355 {
1356 /* chapters have to start with an IDR frame so request that this
1357 frame be coded as IDR. Since there may be multiple frames
1358 currently buffered in the encoder remember the timestamp so
1359 when this frame finally pops out of the encoder we'll mark
1360 its buffer as the start of a chapter. */
1361 frame.pict_type = AV_PICTURE_TYPE_I;
1362 frame.key_frame = 1;
1363 hb_chapter_enqueue(pv->chapter_queue, in);
1364 }
1365
1366 // For constant quality, setting the quality in AVCodecContext
1367 // doesn't do the trick. It must be set in the AVFrame.
1368 frame.quality = pv->context->global_quality;
1369
1370 // Bizarro ffmpeg requires timestamp time_base to be == framerate
1371 // for the encoders we care about. It writes AVCodecContext.time_base
1372 // to the framerate field of encoded bitstream headers, so if we
1373 // want correct bitstreams, we must set time_base = framerate.
1374 // We can't pass timestamps that are not based on the time_base
1375 // because encoders require accurately based timestamps in order to
1376 // do proper rate control.
1377 //
1378 // I.e. ffmpeg doesn't support VFR timestamps.
1379 //
1380 // Because of this, we have to do some fugly things, like storing
1381 // PTS values and computing DTS ourselves.
1382 //
1383 // Remember timestamp info about this frame
1384 save_frame_info(pv, in);
1385 compute_dts_offset(pv, in);
1386
1387 frame.pts = pv->frameno_in++;
1388
1389 // Encode
1390 ret = avcodec_send_frame(pv->context, &frame);
1391 if (ret < 0)
1392 {
1393 hb_log("encavcodec: avcodec_send_frame failed");
1394 return;
1395 }
1396
1397 // Write stats
1398 if (pv->job->pass_id == HB_PASS_ENCODE_1ST &&
1399 pv->context->stats_out != NULL)
1400 {
1401 fprintf( pv->file, "%s", pv->context->stats_out );
1402 }
1403
1404 get_packets(w, list);
1405 }
1406
Flush(hb_work_object_t * w,hb_buffer_list_t * list)1407 static void Flush( hb_work_object_t * w, hb_buffer_list_t * list )
1408 {
1409 hb_work_private_t * pv = w->private_data;
1410
1411 avcodec_send_frame(pv->context, NULL);
1412
1413 // Write stats
1414 // vpx only writes stats at final flush
1415 if (pv->job->pass_id == HB_PASS_ENCODE_1ST &&
1416 pv->context->stats_out != NULL)
1417 {
1418 fprintf( pv->file, "%s", pv->context->stats_out );
1419 }
1420
1421 get_packets(w, list);
1422 }
1423
1424 /***********************************************************************
1425 * Work
1426 ***********************************************************************
1427 *
1428 **********************************************************************/
encavcodecWork(hb_work_object_t * w,hb_buffer_t ** buf_in,hb_buffer_t ** buf_out)1429 int encavcodecWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
1430 hb_buffer_t ** buf_out )
1431 {
1432 hb_work_private_t * pv = w->private_data;
1433 hb_buffer_t * in = *buf_in;
1434 hb_buffer_list_t list;
1435
1436 if (pv->context == NULL || pv->context->codec == NULL)
1437 {
1438 hb_error("encavcodec: codec context is uninitialized");
1439 return HB_WORK_DONE;
1440 }
1441
1442 hb_buffer_list_clear(&list);
1443 if (in->s.flags & HB_BUF_FLAG_EOF)
1444 {
1445 Flush(w, &list);
1446 hb_buffer_list_append(&list, hb_buffer_eof_init());
1447 *buf_out = hb_buffer_list_clear(&list);
1448 return HB_WORK_DONE;
1449 }
1450
1451 Encode(w, in, &list);
1452 *buf_out = hb_buffer_list_clear(&list);
1453
1454 return HB_WORK_OK;
1455 }
1456
apply_vpx_preset(AVDictionary ** av_opts,const char * preset)1457 static int apply_vpx_preset(AVDictionary ** av_opts, const char * preset)
1458 {
1459 if (preset == NULL)
1460 {
1461 // default "medium"
1462 av_dict_set( av_opts, "deadline", "good", 0);
1463 av_dict_set( av_opts, "cpu-used", "2", 0);
1464 }
1465 else if (!strcasecmp("veryfast", preset))
1466 {
1467 av_dict_set( av_opts, "deadline", "good", 0);
1468 av_dict_set( av_opts, "cpu-used", "5", 0);
1469 }
1470 else if (!strcasecmp("faster", preset))
1471 {
1472 av_dict_set( av_opts, "deadline", "good", 0);
1473 av_dict_set( av_opts, "cpu-used", "4", 0);
1474 }
1475 else if (!strcasecmp("fast", preset))
1476 {
1477 av_dict_set( av_opts, "deadline", "good", 0);
1478 av_dict_set( av_opts, "cpu-used", "3", 0);
1479 }
1480 else if (!strcasecmp("medium", preset))
1481 {
1482 av_dict_set( av_opts, "deadline", "good", 0);
1483 av_dict_set( av_opts, "cpu-used", "2", 0);
1484 }
1485 else if (!strcasecmp("slow", preset))
1486 {
1487 av_dict_set( av_opts, "deadline", "good", 0);
1488 av_dict_set( av_opts, "cpu-used", "1", 0);
1489 }
1490 else if (!strcasecmp("slower", preset))
1491 {
1492 av_dict_set( av_opts, "deadline", "good", 0);
1493 av_dict_set( av_opts, "cpu-used", "0", 0);
1494 }
1495 else if (!strcasecmp("veryslow", preset))
1496 {
1497 av_dict_set( av_opts, "deadline", "best", 0);
1498 av_dict_set( av_opts, "cpu-used", "0", 0);
1499 }
1500 else
1501 {
1502 // default "medium"
1503 hb_log("apply_vpx_preset: Unknown VPx encoder preset %s", preset);
1504 return -1;
1505 }
1506
1507 return 0;
1508 }
1509
1510 // VP8 and VP9 have some options in common and some different
apply_vp8_preset(AVDictionary ** av_opts,const char * preset)1511 static int apply_vp8_preset(AVDictionary ** av_opts, const char * preset)
1512 {
1513 return apply_vpx_preset(av_opts, preset);
1514 }
1515
apply_vp9_preset(AVDictionary ** av_opts,const char * preset)1516 static int apply_vp9_preset(AVDictionary ** av_opts, const char * preset)
1517 {
1518 av_dict_set(av_opts, "row-mt", "1", 0);
1519 return apply_vpx_preset(av_opts, preset);
1520 }
1521
apply_encoder_preset(int vcodec,AVDictionary ** av_opts,const char * preset)1522 static int apply_encoder_preset(int vcodec, AVDictionary ** av_opts,
1523 const char * preset)
1524 {
1525 switch (vcodec)
1526 {
1527 case HB_VCODEC_FFMPEG_VP8:
1528 return apply_vp8_preset(av_opts, preset);
1529 case HB_VCODEC_FFMPEG_VP9:
1530 return apply_vp9_preset(av_opts, preset);
1531
1532 #if HB_PROJECT_FEATURE_NVENC
1533 case HB_VCODEC_FFMPEG_NVENC_H264:
1534 case HB_VCODEC_FFMPEG_NVENC_H265:
1535 preset = hb_map_nvenc_preset_name(preset);
1536 av_dict_set( av_opts, "preset", preset, 0);
1537 break;
1538 #endif
1539 default:
1540 break;
1541 }
1542
1543 return 0;
1544 }
1545
hb_av_preset_get_names(int encoder)1546 const char* const* hb_av_preset_get_names(int encoder)
1547 {
1548 switch (encoder)
1549 {
1550 case HB_VCODEC_FFMPEG_VP8:
1551 case HB_VCODEC_FFMPEG_VP9:
1552 return vpx_preset_names;
1553
1554 case HB_VCODEC_FFMPEG_VCE_H264:
1555 case HB_VCODEC_FFMPEG_VCE_H265:
1556 return hb_vce_preset_names;
1557
1558 case HB_VCODEC_FFMPEG_NVENC_H264:
1559 case HB_VCODEC_FFMPEG_NVENC_H265:
1560 return h26x_nvenc_preset_names;
1561
1562 case HB_VCODEC_FFMPEG_VT_H264:
1563 case HB_VCODEC_FFMPEG_VT_H265:
1564 case HB_VCODEC_FFMPEG_VT_H265_10BIT:
1565 return h26x_vt_preset_name;
1566
1567 case HB_VCODEC_FFMPEG_MF_H264:
1568 case HB_VCODEC_FFMPEG_MF_H265:
1569 return h26x_mf_preset_name;
1570
1571 default:
1572 return NULL;
1573 }
1574 }
1575
hb_av_profile_get_names(int encoder)1576 const char* const* hb_av_profile_get_names(int encoder)
1577 {
1578 switch (encoder)
1579 {
1580 case HB_VCODEC_FFMPEG_NVENC_H264:
1581 return h264_nvenc_profile_names;
1582 case HB_VCODEC_FFMPEG_NVENC_H265:
1583 return h265_nvenc_profile_names;
1584 case HB_VCODEC_FFMPEG_VT_H264:
1585 return h264_vt_profile_name;
1586 case HB_VCODEC_FFMPEG_MF_H264:
1587 return h264_mf_profile_name;
1588 case HB_VCODEC_FFMPEG_MF_H265:
1589 return h265_mf_profile_name;
1590
1591 default:
1592 return NULL;
1593 }
1594 }
1595
hb_av_get_pix_fmts(int encoder)1596 const int* hb_av_get_pix_fmts(int encoder)
1597 {
1598 switch (encoder)
1599 {
1600 case HB_VCODEC_FFMPEG_VT_H264:
1601 case HB_VCODEC_FFMPEG_VT_H265:
1602 return h26x_vt_pix_fmts;
1603 case HB_VCODEC_FFMPEG_VT_H265_10BIT:
1604 return h265_10bit_vt_pix_fmts;
1605 case HB_VCODEC_FFMPEG_MF_H264:
1606 case HB_VCODEC_FFMPEG_MF_H265:
1607 return h26x_mf_pix_fmts;
1608
1609 default:
1610 return pix_fmts;
1611 }
1612 }
1613