OSDN Git Service

HD Home Run seems to strip the PCR from some streams (which makes HB refuse to proces...
[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     int            chap_mark;   // saved chap mark when we're propagating it
54     int64_t        last_stop;   // Debugging - stop time of previous input frame
55     int64_t        init_delay;
56     int64_t        next_chap;
57
58     struct {
59         int64_t duration;
60     } frame_info[FRAME_INFO_SIZE];
61
62     char             filename[1024];
63 };
64
65 /***********************************************************************
66  * hb_work_encx264_init
67  ***********************************************************************
68  *
69  **********************************************************************/
70 int encx264Init( hb_work_object_t * w, hb_job_t * job )
71 {
72     x264_param_t       param;
73     x264_nal_t       * nal;
74     int                nal_count;
75     int                nal_size;
76
77     hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
78     w->private_data = pv;
79
80     pv->job = job;
81
82     memset( pv->filename, 0, 1024 );
83     hb_get_tempory_filename( job->h, pv->filename, "x264.log" );
84
85     x264_param_default( &param );
86
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;
92
93     if (job->vrate_base != 1080000)
94     {
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);
100     }
101
102     param.i_log_level  = X264_LOG_INFO;
103     if( job->h264_level )
104     {
105         param.b_cabac     = 0;
106         param.i_level_idc = job->h264_level;
107         hb_log( "encx264: encoding at level %i",
108                 param.i_level_idc );
109     }
110
111     /* Slightly faster with minimal quality lost */
112     param.analyse.i_subpel_refine = 4;
113
114     /*
115         This section passes the string x264opts to libx264 for parsing into
116         parameter names and values.
117
118         The string is set up like this:
119         option1=value1:option2=value 2
120
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.
124
125         This is all based on the universal x264 option handling Loren
126         Merritt implemented in the Mplayer/Mencoder project.
127      */
128
129     if( job->x264opts != NULL && *job->x264opts != '\0' )
130     {
131         char *x264opts, *x264opts_start;
132
133         x264opts = x264opts_start = strdup(job->x264opts);
134
135         while( x264opts_start && *x264opts )
136         {
137             char *name = x264opts;
138             char *value;
139             int ret;
140
141             x264opts += strcspn( x264opts, ":" );
142             if( *x264opts )
143             {
144                 *x264opts = 0;
145                 x264opts++;
146             }
147
148             value = strchr( name, '=' );
149             if( value )
150             {
151                 *value = 0;
152                 value++;
153             }
154
155             /*
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.
161              */
162
163             if( !( strcmp( name, "bframes" ) ) )
164             {
165                 if( atoi( value ) > 0 )
166                 {
167                     job->areBframes = 1;
168                 }
169             }
170
171             /* Note b-pyramid here, so the initial delay can be doubled */
172             if( !( strcmp( name, "b-pyramid" ) ) )
173             {
174                 if( value != NULL )
175                 {
176                     if( atoi( value ) > 0 )
177                     {
178                         job->areBframes = 2;
179                     }
180                 }
181                 else
182                 {
183                     job->areBframes = 2;
184                 }
185             }
186
187             /* Here's where the strings are passed to libx264 for parsing. */
188             ret = x264_param_parse( &param, name, value );
189
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)" );
195         }
196         free(x264opts_start);
197     }
198
199     /* set up the VUI color model & gamma to match what the COLR atom
200      * set in muxmp4.c says. See libhb/muxmp4.c for notes. */
201
202     if ( job->title->height >= 720 )
203     {
204         // we guess that 720p or above is ITU BT.709 HD content
205         param.vui.i_colorprim = 1;
206         param.vui.i_transfer = 1;
207         param.vui.i_colmatrix = 1;
208     }
209     else
210     {
211         // ITU BT.601 DVD or SD TV content
212         param.vui.i_colorprim = 6;
213         param.vui.i_transfer = 1;
214         param.vui.i_colmatrix = 6;
215     }
216
217     if( job->pixel_ratio )
218     {
219         param.vui.i_sar_width = job->pixel_aspect_width;
220         param.vui.i_sar_height = job->pixel_aspect_height;
221
222         hb_log( "encx264: encoding with stored aspect %d/%d",
223                 param.vui.i_sar_width, param.vui.i_sar_height );
224     }
225
226
227     if( job->vquality > 0.0 && job->vquality < 1.0 )
228     {
229         switch( job->crf )
230         {
231             case 1:
232                 /*Constant RF*/
233                 param.rc.i_rc_method = X264_RC_CRF;
234                 param.rc.f_rf_constant = 51 - job->vquality * 51;
235                 hb_log( "encx264: Encoding at constant RF %f",
236                         param.rc.f_rf_constant );
237                 break;
238
239             case 0:
240                 /*Constant QP*/
241                 param.rc.i_rc_method = X264_RC_CQP;
242                 param.rc.i_qp_constant = 51 - job->vquality * 51;
243                 hb_log( "encx264: encoding at constant QP %d",
244                         param.rc.i_qp_constant );
245                 break;
246         }
247     }
248     else if( job->vquality == 0 || job->vquality >= 1.0 )
249     {
250         /* Use the vquality as a raw RF or QP
251           instead of treating it like a percentage. */
252         switch( job->crf )
253         {
254             case 1:
255                 /*Constant RF*/
256                 param.rc.i_rc_method = X264_RC_CRF;
257                 param.rc.f_rf_constant = job->vquality;
258                 hb_log( "encx264: Encoding at constant RF %f",
259                         param.rc.f_rf_constant );
260                 break;
261
262             case 0:
263                 /*Constant QP*/
264                 param.rc.i_rc_method = X264_RC_CQP;
265                 param.rc.i_qp_constant = job->vquality;
266                 hb_log( "encx264: encoding at constant QP %d",
267                         param.rc.i_qp_constant );
268                 break;
269         }        
270     }
271     else
272     {
273         /* Rate control */
274         param.rc.i_rc_method = X264_RC_ABR;
275         param.rc.i_bitrate = job->vbitrate;
276         switch( job->pass )
277         {
278             case 1:
279                 param.rc.b_stat_write  = 1;
280                 param.rc.psz_stat_out = pv->filename;
281                 break;
282             case 2:
283                 param.rc.b_stat_read = 1;
284                 param.rc.psz_stat_in = pv->filename;
285                 break;
286         }
287     }
288
289     hb_log( "encx264: opening libx264 (pass %d)", job->pass );
290     pv->x264 = x264_encoder_open( &param );
291
292     x264_encoder_headers( pv->x264, &nal, &nal_count );
293
294     /* Sequence Parameter Set */
295     x264_nal_encode( w->config->h264.sps, &nal_size, 0, &nal[1] );
296     w->config->h264.sps_length = nal_size;
297
298     /* Picture Parameter Set */
299     x264_nal_encode( w->config->h264.pps, &nal_size, 0, &nal[2] );
300     w->config->h264.pps_length = nal_size;
301
302     x264_picture_alloc( &pv->pic_in, X264_CSP_I420,
303                         job->width, job->height );
304
305     pv->x264_allocated_pic = pv->pic_in.img.plane[0];
306
307     if (job->areBframes)
308     {
309         /* Basic initDelay value is the clockrate divided by the FPS
310            -- the length of one frame in clockticks.                  */
311         pv->init_delay = 90000. / ((double)job->vrate / (double)job->vrate_base);
312
313         /* 23.976-length frames are 3753.75 ticks long on average but the DVD
314            creates that average rate by repeating 59.95 fields so the max
315            frame size is actually 4504.5 (3 field times). The field durations
316            are computed based on quantized times (see below) so we need an extra
317            two ticks to account for the rounding. */
318         if (pv->init_delay == 3753)
319             pv->init_delay = 4507;
320
321         /* frame rates are not exact in the DVD 90KHz PTS clock (they are
322            exact in the DVD 27MHz system clock but we never see that) so the
323            rates computed above are all +-1 due to quantization. Worst case
324            is when a clock-rounded-down frame is adjacent to a rounded-up frame
325            which makes one of the frames 2 ticks longer than the nominal
326            frame time. */
327         pv->init_delay += 2;
328
329         /* For VFR, libhb sees the FPS as 29.97, but the longest frames
330            will use the duration of frames running at 23.976fps instead.. */
331         if (job->vfr)
332         {
333             pv->init_delay = 7506;
334         }
335
336         /* The delay is 1 frames for regular b-frames, 2 for b-pyramid. */
337         pv->init_delay *= job->areBframes;
338     }
339     w->config->h264.init_delay = pv->init_delay;
340
341     return 0;
342 }
343
344 void encx264Close( hb_work_object_t * w )
345 {
346     hb_work_private_t * pv = w->private_data;
347     /*
348      * Patch the x264 allocated data back in so that x264 can free it
349      * we have been using our own buffers during the encode to avoid copying.
350      */
351     pv->pic_in.img.plane[0] = pv->x264_allocated_pic;
352     x264_picture_clean( &pv->pic_in );
353     x264_encoder_close( pv->x264 );
354     free( pv );
355     w->private_data = NULL;
356
357     /* TODO */
358 }
359
360 /*
361  * see comments in definition of 'frame_info' in pv struct for description
362  * of what these routines are doing.
363  */
364 static void save_frame_info( hb_work_private_t * pv, hb_buffer_t * in )
365 {
366     int i = (in->start >> FRAME_INFO_MAX2) & FRAME_INFO_MASK;
367     pv->frame_info[i].duration = in->stop - in->start;
368 }
369
370 static int64_t get_frame_duration( hb_work_private_t * pv, int64_t pts )
371 {
372     int i = (pts >> FRAME_INFO_MAX2) & FRAME_INFO_MASK;
373     return pv->frame_info[i].duration;
374 }
375
376 static hb_buffer_t *nal_encode( hb_work_object_t *w, x264_picture_t *pic_out,
377                                 int i_nal, x264_nal_t *nal )
378 {
379     hb_buffer_t *buf = NULL;
380     hb_work_private_t *pv = w->private_data;
381     hb_job_t *job = pv->job;
382
383     /* Should be way too large */
384     buf = hb_buffer_init( 3 * job->width * job->height / 2 );
385     buf->size = 0;
386     buf->frametype = 0;
387
388     // use the pts to get the original frame's duration.
389     int64_t duration  = get_frame_duration( pv, pic_out->i_pts );
390     buf->start = pic_out->i_pts;
391     buf->stop  = pic_out->i_pts + duration;
392
393     /* Encode all the NALs we were given into buf.
394        NOTE: This code assumes one video frame per NAL (but there can
395              be other stuff like SPS and/or PPS). If there are multiple
396              frames we only get the duration of the first which will
397              eventually screw up the muxer & decoder. */
398     int i;
399     for( i = 0; i < i_nal; i++ )
400     {
401         int data = buf->alloc - buf->size;
402         int size = x264_nal_encode( buf->data + buf->size, &data, 1, &nal[i] );
403         if( size < 1 )
404         {
405             continue;
406         }
407
408         if( job->mux & HB_MUX_AVI )
409         {
410             if( nal[i].i_ref_idc == NAL_PRIORITY_HIGHEST )
411             {
412                 buf->frametype = HB_FRAME_KEY;
413             }
414             buf->size += size;
415             continue;
416         }
417
418         /* H.264 in .mp4 or .mkv */
419         int naltype = buf->data[buf->size+4] & 0x1f;
420         if ( naltype == 0x7 || naltype == 0x8 )
421         {
422             // Sequence Parameter Set & Program Parameter Set go in the
423             // mp4 header so skip them here
424             continue;
425         }
426
427         /* H.264 in mp4 (stolen from mp4creator) */
428         buf->data[buf->size+0] = ( ( size - 4 ) >> 24 ) & 0xFF;
429         buf->data[buf->size+1] = ( ( size - 4 ) >> 16 ) & 0xFF;
430         buf->data[buf->size+2] = ( ( size - 4 ) >>  8 ) & 0xFF;
431         buf->data[buf->size+3] = ( ( size - 4 ) >>  0 ) & 0xFF;
432
433         /* Decide what type of frame we have. */
434         switch( pic_out->i_type )
435         {
436             case X264_TYPE_IDR:
437                 buf->frametype = HB_FRAME_IDR;
438                 /* if we have a chapter marker pending and this
439                    frame's presentation time stamp is at or after
440                    the marker's time stamp, use this as the
441                    chapter start. */
442                 if( pv->next_chap != 0 && pv->next_chap <= pic_out->i_pts )
443                 {
444                     pv->next_chap = 0;
445                     buf->new_chap = pv->chap_mark;
446                 }
447                 break;
448
449             case X264_TYPE_I:
450                 buf->frametype = HB_FRAME_I;
451                 break;
452
453             case X264_TYPE_P:
454                 buf->frametype = HB_FRAME_P;
455                 break;
456
457             case X264_TYPE_B:
458                 buf->frametype = HB_FRAME_B;
459                 break;
460
461         /*  This is for b-pyramid, which has reference b-frames
462             However, it doesn't seem to ever be used... */
463             case X264_TYPE_BREF:
464                 buf->frametype = HB_FRAME_BREF;
465                 break;
466
467             // If it isn't the above, what type of frame is it??
468             default:
469                 buf->frametype = 0;
470                 break;
471         }
472
473         /* Since libx264 doesn't tell us when b-frames are
474            themselves reference frames, figure it out on our own. */
475         if( (buf->frametype == HB_FRAME_B) &&
476             (nal[i].i_ref_idc != NAL_PRIORITY_DISPOSABLE) )
477             buf->frametype = HB_FRAME_BREF;
478
479         buf->size += size;
480     }
481     // make sure we found at least one video frame
482     if ( buf->size <= 0 )
483     {
484         // no video - discard the buf
485         hb_buffer_close( &buf );
486     }
487     return buf;
488 }
489
490 static hb_buffer_t *x264_encode( hb_work_object_t *w, hb_buffer_t *in )
491 {
492     hb_work_private_t *pv = w->private_data;
493     hb_job_t *job = pv->job;
494
495     /* Point x264 at our current buffers Y(UV) data.  */
496     pv->pic_in.img.plane[0] = in->data;
497
498     if( job->grayscale )
499     {
500         /* XXX x264 has currently no option for grayscale encoding */
501         memset( pv->pic_in.img.plane[1], 0x80, job->width * job->height / 4 );
502         memset( pv->pic_in.img.plane[2], 0x80, job->width * job->height / 4 );
503     }
504     else
505     {
506         /* Point x264 at our buffers (Y)UV data */
507         pv->pic_in.img.plane[1] = in->data + job->width * job->height;
508         pv->pic_in.img.plane[2] = in->data + 5 * job->width * job->height / 4;
509     }
510     if( in->new_chap && job->chapter_markers )
511     {
512         /* chapters have to start with an IDR frame so request that this
513            frame be coded as IDR. Since there may be up to 16 frames
514            currently buffered in the encoder remember the timestamp so
515            when this frame finally pops out of the encoder we'll mark
516            its buffer as the start of a chapter. */
517         pv->pic_in.i_type = X264_TYPE_IDR;
518         if( pv->next_chap == 0 )
519         {
520             pv->next_chap = in->start;
521             pv->chap_mark = in->new_chap;
522         }
523         /* don't let 'work_loop' put a chapter mark on the wrong buffer */
524         in->new_chap = 0;
525     }
526     else
527     {
528         pv->pic_in.i_type = X264_TYPE_AUTO;
529     }
530     pv->pic_in.i_qpplus1 = 0;
531
532     /* XXX this is temporary debugging code to check that the upstream
533      * modules (render & sync) have generated a continuous, self-consistent
534      * frame stream with the current frame's start time equal to the
535      * previous frame's stop time.
536      */
537     if( pv->last_stop != in->start )
538     {
539         hb_log("encx264 input continuity err: last stop %lld  start %lld",
540                 pv->last_stop, in->start);
541     }
542     pv->last_stop = in->stop;
543
544     // Remember info about this frame that we need to pass across
545     // the x264_encoder_encode call (since it reorders frames).
546     save_frame_info( pv, in );
547
548     /* Feed the input PTS to x264 so it can figure out proper output PTS */
549     pv->pic_in.i_pts = in->start;
550
551     x264_picture_t pic_out;
552     int i_nal;
553     x264_nal_t *nal;
554
555     x264_encoder_encode( pv->x264, &nal, &i_nal, &pv->pic_in, &pic_out );
556     if ( i_nal > 0 )
557     {
558         return nal_encode( w, &pic_out, i_nal, nal );
559     }
560     return NULL;
561 }
562
563 int encx264Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
564                   hb_buffer_t ** buf_out )
565 {
566     hb_work_private_t *pv = w->private_data;
567     hb_buffer_t *in = *buf_in;
568
569     *buf_out = NULL;
570
571     if( in->size <= 0 )
572     {
573         // EOF on input. Flush any frames still in the decoder then
574         // send the eof downstream to tell the muxer we're done.
575         x264_picture_t pic_out;
576         int i_nal;
577         x264_nal_t *nal;
578         hb_buffer_t *last_buf = NULL;
579
580         while (1)
581         {
582             x264_encoder_encode( pv->x264, &nal, &i_nal, NULL, &pic_out );
583             if ( i_nal <= 0 )
584                 break;
585
586             hb_buffer_t *buf = nal_encode( w, &pic_out, i_nal, nal );
587             if ( buf )
588             {
589                 if ( last_buf == NULL )
590                     *buf_out = buf;
591                 else
592                     last_buf->next = buf;
593                 last_buf = buf;
594             }
595         }
596         // Flushed everything - add the eof to the end of the chain.
597         if ( last_buf == NULL )
598             *buf_out = in;
599         else
600             last_buf->next = in;
601
602         *buf_in = NULL;
603         return HB_WORK_DONE;
604     }
605
606     // Not EOF - encode the packet & wrap it in a NAL
607
608     // if we're re-ordering frames, check if this frame is too large to reorder
609     if ( pv->init_delay && in->stop - in->start > pv->init_delay )
610     {
611         // This frame's duration is larger than the time allotted for b-frame
612         // reordering. That means that if it's used as a reference the decoder
613         // won't be able to move it early enough to render it in correct
614         // sequence & the playback will have odd jumps & twitches. To make
615         // sure this doesn't happen we pretend this frame is multiple
616         // frames, each with duration <= init_delay. Since each of these
617         // new frames contains the same image the visual effect is identical
618         // to the original but the resulting stream can now be coded without
619         // error. We take advantage of the fact that x264 buffers frame
620         // data internally to feed the same image into the encoder multiple
621         // times, just changing its start & stop times each time.
622         int64_t orig_stop = in->stop;
623         int64_t new_stop = in->start;
624         hb_buffer_t *last_buf = NULL;
625
626         // We want to spread the new frames uniformly over the total time
627         // so that we don't end up with a very short frame at the end.
628         // In the number of pieces calculation we add in init_delay-1 to
629         // round up but not add an extra piece if the frame duration is
630         // a multiple of init_delay. The final increment of frame_dur is
631         // to restore the bits that got truncated by the divide on the
632         // previous line. If we don't do this we end up with an extra tiny
633         // frame at the end whose duration is npieces-1.
634         int64_t frame_dur = orig_stop - new_stop;
635         int64_t npieces = ( frame_dur + pv->init_delay - 1 ) / pv->init_delay;
636         frame_dur /= npieces;
637         ++frame_dur;
638
639         while ( in->start < orig_stop )
640         {
641             new_stop += frame_dur;
642             if ( new_stop > orig_stop )
643                 new_stop = orig_stop;
644             in->stop = new_stop;
645             hb_buffer_t *buf = x264_encode( w, in );
646             if ( buf )
647             {
648                 if ( last_buf == NULL )
649                     *buf_out = buf;
650                 else
651                     last_buf->next = buf;
652                 last_buf = buf;
653             }
654             in->start = new_stop;
655         }
656     }
657     else
658     {
659         *buf_out = x264_encode( w, in );
660     }
661     return HB_WORK_OK;
662 }