OSDN Git Service

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