OSDN Git Service

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