OSDN Git Service

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