1 /* $Id: encx264.c,v 1.21 2005/11/04 13:09:41 titer Exp $
3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.fr/>.
5 It may be used under the terms of the GNU General Public License. */
13 int encx264Init( hb_work_object_t *, hb_job_t * );
14 int encx264Work( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
15 void encx264Close( hb_work_object_t * );
17 hb_work_object_t hb_encx264 =
20 "H.264/AVC encoder (libx264)",
26 #define DTS_BUFFER_SIZE 32
29 * The frame info struct remembers information about each frame across calls
30 * to x264_encoder_encode. Since frames are uniquely identified by their
31 * timestamp, we use some bits of the timestamp as an index. The LSB is
32 * chosen so that two successive frames will have different values in the
33 * bits over any plausible range of frame rates. (Starting with bit 8 allows
34 * any frame rate slower than 352fps.) The MSB determines the size of the array.
35 * It is chosen so that two frames can't use the same slot during the
36 * encoder's max frame delay (set by the standard as 16 frames) and so
37 * that, up to some minimum frame rate, frames are guaranteed to map to
38 * different slots. (An MSB of 17 which is 2^(17-8+1) = 1024 slots guarantees
39 * no collisions down to a rate of .7 fps).
41 #define FRAME_INFO_MAX2 (8) // 2^8 = 256; 90000/256 = 352 frames/sec
42 #define FRAME_INFO_MIN2 (17) // 2^17 = 128K; 90000/131072 = 1.4 frames/sec
43 #define FRAME_INFO_SIZE (1 << (FRAME_INFO_MIN2 - FRAME_INFO_MAX2 + 1))
44 #define FRAME_INFO_MASK (FRAME_INFO_SIZE - 1)
46 struct hb_work_private_s
50 x264_picture_t pic_in;
51 uint8_t *x264_allocated_pic;
55 uint32_t frames_split; // number of frames we had to split
56 int chap_mark; // saved chap mark when we're propagating it
57 int64_t last_stop; // Debugging - stop time of previous input frame
62 } frame_info[FRAME_INFO_SIZE];
67 /***********************************************************************
68 * hb_work_encx264_init
69 ***********************************************************************
71 **********************************************************************/
72 int encx264Init( hb_work_object_t * w, hb_job_t * job )
78 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
83 memset( pv->filename, 0, 1024 );
84 hb_get_tempory_filename( job->h, pv->filename, "x264.log" );
86 x264_param_default( ¶m );
88 /* Default weightp to off for baseline,
89 overridable through x264 option strings. */
90 if( job->x264opts != NULL && *job->x264opts != '\0' )
92 char *x264opts, *x264opts_start;
94 x264opts = x264opts_start = strdup(job->x264opts);
96 while( x264opts_start && *x264opts )
98 char *name = x264opts;
101 x264opts += strcspn( x264opts, ":" );
108 value = strchr( name, '=' );
115 if( !( strcmp( name, "bframes" ) ) )
117 if( atoi( value ) == 0 )
119 param.analyse.i_weighted_pred = X264_WEIGHTP_NONE;
120 hb_log("encx264: no bframes, disabling weight-p unless told otherwise");
127 param.analyse.b_psnr = 1;
128 param.analyse.b_ssim = 1;
130 param.i_threads = ( hb_get_cpu_count() * 3 / 2 );
131 param.i_width = job->width;
132 param.i_height = job->height;
133 param.i_fps_num = job->vrate;
134 param.i_fps_den = job->vrate_base;
137 param.i_timebase_num = 0;
138 param.i_timebase_den = 0;
139 param.b_vfr_input = 0;
143 param.i_timebase_num = 1;
144 param.i_timebase_den = 90000;
147 /* Disable annexb. Inserts size into nal header instead of start code */
150 /* Set min:max key intervals ratio to 1:10 of fps.
151 * This section is skipped if fps=25 (default).
153 if (job->vrate_base != 1080000)
155 if (job->pass == 2 && !job->cfr )
157 /* Even though the framerate might be different due to VFR,
158 we still want the same keyframe intervals as the 1st pass,
159 so the 1st pass stats won't conflict on frame decisions. */
160 hb_interjob_t * interjob = hb_interjob_get( job->h );
161 param.i_keyint_max = ( ( 10 * (double)interjob->vrate / (double)interjob->vrate_base ) + 0.5 );
165 /* adjust +0.5 for when fps has remainder to bump
166 { 23.976, 29.976, 59.94 } to { 24, 30, 60 } */
167 param.i_keyint_max = ( ( 10 * (double)job->vrate / (double)job->vrate_base ) + 0.5 );
171 param.i_log_level = X264_LOG_INFO;
172 if( job->h264_level )
175 param.i_level_idc = job->h264_level;
176 hb_log( "encx264: encoding at level %i",
181 This section passes the string x264opts to libx264 for parsing into
182 parameter names and values.
184 The string is set up like this:
185 option1=value1:option2=value 2
187 So, you have to iterate through based on the colons, and then put
188 the left side of the equals sign in "name" and the right side into
189 "value." Then you hand those strings off to x264 for interpretation.
191 This is all based on the universal x264 option handling Loren
192 Merritt implemented in the Mplayer/Mencoder project.
195 if( job->x264opts != NULL && *job->x264opts != '\0' )
197 char *x264opts, *x264opts_start;
199 x264opts = x264opts_start = strdup(job->x264opts);
201 while( x264opts_start && *x264opts )
203 char *name = x264opts;
207 x264opts += strcspn( x264opts, ":" );
214 value = strchr( name, '=' );
221 /* Here's where the strings are passed to libx264 for parsing. */
222 ret = x264_param_parse( ¶m, name, value );
224 /* Let x264 sanity check the options for us*/
225 if( ret == X264_PARAM_BAD_NAME )
226 hb_log( "x264 options: Unknown suboption %s", name );
227 if( ret == X264_PARAM_BAD_VALUE )
228 hb_log( "x264 options: Bad argument %s=%s", name, value ? value : "(null)" );
230 free(x264opts_start);
233 /* B-frames are on by default.*/
236 if( param.i_bframe && param.i_bframe_pyramid )
238 /* Note b-pyramid here, so the initial delay can be doubled */
241 else if( !param.i_bframe )
244 When B-frames are enabled, the max frame count increments
245 by 1 (regardless of the number of B-frames). If you don't
246 change the duration of the video track when you mux, libmp4
247 barfs. So, check if the x264opts aren't using B-frames, and
248 when they aren't, set the boolean job->areBframes as false.
253 if( param.i_keyint_min != X264_KEYINT_MIN_AUTO || param.i_keyint_max != 250 )
254 hb_log("encx264: min-keyint: %i, keyint: %i", param.i_keyint_min == X264_KEYINT_MIN_AUTO ? param.i_keyint_max / 10 : param.i_keyint_min,
257 /* set up the VUI color model & gamma to match what the COLR atom
258 * set in muxmp4.c says. See libhb/muxmp4.c for notes. */
259 if( job->color_matrix == 1 )
261 // ITU BT.601 DVD or SD TV content
262 param.vui.i_colorprim = 6;
263 param.vui.i_transfer = 1;
264 param.vui.i_colmatrix = 6;
266 else if( job->color_matrix == 2 )
268 // ITU BT.709 HD content
269 param.vui.i_colorprim = 1;
270 param.vui.i_transfer = 1;
271 param.vui.i_colmatrix = 1;
273 else if ( job->title->width >= 1280 || job->title->height >= 720 )
275 // we guess that 720p or above is ITU BT.709 HD content
276 param.vui.i_colorprim = 1;
277 param.vui.i_transfer = 1;
278 param.vui.i_colmatrix = 1;
282 // ITU BT.601 DVD or SD TV content
283 param.vui.i_colorprim = 6;
284 param.vui.i_transfer = 1;
285 param.vui.i_colmatrix = 6;
288 if( job->anamorphic.mode )
290 param.vui.i_sar_width = job->anamorphic.par_width;
291 param.vui.i_sar_height = job->anamorphic.par_height;
293 hb_log( "encx264: encoding with stored aspect %d/%d",
294 param.vui.i_sar_width, param.vui.i_sar_height );
298 if( job->vquality > 0.0 && job->vquality < 1.0 )
301 param.rc.i_rc_method = X264_RC_CRF;
302 param.rc.f_rf_constant = 51 - job->vquality * 51;
303 hb_log( "encx264: Encoding at constant RF %f", param.rc.f_rf_constant );
305 else if( job->vquality == 0 || job->vquality >= 1.0 )
307 /* Use the vquality as a raw RF or QP
308 instead of treating it like a percentage. */
310 param.rc.i_rc_method = X264_RC_CRF;
311 param.rc.f_rf_constant = job->vquality;
312 hb_log( "encx264: Encoding at constant RF %f", param.rc.f_rf_constant );
317 param.rc.i_rc_method = X264_RC_ABR;
318 param.rc.i_bitrate = job->vbitrate;
322 param.rc.b_stat_write = 1;
323 param.rc.psz_stat_out = pv->filename;
326 param.rc.b_stat_read = 1;
327 param.rc.psz_stat_in = pv->filename;
332 hb_deep_log( 2, "encx264: opening libx264 (pass %d)", job->pass );
333 pv->x264 = x264_encoder_open( ¶m );
335 x264_encoder_headers( pv->x264, &nal, &nal_count );
337 /* Sequence Parameter Set */
338 memcpy(w->config->h264.sps, nal[0].p_payload + 4, nal[0].i_payload - 4);
339 w->config->h264.sps_length = nal[0].i_payload - 4;
341 /* Picture Parameter Set */
342 memcpy(w->config->h264.pps, nal[1].p_payload + 4, nal[1].i_payload - 4);
343 w->config->h264.pps_length = nal[1].i_payload - 4;
345 x264_picture_alloc( &pv->pic_in, X264_CSP_I420,
346 job->width, job->height );
348 pv->pic_in.img.i_stride[2] = pv->pic_in.img.i_stride[1] = ( ( job->width + 1 ) >> 1 );
349 pv->x264_allocated_pic = pv->pic_in.img.plane[0];
354 void encx264Close( hb_work_object_t * w )
356 hb_work_private_t * pv = w->private_data;
358 if ( pv->frames_split )
360 hb_log( "encx264: %u frames had to be split (%u in, %u out)",
361 pv->frames_split, pv->frames_in, pv->frames_out );
364 * Patch the x264 allocated data back in so that x264 can free it
365 * we have been using our own buffers during the encode to avoid copying.
367 pv->pic_in.img.plane[0] = pv->x264_allocated_pic;
368 x264_picture_clean( &pv->pic_in );
369 x264_encoder_close( pv->x264 );
371 w->private_data = NULL;
377 * see comments in definition of 'frame_info' in pv struct for description
378 * of what these routines are doing.
380 static void save_frame_info( hb_work_private_t * pv, hb_buffer_t * in )
382 int i = (in->start >> FRAME_INFO_MAX2) & FRAME_INFO_MASK;
383 pv->frame_info[i].duration = in->stop - in->start;
386 static int64_t get_frame_duration( hb_work_private_t * pv, int64_t pts )
388 int i = (pts >> FRAME_INFO_MAX2) & FRAME_INFO_MASK;
389 return pv->frame_info[i].duration;
392 static hb_buffer_t *nal_encode( hb_work_object_t *w, x264_picture_t *pic_out,
393 int i_nal, x264_nal_t *nal )
395 hb_buffer_t *buf = NULL;
396 hb_work_private_t *pv = w->private_data;
397 hb_job_t *job = pv->job;
399 /* Should be way too large */
400 buf = hb_video_buffer_init( job->width, job->height );
404 // use the pts to get the original frame's duration.
405 int64_t duration = get_frame_duration( pv, pic_out->i_pts );
406 buf->start = pic_out->i_pts;
407 buf->stop = pic_out->i_pts + duration;
408 buf->renderOffset = pic_out->i_dts;
409 if ( !w->config->h264.init_delay && pic_out->i_dts < 0 )
411 w->config->h264.init_delay = -pic_out->i_dts;
414 /* Encode all the NALs we were given into buf.
415 NOTE: This code assumes one video frame per NAL (but there can
416 be other stuff like SPS and/or PPS). If there are multiple
417 frames we only get the duration of the first which will
418 eventually screw up the muxer & decoder. */
420 for( i = 0; i < i_nal; i++ )
422 int size = nal[i].i_payload;
423 memcpy(buf->data + buf->size, nal[i].p_payload, size);
429 if( job->mux & HB_MUX_AVI )
431 if( nal[i].i_ref_idc == NAL_PRIORITY_HIGHEST )
433 buf->frametype = HB_FRAME_KEY;
439 /* H.264 in .mp4 or .mkv */
440 switch( nal[i].i_type )
442 /* Sequence Parameter Set & Program Parameter Set go in the
443 * mp4 header so skip them here
456 /* Decide what type of frame we have. */
457 switch( pic_out->i_type )
460 buf->frametype = HB_FRAME_IDR;
461 /* if we have a chapter marker pending and this
462 frame's presentation time stamp is at or after
463 the marker's time stamp, use this as the
465 if( pv->next_chap != 0 && pv->next_chap <= pic_out->i_pts )
468 buf->new_chap = pv->chap_mark;
473 buf->frametype = HB_FRAME_I;
477 buf->frametype = HB_FRAME_P;
481 buf->frametype = HB_FRAME_B;
484 /* This is for b-pyramid, which has reference b-frames
485 However, it doesn't seem to ever be used... */
487 buf->frametype = HB_FRAME_BREF;
490 // If it isn't the above, what type of frame is it??
496 /* Since libx264 doesn't tell us when b-frames are
497 themselves reference frames, figure it out on our own. */
498 if( (buf->frametype == HB_FRAME_B) &&
499 (nal[i].i_ref_idc != NAL_PRIORITY_DISPOSABLE) )
500 buf->frametype = HB_FRAME_BREF;
502 /* Expose disposable bit to muxer. */
503 if( nal[i].i_ref_idc == NAL_PRIORITY_DISPOSABLE )
504 buf->flags &= ~HB_FRAME_REF;
506 buf->flags |= HB_FRAME_REF;
510 // make sure we found at least one video frame
511 if ( buf->size <= 0 )
513 // no video - discard the buf
514 hb_buffer_close( &buf );
519 static hb_buffer_t *x264_encode( hb_work_object_t *w, hb_buffer_t *in )
521 hb_work_private_t *pv = w->private_data;
522 hb_job_t *job = pv->job;
524 /* Point x264 at our current buffers Y(UV) data. */
525 pv->pic_in.img.plane[0] = in->data;
527 int uvsize = ( (job->width + 1) >> 1 ) * ( (job->height + 1) >> 1 );
530 /* XXX x264 has currently no option for grayscale encoding */
531 memset( pv->pic_in.img.plane[1], 0x80, uvsize );
532 memset( pv->pic_in.img.plane[2], 0x80, uvsize );
536 /* Point x264 at our buffers (Y)UV data */
537 pv->pic_in.img.plane[1] = in->data + job->width * job->height;
538 pv->pic_in.img.plane[2] = pv->pic_in.img.plane[1] + uvsize;
540 if( in->new_chap && job->chapter_markers )
542 /* chapters have to start with an IDR frame so request that this
543 frame be coded as IDR. Since there may be up to 16 frames
544 currently buffered in the encoder remember the timestamp so
545 when this frame finally pops out of the encoder we'll mark
546 its buffer as the start of a chapter. */
547 pv->pic_in.i_type = X264_TYPE_IDR;
548 if( pv->next_chap == 0 )
550 pv->next_chap = in->start;
551 pv->chap_mark = in->new_chap;
553 /* don't let 'work_loop' put a chapter mark on the wrong buffer */
558 pv->pic_in.i_type = X264_TYPE_AUTO;
560 pv->pic_in.i_qpplus1 = 0;
562 /* XXX this is temporary debugging code to check that the upstream
563 * modules (render & sync) have generated a continuous, self-consistent
564 * frame stream with the current frame's start time equal to the
565 * previous frame's stop time.
567 if( pv->last_stop != in->start )
569 hb_log("encx264 input continuity err: last stop %"PRId64" start %"PRId64,
570 pv->last_stop, in->start);
572 pv->last_stop = in->stop;
574 // Remember info about this frame that we need to pass across
575 // the x264_encoder_encode call (since it reorders frames).
576 save_frame_info( pv, in );
578 /* Feed the input PTS to x264 so it can figure out proper output PTS */
579 pv->pic_in.i_pts = in->start;
581 x264_picture_t pic_out;
585 x264_encoder_encode( pv->x264, &nal, &i_nal, &pv->pic_in, &pic_out );
588 return nal_encode( w, &pic_out, i_nal, nal );
593 int encx264Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
594 hb_buffer_t ** buf_out )
596 hb_work_private_t *pv = w->private_data;
597 hb_buffer_t *in = *buf_in;
603 // EOF on input. Flush any frames still in the decoder then
604 // send the eof downstream to tell the muxer we're done.
605 x264_picture_t pic_out;
608 hb_buffer_t *last_buf = NULL;
610 while ( x264_encoder_delayed_frames( pv->x264 ) )
612 x264_encoder_encode( pv->x264, &nal, &i_nal, NULL, &pic_out );
618 hb_buffer_t *buf = nal_encode( w, &pic_out, i_nal, nal );
622 if ( last_buf == NULL )
625 last_buf->next = buf;
629 // Flushed everything - add the eof to the end of the chain.
630 if ( last_buf == NULL )
639 // Not EOF - encode the packet & wrap it in a NAL
642 *buf_out = x264_encode( w, in );