OSDN Git Service

cli: add vbv settings to appletv preset
[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.fr/>.
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 /*
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).
40  */
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)
45
46 struct hb_work_private_s
47 {
48     hb_job_t       * job;
49     x264_t         * x264;
50     x264_picture_t   pic_in;
51     uint8_t         *x264_allocated_pic;
52
53     uint32_t       frames_in;
54     uint32_t       frames_out;
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
58     int64_t        next_chap;
59
60     struct {
61         int64_t duration;
62     } frame_info[FRAME_INFO_SIZE];
63
64     char             filename[1024];
65 };
66
67 /***********************************************************************
68  * hb_work_encx264_init
69  ***********************************************************************
70  *
71  **********************************************************************/
72 int encx264Init( hb_work_object_t * w, hb_job_t * job )
73 {
74     x264_param_t       param;
75     x264_nal_t       * nal;
76     int                nal_count;
77
78     hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
79     w->private_data = pv;
80
81     pv->job = job;
82
83     memset( pv->filename, 0, 1024 );
84     hb_get_tempory_filename( job->h, pv->filename, "x264.log" );
85
86     x264_param_default( &param );
87     
88     /* Default weightp to off for baseline,
89        overridable through x264 option strings. */
90     if( job->x264opts != NULL && *job->x264opts != '\0' )
91     {
92         char *x264opts, *x264opts_start;
93     
94         x264opts = x264opts_start = strdup(job->x264opts);
95     
96         while( x264opts_start && *x264opts )
97         {
98             char *name = x264opts;
99             char *value;
100     
101             x264opts += strcspn( x264opts, ":" );
102             if( *x264opts )
103             {
104                 *x264opts = 0;
105                 x264opts++;
106             }
107     
108             value = strchr( name, '=' );
109             if( value )
110             {
111                 *value = 0;
112                 value++;
113             }
114     
115             if( !( strcmp( name, "bframes" ) ) )
116             {
117                 if( atoi( value ) == 0 )
118                 {
119                     param.analyse.i_weighted_pred = X264_WEIGHTP_NONE;
120                     hb_log("encx264: no bframes, disabling weight-p unless told otherwise");
121                 }
122             }
123         }
124     }
125     
126     /* Enable metrics */
127     param.analyse.b_psnr = 1;
128     param.analyse.b_ssim = 1;
129     
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;
135     param.i_timebase_num   = 1;
136     param.i_timebase_den   = 90000;
137
138     /* Disable annexb. Inserts size into nal header instead of start code */
139     param.b_annexb     = 0;
140
141     /* Set min:max key intervals ratio to 1:10 of fps.
142      * This section is skipped if fps=25 (default).
143      */
144     if (job->vrate_base != 1080000)
145     {
146         if (job->pass == 2 && !job->cfr )
147         {
148             /* Even though the framerate might be different due to VFR,
149                we still want the same keyframe intervals as the 1st pass,
150                so the 1st pass stats won't conflict on frame decisions.    */
151             hb_interjob_t * interjob = hb_interjob_get( job->h );
152             param.i_keyint_min =      ( ( (double)interjob->vrate / (double)interjob->vrate_base ) + 0.5 );
153             param.i_keyint_max = ( ( 10 * (double)interjob->vrate / (double)interjob->vrate_base ) + 0.5 );
154         }
155         else
156         {
157             /* adjust +0.5 for when fps has remainder to bump
158                { 23.976, 29.976, 59.94 } to { 24, 30, 60 } */
159             param.i_keyint_min =      ( ( (double)job->vrate / (double)job->vrate_base ) + 0.5 );
160             param.i_keyint_max = ( ( 10 * (double)job->vrate / (double)job->vrate_base ) + 0.5 );
161         }
162     }
163
164     param.i_log_level  = X264_LOG_INFO;
165     if( job->h264_level )
166     {
167         param.b_cabac     = 0;
168         param.i_level_idc = job->h264_level;
169         hb_log( "encx264: encoding at level %i",
170                 param.i_level_idc );
171     }
172     
173     /*
174         This section passes the string x264opts to libx264 for parsing into
175         parameter names and values.
176
177         The string is set up like this:
178         option1=value1:option2=value 2
179
180         So, you have to iterate through based on the colons, and then put
181         the left side of the equals sign in "name" and the right side into
182         "value." Then you hand those strings off to x264 for interpretation.
183
184         This is all based on the universal x264 option handling Loren
185         Merritt implemented in the Mplayer/Mencoder project.
186      */
187
188     if( job->x264opts != NULL && *job->x264opts != '\0' )
189     {
190         char *x264opts, *x264opts_start;
191
192         x264opts = x264opts_start = strdup(job->x264opts);
193
194         while( x264opts_start && *x264opts )
195         {
196             char *name = x264opts;
197             char *value;
198             int ret;
199
200             x264opts += strcspn( x264opts, ":" );
201             if( *x264opts )
202             {
203                 *x264opts = 0;
204                 x264opts++;
205             }
206
207             value = strchr( name, '=' );
208             if( value )
209             {
210                 *value = 0;
211                 value++;
212             }
213
214             if( !( strcmp( name, "b-pyramid" ) ) )
215             {
216                 if( value == NULL || !strcmp( value, "1" ) )
217                 {
218                     value = "normal";
219                 }
220                 else if( !strcmp( value, "0" ) )
221                 {
222                     value = "none";
223                 }
224             }
225
226             /* Here's where the strings are passed to libx264 for parsing. */
227             ret = x264_param_parse( &param, name, value );
228
229             /*  Let x264 sanity check the options for us*/
230             if( ret == X264_PARAM_BAD_NAME )
231                 hb_log( "x264 options: Unknown suboption %s", name );
232             if( ret == X264_PARAM_BAD_VALUE )
233                 hb_log( "x264 options: Bad argument %s=%s", name, value ? value : "(null)" );
234         }
235         free(x264opts_start);
236     }
237     
238     /* B-frames are on by default.*/
239     job->areBframes = 1;
240     
241     if( param.i_bframe && param.i_bframe_pyramid )
242     {
243         /* Note b-pyramid here, so the initial delay can be doubled */
244         job->areBframes = 2;
245     }
246     else if( !param.i_bframe )
247     {
248         /*
249          When B-frames are enabled, the max frame count increments
250          by 1 (regardless of the number of B-frames). If you don't
251          change the duration of the video track when you mux, libmp4
252          barfs.  So, check if the x264opts aren't using B-frames, and
253          when they aren't, set the boolean job->areBframes as false.
254          */
255         job->areBframes = 0;
256     }
257     
258     if( param.i_keyint_min != 25 || param.i_keyint_max != 250 )
259         hb_log("encx264: keyint-min: %i, keyint-max: %i", param.i_keyint_min, param.i_keyint_max);
260
261     /* set up the VUI color model & gamma to match what the COLR atom
262      * set in muxmp4.c says. See libhb/muxmp4.c for notes. */
263     if( job->color_matrix == 1 )
264     {
265         // ITU BT.601 DVD or SD TV content
266         param.vui.i_colorprim = 6;
267         param.vui.i_transfer = 1;
268         param.vui.i_colmatrix = 6;
269     }
270     else if( job->color_matrix == 2 )
271     {
272         // ITU BT.709 HD content
273         param.vui.i_colorprim = 1;
274         param.vui.i_transfer = 1;
275         param.vui.i_colmatrix = 1;
276     }
277     else if ( job->title->width >= 1280 || job->title->height >= 720 )
278     {
279         // we guess that 720p or above is ITU BT.709 HD content
280         param.vui.i_colorprim = 1;
281         param.vui.i_transfer = 1;
282         param.vui.i_colmatrix = 1;
283     }
284     else
285     {
286         // ITU BT.601 DVD or SD TV content
287         param.vui.i_colorprim = 6;
288         param.vui.i_transfer = 1;
289         param.vui.i_colmatrix = 6;
290     }
291
292     if( job->anamorphic.mode )
293     {
294         param.vui.i_sar_width  = job->anamorphic.par_width;
295         param.vui.i_sar_height = job->anamorphic.par_height;
296
297         hb_log( "encx264: encoding with stored aspect %d/%d",
298                 param.vui.i_sar_width, param.vui.i_sar_height );
299     }
300
301
302     if( job->vquality > 0.0 && job->vquality < 1.0 )
303     {
304         /*Constant RF*/
305         param.rc.i_rc_method = X264_RC_CRF;
306         param.rc.f_rf_constant = 51 - job->vquality * 51;
307         hb_log( "encx264: Encoding at constant RF %f", param.rc.f_rf_constant );
308     }
309     else if( job->vquality == 0 || job->vquality >= 1.0 )
310     {
311         /* Use the vquality as a raw RF or QP
312           instead of treating it like a percentage. */
313         /*Constant RF*/
314         param.rc.i_rc_method = X264_RC_CRF;
315         param.rc.f_rf_constant = job->vquality;
316         hb_log( "encx264: Encoding at constant RF %f", param.rc.f_rf_constant );
317     }
318     else
319     {
320         /* Rate control */
321         param.rc.i_rc_method = X264_RC_ABR;
322         param.rc.i_bitrate = job->vbitrate;
323         switch( job->pass )
324         {
325             case 1:
326                 param.rc.b_stat_write  = 1;
327                 param.rc.psz_stat_out = pv->filename;
328                 break;
329             case 2:
330                 param.rc.b_stat_read = 1;
331                 param.rc.psz_stat_in = pv->filename;
332                 break;
333         }
334     }
335
336     hb_deep_log( 2, "encx264: opening libx264 (pass %d)", job->pass );
337     pv->x264 = x264_encoder_open( &param );
338
339     x264_encoder_headers( pv->x264, &nal, &nal_count );
340
341     /* Sequence Parameter Set */
342     memcpy(w->config->h264.sps, nal[1].p_payload + 4, nal[1].i_payload - 4);
343     w->config->h264.sps_length = nal[1].i_payload - 4;
344
345     /* Picture Parameter Set */
346     memcpy(w->config->h264.pps, nal[2].p_payload + 4, nal[2].i_payload - 4);
347     w->config->h264.pps_length = nal[2].i_payload - 4;
348
349     x264_picture_alloc( &pv->pic_in, X264_CSP_I420,
350                         job->width, job->height );
351
352     pv->pic_in.img.i_stride[2] = pv->pic_in.img.i_stride[1] = ( ( job->width + 1 ) >> 1 );
353     pv->x264_allocated_pic = pv->pic_in.img.plane[0];
354
355     return 0;
356 }
357
358 void encx264Close( hb_work_object_t * w )
359 {
360     hb_work_private_t * pv = w->private_data;
361
362     if ( pv->frames_split )
363     {
364         hb_log( "encx264: %u frames had to be split (%u in, %u out)",
365                 pv->frames_split, pv->frames_in, pv->frames_out );
366     }
367     /*
368      * Patch the x264 allocated data back in so that x264 can free it
369      * we have been using our own buffers during the encode to avoid copying.
370      */
371     pv->pic_in.img.plane[0] = pv->x264_allocated_pic;
372     x264_picture_clean( &pv->pic_in );
373     x264_encoder_close( pv->x264 );
374     free( pv );
375     w->private_data = NULL;
376
377     /* TODO */
378 }
379
380 /*
381  * see comments in definition of 'frame_info' in pv struct for description
382  * of what these routines are doing.
383  */
384 static void save_frame_info( hb_work_private_t * pv, hb_buffer_t * in )
385 {
386     int i = (in->start >> FRAME_INFO_MAX2) & FRAME_INFO_MASK;
387     pv->frame_info[i].duration = in->stop - in->start;
388 }
389
390 static int64_t get_frame_duration( hb_work_private_t * pv, int64_t pts )
391 {
392     int i = (pts >> FRAME_INFO_MAX2) & FRAME_INFO_MASK;
393     return pv->frame_info[i].duration;
394 }
395
396 static hb_buffer_t *nal_encode( hb_work_object_t *w, x264_picture_t *pic_out,
397                                 int i_nal, x264_nal_t *nal )
398 {
399     hb_buffer_t *buf = NULL;
400     hb_work_private_t *pv = w->private_data;
401     hb_job_t *job = pv->job;
402
403     /* Should be way too large */
404     buf = hb_video_buffer_init( job->width, job->height );
405     buf->size = 0;
406     buf->frametype = 0;
407
408     // use the pts to get the original frame's duration.
409     int64_t duration  = get_frame_duration( pv, pic_out->i_pts );
410     buf->start = pic_out->i_pts;
411     buf->stop  = pic_out->i_pts + duration;
412     buf->renderOffset = pic_out->i_dts;
413     if ( !w->config->h264.init_delay && pic_out->i_dts < 0 )
414     {
415         w->config->h264.init_delay = -pic_out->i_dts;
416     }
417
418     /* Encode all the NALs we were given into buf.
419        NOTE: This code assumes one video frame per NAL (but there can
420              be other stuff like SPS and/or PPS). If there are multiple
421              frames we only get the duration of the first which will
422              eventually screw up the muxer & decoder. */
423     int i;
424     for( i = 0; i < i_nal; i++ )
425     {
426         int size = nal[i].i_payload;
427         memcpy(buf->data + buf->size, nal[i].p_payload, size);
428         if( size < 1 )
429         {
430             continue;
431         }
432
433         if( job->mux & HB_MUX_AVI )
434         {
435             if( nal[i].i_ref_idc == NAL_PRIORITY_HIGHEST )
436             {
437                 buf->frametype = HB_FRAME_KEY;
438             }
439             buf->size += size;
440             continue;
441         }
442
443         /* H.264 in .mp4 or .mkv */
444         switch( nal[i].i_type )
445         {
446             /* Sequence Parameter Set & Program Parameter Set go in the
447              * mp4 header so skip them here
448              */
449             case NAL_SPS:
450             case NAL_PPS:
451                 continue;
452
453             case NAL_SLICE:
454             case NAL_SLICE_IDR:
455             case NAL_SEI:
456             default:
457                 break;
458         }
459
460         /* Decide what type of frame we have. */
461         switch( pic_out->i_type )
462         {
463             case X264_TYPE_IDR:
464                 buf->frametype = HB_FRAME_IDR;
465                 /* if we have a chapter marker pending and this
466                    frame's presentation time stamp is at or after
467                    the marker's time stamp, use this as the
468                    chapter start. */
469                 if( pv->next_chap != 0 && pv->next_chap <= pic_out->i_pts )
470                 {
471                     pv->next_chap = 0;
472                     buf->new_chap = pv->chap_mark;
473                 }
474                 break;
475
476             case X264_TYPE_I:
477                 buf->frametype = HB_FRAME_I;
478                 break;
479
480             case X264_TYPE_P:
481                 buf->frametype = HB_FRAME_P;
482                 break;
483
484             case X264_TYPE_B:
485                 buf->frametype = HB_FRAME_B;
486                 break;
487
488         /*  This is for b-pyramid, which has reference b-frames
489             However, it doesn't seem to ever be used... */
490             case X264_TYPE_BREF:
491                 buf->frametype = HB_FRAME_BREF;
492                 break;
493
494             // If it isn't the above, what type of frame is it??
495             default:
496                 buf->frametype = 0;
497                 break;
498         }
499
500         /* Since libx264 doesn't tell us when b-frames are
501            themselves reference frames, figure it out on our own. */
502         if( (buf->frametype == HB_FRAME_B) &&
503             (nal[i].i_ref_idc != NAL_PRIORITY_DISPOSABLE) )
504             buf->frametype = HB_FRAME_BREF;
505
506         /* Expose disposable bit to muxer. */
507         if( nal[i].i_ref_idc == NAL_PRIORITY_DISPOSABLE )
508             buf->flags &= ~HB_FRAME_REF;
509         else
510             buf->flags |= HB_FRAME_REF;
511
512         buf->size += size;
513     }
514     // make sure we found at least one video frame
515     if ( buf->size <= 0 )
516     {
517         // no video - discard the buf
518         hb_buffer_close( &buf );
519     }
520     return buf;
521 }
522
523 static hb_buffer_t *x264_encode( hb_work_object_t *w, hb_buffer_t *in )
524 {
525     hb_work_private_t *pv = w->private_data;
526     hb_job_t *job = pv->job;
527
528     /* Point x264 at our current buffers Y(UV) data.  */
529     pv->pic_in.img.plane[0] = in->data;
530
531     int uvsize = ( (job->width + 1) >> 1 ) * ( (job->height + 1) >> 1 );
532     if( job->grayscale )
533     {
534         /* XXX x264 has currently no option for grayscale encoding */
535         memset( pv->pic_in.img.plane[1], 0x80, uvsize );
536         memset( pv->pic_in.img.plane[2], 0x80, uvsize );
537     }
538     else
539     {
540         /* Point x264 at our buffers (Y)UV data */
541         pv->pic_in.img.plane[1] = in->data + job->width * job->height;
542         pv->pic_in.img.plane[2] = pv->pic_in.img.plane[1] + uvsize;
543     }
544     if( in->new_chap && job->chapter_markers )
545     {
546         /* chapters have to start with an IDR frame so request that this
547            frame be coded as IDR. Since there may be up to 16 frames
548            currently buffered in the encoder remember the timestamp so
549            when this frame finally pops out of the encoder we'll mark
550            its buffer as the start of a chapter. */
551         pv->pic_in.i_type = X264_TYPE_IDR;
552         if( pv->next_chap == 0 )
553         {
554             pv->next_chap = in->start;
555             pv->chap_mark = in->new_chap;
556         }
557         /* don't let 'work_loop' put a chapter mark on the wrong buffer */
558         in->new_chap = 0;
559     }
560     else
561     {
562         pv->pic_in.i_type = X264_TYPE_AUTO;
563     }
564     pv->pic_in.i_qpplus1 = 0;
565
566     /* XXX this is temporary debugging code to check that the upstream
567      * modules (render & sync) have generated a continuous, self-consistent
568      * frame stream with the current frame's start time equal to the
569      * previous frame's stop time.
570      */
571     if( pv->last_stop != in->start )
572     {
573         hb_log("encx264 input continuity err: last stop %"PRId64"  start %"PRId64,
574                 pv->last_stop, in->start);
575     }
576     pv->last_stop = in->stop;
577
578     // Remember info about this frame that we need to pass across
579     // the x264_encoder_encode call (since it reorders frames).
580     save_frame_info( pv, in );
581
582     /* Feed the input PTS to x264 so it can figure out proper output PTS */
583     pv->pic_in.i_pts = in->start;
584
585     x264_picture_t pic_out;
586     int i_nal;
587     x264_nal_t *nal;
588
589     x264_encoder_encode( pv->x264, &nal, &i_nal, &pv->pic_in, &pic_out );
590     if ( i_nal > 0 )
591     {
592         return nal_encode( w, &pic_out, i_nal, nal );
593     }
594     return NULL;
595 }
596
597 int encx264Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
598                   hb_buffer_t ** buf_out )
599 {
600     hb_work_private_t *pv = w->private_data;
601     hb_buffer_t *in = *buf_in;
602
603     *buf_out = NULL;
604
605     if( in->size <= 0 )
606     {
607         // EOF on input. Flush any frames still in the decoder then
608         // send the eof downstream to tell the muxer we're done.
609         x264_picture_t pic_out;
610         int i_nal;
611         x264_nal_t *nal;
612         hb_buffer_t *last_buf = NULL;
613
614         while (1)
615         {
616             x264_encoder_encode( pv->x264, &nal, &i_nal, NULL, &pic_out );
617             if ( i_nal <= 0 )
618                 break;
619
620             hb_buffer_t *buf = nal_encode( w, &pic_out, i_nal, nal );
621             if ( buf )
622             {
623                 ++pv->frames_out;
624                 if ( last_buf == NULL )
625                     *buf_out = buf;
626                 else
627                     last_buf->next = buf;
628                 last_buf = buf;
629             }
630         }
631         // Flushed everything - add the eof to the end of the chain.
632         if ( last_buf == NULL )
633             *buf_out = in;
634         else
635             last_buf->next = in;
636
637         *buf_in = NULL;
638         return HB_WORK_DONE;
639     }
640
641     // Not EOF - encode the packet & wrap it in a NAL
642     ++pv->frames_in;
643     ++pv->frames_out;
644     *buf_out = x264_encode( w, in );
645     return HB_WORK_OK;
646 }