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 9 allows
34 * any frame rate slower than 175fps.) 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 16 which is 2^(16-9+1) = 256 slots guarantees
39 * no collisions down to a rate of 1.4 fps).
41 #define FRAME_INFO_MAX2 (9) // 2^9 = 512; 90000/512 = 175 frames/sec
42 #define FRAME_INFO_MIN2 (16) // 2^16 = 65536; 90000/65536 = 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;
53 int chap_mark; // saved chap mark when we're propagating it
54 int64_t dts_next; // DTS start time value for next output frame
55 int64_t last_stop; // Debugging - stop time of previous input frame
61 } frame_info[FRAME_INFO_SIZE];
66 /***********************************************************************
67 * hb_work_encx264_init
68 ***********************************************************************
70 **********************************************************************/
71 int encx264Init( hb_work_object_t * w, hb_job_t * job )
77 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
82 memset( pv->filename, 0, 1024 );
83 hb_get_tempory_filename( job->h, pv->filename, "x264.log" );
85 x264_param_default( ¶m );
87 param.i_threads = ( hb_get_cpu_count() * 3 / 2 );
88 param.i_width = job->width;
89 param.i_height = job->height;
90 param.i_fps_num = job->vrate;
91 param.i_fps_den = job->vrate_base;
93 if (job->vrate_base != 1080000)
95 /* If the fps isn't 25, adjust the key intervals. Add 1 because
96 we want 24, not 23 with a truncated remainder. */
97 param.i_keyint_min = (job->vrate / job->vrate_base) + 1;
98 param.i_keyint_max = (10 * job->vrate / job->vrate_base) + 1;
99 hb_log("encx264: keyint-min: %i, keyint-max: %i", param.i_keyint_min, param.i_keyint_max);
102 param.i_log_level = X264_LOG_INFO;
103 if( job->h264_level )
106 param.i_level_idc = job->h264_level;
107 hb_log( "encx264: encoding at level %i",
111 /* Slightly faster with minimal quality lost */
112 param.analyse.i_subpel_refine = 4;
115 This section passes the string x264opts to libx264 for parsing into
116 parameter names and values.
118 The string is set up like this:
119 option1=value1:option2=value 2
121 So, you have to iterate through based on the colons, and then put
122 the left side of the equals sign in "name" and the right side into
123 "value." Then you hand those strings off to x264 for interpretation.
125 This is all based on the universal x264 option handling Loren
126 Merritt implemented in the Mplayer/Mencoder project.
129 if( job->x264opts != NULL && *job->x264opts != '\0' )
131 char *x264opts, *x264opts_start;
133 x264opts = x264opts_start = strdup(job->x264opts);
135 while( x264opts_start && *x264opts )
137 char *name = x264opts;
141 x264opts += strcspn( x264opts, ":" );
148 value = strchr( name, '=' );
156 When B-frames are enabled, the max frame count increments
157 by 1 (regardless of the number of B-frames). If you don't
158 change the duration of the video track when you mux, libmp4
159 barfs. So, check if the x264opts are using B-frames, and
160 when they are, set the boolean job->areBframes as true.
163 if( !( strcmp( name, "bframes" ) ) )
165 if( atoi( value ) > 0 )
171 /* Note b-pyramid here, so the initial delay can be doubled */
172 if( !( strcmp( name, "b-pyramid" ) ) )
176 if( atoi( value ) > 0 )
187 /* Here's where the strings are passed to libx264 for parsing. */
188 ret = x264_param_parse( ¶m, name, value );
190 /* Let x264 sanity check the options for us*/
191 if( ret == X264_PARAM_BAD_NAME )
192 hb_log( "x264 options: Unknown suboption %s", name );
193 if( ret == X264_PARAM_BAD_VALUE )
194 hb_log( "x264 options: Bad argument %s=%s", name, value ? value : "(null)" );
196 free(x264opts_start);
200 if( job->pixel_ratio )
202 param.vui.i_sar_width = job->pixel_aspect_width;
203 param.vui.i_sar_height = job->pixel_aspect_height;
205 hb_log( "encx264: encoding with stored aspect %d/%d",
206 param.vui.i_sar_width, param.vui.i_sar_height );
210 if( job->vquality >= 0.0 && job->vquality <= 1.0 )
216 param.rc.i_rc_method = X264_RC_CRF;
217 param.rc.f_rf_constant = 51 - job->vquality * 51;
218 hb_log( "encx264: Encoding at constant RF %f",
219 param.rc.f_rf_constant );
224 param.rc.i_rc_method = X264_RC_CQP;
225 param.rc.i_qp_constant = 51 - job->vquality * 51;
226 hb_log( "encx264: encoding at constant QP %d",
227 param.rc.i_qp_constant );
234 param.rc.i_rc_method = X264_RC_ABR;
235 param.rc.i_bitrate = job->vbitrate;
239 param.rc.b_stat_write = 1;
240 param.rc.psz_stat_out = pv->filename;
243 param.rc.b_stat_read = 1;
244 param.rc.psz_stat_in = pv->filename;
249 hb_log( "encx264: opening libx264 (pass %d)", job->pass );
250 pv->x264 = x264_encoder_open( ¶m );
252 x264_encoder_headers( pv->x264, &nal, &nal_count );
254 /* Sequence Parameter Set */
255 w->config->h264.sps_length = 1 + nal[1].i_payload;
256 w->config->h264.sps[0] = 0x67;
257 memcpy( &w->config->h264.sps[1], nal[1].p_payload, nal[1].i_payload );
259 /* Picture Parameter Set */
260 w->config->h264.pps_length = 1 + nal[2].i_payload;
261 w->config->h264.pps[0] = 0x68;
262 memcpy( &w->config->h264.pps[1], nal[2].p_payload, nal[2].i_payload );
264 x264_picture_alloc( &pv->pic_in, X264_CSP_I420,
265 job->width, job->height );
267 pv->x264_allocated_pic = pv->pic_in.img.plane[0];
274 /* Basic initDelay value is the clockrate divided by the FPS
275 -- the length of one frame in clockticks. */
276 pv->init_delay = (float)90000 / (float)((float)job->vrate / (float)job->vrate_base);
278 /* 23.976-length frames are 3753.75 ticks long. That means 25%
279 will come out as 3753, 75% will be 3754. The delay has to be
280 the longest possible frame duration, 3754. However, 3753.75
281 gets truncated to 3753, so if that's what it is, ++ it. */
282 if (pv->init_delay == 3753)
285 /* For VFR, libhb sees the FPS as 29.97, but the longest frames
286 will use the duration of frames running at 23.976fps instead.. */
289 pv->init_delay = 7506;
292 /* The delay is 1 frames for regular b-frames, 2 for b-pyramid. */
293 pv->init_delay *= job->areBframes;
299 void encx264Close( hb_work_object_t * w )
301 hb_work_private_t * pv = w->private_data;
303 * Patch the x264 allocated data back in so that x264 can free it
304 * we have been using our own buffers during the encode to avoid copying.
306 pv->pic_in.img.plane[0] = pv->x264_allocated_pic;
307 x264_picture_clean( &pv->pic_in );
308 x264_encoder_close( pv->x264 );
310 w->private_data = NULL;
316 * see comments in definition of 'frame_info' in pv struct for description
317 * of what these routines are doing.
319 static void save_frame_info( hb_work_private_t * pv, hb_buffer_t * in )
321 int i = (in->start >> FRAME_INFO_MAX2) & FRAME_INFO_MASK;
322 pv->frame_info[i].duration = in->stop - in->start;
325 static int64_t get_frame_duration( hb_work_private_t * pv, int64_t pts )
327 int i = (pts >> FRAME_INFO_MAX2) & FRAME_INFO_MASK;
328 return pv->frame_info[i].duration;
331 int encx264Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
332 hb_buffer_t ** buf_out )
334 hb_work_private_t * pv = w->private_data;
335 hb_job_t * job = pv->job;
336 hb_buffer_t * in = *buf_in, * buf;
337 x264_picture_t pic_out;
345 * Point x264 at our current buffers Y(UV) data.
347 pv->pic_in.img.plane[0] = in->data;
351 /* XXX x264 has currently no option for grayscale encoding */
352 memset( pv->pic_in.img.plane[1], 0x80, job->width * job->height / 4 );
353 memset( pv->pic_in.img.plane[2], 0x80, job->width * job->height / 4 );
358 * Point x264 at our buffers (Y)UV data
360 pv->pic_in.img.plane[1] = in->data + job->width * job->height;
361 pv->pic_in.img.plane[2] = in->data + 5 * job->width *
365 if( pv->dts_next == -1 )
367 /* we don't have a start time yet so use the first frame's
368 * start. All other frame times will be determined by the
369 * sum of the prior output frame durations in *DTS* order
370 * (not by the order they arrive here). This timing change is
371 * essential for VFR with b-frames but a complete nop otherwise.
373 pv->dts_next = in->start;
375 if( in->new_chap && job->chapter_markers )
377 /* chapters have to start with an IDR frame so request that this
378 frame be coded as IDR. Since there may be up to 16 frames
379 currently buffered in the encoder remember the timestamp so
380 when this frame finally pops out of the encoder we'll mark
381 its buffer as the start of a chapter. */
382 pv->pic_in.i_type = X264_TYPE_IDR;
383 if( pv->next_chap == 0 )
385 pv->next_chap = in->start;
386 pv->chap_mark = in->new_chap;
388 /* don't let 'work_loop' put a chapter mark on the wrong buffer */
393 pv->pic_in.i_type = X264_TYPE_AUTO;
395 pv->pic_in.i_qpplus1 = 0;
397 /* XXX this is temporary debugging code to check that the upstream
398 * modules (render & sync) have generated a continuous, self-consistent
399 * frame stream with the current frame's start time equal to the
400 * previous frame's stop time.
402 if( pv->last_stop != in->start )
404 hb_log("encx264 input continuity err: last stop %lld start %lld",
405 pv->last_stop, in->start);
407 pv->last_stop = in->stop;
409 // Remember info about this frame that we need to pass across
410 // the x264_encoder_encode call (since it reorders frames).
411 save_frame_info( pv, in );
413 /* Feed the input DTS to x264 so it can figure out proper output PTS */
414 pv->pic_in.i_pts = in->start;
416 x264_encoder_encode( pv->x264, &nal, &i_nal,
417 &pv->pic_in, &pic_out );
421 x264_encoder_encode( pv->x264, &nal, &i_nal,
423 /* No more delayed B frames */
431 /* Since we output at least one more frame, drop another empty
432 one onto our input fifo. We'll keep doing this automatically
433 until we stop getting frames out of the encoder. */
434 hb_fifo_push(w->fifo_in, hb_buffer_init(0));
440 /* Should be way too large */
441 buf = hb_buffer_init( 3 * job->width * job->height / 2 );
445 /* Get next DTS value to use */
446 int64_t dts_start = pv->dts_next;
448 /* compute the stop time based on the original frame's duration */
449 int64_t dts_stop = dts_start + get_frame_duration( pv, pic_out.i_pts );
450 pv->dts_next = dts_stop;
452 for( i = 0; i < i_nal; i++ )
456 data = buf->alloc - buf->size;
457 if( ( size = x264_nal_encode( buf->data + buf->size, &data,
463 if( job->mux & HB_MUX_AVI )
465 if( nal[i].i_ref_idc == NAL_PRIORITY_HIGHEST )
467 buf->frametype = HB_FRAME_KEY;
474 switch( buf->data[buf->size+4] & 0x1f )
482 /* H.264 in mp4 (stolen from mp4creator) */
483 buf->data[buf->size+0] = ( ( size - 4 ) >> 24 ) & 0xFF;
484 buf->data[buf->size+1] = ( ( size - 4 ) >> 16 ) & 0xFF;
485 buf->data[buf->size+2] = ( ( size - 4 ) >> 8 ) & 0xFF;
486 buf->data[buf->size+3] = ( ( size - 4 ) >> 0 ) & 0xFF;
487 switch( pic_out.i_type )
489 /* Decide what type of frame we have. */
491 buf->frametype = HB_FRAME_IDR;
492 /* if we have a chapter marker pending and this
493 frame's presentation time stamp is at or after
494 the marker's time stamp, use this as the
496 if( pv->next_chap != 0 && pv->next_chap <= pic_out.i_pts )
499 buf->new_chap = pv->chap_mark;
503 buf->frametype = HB_FRAME_I;
506 buf->frametype = HB_FRAME_P;
509 buf->frametype = HB_FRAME_B;
511 /* This is for b-pyramid, which has reference b-frames
512 However, it doesn't seem to ever be used... */
514 buf->frametype = HB_FRAME_BREF;
516 /* If it isn't the above, what type of frame is it?? */
521 /* Since libx264 doesn't tell us when b-frames are
522 themselves reference frames, figure it out on our own. */
523 if( (buf->frametype == HB_FRAME_B) && (nal[i].i_ref_idc != NAL_PRIORITY_DISPOSABLE) )
524 buf->frametype = HB_FRAME_BREF;
526 /* Store the output presentation time stamp
527 from x264 for use by muxmp4 in off-setting
528 b-frames with the CTTS atom. */
529 buf->renderOffset = pic_out.i_pts - dts_start + pv->init_delay;
534 /* Send out the next dts values */
535 buf->start = dts_start;
536 buf->stop = dts_stop;