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.m0k.org/>.
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
28 struct hb_work_private_s
32 x264_picture_t pic_in;
33 uint8_t *x264_allocated_pic;
35 // Internal queue of DTS start/stop values.
36 int64_t dts_start[DTS_BUFFER_SIZE];
37 int64_t dts_stop[DTS_BUFFER_SIZE];
40 int64_t dts_write_index;
41 int64_t dts_read_index;
47 /***********************************************************************
48 * hb_work_encx264_init
49 ***********************************************************************
51 **********************************************************************/
52 int encx264Init( hb_work_object_t * w, hb_job_t * job )
58 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
63 memset( pv->filename, 0, 1024 );
64 hb_get_tempory_filename( job->h, pv->filename, "x264.log" );
66 x264_param_default( ¶m );
68 param.i_threads = ( hb_get_cpu_count() * 3 / 2 );
69 param.i_width = job->width;
70 param.i_height = job->height;
71 param.i_fps_num = job->vrate;
72 param.i_fps_den = job->vrate_base;
74 if (job->vrate_base != 1080000)
76 /* If the fps isn't 25, adjust the key intervals. Add 1 because
77 we want 24, not 23 with a truncated remainder. */
78 param.i_keyint_min = (job->vrate / job->vrate_base) + 1;
79 param.i_keyint_max = (10 * job->vrate / job->vrate_base) + 1;
80 hb_log("encx264: keyint-min: %i, keyint-max: %i", param.i_keyint_min, param.i_keyint_max);
83 param.i_log_level = X264_LOG_INFO;
87 param.i_level_idc = job->h264_level;
88 hb_log( "encx264: encoding at level %i",
92 /* Slightly faster with minimal quality lost */
93 param.analyse.i_subpel_refine = 4;
96 This section passes the string x264opts to libx264 for parsing into
97 parameter names and values.
99 The string is set up like this:
100 option1=value1:option2=value 2
102 So, you have to iterate through based on the colons, and then put
103 the left side of the equals sign in "name" and the right side into
104 "value." Then you hand those strings off to x264 for interpretation.
106 This is all based on the universal x264 option handling Loren
107 Merritt implemented in the Mplayer/Mencoder project.
110 if( job->x264opts != NULL && *job->x264opts != '\0' )
112 char *x264opts, *x264opts_start;
114 x264opts = x264opts_start = strdup(job->x264opts);
116 while( x264opts_start && *x264opts )
118 char *name = x264opts;
122 x264opts += strcspn( x264opts, ":" );
129 value = strchr( name, '=' );
137 When B-frames are enabled, the max frame count increments
138 by 1 (regardless of the number of B-frames). If you don't
139 change the duration of the video track when you mux, libmp4
140 barfs. So, check if the x264opts are using B-frames, and
141 when they are, set the boolean job->areBframes as true.
144 if( !( strcmp( name, "bframes" ) ) )
146 if( atoi( value ) > 0 )
152 /* Note b-pyramid here, so the initial delay can be doubled */
153 if( !( strcmp( name, "b-pyramid" ) ) )
157 if( atoi( value ) > 0 )
168 /* Here's where the strings are passed to libx264 for parsing. */
169 ret = x264_param_parse( ¶m, name, value );
171 /* Let x264 sanity check the options for us*/
172 if( ret == X264_PARAM_BAD_NAME )
173 hb_log( "x264 options: Unknown suboption %s", name );
174 if( ret == X264_PARAM_BAD_VALUE )
175 hb_log( "x264 options: Bad argument %s=%s", name, value ? value : "(null)" );
177 free(x264opts_start);
181 if( job->pixel_ratio )
183 param.vui.i_sar_width = job->pixel_aspect_width;
184 param.vui.i_sar_height = job->pixel_aspect_height;
186 hb_log( "encx264: encoding with stored aspect %d/%d",
187 param.vui.i_sar_width, param.vui.i_sar_height );
191 if( job->vquality >= 0.0 && job->vquality <= 1.0 )
197 param.rc.i_rc_method = X264_RC_CRF;
198 param.rc.f_rf_constant = 51 - job->vquality * 51;
199 hb_log( "encx264: Encoding at constant RF %f",
200 param.rc.f_rf_constant );
205 param.rc.i_rc_method = X264_RC_CQP;
206 param.rc.i_qp_constant = 51 - job->vquality * 51;
207 hb_log( "encx264: encoding at constant QP %d",
208 param.rc.i_qp_constant );
215 param.rc.i_rc_method = X264_RC_ABR;
216 param.rc.i_bitrate = job->vbitrate;
220 param.rc.b_stat_write = 1;
221 param.rc.psz_stat_out = pv->filename;
224 param.rc.b_stat_read = 1;
225 param.rc.psz_stat_in = pv->filename;
230 hb_log( "encx264: opening libx264 (pass %d)", job->pass );
231 pv->x264 = x264_encoder_open( ¶m );
233 x264_encoder_headers( pv->x264, &nal, &nal_count );
235 /* Sequence Parameter Set */
236 w->config->h264.sps_length = 1 + nal[1].i_payload;
237 w->config->h264.sps[0] = 0x67;
238 memcpy( &w->config->h264.sps[1], nal[1].p_payload, nal[1].i_payload );
240 /* Picture Parameter Set */
241 w->config->h264.pps_length = 1 + nal[2].i_payload;
242 w->config->h264.pps[0] = 0x68;
243 memcpy( &w->config->h264.pps[1], nal[2].p_payload, nal[2].i_payload );
245 x264_picture_alloc( &pv->pic_in, X264_CSP_I420,
246 job->width, job->height );
248 pv->x264_allocated_pic = pv->pic_in.img.plane[0];
250 pv->dts_write_index = 0;
251 pv->dts_read_index = 0;
256 /* Basic initDelay value is the clockrate divided by the FPS
257 -- the length of one frame in clockticks. */
258 pv->init_delay = (float)90000 / (float)((float)job->vrate / (float)job->vrate_base);
260 /* 23.976-length frames are 3753.75 ticks long. That means 25%
261 will come out as 3753, 75% will be 3754. The delay has to be
262 the longest possible frame duration, 3754. However, 3753.75
263 gets truncated to 3753, so if that's what it is, ++ it. */
264 if (pv->init_delay == 3753)
267 /* For VFR, libhb sees the FPS as 29.97, but the longest frames
268 will use the duration of frames running at 23.976fps instead.. */
271 pv->init_delay = 3754;
274 /* The delay is 2 frames for regular b-frames, 3 for b-pyramid.
275 Since job->areBframes is 1 for b-frames and 2 for b-pyramid,
276 add one to it and use it as a multiplier. */
277 pv->init_delay *= ( job->areBframes + 1);
283 void encx264Close( hb_work_object_t * w )
285 hb_work_private_t * pv = w->private_data;
287 * Patch the x264 allocated data back in so that x264 can free it
288 * we have been using our own buffers during the encode to avoid copying.
290 pv->pic_in.img.plane[0] = pv->x264_allocated_pic;
291 x264_picture_clean( &pv->pic_in );
292 x264_encoder_close( pv->x264 );
294 w->private_data = NULL;
299 int encx264Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
300 hb_buffer_t ** buf_out )
302 hb_work_private_t * pv = w->private_data;
303 hb_job_t * job = pv->job;
304 hb_buffer_t * in = *buf_in, * buf;
305 x264_picture_t pic_out;
313 * Point x264 at our current buffers Y(UV) data.
315 pv->pic_in.img.plane[0] = in->data;
319 /* XXX x264 has currently no option for grayscale encoding */
320 memset( pv->pic_in.img.plane[1], 0x80, job->width * job->height / 4 );
321 memset( pv->pic_in.img.plane[2], 0x80, job->width * job->height / 4 );
326 * Point x264 at our buffers (Y)UV data
328 pv->pic_in.img.plane[1] = in->data + job->width * job->height;
329 pv->pic_in.img.plane[2] = in->data + 5 * job->width *
333 if( in->new_chap && job->chapter_markers )
335 /* chapters have to start with an IDR frame so request that this
336 frame be coded as IDR. Since there may be up to 16 frames
337 currently buffered in the encoder remember the timestamp so
338 when this frame finally pops out of the encoder we'll mark
339 its buffer as the start of a chapter. */
340 pv->pic_in.i_type = X264_TYPE_IDR;
341 if( pv->next_chap == 0 )
343 pv->next_chap = in->start;
345 /* don't let 'work_loop' put a chapter mark on the wrong buffer */
350 pv->pic_in.i_type = X264_TYPE_AUTO;
352 pv->pic_in.i_qpplus1 = 0;
354 // Remember current PTS value, use as DTS later
355 pv->dts_start[pv->dts_write_index & (DTS_BUFFER_SIZE-1)] = in->start;
356 pv->dts_stop[pv->dts_write_index & (DTS_BUFFER_SIZE-1)] = in->stop;
357 pv->dts_write_index++;
359 /* Feed the input DTS to x264 so it can figure out proper output PTS */
360 pv->pic_in.i_pts = in->start;
362 x264_encoder_encode( pv->x264, &nal, &i_nal,
363 &pv->pic_in, &pic_out );
367 x264_encoder_encode( pv->x264, &nal, &i_nal,
369 /* No more delayed B frames */
377 /* Since we output at least one more frame, drop another empty
378 one onto our input fifo. We'll keep doing this automatically
379 until we stop getting frames out of the encoder. */
380 hb_fifo_push(w->fifo_in, hb_buffer_init(0));
386 /* Should be way too large */
387 buf = hb_buffer_init( 3 * job->width * job->height / 2 );
389 buf->start = in->start;
390 buf->stop = in->stop;
393 int64_t dts_start, dts_stop;
395 /* Get next DTS value to use */
396 dts_start = pv->dts_start[pv->dts_read_index & (DTS_BUFFER_SIZE-1)];
397 dts_stop = pv->dts_stop[pv->dts_read_index & (DTS_BUFFER_SIZE-1)];
398 pv->dts_read_index++;
400 for( i = 0; i < i_nal; i++ )
404 data = buf->alloc - buf->size;
405 if( ( size = x264_nal_encode( buf->data + buf->size, &data,
411 if( job->mux & HB_MUX_AVI )
413 if( nal[i].i_ref_idc == NAL_PRIORITY_HIGHEST )
415 buf->frametype = HB_FRAME_KEY;
422 switch( buf->data[buf->size+4] & 0x1f )
430 /* H.264 in mp4 (stolen from mp4creator) */
431 buf->data[buf->size+0] = ( ( size - 4 ) >> 24 ) & 0xFF;
432 buf->data[buf->size+1] = ( ( size - 4 ) >> 16 ) & 0xFF;
433 buf->data[buf->size+2] = ( ( size - 4 ) >> 8 ) & 0xFF;
434 buf->data[buf->size+3] = ( ( size - 4 ) >> 0 ) & 0xFF;
435 switch( pic_out.i_type )
437 /* Decide what type of frame we have. */
439 buf->frametype = HB_FRAME_IDR;
440 /* if we have a chapter marker pending and this
441 frame's presentation time stamp is at or after
442 the marker's time stamp, use this as the
444 if( pv->next_chap != 0 && pv->next_chap <= pic_out.i_pts )
451 buf->frametype = HB_FRAME_I;
454 buf->frametype = HB_FRAME_P;
457 buf->frametype = HB_FRAME_B;
459 /* This is for b-pyramid, which has reference b-frames
460 However, it doesn't seem to ever be used... */
462 buf->frametype = HB_FRAME_BREF;
464 /* If it isn't the above, what type of frame is it?? */
470 /* Store the output presentation time stamp
471 from x264 for use by muxmp4 in off-setting
472 b-frames with the CTTS atom.
473 For now, just add 1000000 to the offset so that the
474 value is pretty much guaranteed to be positive. The
475 muxing code will minimize the renderOffset at the end. */
477 buf->renderOffset = pic_out.i_pts - dts_start + pv->init_delay;
479 /* Send out the next dts values */
480 buf->start = dts_start;
481 buf->stop = dts_stop;