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 // 16 is probably overkill but it's also the maximum for h.264 reference frames
27 #define MAX_INFLIGHT_FRAMES 16
29 struct hb_work_private_s
33 x264_picture_t pic_in;
35 // Internal queue of DTS start/stop values.
36 int64_t dts_start[MAX_INFLIGHT_FRAMES];
37 int64_t dts_stop[MAX_INFLIGHT_FRAMES];
39 int64_t dts_write_index;
40 int64_t dts_read_index;
45 /***********************************************************************
46 * hb_work_encx264_init
47 ***********************************************************************
49 **********************************************************************/
50 int encx264Init( hb_work_object_t * w, hb_job_t * job )
56 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
61 memset( pv->filename, 0, 1024 );
62 hb_get_tempory_filename( job->h, pv->filename, "x264.log" );
64 x264_param_default( ¶m );
66 param.i_threads = ( hb_get_cpu_count() * 3 / 2 );
67 param.i_width = job->width;
68 param.i_height = job->height;
69 param.i_fps_num = job->vrate;
70 param.i_fps_den = job->vrate_base;
71 param.i_keyint_max = 20 * job->vrate / job->vrate_base;
72 param.i_log_level = X264_LOG_INFO;
76 param.i_level_idc = job->h264_level;
77 hb_log( "encx264: encoding at level %i",
81 /* Slightly faster with minimal quality lost */
82 param.analyse.i_subpel_refine = 4;
85 This section passes the string x264opts to libx264 for parsing into
86 parameter names and values.
88 The string is set up like this:
89 option1=value1:option2=value 2
91 So, you have to iterate through based on the colons, and then put
92 the left side of the equals sign in "name" and the right side into
93 "value." Then you hand those strings off to x264 for interpretation.
95 This is all based on the universal x264 option handling Loren
96 Merritt implemented in the Mplayer/Mencoder project.
99 char *x264opts = job->x264opts;
100 if( x264opts != NULL && *x264opts != '\0' )
104 char *name = x264opts;
108 x264opts += strcspn( x264opts, ":" );
115 value = strchr( name, '=' );
123 When B-frames are enabled, the max frame count increments
124 by 1 (regardless of the number of B-frames). If you don't
125 change the duration of the video track when you mux, libmp4
126 barfs. So, check if the x264opts are using B-frames, and
127 when they are, set the boolean job->areBframes as true.
130 if( !( strcmp( name, "bframes" ) ) )
132 if( atoi( value ) > 0 )
138 /* Note b-pyramid here, so the initial delay can be doubled */
139 if( !( strcmp( name, "b-pyramid" ) ) )
143 if( atoi( value ) > 0 )
154 /* Here's where the strings are passed to libx264 for parsing. */
155 ret = x264_param_parse( ¶m, name, value );
157 /* Let x264 sanity check the options for us*/
158 if( ret == X264_PARAM_BAD_NAME )
159 hb_log( "x264 options: Unknown suboption %s", name );
160 if( ret == X264_PARAM_BAD_VALUE )
161 hb_log( "x264 options: Bad argument %s=%s", name, value ? value : "(null)" );
166 if( job->pixel_ratio )
168 param.vui.i_sar_width = job->pixel_aspect_width;
169 param.vui.i_sar_height = job->pixel_aspect_height;
171 hb_log( "encx264: encoding with stored aspect %d/%d",
172 param.vui.i_sar_width, param.vui.i_sar_height );
176 if( job->vquality >= 0.0 && job->vquality <= 1.0 )
182 param.rc.i_rc_method = X264_RC_CRF;
183 param.rc.f_rf_constant = 51 - job->vquality * 51;
184 hb_log( "encx264: Encoding at constant RF %f",
185 param.rc.f_rf_constant );
190 param.rc.i_rc_method = X264_RC_CQP;
191 param.rc.i_qp_constant = 51 - job->vquality * 51;
192 hb_log( "encx264: encoding at constant QP %d",
193 param.rc.i_qp_constant );
200 param.rc.i_rc_method = X264_RC_ABR;
201 param.rc.i_bitrate = job->vbitrate;
205 param.rc.b_stat_write = 1;
206 param.rc.psz_stat_out = pv->filename;
209 param.rc.b_stat_read = 1;
210 param.rc.psz_stat_in = pv->filename;
215 hb_log( "encx264: opening libx264 (pass %d)", job->pass );
216 pv->x264 = x264_encoder_open( ¶m );
218 x264_encoder_headers( pv->x264, &nal, &nal_count );
220 /* Sequence Parameter Set */
221 w->config->h264.sps_length = 1 + nal[1].i_payload;
222 w->config->h264.sps[0] = 0x67;
223 memcpy( &w->config->h264.sps[1], nal[1].p_payload, nal[1].i_payload );
225 /* Picture Parameter Set */
226 w->config->h264.pps_length = 1 + nal[2].i_payload;
227 w->config->h264.pps[0] = 0x68;
228 memcpy( &w->config->h264.pps[1], nal[2].p_payload, nal[2].i_payload );
230 x264_picture_alloc( &pv->pic_in, X264_CSP_I420,
231 job->width, job->height );
233 pv->dts_write_index = 0;
234 pv->dts_read_index = 0;
239 void encx264Close( hb_work_object_t * w )
241 hb_work_private_t * pv = w->private_data;
242 x264_picture_clean( &pv->pic_in );
243 x264_encoder_close( pv->x264 );
245 w->private_data = NULL;
250 int encx264Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
251 hb_buffer_t ** buf_out )
253 hb_work_private_t * pv = w->private_data;
254 hb_job_t * job = pv->job;
255 hb_buffer_t * in = *buf_in, * buf;
256 x264_picture_t pic_out;
263 /* XXX avoid this memcpy ? */
264 memcpy( pv->pic_in.img.plane[0], in->data, job->width * job->height );
267 /* XXX x264 has currently no option for grayscale encoding */
268 memset( pv->pic_in.img.plane[1], 0x80, job->width * job->height / 4 );
269 memset( pv->pic_in.img.plane[2], 0x80, job->width * job->height / 4 );
273 memcpy( pv->pic_in.img.plane[1], in->data + job->width * job->height,
274 job->width * job->height / 4 );
275 memcpy( pv->pic_in.img.plane[2], in->data + 5 * job->width *
276 job->height / 4, job->width * job->height / 4 );
279 pv->pic_in.i_type = X264_TYPE_AUTO;
280 pv->pic_in.i_qpplus1 = 0;
282 // Remember current PTS value, use as DTS later
283 pv->dts_start[pv->dts_write_index & (MAX_INFLIGHT_FRAMES-1)] = in->start;
284 pv->dts_stop[pv->dts_write_index & (MAX_INFLIGHT_FRAMES-1)] = in->stop;
285 pv->dts_write_index++;
287 /* Feed the input DTS to x264 so it can figure out proper output PTS */
288 pv->pic_in.i_pts = in->start;
290 x264_encoder_encode( pv->x264, &nal, &i_nal,
291 &pv->pic_in, &pic_out );
295 x264_encoder_encode( pv->x264, &nal, &i_nal,
297 /* No more delayed B frames */
305 /* Since we output at least one more frame, drop another empty
306 one onto our input fifo. We'll keep doing this automatically
307 until we stop getting frames out of the encoder. */
308 hb_fifo_push(w->fifo_in, hb_buffer_init(0));
314 /* Should be way too large */
315 buf = hb_buffer_init( 3 * job->width * job->height / 2 );
317 buf->start = in->start;
318 buf->stop = in->stop;
321 int64_t dts_start, dts_stop;
323 /* Get next DTS value to use */
324 dts_start = pv->dts_start[pv->dts_read_index & (MAX_INFLIGHT_FRAMES-1)];
325 dts_stop = pv->dts_stop[pv->dts_read_index & (MAX_INFLIGHT_FRAMES-1)];
326 pv->dts_read_index++;
328 for( i = 0; i < i_nal; i++ )
332 data = buf->alloc - buf->size;
333 if( ( size = x264_nal_encode( buf->data + buf->size, &data,
339 if( job->mux & HB_MUX_AVI )
341 if( nal[i].i_ref_idc == NAL_PRIORITY_HIGHEST )
343 buf->frametype = HB_FRAME_KEY;
350 switch( buf->data[buf->size+4] & 0x1f )
358 /* H.264 in mp4 (stolen from mp4creator) */
359 buf->data[buf->size+0] = ( ( size - 4 ) >> 24 ) & 0xFF;
360 buf->data[buf->size+1] = ( ( size - 4 ) >> 16 ) & 0xFF;
361 buf->data[buf->size+2] = ( ( size - 4 ) >> 8 ) & 0xFF;
362 buf->data[buf->size+3] = ( ( size - 4 ) >> 0 ) & 0xFF;
363 switch( pic_out.i_type )
365 /* Decide what type of frame we have. */
367 buf->frametype = HB_FRAME_IDR;
370 buf->frametype = HB_FRAME_I;
373 buf->frametype = HB_FRAME_P;
376 buf->frametype = HB_FRAME_B;
378 /* This is for b-pyramid, which has reference b-frames
379 However, it doesn't seem to ever be used... */
381 buf->frametype = HB_FRAME_BREF;
383 /* If it isn't the above, what type of frame is it?? */
389 /* Store the output presentation time stamp
390 from x264 for use by muxmp4 in off-setting
391 b-frames with the CTTS atom.
392 For now, just add 1000000 to the offset so that the
393 value is pretty much guaranteed to be positive. The
394 muxing code will minimize the renderOffset at the end. */
396 buf->renderOffset = pic_out.i_pts - dts_start + 1000000;
398 /* Send out the next dts values */
399 buf->start = dts_start;
400 buf->stop = dts_stop;