OSDN Git Service

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