OSDN Git Service

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