OSDN Git Service

Change subtitle position to prevent displaying within a 2% margin of the height of...
[handbrake-jp/handbrake-jp-git.git] / libhb / encx264.c
1 /* $Id: encx264.c,v 1.21 2005/11/04 13:09:41 titer Exp $
2
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. */
6
7 #include <stdarg.h>
8
9 #include "hb.h"
10
11 #include "x264.h"
12
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 * );
16
17 hb_work_object_t hb_encx264 =
18 {
19     WORK_ENCX264,
20     "H.264/AVC encoder (libx264)",
21     encx264Init,
22     encx264Work,
23     encx264Close
24 };
25
26 #define DTS_BUFFER_SIZE 32 
27
28 struct hb_work_private_s
29 {
30     hb_job_t       * job;
31     x264_t         * x264;
32     x264_picture_t   pic_in;
33     uint8_t         *x264_allocated_pic;
34
35     // Internal queue of DTS start/stop values.
36     int64_t        dts_start[DTS_BUFFER_SIZE];
37     int64_t        dts_stop[DTS_BUFFER_SIZE];
38     int64_t        init_delay;
39
40     int64_t        dts_write_index;
41     int64_t        dts_read_index;
42     int64_t        next_chap;
43
44     char             filename[1024];
45 };
46
47 /***********************************************************************
48  * hb_work_encx264_init
49  ***********************************************************************
50  *
51  **********************************************************************/
52 int encx264Init( hb_work_object_t * w, hb_job_t * job )
53 {
54     x264_param_t       param;
55     x264_nal_t       * nal;
56     int                nal_count;
57
58     hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
59     w->private_data = pv;
60
61     pv->job = job;
62
63     memset( pv->filename, 0, 1024 );
64     hb_get_tempory_filename( job->h, pv->filename, "x264.log" );
65
66     x264_param_default( &param );
67
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;
73     
74     if (job->vrate_base != 1080000)
75     {
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);        
81     }
82     
83     param.i_log_level  = X264_LOG_INFO;
84     if( job->h264_level )
85     {
86         param.b_cabac     = 0;
87         param.i_level_idc = job->h264_level;
88         hb_log( "encx264: encoding at level %i",
89                 param.i_level_idc );
90     }
91
92     /* Slightly faster with minimal quality lost */
93     param.analyse.i_subpel_refine = 4;
94
95     /*
96         This section passes the string x264opts to libx264 for parsing into 
97         parameter names and values.
98
99         The string is set up like this:
100         option1=value1:option2=value 2
101
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.
105
106         This is all based on the universal x264 option handling Loren
107         Merritt implemented in the Mplayer/Mencoder project.
108      */
109
110     if( job->x264opts != NULL && *job->x264opts != '\0' )
111     {
112         char *x264opts, *x264opts_start;
113
114         x264opts = x264opts_start = strdup(job->x264opts);
115
116         while( x264opts_start && *x264opts )
117         {
118             char *name = x264opts;
119             char *value;
120             int ret;
121
122             x264opts += strcspn( x264opts, ":" );
123             if( *x264opts )
124             {
125                 *x264opts = 0;
126                 x264opts++;
127             }
128
129             value = strchr( name, '=' );
130             if( value )
131             {
132                 *value = 0;
133                 value++;
134             }
135
136             /*
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.
142              */
143
144             if( !( strcmp( name, "bframes" ) ) )
145             {
146                 if( atoi( value ) > 0 )
147                 {
148                     job->areBframes = 1;
149                 }
150             }
151
152             /* Note b-pyramid here, so the initial delay can be doubled */
153             if( !( strcmp( name, "b-pyramid" ) ) )
154             {
155                 if( value != NULL )
156                 {
157                     if( atoi( value ) > 0 )
158                     {
159                         job->areBframes = 2;
160                     }
161                 }
162                 else
163                 {
164                     job->areBframes = 2;
165                 }
166             }
167
168             /* Here's where the strings are passed to libx264 for parsing. */
169             ret = x264_param_parse( &param, name, value );
170
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)" );
176         }
177         free(x264opts_start);
178     }
179
180
181     if( job->pixel_ratio )
182     {
183         param.vui.i_sar_width = job->pixel_aspect_width;
184         param.vui.i_sar_height = job->pixel_aspect_height;
185
186         hb_log( "encx264: encoding with stored aspect %d/%d",
187                 param.vui.i_sar_width, param.vui.i_sar_height );
188     }
189
190
191     if( job->vquality >= 0.0 && job->vquality <= 1.0 )
192     {
193         switch( job->crf )
194         {
195             case 1:
196                 /*Constant RF*/
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 );
201                 break;
202
203             case 0:
204                 /*Constant QP*/
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 );
209                 break;
210         }
211     }
212     else
213     {
214         /* Rate control */
215         param.rc.i_rc_method = X264_RC_ABR;
216         param.rc.i_bitrate = job->vbitrate;
217         switch( job->pass )
218         {
219             case 1:
220                 param.rc.b_stat_write  = 1;
221                 param.rc.psz_stat_out = pv->filename;
222                 break;
223             case 2:
224                 param.rc.b_stat_read = 1;
225                 param.rc.psz_stat_in = pv->filename;
226                 break;
227         }
228     }
229
230     hb_log( "encx264: opening libx264 (pass %d)", job->pass );
231     pv->x264 = x264_encoder_open( &param );
232
233     x264_encoder_headers( pv->x264, &nal, &nal_count );
234
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 );
239
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 );
244
245     x264_picture_alloc( &pv->pic_in, X264_CSP_I420,
246                         job->width, job->height );
247
248     pv->x264_allocated_pic = pv->pic_in.img.plane[0];
249
250     pv->dts_write_index = 0;
251     pv->dts_read_index = 0;
252     pv->next_chap = 0;
253
254     if (job->areBframes)
255     {
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);
259        
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)
265             pv->init_delay++;
266        
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.. */
269         if (job->vfr)
270         {
271             pv->init_delay = 3754;
272         }
273    
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);
278     }
279
280     return 0;
281 }
282
283 void encx264Close( hb_work_object_t * w )
284 {
285     hb_work_private_t * pv = w->private_data;
286     /*
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.
289      */
290     pv->pic_in.img.plane[0] = pv->x264_allocated_pic;
291     x264_picture_clean( &pv->pic_in );
292     x264_encoder_close( pv->x264 );
293     free( pv );
294     w->private_data = NULL;
295
296     /* TODO */
297 }
298
299 int encx264Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
300                   hb_buffer_t ** buf_out )
301 {
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;
306     int           i_nal;
307     x264_nal_t  * nal;
308     int i;
309
310     if( in->data )
311     {
312         /*
313          * Point x264 at our current buffers Y(UV) data.
314          */
315         pv->pic_in.img.plane[0] = in->data;
316
317         if( job->grayscale )
318         {
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 );
322         }
323         else
324         {
325             /*
326              * Point x264 at our buffers (Y)UV data
327              */
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 *
330                 job->height / 4;
331         }
332
333         if( in->new_chap && job->chapter_markers )
334         {
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 )
342             {
343                 pv->next_chap = in->start;
344             }
345             /* don't let 'work_loop' put a chapter mark on the wrong buffer */
346             in->new_chap = 0;
347         }
348         else
349         {
350             pv->pic_in.i_type = X264_TYPE_AUTO;
351         }
352         pv->pic_in.i_qpplus1 = 0;
353
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++;
358
359         /* Feed the input DTS to x264 so it can figure out proper output PTS */
360         pv->pic_in.i_pts = in->start;
361
362         x264_encoder_encode( pv->x264, &nal, &i_nal,
363                              &pv->pic_in, &pic_out );        
364     }
365     else
366     {
367         x264_encoder_encode( pv->x264, &nal, &i_nal,
368                              NULL, &pic_out );
369         /* No more delayed B frames */
370         if( i_nal == 0 )
371         {
372             *buf_out = NULL;
373             return HB_WORK_DONE;
374         }
375         else
376         {
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));
381         }
382     }
383
384     if( i_nal )
385     {
386         /* Should be way too large */
387         buf        = hb_buffer_init( 3 * job->width * job->height / 2 );
388         buf->size  = 0;
389         buf->start = in->start;
390         buf->stop  = in->stop;
391         buf->frametype   = 0;
392
393         int64_t dts_start, dts_stop;
394
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++;
399
400         for( i = 0; i < i_nal; i++ )
401         {
402             int size, data;
403
404             data = buf->alloc - buf->size;
405             if( ( size = x264_nal_encode( buf->data + buf->size, &data,
406                                           1, &nal[i] ) ) < 1 )
407             {
408                 continue;
409             }
410
411             if( job->mux & HB_MUX_AVI )
412             {
413                 if( nal[i].i_ref_idc == NAL_PRIORITY_HIGHEST )
414                 {
415                     buf->frametype = HB_FRAME_KEY;
416                 }
417                 buf->size += size;
418                 continue;
419             }
420
421             /* H.264 in .mp4 */
422             switch( buf->data[buf->size+4] & 0x1f )
423             {
424                 case 0x7:
425                 case 0x8:
426                     /* SPS, PPS */
427                     break;
428
429                 default:
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 )
436                     {
437                     /*  Decide what type of frame we have. */
438                         case X264_TYPE_IDR:
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
443                                chapter start. */
444                             if( pv->next_chap != 0 && pv->next_chap <= pic_out.i_pts )
445                             {
446                                 pv->next_chap = 0;
447                                 buf->new_chap = 1;
448                             }
449                             break;
450                         case X264_TYPE_I:
451                             buf->frametype = HB_FRAME_I;
452                             break;
453                         case X264_TYPE_P:
454                             buf->frametype = HB_FRAME_P;
455                             break;
456                         case X264_TYPE_B:
457                             buf->frametype = HB_FRAME_B;
458                             break;
459                     /*  This is for b-pyramid, which has reference b-frames
460                         However, it doesn't seem to ever be used... */
461                         case X264_TYPE_BREF:
462                             buf->frametype = HB_FRAME_BREF;
463                             break;
464                     /*  If it isn't the above, what type of frame is it?? */
465                         default:
466                             buf->frametype = 0;
467                     }
468
469
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. */
476
477                     buf->renderOffset = pic_out.i_pts - dts_start + pv->init_delay;
478
479                     /* Send out the next dts values */
480                     buf->start = dts_start;
481                     buf->stop  = dts_stop;
482
483                     buf->size += size;
484             }
485         }
486     }
487
488     else
489         buf = NULL;
490
491     *buf_out = buf;
492
493     return HB_WORK_OK;
494 }
495
496