OSDN Git Service

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