WinGui: Fix another instance of the Caliburn vs Json.net sillyness where objects...
[HandBrake.git] / libhb / encavcodec.c
blob856fff31a9940f51e9698b8572b9db63bd0a7661
1 /* encavcodec.c
3 Copyright (c) 2003-2015 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 */
10 #include "hb.h"
11 #include "hb_dict.h"
12 #include "hbffmpeg.h"
15 * The frame info struct remembers information about each frame across calls
16 * to avcodec_encode_video. Since frames are uniquely identified by their
17 * frame number, we use this as an index.
19 * The size of the array is chosen so that two frames can't use the same
20 * slot during the encoder's max frame delay (set by the standard as 16
21 * frames) and so that, up to some minimum frame rate, frames are guaranteed
22 * to map to * different slots.
24 #define FRAME_INFO_SIZE 32
25 #define FRAME_INFO_MASK (FRAME_INFO_SIZE - 1)
27 struct hb_work_private_s
29 hb_job_t * job;
30 AVCodecContext * context;
31 FILE * file;
33 int frameno_in;
34 int frameno_out;
35 hb_buffer_t * delay_head;
36 hb_buffer_t * delay_tail;
38 int64_t dts_delay;
40 struct {
41 int64_t start;
42 int64_t duration;
43 } frame_info[FRAME_INFO_SIZE];
46 int encavcodecInit( hb_work_object_t *, hb_job_t * );
47 int encavcodecWork( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
48 void encavcodecClose( hb_work_object_t * );
50 hb_work_object_t hb_encavcodec =
52 WORK_ENCAVCODEC,
53 "FFMPEG encoder (libavcodec)",
54 encavcodecInit,
55 encavcodecWork,
56 encavcodecClose
59 int encavcodecInit( hb_work_object_t * w, hb_job_t * job )
61 AVCodec * codec;
62 AVCodecContext * context;
63 AVRational fps;
65 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
66 w->private_data = pv;
68 pv->job = job;
70 switch ( w->codec_param )
72 case AV_CODEC_ID_MPEG4:
74 hb_log("encavcodecInit: MPEG-4 ASP encoder");
75 } break;
76 case AV_CODEC_ID_MPEG2VIDEO:
78 hb_log("encavcodecInit: MPEG-2 encoder");
79 } break;
80 case AV_CODEC_ID_VP8:
82 hb_log("encavcodecInit: VP8 encoder");
83 } break;
84 default:
86 hb_error("encavcodecInit: unsupported encoder!");
87 return 1;
91 codec = avcodec_find_encoder( w->codec_param );
92 if( !codec )
94 hb_log( "encavcodecInit: avcodec_find_encoder "
95 "failed" );
96 return 1;
98 context = avcodec_alloc_context3( codec );
100 // Set things in context that we will allow the user to
101 // override with advanced settings.
102 if( job->pass_id == HB_PASS_ENCODE_2ND )
104 hb_interjob_t * interjob = hb_interjob_get( job->h );
105 fps.den = interjob->vrate.den;
106 fps.num = interjob->vrate.num;
108 else
110 fps.den = job->vrate.den;
111 fps.num = job->vrate.num;
114 // If the fps.num is 27000000, there's a good chance this is
115 // a standard rate that we have in our hb_video_rates table.
116 // Because of rounding errors and approximations made while
117 // measuring framerate, the actual value may not be exact. So
118 // we look for rates that are "close" and make an adjustment
119 // to fps.den.
120 if (fps.num == 27000000)
122 const hb_rate_t *video_framerate = NULL;
123 while ((video_framerate = hb_video_framerate_get_next(video_framerate)) != NULL)
125 if (abs(fps.den - video_framerate->rate) < 10)
127 fps.den = video_framerate->rate;
128 break;
132 hb_reduce(&fps.den, &fps.num, fps.den, fps.num);
134 // Check that the framerate is supported. If not, pick the closest.
135 // The mpeg2 codec only supports a specific list of frame rates.
136 if (codec->supported_framerates)
138 AVRational supported_fps;
139 supported_fps = codec->supported_framerates[av_find_nearest_q_idx(fps, codec->supported_framerates)];
140 if (supported_fps.num != fps.num || supported_fps.den != fps.den)
142 hb_log( "encavcodec: framerate %d / %d is not supported. Using %d / %d.",
143 fps.num, fps.den, supported_fps.num, supported_fps.den );
144 fps = supported_fps;
147 else if ((fps.num & ~0xFFFF) || (fps.den & ~0xFFFF))
149 // This may only be required for mpeg4 video. But since
150 // our only supported options are mpeg2 and mpeg4, there is
151 // no need to check codec type.
152 hb_log( "encavcodec: truncating framerate %d / %d",
153 fps.num, fps.den );
154 while ((fps.num & ~0xFFFF) || (fps.den & ~0xFFFF))
156 fps.num >>= 1;
157 fps.den >>= 1;
161 context->time_base.den = fps.num;
162 context->time_base.num = fps.den;
163 context->gop_size = 10 * ((double)job->vrate.num / job->vrate.den + 0.5);
165 /* place job->encoder_options in an hb_dict_t for convenience */
166 hb_dict_t * lavc_opts = NULL;
167 if (job->encoder_options != NULL && *job->encoder_options)
169 lavc_opts = hb_encopts_to_dict(job->encoder_options, job->vcodec);
171 /* iterate through lavc_opts and have avutil parse the options for us */
172 AVDictionary * av_opts = NULL;
173 hb_dict_iter_t iter;
174 for (iter = hb_dict_iter_init(lavc_opts);
175 iter != HB_DICT_ITER_DONE;
176 iter = hb_dict_iter_next(lavc_opts, iter))
178 const char *key = hb_dict_iter_key(iter);
179 hb_value_t *value = hb_dict_iter_value(iter);
180 char *str = hb_value_get_string_xform(value);
182 /* Here's where the strings are passed to avutil for parsing. */
183 av_dict_set( &av_opts, key, str, 0 );
184 free(str);
186 hb_dict_free( &lavc_opts );
188 // Now set the things in context that we don't want to allow
189 // the user to override.
190 if( job->vquality < 0.0 )
192 /* Average bitrate */
193 context->bit_rate = 1000 * job->vbitrate;
194 // ffmpeg's mpeg2 encoder requires that the bit_rate_tolerance be >=
195 // bitrate * fps
196 context->bit_rate_tolerance = context->bit_rate * av_q2d(fps) + 1;
198 else
200 /* Constant quantizer */
201 // These settings produce better image quality than
202 // what was previously used
203 context->flags |= CODEC_FLAG_QSCALE;
204 context->global_quality = FF_QP2LAMBDA * job->vquality + 0.5;
205 //Set constant quality for libvpx
206 if ( w->codec_param == AV_CODEC_ID_VP8 )
208 char quality[7];
209 snprintf(quality, 7, "%.2f", job->vquality);
210 av_dict_set( &av_opts, "crf", quality, 0 );
211 //Setting the deadline to good and cpu-used to 0
212 //causes the encoder to balance video quality and
213 //encode time, with a bias to video quality.
214 av_dict_set( &av_opts, "deadline", "good", 0);
215 av_dict_set( &av_opts, "cpu-used", "0", 0);
216 //This value was chosen to make the bitrate high enough
217 //for libvpx to "turn off" the maximum bitrate feature
218 //that is normally applied to constant quality.
219 context->bit_rate = job->width * job->height * fps.num / fps.den;
220 hb_log( "encavcodec: encoding at CQ %.2f", job->vquality );
222 else
224 hb_log( "encavcodec: encoding at constant quantizer %d",
225 context->global_quality );
228 context->width = job->width;
229 context->height = job->height;
230 context->pix_fmt = AV_PIX_FMT_YUV420P;
232 context->sample_aspect_ratio.num = job->par.num;
233 context->sample_aspect_ratio.den = job->par.den;
235 hb_log( "encavcodec: encoding with stored aspect %d/%d",
236 job->par.num, job->par.den );
238 if( job->mux & HB_MUX_MASK_MP4 )
240 context->flags |= CODEC_FLAG_GLOBAL_HEADER;
242 if( job->grayscale )
244 context->flags |= CODEC_FLAG_GRAY;
247 if( job->pass_id == HB_PASS_ENCODE_1ST ||
248 job->pass_id == HB_PASS_ENCODE_2ND )
250 char filename[1024]; memset( filename, 0, 1024 );
251 hb_get_tempory_filename( job->h, filename, "ffmpeg.log" );
253 if( job->pass_id == HB_PASS_ENCODE_1ST )
255 pv->file = hb_fopen(filename, "wb");
256 context->flags |= CODEC_FLAG_PASS1;
258 else
260 int size;
261 char * log;
263 pv->file = hb_fopen(filename, "rb");
264 fseek( pv->file, 0, SEEK_END );
265 size = ftell( pv->file );
266 fseek( pv->file, 0, SEEK_SET );
267 log = malloc( size + 1 );
268 log[size] = '\0';
269 fread( log, size, 1, pv->file );
270 fclose( pv->file );
271 pv->file = NULL;
273 context->flags |= CODEC_FLAG_PASS2;
274 context->stats_in = log;
278 if (hb_avcodec_open(context, codec, &av_opts, HB_FFMPEG_THREADS_AUTO))
280 hb_log( "encavcodecInit: avcodec_open failed" );
282 // avcodec_open populates the opts dictionary with the
283 // things it didn't recognize.
284 AVDictionaryEntry *t = NULL;
285 while( ( t = av_dict_get( av_opts, "", t, AV_DICT_IGNORE_SUFFIX ) ) )
287 hb_log( "encavcodecInit: Unknown avcodec option %s", t->key );
289 av_dict_free( &av_opts );
291 pv->context = context;
293 job->areBframes = 0;
294 if ( context->has_b_frames )
296 job->areBframes = 1;
298 if( ( job->mux & HB_MUX_MASK_MP4 ) && job->pass_id != HB_PASS_ENCODE_1ST )
300 w->config->mpeg4.length = context->extradata_size;
301 memcpy( w->config->mpeg4.bytes, context->extradata,
302 context->extradata_size );
305 return 0;
308 /***********************************************************************
309 * Close
310 ***********************************************************************
312 **********************************************************************/
313 void encavcodecClose( hb_work_object_t * w )
315 hb_work_private_t * pv = w->private_data;
317 if( pv->context && pv->context->codec )
319 hb_deep_log( 2, "encavcodec: closing libavcodec" );
320 avcodec_flush_buffers( pv->context );
321 hb_avcodec_close( pv->context );
322 av_free( pv->context );
324 if( pv->file )
326 fclose( pv->file );
328 free( pv );
329 w->private_data = NULL;
333 * see comments in definition of 'frame_info' in pv struct for description
334 * of what these routines are doing.
336 static void save_frame_info( hb_work_private_t * pv, hb_buffer_t * in )
338 int i = pv->frameno_in & FRAME_INFO_MASK;
339 pv->frame_info[i].start = in->s.start;
340 pv->frame_info[i].duration = in->s.stop - in->s.start;
343 static int64_t get_frame_start( hb_work_private_t * pv, int64_t frameno )
345 int i = frameno & FRAME_INFO_MASK;
346 return pv->frame_info[i].start;
349 static int64_t get_frame_duration( hb_work_private_t * pv, int64_t frameno )
351 int i = frameno & FRAME_INFO_MASK;
352 return pv->frame_info[i].duration;
355 static void compute_dts_offset( hb_work_private_t * pv, hb_buffer_t * buf )
357 if ( pv->job->areBframes )
359 if ( ( pv->frameno_in ) == pv->job->areBframes )
361 pv->dts_delay = buf->s.start;
362 pv->job->config.h264.init_delay = pv->dts_delay;
367 static uint8_t convert_pict_type( int pict_type, char pkt_flag_key, uint16_t* sflags )
369 uint8_t retval = 0;
370 switch ( pict_type )
372 case AV_PICTURE_TYPE_P:
374 retval = HB_FRAME_P;
375 } break;
377 case AV_PICTURE_TYPE_B:
379 retval = HB_FRAME_B;
380 } break;
382 case AV_PICTURE_TYPE_S:
384 retval = HB_FRAME_P;
385 } break;
387 case AV_PICTURE_TYPE_SP:
389 retval = HB_FRAME_P;
390 } break;
392 case AV_PICTURE_TYPE_BI:
393 case AV_PICTURE_TYPE_SI:
394 case AV_PICTURE_TYPE_I:
396 *sflags |= HB_FRAME_REF;
397 if ( pkt_flag_key )
399 retval = HB_FRAME_IDR;
401 else
403 retval = HB_FRAME_I;
405 } break;
407 default:
409 if ( pkt_flag_key )
411 //buf->s.flags |= HB_FRAME_REF;
412 *sflags |= HB_FRAME_REF;
413 retval = HB_FRAME_KEY;
415 else
417 retval = HB_FRAME_REF;
419 } break;
421 return retval;
424 // Generate DTS by rearranging PTS in this sequence:
425 // pts0 - delay, pts1 - delay, pts2 - delay, pts1, pts2, pts3...
427 // Where pts0 - ptsN are in decoded monotonically increasing presentation
428 // order and delay == pts1 (1 being the number of frames the decoder must
429 // delay before it has suffecient information to decode). The number of
430 // frames to delay is set by job->areBframes, so it is configurable.
431 // This guarantees that DTS <= PTS for any frame.
433 // This is similar to how x264 generates DTS
434 static hb_buffer_t * process_delay_list( hb_work_private_t * pv, hb_buffer_t * buf )
436 if ( pv->job->areBframes )
438 // Has dts_delay been set yet?
439 if ( pv->frameno_in <= pv->job->areBframes )
441 // dts_delay not yet set. queue up buffers till it is set.
442 if ( pv->delay_tail == NULL )
444 pv->delay_head = pv->delay_tail = buf;
446 else
448 pv->delay_tail->next = buf;
449 pv->delay_tail = buf;
451 return NULL;
454 // We have dts_delay. Apply it to any queued buffers renderOffset
455 // and return all queued buffers.
456 if ( pv->delay_tail == NULL && buf != NULL )
458 // Use the cached frame info to get the start time of Nth frame
459 // Note that start Nth frame != start time this buffer since the
460 // output buffers have rearranged start times.
461 if (pv->frameno_out < pv->job->areBframes)
463 int64_t start = get_frame_start( pv, pv->frameno_out );
464 buf->s.renderOffset = start - pv->dts_delay;
466 else
468 buf->s.renderOffset =
469 get_frame_start(pv, pv->frameno_out - pv->job->areBframes);
471 pv->frameno_out++;
472 return buf;
474 else
476 pv->delay_tail->next = buf;
477 buf = pv->delay_head;
478 while ( buf )
480 // Use the cached frame info to get the start time of Nth frame
481 // Note that start Nth frame != start time this buffer since the
482 // output buffers have rearranged start times.
483 if (pv->frameno_out < pv->job->areBframes)
485 int64_t start = get_frame_start( pv, pv->frameno_out );
486 buf->s.renderOffset = start - pv->dts_delay;
488 else
490 buf->s.renderOffset = get_frame_start(pv,
491 pv->frameno_out - pv->job->areBframes);
493 buf = buf->next;
494 pv->frameno_out++;
496 buf = pv->delay_head;
497 pv->delay_head = pv->delay_tail = NULL;
498 return buf;
501 else if ( buf )
503 buf->s.renderOffset = buf->s.start;
504 return buf;
506 return NULL;
509 /***********************************************************************
510 * Work
511 ***********************************************************************
513 **********************************************************************/
514 int encavcodecWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
515 hb_buffer_t ** buf_out )
517 hb_work_private_t * pv = w->private_data;
518 hb_job_t * job = pv->job;
519 AVFrame * frame;
520 hb_buffer_t * in = *buf_in, * buf;
521 char final_flushing_call = !!(in->s.flags & HB_BUF_FLAG_EOF);
522 if ( final_flushing_call )
524 //make a flushing call to encode for codecs that can encode out of order
525 /* EOF on input - send it downstream & say we're done */
526 *buf_in = NULL;
527 frame = NULL;
529 else
531 frame = av_frame_alloc();
532 frame->data[0] = in->plane[0].data;
533 frame->data[1] = in->plane[1].data;
534 frame->data[2] = in->plane[2].data;
535 frame->linesize[0] = in->plane[0].stride;
536 frame->linesize[1] = in->plane[1].stride;
537 frame->linesize[2] = in->plane[2].stride;
539 // For constant quality, setting the quality in AVCodecContext
540 // doesn't do the trick. It must be set in the AVFrame.
541 frame->quality = pv->context->global_quality;
543 // Remember info about this frame that we need to pass across
544 // the avcodec_encode_video call (since it reorders frames).
545 save_frame_info( pv, in );
546 compute_dts_offset( pv, in );
548 // Bizarro ffmpeg appears to require the input AVFrame.pts to be
549 // set to a frame number. Setting it to an actual pts causes
550 // jerky video.
551 // frame->pts = in->s.start;
552 frame->pts = pv->frameno_in++;
555 if ( pv->context->codec )
557 int ret;
558 AVPacket pkt;
559 int got_packet;
560 char still_flushing = final_flushing_call;
561 hb_buffer_t* buf_head = NULL;
562 hb_buffer_t* buf_last = NULL;
566 av_init_packet(&pkt);
567 /* Should be way too large */
568 buf = hb_video_buffer_init(job->width, job->height);
569 pkt.data = buf->data;
570 pkt.size = buf->alloc;
572 ret = avcodec_encode_video2( pv->context, &pkt, frame, &got_packet );
573 if ( ret < 0 || pkt.size <= 0 || !got_packet )
575 hb_buffer_close( &buf );
576 still_flushing = 0;
578 else
580 int64_t frameno = pkt.pts;
581 buf->size = pkt.size;
582 buf->s.start = get_frame_start( pv, frameno );
583 buf->s.duration = get_frame_duration( pv, frameno );
584 buf->s.stop = buf->s.stop + buf->s.duration;
585 buf->s.flags &= ~HB_FRAME_REF;
586 buf->s.frametype = convert_pict_type( pv->context->coded_frame->pict_type, pkt.flags & AV_PKT_FLAG_KEY, &buf->s.flags );
587 buf = process_delay_list( pv, buf );
589 if (buf_head == NULL)
591 buf_head = buf;
593 else
595 buf_last->next = buf;
597 buf_last = buf;
599 /* Write stats */
600 if (job->pass_id == HB_PASS_ENCODE_1ST &&
601 pv->context->stats_out != NULL)
603 fprintf( pv->file, "%s", pv->context->stats_out );
605 } while (still_flushing);
606 if (buf_last != NULL && final_flushing_call)
608 buf_last->next = in;
609 buf = buf_head;
611 else if (final_flushing_call)
613 buf = in;
616 else
618 buf = NULL;
620 hb_error( "encavcodec: codec context has uninitialized codec; skipping frame" );
623 av_frame_free( &frame );
625 *buf_out = buf;
627 return final_flushing_call? HB_WORK_DONE : HB_WORK_OK;