OSDN Git Service

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