OSDN Git Service

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