OSDN Git Service

fix a crash when a TS has no aspect ratio set in the video stream
[handbrake-jp/handbrake-jp-git.git] / libhb / decavcodec.c
1 /* $Id: decavcodec.c,v 1.6 2005/03/06 04:08:54 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 /* This module is Handbrake's interface to the ffmpeg decoder library
8    (libavcodec & small parts of libavformat). It contains four Handbrake
9    "work objects":
10
11     decavcodec  connects HB to an ffmpeg audio decoder
12     decavcodecv connects HB to an ffmpeg video decoder
13
14         (Two different routines are needed because the ffmpeg library
15         has different decoder calling conventions for audio & video.
16         The audio decoder should have had its name changed to "decavcodeca"
17         but I got lazy.) These work objects are self-contained & follow all
18         of HB's conventions for a decoder module. They can be used like
19         any other HB decoder (deca52, decmpeg2, etc.).
20
21     decavcodecai "internal" (incestuous?) version of decavcodec
22     decavcodecvi "internal" (incestuous?) version of decavcodecv
23
24         These routine are functionally equivalent to the routines above but
25         can only be used by the ffmpeg-based stream reader in libhb/stream.c.
26         The reason they exist is because the ffmpeg library leaves some of
27         the information needed by the decoder in the AVStream (the data
28         structure used by the stream reader) and we need to retrieve it
29         to successfully decode frames. But in HB the reader and decoder
30         modules are in completely separate threads and nothing goes between
31         them but hb_buffers containing frames to be decoded. I.e., there's
32         no easy way for the ffmpeg stream reader to pass a pointer to its
33         AVStream over to the ffmpeg video or audio decoder. So the *i work
34         objects use a private back door to the stream reader to get access
35         to the AVStream (routines hb_ffmpeg_avstream and hb_ffmpeg_context)
36         and the codec_param passed to these work objects is the key to this
37         back door (it's basically an index that allows the correct AVStream
38         to be retrieved).
39
40     The normal & *i objects share a lot of code (the basic frame decoding
41     and bitstream info code is factored out into subroutines that can be
42     called by either) but the top level routines of the *i objects
43     (decavcodecviWork, decavcodecviInfo, etc.) are different because:
44      1) they *have* to use the AVCodecContext that's contained in the
45         reader's AVStream rather than just allocating & using their own,
46      2) the Info routines have access to stuff kept in the AVStream in addition
47         to stuff kept in the AVCodecContext. This shouldn't be necessary but
48         crucial information like video frame rate that should be in the
49         AVCodecContext is either missing or wrong in the version of ffmpeg
50         we're currently using.
51
52     A consequence of the above is that the non-i work objects *can't* use
53     information from the AVStream because there isn't one - they get their
54     data from either the dvd reader or the mpeg reader, not the ffmpeg stream
55     reader. That means that they have to make up for deficiencies in the
56     AVCodecContext info by using stuff kept in the HB "title" struct. It
57     also means that ffmpeg codecs that randomly scatter state needed by
58     the decoder across both the AVCodecContext & the AVStream (e.g., the
59     VC1 decoder) can't easily be used by the HB mpeg stream reader.
60  */
61
62 #include "hb.h"
63 #include "hbffmpeg.h"
64 #include "downmix.h"
65 #include "libavcodec/audioconvert.h"
66
67 static int  decavcodecInit( hb_work_object_t *, hb_job_t * );
68 static int  decavcodecWork( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
69 static void decavcodecClose( hb_work_object_t * );
70 static int decavcodecInfo( hb_work_object_t *, hb_work_info_t * );
71 static int decavcodecBSInfo( hb_work_object_t *, const hb_buffer_t *, hb_work_info_t * );
72
73 hb_work_object_t hb_decavcodec =
74 {
75     WORK_DECAVCODEC,
76     "MPGA decoder (libavcodec)",
77     decavcodecInit,
78     decavcodecWork,
79     decavcodecClose,
80     decavcodecInfo,
81     decavcodecBSInfo
82 };
83
84 #define HEAP_SIZE 8
85 typedef struct {
86     // there are nheap items on the heap indexed 1..nheap (i.e., top of
87     // heap is 1). The 0th slot is unused - a marker is put there to check
88     // for overwrite errs.
89     int64_t h[HEAP_SIZE+1];
90     int     nheap;
91 } pts_heap_t;
92
93 struct hb_work_private_s
94 {
95     hb_job_t        *job;
96     AVCodecContext  *context;
97     AVCodecParserContext *parser;
98     hb_list_t       *list;
99     double          duration;   // frame duration (for video)
100     double          pts_next;   // next pts we expect to generate
101     int64_t         pts;        // (video) pts passing from parser to decoder
102     int64_t         chap_time;  // time of next chap mark (if new_chap != 0)
103     int             new_chap;   // output chapter mark pending
104     uint32_t        nframes;
105     uint32_t        ndrops;
106     uint32_t        decode_errors;
107     int             brokenByMicrosoft; // video stream may contain packed b-frames
108     hb_buffer_t*    delayq[HEAP_SIZE];
109     pts_heap_t      pts_heap;
110     void*           buffer;
111     struct SwsContext *sws_context; // if we have to rescale or convert color space
112     hb_downmix_t    *downmix;
113     hb_sample_t     *downmix_buffer;
114 };
115
116 static void decodeAudio( hb_audio_t * audio, hb_work_private_t *pv, uint8_t *data, int size );
117 static hb_buffer_t *link_buf_list( hb_work_private_t *pv );
118
119
120 static int64_t heap_pop( pts_heap_t *heap )
121 {
122     int64_t result;
123
124     if ( heap->nheap <= 0 )
125     {
126         return -1;
127     }
128
129     // return the top of the heap then put the bottom element on top,
130     // decrease the heap size by one & rebalence the heap.
131     result = heap->h[1];
132
133     int64_t v = heap->h[heap->nheap--];
134     int parent = 1;
135     int child = parent << 1;
136     while ( child <= heap->nheap )
137     {
138         // find the smallest of the two children of parent
139         if (child < heap->nheap && heap->h[child] > heap->h[child+1] )
140             ++child;
141
142         if (v <= heap->h[child])
143             // new item is smaller than either child so it's the new parent.
144             break;
145
146         // smallest child is smaller than new item so move it up then
147         // check its children.
148         int64_t hp = heap->h[child];
149         heap->h[parent] = hp;
150         parent = child;
151         child = parent << 1;
152     }
153     heap->h[parent] = v;
154     return result;
155 }
156
157 static void heap_push( pts_heap_t *heap, int64_t v )
158 {
159     if ( heap->nheap < HEAP_SIZE )
160     {
161         ++heap->nheap;
162     }
163
164     // stick the new value on the bottom of the heap then bubble it
165     // up to its correct spot.
166         int child = heap->nheap;
167         while (child > 1) {
168                 int parent = child >> 1;
169                 if (heap->h[parent] <= v)
170                         break;
171                 // move parent down
172                 int64_t hp = heap->h[parent];
173                 heap->h[child] = hp;
174                 child = parent;
175         }
176         heap->h[child] = v;
177 }
178
179
180 /***********************************************************************
181  * hb_work_decavcodec_init
182  ***********************************************************************
183  *
184  **********************************************************************/
185 static int decavcodecInit( hb_work_object_t * w, hb_job_t * job )
186 {
187     AVCodec * codec;
188
189     hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
190     w->private_data = pv;
191
192     pv->job   = job;
193     pv->list  = hb_list_init();
194
195     int codec_id = w->codec_param;
196     /*XXX*/
197     if ( codec_id == 0 )
198         codec_id = CODEC_ID_MP2;
199
200     codec = avcodec_find_decoder( codec_id );
201     pv->parser = av_parser_init( codec_id );
202
203     pv->context = avcodec_alloc_context();
204     hb_avcodec_open( pv->context, codec );
205
206     if ( w->audio != NULL &&
207          hb_need_downmix( w->audio->config.in.channel_layout, 
208                           w->audio->config.out.mixdown) )
209     {
210         pv->downmix = hb_downmix_init(w->audio->config.in.channel_layout, 
211                                       w->audio->config.out.mixdown);
212         hb_downmix_set_chan_map( pv->downmix, &hb_smpte_chan_map, &hb_qt_chan_map );
213     }
214
215     return 0;
216 }
217
218 /***********************************************************************
219  * Close
220  ***********************************************************************
221  *
222  **********************************************************************/
223 static void decavcodecClose( hb_work_object_t * w )
224 {
225     hb_work_private_t * pv = w->private_data;
226
227     if ( pv )
228     {
229         if ( pv->job && pv->context && pv->context->codec )
230         {
231             hb_log( "%s-decoder done: %u frames, %u decoder errors, %u drops",
232                     pv->context->codec->name, pv->nframes, pv->decode_errors,
233                     pv->ndrops );
234         }
235         if ( pv->sws_context )
236         {
237             sws_freeContext( pv->sws_context );
238         }
239         if ( pv->parser )
240         {
241             av_parser_close(pv->parser);
242         }
243         if ( pv->context && pv->context->codec )
244         {
245             hb_avcodec_close( pv->context );
246         }
247         if ( pv->list )
248         {
249             hb_list_close( &pv->list );
250         }
251         if ( pv->buffer )
252         {
253             av_free( pv->buffer );
254             pv->buffer = NULL;
255         }
256         if ( pv->downmix )
257         {
258             hb_downmix_close( &(pv->downmix) );
259         }
260         if ( pv->downmix_buffer )
261         {
262             free( pv->downmix_buffer );
263             pv->downmix_buffer = NULL;
264         }
265         free( pv );
266         w->private_data = NULL;
267     }
268 }
269
270 /***********************************************************************
271  * Work
272  ***********************************************************************
273  *
274  **********************************************************************/
275 static int decavcodecWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
276                     hb_buffer_t ** buf_out )
277 {
278     hb_work_private_t * pv = w->private_data;
279     hb_buffer_t * in = *buf_in;
280
281     if ( in->size <= 0 )
282     {
283         /* EOF on input stream - send it downstream & say that we're done */
284         *buf_out = in;
285         *buf_in = NULL;
286         return HB_WORK_DONE;
287     }
288
289     *buf_out = NULL;
290
291     if ( in->start < -1 && pv->pts_next <= 0 )
292     {
293         // discard buffers that start before video time 0
294         return HB_WORK_OK;
295     }
296
297     // if the packet has a timestamp use it 
298     if ( in->start != -1 )
299     {
300         pv->pts_next = in->start;
301     }
302
303     int pos, len;
304     for ( pos = 0; pos < in->size; pos += len )
305     {
306         uint8_t *parser_output_buffer;
307         int parser_output_buffer_len;
308         int64_t cur = pv->pts_next;
309
310         if ( pv->parser != NULL )
311         {
312             len = av_parser_parse2( pv->parser, pv->context,
313                     &parser_output_buffer, &parser_output_buffer_len,
314                     in->data + pos, in->size - pos, cur, cur, AV_NOPTS_VALUE );
315         }
316         else
317         {
318             parser_output_buffer = in->data;
319             len = parser_output_buffer_len = in->size;
320         }
321         if (parser_output_buffer_len)
322         {
323             // set the duration on every frame since the stream format can
324             // change (it shouldn't but there's no way to guarantee it).
325             // duration is a scaling factor to go from #bytes in the decoded
326             // frame to frame time (in 90KHz mpeg ticks). 'channels' converts
327             // total samples to per-channel samples. 'sample_rate' converts
328             // per-channel samples to seconds per sample and the 90000
329             // is mpeg ticks per second.
330             if ( pv->context->sample_rate && pv->context->channels )
331             {
332                 pv->duration = 90000. /
333                             (double)( pv->context->sample_rate * pv->context->channels );
334             }
335             decodeAudio( w->audio, pv, parser_output_buffer, parser_output_buffer_len );
336         }
337     }
338     *buf_out = link_buf_list( pv );
339     return HB_WORK_OK;
340 }
341
342 static int decavcodecInfo( hb_work_object_t *w, hb_work_info_t *info )
343 {
344     hb_work_private_t *pv = w->private_data;
345
346     memset( info, 0, sizeof(*info) );
347
348     if ( pv && pv->context )
349     {
350         AVCodecContext *context = pv->context;
351         info->bitrate = context->bit_rate;
352         info->rate = context->time_base.num;
353         info->rate_base = context->time_base.den;
354         info->profile = context->profile;
355         info->level = context->level;
356         return 1;
357     }
358     return 0;
359 }
360
361 static int decavcodecBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
362                              hb_work_info_t *info )
363 {
364     hb_work_private_t *pv = w->private_data;
365     int ret = 0;
366
367     memset( info, 0, sizeof(*info) );
368
369     if ( pv && pv->context )
370     {
371         return decavcodecInfo( w, info );
372     }
373     // XXX
374     // We should parse the bitstream to find its parameters but for right
375     // now we just return dummy values if there's a codec that will handle it.
376     AVCodec *codec = avcodec_find_decoder( w->codec_param? w->codec_param :
377                                                            CODEC_ID_MP2 );
378     if ( ! codec )
379     {
380         // there's no ffmpeg codec for this audio type - give up
381         return -1;
382     }
383
384     static char codec_name[64];
385     info->name =  strncpy( codec_name, codec->name, sizeof(codec_name)-1 );
386
387     AVCodecParserContext *parser = av_parser_init( codec->id );
388     AVCodecContext *context = avcodec_alloc_context();
389     hb_avcodec_open( context, codec );
390     uint8_t *buffer = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
391     int out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
392     unsigned char *pbuffer;
393     int pos, pbuffer_size;
394
395     while ( buf && !ret )
396     {
397         pos = 0;
398         while ( pos < buf->size )
399         {
400             int len;
401
402             if (parser != NULL )
403             {
404                 len = av_parser_parse2( parser, context, &pbuffer, 
405                                         &pbuffer_size, buf->data + pos, 
406                                         buf->size - pos, buf->start, 
407                                         buf->start, AV_NOPTS_VALUE );
408             }
409             else
410             {
411                 pbuffer = buf->data;
412                 len = pbuffer_size = buf->size;
413             }
414             pos += len;
415             if ( pbuffer_size > 0 )
416             {
417                 AVPacket avp;
418                 av_init_packet( &avp );
419                 avp.data = pbuffer;
420                 avp.size = pbuffer_size;
421
422                 len = avcodec_decode_audio3( context, (int16_t*)buffer, 
423                                              &out_size, &avp );
424                 if ( len > 0 && context->sample_rate > 0 )
425                 {
426                     info->bitrate = context->bit_rate;
427                     info->rate = context->sample_rate;
428                     info->rate_base = 1;
429                     info->channel_layout = 
430                         hb_ff_layout_xlat(context->channel_layout, 
431                                           context->channels);
432                     ret = 1;
433                     break;
434                 }
435             }
436         }
437         buf = buf->next;
438     }
439
440     av_free( buffer );
441     if ( parser != NULL )
442         av_parser_close( parser );
443     hb_avcodec_close( context );
444     return ret;
445 }
446
447 /* -------------------------------------------------------------
448  * General purpose video decoder using libavcodec
449  */
450
451 static uint8_t *copy_plane( uint8_t *dst, uint8_t* src, int dstride, int sstride,
452                             int h )
453 {
454     if ( dstride == sstride )
455     {
456         memcpy( dst, src, dstride * h );
457         return dst + dstride * h;
458     }
459     int lbytes = dstride <= sstride? dstride : sstride;
460     while ( --h >= 0 )
461     {
462         memcpy( dst, src, lbytes );
463         src += sstride;
464         dst += dstride;
465     }
466     return dst;
467 }
468
469 // copy one video frame into an HB buf. If the frame isn't in our color space
470 // or at least one of its dimensions is odd, use sws_scale to convert/rescale it.
471 // Otherwise just copy the bits.
472 static hb_buffer_t *copy_frame( hb_work_private_t *pv, AVFrame *frame )
473 {
474     AVCodecContext *context = pv->context;
475     int w, h;
476     if ( ! pv->job )
477     {
478         // if the dimensions are odd, drop the lsb since h264 requires that
479         // both width and height be even.
480         w = ( context->width >> 1 ) << 1;
481         h = ( context->height >> 1 ) << 1;
482     }
483     else
484     {
485         w =  pv->job->title->width;
486         h =  pv->job->title->height;
487     }
488     hb_buffer_t *buf = hb_video_buffer_init( w, h );
489     uint8_t *dst = buf->data;
490
491     if ( context->pix_fmt != PIX_FMT_YUV420P || w != context->width ||
492          h != context->height )
493     {
494         // have to convert to our internal color space and/or rescale
495         AVPicture dstpic;
496         avpicture_fill( &dstpic, dst, PIX_FMT_YUV420P, w, h );
497
498         if ( ! pv->sws_context )
499         {
500             pv->sws_context = sws_getContext( context->width, context->height, context->pix_fmt,
501                                               w, h, PIX_FMT_YUV420P,
502                                               SWS_LANCZOS|SWS_ACCURATE_RND,
503                                               NULL, NULL, NULL );
504         }
505         sws_scale( pv->sws_context, frame->data, frame->linesize, 0, h,
506                    dstpic.data, dstpic.linesize );
507     }
508     else
509     {
510         dst = copy_plane( dst, frame->data[0], w, frame->linesize[0], h );
511         w = (w + 1) >> 1; h = (h + 1) >> 1;
512         dst = copy_plane( dst, frame->data[1], w, frame->linesize[1], h );
513         dst = copy_plane( dst, frame->data[2], w, frame->linesize[2], h );
514     }
515     return buf;
516 }
517
518 static int get_frame_buf( AVCodecContext *context, AVFrame *frame )
519 {
520     hb_work_private_t *pv = context->opaque;
521     frame->pts = pv->pts;
522     pv->pts = -1;
523     return avcodec_default_get_buffer( context, frame );
524 }
525
526 static int reget_frame_buf( AVCodecContext *context, AVFrame *frame )
527 {
528     hb_work_private_t *pv = context->opaque;
529     frame->pts = pv->pts;
530     pv->pts = -1;
531     return avcodec_default_reget_buffer( context, frame );
532 }
533
534 static void log_chapter( hb_work_private_t *pv, int chap_num, int64_t pts )
535 {
536     hb_chapter_t *c;
537
538     if ( !pv->job )
539         return;
540
541     c = hb_list_item( pv->job->title->list_chapter, chap_num - 1 );
542     if ( c && c->title )
543     {
544         hb_log( "%s: \"%s\" (%d) at frame %u time %"PRId64,
545                 pv->context->codec->name, c->title, chap_num, pv->nframes, pts );
546     }
547     else
548     {
549         hb_log( "%s: Chapter %d at frame %u time %"PRId64,
550                 pv->context->codec->name, chap_num, pv->nframes, pts );
551     }
552 }
553
554 static void flushDelayQueue( hb_work_private_t *pv )
555 {
556     hb_buffer_t *buf;
557     int slot = pv->nframes & (HEAP_SIZE-1);
558
559     // flush all the video packets left on our timestamp-reordering delay q
560     while ( ( buf = pv->delayq[slot] ) != NULL )
561     {
562         buf->start = heap_pop( &pv->pts_heap );
563         hb_list_add( pv->list, buf );
564         pv->delayq[slot] = NULL;
565         slot = ( slot + 1 ) & (HEAP_SIZE-1);
566     }
567 }
568
569 /*
570  * Decodes a video frame from the specified raw packet data ('data', 'size', 'sequence').
571  * The output of this function is stored in 'pv->list', which contains a list
572  * of zero or more decoded packets.
573  * 
574  * The returned packets are guaranteed to have their timestamps in the correct order,
575  * even if the original packets decoded by libavcodec have misordered timestamps,
576  * due to the use of 'packed B-frames'.
577  * 
578  * Internally the set of decoded packets may be buffered in 'pv->delayq'
579  * until enough packets have been decoded so that the timestamps can be
580  * correctly rewritten, if this is necessary.
581  */
582 static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size, int sequence )
583 {
584     int got_picture, oldlevel = 0;
585     AVFrame frame;
586     AVPacket avp;
587
588     if ( global_verbosity_level <= 1 )
589     {
590         oldlevel = av_log_get_level();
591         av_log_set_level( AV_LOG_QUIET );
592     }
593
594     av_init_packet( &avp );
595     avp.data = data;
596     avp.size = size;
597     if ( avcodec_decode_video2( pv->context, &frame, &got_picture, &avp ) < 0 )
598     {
599         ++pv->decode_errors;     
600     }
601     if ( global_verbosity_level <= 1 )
602     {
603         av_log_set_level( oldlevel );
604     }
605     if( got_picture )
606     {
607         // ffmpeg makes it hard to attach a pts to a frame. if the MPEG ES
608         // packet had a pts we handed it to av_parser_parse (if the packet had
609         // no pts we set it to -1 but before the parse we can't distinguish between
610         // the start of a video frame with no pts & an intermediate packet of
611         // some frame which never has a pts). we hope that when parse returns
612         // the frame to us the pts we originally handed it will be in parser->pts.
613         // we put this pts into pv->pts so that when a avcodec_decode_video
614         // finally gets around to allocating an AVFrame to hold the decoded
615         // frame we can stuff that pts into the frame. if all of these relays
616         // worked at this point frame.pts should hold the frame's pts from the
617         // original data stream or -1 if it didn't have one. in the latter case
618         // we generate the next pts in sequence for it.
619         double frame_dur = pv->duration;
620         if ( frame_dur <= 0 )
621         {
622             frame_dur = 90000. * (double)pv->context->time_base.num /
623                         (double)pv->context->time_base.den;
624             pv->duration = frame_dur;
625         }
626         if ( frame.repeat_pict )
627         {
628             frame_dur += frame.repeat_pict * frame_dur * 0.5;
629         }
630         // XXX Unlike every other video decoder, the Raw decoder doesn't
631         //     use the standard buffer allocation routines so we never
632         //     get to put a PTS in the frame. Do it now.
633         if ( pv->context->codec_id == CODEC_ID_RAWVIDEO )
634         {
635             frame.pts = pv->pts;
636             pv->pts = -1;
637         }
638         // If there was no pts for this frame, assume constant frame rate
639         // video & estimate the next frame time from the last & duration.
640         double pts = frame.pts;
641         if ( pts < 0 )
642         {
643             pts = pv->pts_next;
644         }
645         pv->pts_next = pts + frame_dur;
646
647         hb_buffer_t *buf;
648
649         // if we're doing a scan or this content couldn't have been broken
650         // by Microsoft we don't worry about timestamp reordering
651         if ( ! pv->job || ! pv->brokenByMicrosoft )
652         {
653             buf = copy_frame( pv, &frame );
654             buf->start = pts;
655             buf->sequence = sequence;
656             if ( pv->new_chap && buf->start >= pv->chap_time )
657             {
658                 buf->new_chap = pv->new_chap;
659                 pv->new_chap = 0;
660                 pv->chap_time = 0;
661                 log_chapter( pv, buf->new_chap, buf->start );
662             }
663             else if ( pv->nframes == 0 && pv->job )
664             {
665                 log_chapter( pv, pv->job->chapter_start, buf->start );
666             }
667             hb_list_add( pv->list, buf );
668             ++pv->nframes;
669             return got_picture;
670         }
671
672         // XXX This following probably addresses a libavcodec bug but I don't
673         //     see an easy fix so we workaround it here.
674         //
675         // The M$ 'packed B-frames' atrocity results in decoded frames with
676         // the wrong timestamp. E.g., if there are 2 b-frames the timestamps
677         // we see here will be "2 3 1 5 6 4 ..." instead of "1 2 3 4 5 6".
678         // The frames are actually delivered in the right order but with
679         // the wrong timestamp. To get the correct timestamp attached to
680         // each frame we have a delay queue (longer than the max number of
681         // b-frames) & a sorting heap for the timestamps. As each frame
682         // comes out of the decoder the oldest frame in the queue is removed
683         // and associated with the smallest timestamp. Then the new frame is
684         // added to the queue & its timestamp is pushed on the heap.
685         // This does nothing if the timestamps are correct (i.e., the video
686         // uses a codec that Micro$oft hasn't broken yet) but the frames
687         // get timestamped correctly even when M$ has munged them.
688
689         // remove the oldest picture from the frame queue (if any) &
690         // give it the smallest timestamp from our heap. The queue size
691         // is a power of two so we get the slot of the oldest by masking
692         // the frame count & this will become the slot of the newest
693         // once we've removed & processed the oldest.
694         int slot = pv->nframes & (HEAP_SIZE-1);
695         if ( ( buf = pv->delayq[slot] ) != NULL )
696         {
697             buf->start = heap_pop( &pv->pts_heap );
698
699             if ( pv->new_chap && buf->start >= pv->chap_time )
700             {
701                 buf->new_chap = pv->new_chap;
702                 pv->new_chap = 0;
703                 pv->chap_time = 0;
704                 log_chapter( pv, buf->new_chap, buf->start );
705             }
706             else if ( pv->nframes == 0 && pv->job )
707             {
708                 log_chapter( pv, pv->job->chapter_start, buf->start );
709             }
710             hb_list_add( pv->list, buf );
711         }
712
713         // add the new frame to the delayq & push its timestamp on the heap
714         buf = copy_frame( pv, &frame );
715         buf->sequence = sequence;
716         pv->delayq[slot] = buf;
717         heap_push( &pv->pts_heap, pts );
718
719         ++pv->nframes;
720     }
721
722     return got_picture;
723 }
724
725 static void decodeVideo( hb_work_private_t *pv, uint8_t *data, int size, int sequence,
726                          int64_t pts, int64_t dts )
727 {
728     /*
729      * The following loop is a do..while because we need to handle both
730      * data & the flush at the end (signaled by size=0). At the end there's
731      * generally a frame in the parser & one or more frames in the decoder
732      * (depending on the bframes setting).
733      */
734     int pos = 0;
735     do {
736         uint8_t *pout;
737         int pout_len;
738         int len = av_parser_parse2( pv->parser, pv->context, &pout, &pout_len,
739                                     data + pos, size - pos, pts, dts, AV_NOPTS_VALUE );
740         pos += len;
741
742         if ( pout_len > 0 )
743         {
744             pv->pts = pv->parser->pts;
745             decodeFrame( pv, pout, pout_len, sequence );
746         }
747     } while ( pos < size );
748
749     /* the stuff above flushed the parser, now flush the decoder */
750     if ( size <= 0 )
751     {
752         while ( decodeFrame( pv, NULL, 0, sequence ) )
753         {
754         }
755         flushDelayQueue( pv );
756     }
757 }
758
759 /*
760  * Removes all packets from 'pv->list', links them together into
761  * a linked-list, and returns the first packet in the list.
762  */
763 static hb_buffer_t *link_buf_list( hb_work_private_t *pv )
764 {
765     hb_buffer_t *head = hb_list_item( pv->list, 0 );
766
767     if ( head )
768     {
769         hb_list_rem( pv->list, head );
770
771         hb_buffer_t *last = head, *buf;
772
773         while ( ( buf = hb_list_item( pv->list, 0 ) ) != NULL )
774         {
775             hb_list_rem( pv->list, buf );
776             last->next = buf;
777             last = buf;
778         }
779     }
780     return head;
781 }
782
783
784 static int decavcodecvInit( hb_work_object_t * w, hb_job_t * job )
785 {
786
787     hb_work_private_t *pv = calloc( 1, sizeof( hb_work_private_t ) );
788     w->private_data = pv;
789     pv->job   = job;
790     pv->list = hb_list_init();
791
792     int codec_id = w->codec_param;
793     pv->parser = av_parser_init( codec_id );
794     pv->context = avcodec_alloc_context2( CODEC_TYPE_VIDEO );
795
796     /* we have to wrap ffmpeg's get_buffer to be able to set the pts (?!) */
797     pv->context->opaque = pv;
798     pv->context->get_buffer = get_frame_buf;
799     pv->context->reget_buffer = reget_frame_buf;
800
801     return 0;
802 }
803
804 static int next_hdr( hb_buffer_t *in, int offset )
805 {
806     uint8_t *dat = in->data;
807     uint16_t last2 = 0xffff;
808     for ( ; in->size - offset > 1; ++offset )
809     {
810         if ( last2 == 0 && dat[offset] == 0x01 )
811             // found an mpeg start code
812             return offset - 2;
813
814         last2 = ( last2 << 8 ) | dat[offset];
815     }
816
817     return -1;
818 }
819
820 static int find_hdr( hb_buffer_t *in, int offset, uint8_t hdr_type )
821 {
822     if ( in->size - offset < 4 )
823         // not enough room for an mpeg start code
824         return -1;
825
826     for ( ; ( offset = next_hdr( in, offset ) ) >= 0; ++offset )
827     {
828         if ( in->data[offset+3] == hdr_type )
829             // found it
830             break;
831     }
832     return offset;
833 }
834
835 static int setup_extradata( hb_work_object_t *w, hb_buffer_t *in )
836 {
837     hb_work_private_t *pv = w->private_data;
838
839     // we can't call the avstream funcs but the read_header func in the
840     // AVInputFormat may set up some state in the AVContext. In particular 
841     // vc1t_read_header allocates 'extradata' to deal with header issues
842     // related to Microsoft's bizarre engineering notions. We alloc a chunk
843     // of space to make vc1 work then associate the codec with the context.
844     if ( w->codec_param != CODEC_ID_VC1 )
845     {
846         // we haven't been inflicted with M$ - allocate a little space as
847         // a marker and return success.
848         pv->context->extradata_size = 16;
849         pv->context->extradata = av_malloc(pv->context->extradata_size);
850         return 0;
851     }
852
853     // find the start and and of the sequence header
854     int shdr, shdr_end;
855     if ( ( shdr = find_hdr( in, 0, 0x0f ) ) < 0 )
856     {
857         // didn't find start of seq hdr
858         return 1;
859     }
860     if ( ( shdr_end = next_hdr( in, shdr + 4 ) ) < 0 )
861     {
862         shdr_end = in->size;
863     }
864     shdr_end -= shdr;
865
866     // find the start and and of the entry point header
867     int ehdr, ehdr_end;
868     if ( ( ehdr = find_hdr( in, 0, 0x0e ) ) < 0 )
869     {
870         // didn't find start of entry point hdr
871         return 1;
872     }
873     if ( ( ehdr_end = next_hdr( in, ehdr + 4 ) ) < 0 )
874     {
875         ehdr_end = in->size;
876     }
877     ehdr_end -= ehdr;
878
879     // found both headers - allocate an extradata big enough to hold both
880     // then copy them into it.
881     pv->context->extradata_size = shdr_end + ehdr_end;
882     pv->context->extradata = av_malloc(pv->context->extradata_size + 8);
883     memcpy( pv->context->extradata, in->data + shdr, shdr_end );
884     memcpy( pv->context->extradata + shdr_end, in->data + ehdr, ehdr_end );
885     memset( pv->context->extradata + shdr_end + ehdr_end, 0, 8);
886     return 0;
887 }
888
889 static int decavcodecvWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
890                             hb_buffer_t ** buf_out )
891 {
892     hb_work_private_t *pv = w->private_data;
893     hb_buffer_t *in = *buf_in;
894     int64_t pts = AV_NOPTS_VALUE;
895     int64_t dts = pts;
896
897     *buf_in = NULL;
898
899     /* if we got an empty buffer signaling end-of-stream send it downstream */
900     if ( in->size == 0 )
901     {
902         decodeVideo( pv, in->data, in->size, in->sequence, pts, dts );
903         hb_list_add( pv->list, in );
904         *buf_out = link_buf_list( pv );
905         return HB_WORK_DONE;
906     }
907
908     // if this is the first frame open the codec (we have to wait for the
909     // first frame because of M$ VC1 braindamage).
910     if ( pv->context->extradata_size == 0 )
911     {
912         if ( setup_extradata( w, in ) )
913         {
914             // we didn't find the headers needed to set up extradata.
915             // the codec will abort if we open it so just free the buf
916             // and hope we eventually get the info we need.
917             hb_buffer_close( &in );
918             return HB_WORK_OK;
919         }
920         AVCodec *codec = avcodec_find_decoder( w->codec_param );
921         // There's a mis-feature in ffmpeg that causes the context to be 
922         // incorrectly initialized the 1st time avcodec_open is called.
923         // If you close it and open a 2nd time, it finishes the job.
924         hb_avcodec_open( pv->context, codec );
925         hb_avcodec_close( pv->context );
926         hb_avcodec_open( pv->context, codec );
927     }
928
929     if( in->start >= 0 )
930     {
931         pts = in->start;
932         dts = in->renderOffset;
933     }
934     if ( in->new_chap )
935     {
936         pv->new_chap = in->new_chap;
937         pv->chap_time = pts >= 0? pts : pv->pts_next;
938     }
939     decodeVideo( pv, in->data, in->size, in->sequence, pts, dts );
940     hb_buffer_close( &in );
941     *buf_out = link_buf_list( pv );
942     return HB_WORK_OK;
943 }
944
945 static int decavcodecvInfo( hb_work_object_t *w, hb_work_info_t *info )
946 {
947     hb_work_private_t *pv = w->private_data;
948
949     memset( info, 0, sizeof(*info) );
950
951     if ( pv && pv->context )
952     {
953         AVCodecContext *context = pv->context;
954         info->bitrate = context->bit_rate;
955         info->width = context->width;
956         info->height = context->height;
957
958         /* ffmpeg gives the frame rate in frames per second while HB wants
959          * it in units of the 27MHz MPEG clock. */
960         info->rate = 27000000;
961         info->rate_base = (int64_t)context->time_base.num * 27000000LL /
962                           context->time_base.den;
963         if ( context->ticks_per_frame > 1 )
964         {
965             // for ffmpeg 0.5 & later, the H.264 & MPEG-2 time base is
966             // field rate rather than frame rate so convert back to frames.
967             info->rate_base *= context->ticks_per_frame;
968         }
969         
970         info->pixel_aspect_width = context->sample_aspect_ratio.num;
971         info->pixel_aspect_height = context->sample_aspect_ratio.den;
972
973         /* Sometimes there's no pixel aspect set in the source ffmpeg context
974          * which appears to come from the video stream. In that case,
975          * try the pixel aspect in AVStream (which appears to come from
976          * the container). Else assume a 1:1 PAR. */
977         if ( info->pixel_aspect_width == 0 ||
978              info->pixel_aspect_height == 0 )
979         {
980             // There will not be an ffmpeg stream if the file is TS
981             AVStream *st = hb_ffmpeg_avstream( w->codec_param );
982             info->pixel_aspect_width = st && st->sample_aspect_ratio.num ?
983                                        st->sample_aspect_ratio.num : 1;
984             info->pixel_aspect_height = st && st->sample_aspect_ratio.den ?
985                                         st->sample_aspect_ratio.den : 1;
986         }
987         /* ffmpeg returns the Pixel Aspect Ratio (PAR). Handbrake wants the
988          * Display Aspect Ratio so we convert by scaling by the Storage
989          * Aspect Ratio (w/h). We do the calc in floating point to get the
990          * rounding right. */
991         info->aspect = (double)info->pixel_aspect_width * 
992                        (double)context->width /
993                        (double)info->pixel_aspect_height /
994                        (double)context->height;
995
996         info->profile = context->profile;
997         info->level = context->level;
998         info->name = context->codec->name;
999         return 1;
1000     }
1001     return 0;
1002 }
1003
1004 static int decavcodecvBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
1005                              hb_work_info_t *info )
1006 {
1007     return 0;
1008 }
1009
1010 hb_work_object_t hb_decavcodecv =
1011 {
1012     WORK_DECAVCODECV,
1013     "Video decoder (libavcodec)",
1014     decavcodecvInit,
1015     decavcodecvWork,
1016     decavcodecClose,
1017     decavcodecvInfo,
1018     decavcodecvBSInfo
1019 };
1020
1021
1022 // This is a special decoder for ffmpeg streams. The ffmpeg stream reader
1023 // includes a parser and passes information from the parser to the decoder
1024 // via a codec context kept in the AVStream of the reader's AVFormatContext.
1025 // We *have* to use that codec context to decode the stream or we'll get
1026 // garbage. ffmpeg_title_scan put a cookie that can be used to get to that
1027 // codec context in our codec_param.
1028
1029 // this routine gets the appropriate context pointer from the ffmpeg
1030 // stream reader. it can't be called until we get the first buffer because
1031 // we can't guarantee that reader will be called before the our init
1032 // routine and if our init is called first we'll get a pointer to the
1033 // old scan stream (which has already been closed).
1034 static void init_ffmpeg_context( hb_work_object_t *w )
1035 {
1036     hb_work_private_t *pv = w->private_data;
1037     pv->context = hb_ffmpeg_context( w->codec_param );
1038
1039     // during scan the decoder gets closed & reopened which will
1040     // close the codec so reopen it if it's not there
1041     if ( ! pv->context->codec )
1042     {
1043         AVCodec *codec = avcodec_find_decoder( pv->context->codec_id );
1044         hb_avcodec_open( pv->context, codec );
1045     }
1046     // set up our best guess at the frame duration.
1047     // the frame rate in the codec is usually bogus but it's sometimes
1048     // ok in the stream.
1049     AVStream *st = hb_ffmpeg_avstream( w->codec_param );
1050
1051     if ( st->nb_frames && st->duration )
1052     {
1053         // compute the average frame duration from the total number
1054         // of frames & the total duration.
1055         pv->duration = ( (double)st->duration * (double)st->time_base.num ) /
1056                        ( (double)st->nb_frames * (double)st->time_base.den );
1057     }
1058     else
1059     {
1060         // XXX We don't have a frame count or duration so try to use the
1061         // far less reliable time base info in the stream.
1062         // Because the time bases are so screwed up, we only take values
1063         // in the range 8fps - 64fps.
1064         AVRational tb;
1065         if ( st->avg_frame_rate.den * 64 > st->avg_frame_rate.num &&
1066              st->avg_frame_rate.num > st->avg_frame_rate.den * 8 )
1067         {
1068             tb.num = st->avg_frame_rate.den;
1069             tb.den = st->avg_frame_rate.num;
1070         }
1071         else if ( st->time_base.num * 64 > st->time_base.den &&
1072                   st->time_base.den > st->time_base.num * 8 )
1073         {
1074             tb = st->time_base;
1075         }
1076         else if ( st->r_frame_rate.den * 64 > st->r_frame_rate.num &&
1077                   st->r_frame_rate.num > st->r_frame_rate.den * 8 )
1078         {
1079             tb.num = st->r_frame_rate.den;
1080             tb.den = st->r_frame_rate.num;
1081         }
1082         else
1083         {
1084             tb.num = 1001;  /*XXX*/
1085             tb.den = 24000; /*XXX*/
1086         }
1087         pv->duration =  (double)tb.num / (double)tb.den;
1088     }
1089     pv->duration *= 90000.;
1090
1091     // we have to wrap ffmpeg's get_buffer to be able to set the pts (?!)
1092     pv->context->opaque = pv;
1093     pv->context->get_buffer = get_frame_buf;
1094     pv->context->reget_buffer = reget_frame_buf;
1095
1096     // avi, mkv and possibly mp4 containers can contain the M$ VFW packed
1097     // b-frames abortion that messes up frame ordering and timestamps.
1098     // XXX ffmpeg knows which streams are broken but doesn't expose the
1099     //     info externally. We should patch ffmpeg to add a flag to the
1100     //     codec context for this but until then we mark all ffmpeg streams
1101     //     as suspicious.
1102     pv->brokenByMicrosoft = 1;
1103 }
1104
1105 static void prepare_ffmpeg_buffer( hb_buffer_t * in )
1106 {
1107     // ffmpeg requires an extra 8 bytes of zero at the end of the buffer and
1108     // will seg fault in odd, data dependent ways if it's not there. (my guess
1109     // is this is a case of a local performance optimization creating a global
1110     // performance degradation since all the time wasted by extraneous data
1111     // copies & memory zeroing has to be huge compared to the minor reduction
1112     // in inner-loop instructions this affords - modern cpus bottleneck on
1113     // memory bandwidth not instruction bandwidth).
1114     if ( in->size + FF_INPUT_BUFFER_PADDING_SIZE > in->alloc )
1115     {
1116         // have to realloc to add the padding
1117         hb_buffer_realloc( in, in->size + FF_INPUT_BUFFER_PADDING_SIZE );
1118     }
1119     memset( in->data + in->size, 0, FF_INPUT_BUFFER_PADDING_SIZE );
1120 }
1121
1122 static int decavcodecviInit( hb_work_object_t * w, hb_job_t * job )
1123 {
1124
1125     hb_work_private_t *pv = calloc( 1, sizeof( hb_work_private_t ) );
1126     w->private_data = pv;
1127     pv->job   = job;
1128     pv->list = hb_list_init();
1129     pv->pts_next = -1;
1130     pv->pts = -1;
1131
1132     if ( w->audio != NULL &&
1133          hb_need_downmix( w->audio->config.in.channel_layout, 
1134                           w->audio->config.out.mixdown) )
1135     {
1136         pv->downmix = hb_downmix_init(w->audio->config.in.channel_layout, 
1137                                       w->audio->config.out.mixdown);
1138         hb_downmix_set_chan_map( pv->downmix, &hb_smpte_chan_map, &hb_qt_chan_map );
1139     }
1140
1141     return 0;
1142 }
1143
1144 static int decavcodecviWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
1145                              hb_buffer_t ** buf_out )
1146 {
1147     hb_work_private_t *pv = w->private_data;
1148     hb_buffer_t *in = *buf_in;
1149     *buf_in = NULL;
1150
1151     /* if we got an empty buffer signaling end-of-stream send it downstream */
1152     if ( in->size == 0 )
1153     {
1154         /* flush any frames left in the decoder */
1155         while ( pv->context && decodeFrame( pv, NULL, 0, in->sequence ) )
1156         {
1157         }
1158         flushDelayQueue( pv );
1159         hb_list_add( pv->list, in );
1160         *buf_out = link_buf_list( pv );
1161         return HB_WORK_DONE;
1162     }
1163
1164     if ( ! pv->context )
1165     {
1166         init_ffmpeg_context( w );
1167     }
1168
1169     int64_t pts = in->start;
1170     if( pts >= 0 )
1171     {
1172         // use the first timestamp as our 'next expected' pts
1173         if ( pv->pts_next < 0 )
1174         {
1175             pv->pts_next = pts;
1176         }
1177         pv->pts = pts;
1178     }
1179
1180     if ( in->new_chap )
1181     {
1182         pv->new_chap = in->new_chap;
1183         pv->chap_time = pts >= 0? pts : pv->pts_next;
1184     }
1185     prepare_ffmpeg_buffer( in );
1186     decodeFrame( pv, in->data, in->size, in->sequence );
1187     hb_buffer_close( &in );
1188     *buf_out = link_buf_list( pv );
1189     return HB_WORK_OK;
1190 }
1191
1192 static int decavcodecviInfo( hb_work_object_t *w, hb_work_info_t *info )
1193 {
1194     if ( decavcodecvInfo( w, info ) )
1195     {
1196         hb_work_private_t *pv = w->private_data;
1197         if ( ! pv->context )
1198         {
1199             init_ffmpeg_context( w );
1200         }
1201         // we have the frame duration in units of the 90KHz pts clock but
1202         // need it in units of the 27MHz MPEG clock. */
1203         info->rate = 27000000;
1204         info->rate_base = pv->duration * 300.;
1205         return 1;
1206     }
1207     return 0;
1208 }
1209
1210 static void decodeAudio( hb_audio_t * audio, hb_work_private_t *pv, uint8_t *data, int size )
1211 {
1212     AVCodecContext *context = pv->context;
1213     int pos = 0;
1214     int loop_limit = 256;
1215
1216     while ( pos < size )
1217     {
1218         int16_t *buffer = pv->buffer;
1219         if ( buffer == NULL )
1220         {
1221             pv->buffer = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
1222             buffer = pv->buffer;
1223         }
1224
1225         AVPacket avp;
1226         av_init_packet( &avp );
1227         avp.data = data + pos;
1228         avp.size = size - pos;
1229
1230         int out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
1231         int nsamples;
1232         int len = avcodec_decode_audio3( context, buffer, &out_size, &avp );
1233         if ( len < 0 )
1234         {
1235             return;
1236         }
1237         if ( len == 0 )
1238         {
1239             if ( !(loop_limit--) )
1240                 return;
1241         }
1242         else
1243             loop_limit = 256;
1244
1245         pos += len;
1246         if( out_size > 0 )
1247         {
1248             // We require signed 16-bit ints for the output format. If
1249             // we got something different convert it.
1250             if ( context->sample_fmt != SAMPLE_FMT_S16 )
1251             {
1252                 // Note: av_audio_convert seems to be a work-in-progress but
1253                 //       looks like it will eventually handle general audio
1254                 //       mixdowns which would allow us much more flexibility
1255                 //       in handling multichannel audio in HB. If we were doing
1256                 //       anything more complicated than a one-for-one format
1257                 //       conversion we'd probably want to cache the converter
1258                 //       context in the pv.
1259                 int isamp = av_get_bits_per_sample_format( context->sample_fmt ) / 8;
1260                 AVAudioConvert *ctx = av_audio_convert_alloc( SAMPLE_FMT_S16, 1,
1261                                                               context->sample_fmt, 1,
1262                                                               NULL, 0 );
1263                 // get output buffer size (in 2-byte samples) then malloc a buffer
1264                 nsamples = out_size / isamp;
1265                 buffer = av_malloc( nsamples * 2 );
1266
1267                 // we're doing straight sample format conversion which behaves as if
1268                 // there were only one channel.
1269                 const void * const ibuf[6] = { pv->buffer };
1270                 void * const obuf[6] = { buffer };
1271                 const int istride[6] = { isamp };
1272                 const int ostride[6] = { 2 };
1273
1274                 av_audio_convert( ctx, obuf, ostride, ibuf, istride, nsamples );
1275                 av_audio_convert_free( ctx );
1276             }
1277             else
1278             {
1279                 nsamples = out_size / 2;
1280             }
1281
1282             hb_buffer_t * buf;
1283
1284             if ( pv->downmix )
1285             {
1286                 pv->downmix_buffer = realloc(pv->downmix_buffer, nsamples * sizeof(hb_sample_t));
1287                 
1288                 int i;
1289                 for( i = 0; i < nsamples; ++i )
1290                 {
1291                     pv->downmix_buffer[i] = buffer[i];
1292                 }
1293
1294                 int n_ch_samples = nsamples / context->channels;
1295                 int channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown);
1296
1297                 buf = hb_buffer_init( n_ch_samples * channels * sizeof(float) );
1298                 hb_sample_t *samples = (hb_sample_t *)buf->data;
1299                 hb_downmix(pv->downmix, samples, pv->downmix_buffer, n_ch_samples);
1300             }
1301             else
1302             {
1303                 buf = hb_buffer_init( nsamples * sizeof(float) );
1304                 float *fl32 = (float *)buf->data;
1305                 int i;
1306                 for( i = 0; i < nsamples; ++i )
1307                 {
1308                     fl32[i] = buffer[i];
1309                 }
1310                 int n_ch_samples = nsamples / context->channels;
1311                 hb_layout_remap( &hb_smpte_chan_map, &hb_qt_chan_map,
1312                                  audio->config.in.channel_layout, 
1313                                  fl32, n_ch_samples );
1314             }
1315
1316             double pts = pv->pts_next;
1317             buf->start = pts;
1318             pts += nsamples * pv->duration;
1319             buf->stop  = pts;
1320             pv->pts_next = pts;
1321
1322             hb_list_add( pv->list, buf );
1323
1324             // if we allocated a buffer for sample format conversion, free it
1325             if ( buffer != pv->buffer )
1326             {
1327                 av_free( buffer );
1328             }
1329         }
1330     }
1331 }
1332
1333 static int decavcodecaiWork( hb_work_object_t *w, hb_buffer_t **buf_in,
1334                     hb_buffer_t **buf_out )
1335 {
1336     if ( (*buf_in)->size <= 0 )
1337     {
1338         /* EOF on input stream - send it downstream & say that we're done */
1339         *buf_out = *buf_in;
1340         *buf_in = NULL;
1341         return HB_WORK_DONE;
1342     }
1343
1344     hb_work_private_t *pv = w->private_data;
1345
1346     if ( (*buf_in)->start < -1 && pv->pts_next <= 0 )
1347     {
1348         // discard buffers that start before video time 0
1349         *buf_out = NULL;
1350         return HB_WORK_OK;
1351     }
1352
1353     if ( ! pv->context )
1354     {
1355         init_ffmpeg_context( w );
1356         // duration is a scaling factor to go from #bytes in the decoded
1357         // frame to frame time (in 90KHz mpeg ticks). 'channels' converts
1358         // total samples to per-channel samples. 'sample_rate' converts
1359         // per-channel samples to seconds per sample and the 90000
1360         // is mpeg ticks per second.
1361         pv->duration = 90000. /
1362                     (double)( pv->context->sample_rate * pv->context->channels );
1363     }
1364     hb_buffer_t *in = *buf_in;
1365
1366     // if the packet has a timestamp use it if we don't have a timestamp yet
1367     // or if there's been a timing discontinuity of more than 100ms.
1368     if ( in->start >= 0 &&
1369          ( pv->pts_next < 0 || ( in->start - pv->pts_next ) > 90*100 ) )
1370     {
1371         pv->pts_next = in->start;
1372     }
1373     prepare_ffmpeg_buffer( in );
1374     decodeAudio( w->audio, pv, in->data, in->size );
1375     *buf_out = link_buf_list( pv );
1376
1377     return HB_WORK_OK;
1378 }
1379
1380 hb_work_object_t hb_decavcodecvi =
1381 {
1382     WORK_DECAVCODECVI,
1383     "Video decoder (ffmpeg streams)",
1384     decavcodecviInit,
1385     decavcodecviWork,
1386     decavcodecClose,
1387     decavcodecviInfo,
1388     decavcodecvBSInfo
1389 };
1390
1391 hb_work_object_t hb_decavcodecai =
1392 {
1393     WORK_DECAVCODECAI,
1394     "Audio decoder (ffmpeg streams)",
1395     decavcodecviInit,
1396     decavcodecaiWork,
1397     decavcodecClose,
1398     decavcodecInfo,
1399     decavcodecBSInfo
1400 };