OSDN Git Service

Fix a hang in sync
[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     hb_list_t       *ff_audio_list;
100     double          duration;   // frame duration (for video)
101     double          pts_next;   // next pts we expect to generate
102     int64_t         pts;        // (video) pts passing from parser to decoder
103     int64_t         chap_time;  // time of next chap mark (if new_chap != 0)
104     int             new_chap;   // output chapter mark pending
105     uint32_t        nframes;
106     uint32_t        ndrops;
107     uint32_t        decode_errors;
108     int             brokenByMicrosoft; // video stream may contain packed b-frames
109     hb_buffer_t*    delayq[HEAP_SIZE];
110     pts_heap_t      pts_heap;
111     void*           buffer;
112     struct SwsContext *sws_context; // if we have to rescale or convert color space
113     hb_downmix_t    *downmix;
114     hb_sample_t     *downmix_buffer;
115     int cadence[12];
116     hb_chan_map_t   *out_map;
117 };
118
119 static void decodeAudio( hb_audio_t * audio, hb_work_private_t *pv, uint8_t *data, int size );
120 static hb_buffer_t *link_buf_list( hb_work_private_t *pv );
121
122
123 static int64_t heap_pop( pts_heap_t *heap )
124 {
125     int64_t result;
126
127     if ( heap->nheap <= 0 )
128     {
129         return -1;
130     }
131
132     // return the top of the heap then put the bottom element on top,
133     // decrease the heap size by one & rebalence the heap.
134     result = heap->h[1];
135
136     int64_t v = heap->h[heap->nheap--];
137     int parent = 1;
138     int child = parent << 1;
139     while ( child <= heap->nheap )
140     {
141         // find the smallest of the two children of parent
142         if (child < heap->nheap && heap->h[child] > heap->h[child+1] )
143             ++child;
144
145         if (v <= heap->h[child])
146             // new item is smaller than either child so it's the new parent.
147             break;
148
149         // smallest child is smaller than new item so move it up then
150         // check its children.
151         int64_t hp = heap->h[child];
152         heap->h[parent] = hp;
153         parent = child;
154         child = parent << 1;
155     }
156     heap->h[parent] = v;
157     return result;
158 }
159
160 static void heap_push( pts_heap_t *heap, int64_t v )
161 {
162     if ( heap->nheap < HEAP_SIZE )
163     {
164         ++heap->nheap;
165     }
166
167     // stick the new value on the bottom of the heap then bubble it
168     // up to its correct spot.
169         int child = heap->nheap;
170         while (child > 1) {
171                 int parent = child >> 1;
172                 if (heap->h[parent] <= v)
173                         break;
174                 // move parent down
175                 int64_t hp = heap->h[parent];
176                 heap->h[child] = hp;
177                 child = parent;
178         }
179         heap->h[child] = v;
180 }
181
182
183 /***********************************************************************
184  * hb_work_decavcodec_init
185  ***********************************************************************
186  *
187  **********************************************************************/
188 static int decavcodecInit( hb_work_object_t * w, hb_job_t * job )
189 {
190     AVCodec * codec;
191     int i;
192
193     hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
194     w->private_data = pv;
195
196     pv->job   = job;
197     pv->list  = hb_list_init();
198
199     int codec_id = w->codec_param;
200     /*XXX*/
201     if ( codec_id == 0 )
202         codec_id = CODEC_ID_MP2;
203
204     codec = avcodec_find_decoder( codec_id );
205     pv->parser = av_parser_init( codec_id );
206
207     pv->context = avcodec_alloc_context();
208     hb_avcodec_open( pv->context, codec );
209
210     if ( w->audio != NULL )
211     {
212         if ( w->audio->config.out.codec == HB_ACODEC_AC3 )
213         {
214             // ffmpegs audio encoder expect an smpte chan map as input.
215             // So we need to map the decoders output to smpte.
216             pv->out_map = &hb_smpte_chan_map;
217         }
218         else
219         {
220             pv->out_map = &hb_qt_chan_map;
221         }
222         if ( hb_need_downmix( w->audio->config.in.channel_layout, 
223                               w->audio->config.out.mixdown) )
224         {
225             pv->downmix = hb_downmix_init(w->audio->config.in.channel_layout, 
226                                           w->audio->config.out.mixdown);
227             hb_downmix_set_chan_map( pv->downmix, &hb_smpte_chan_map, pv->out_map );
228         }
229
230         pv->ff_audio_list = hb_list_init();
231         for ( i = 0; i < hb_list_count( w->audio->priv.ff_audio_list ); i++ )
232         {
233             hb_work_private_t * ff_pv = calloc( 1, sizeof( hb_work_private_t ) );
234             hb_list_add( pv->ff_audio_list, ff_pv );
235
236             hb_audio_t *audio = hb_list_item( w->audio->priv.ff_audio_list, i );
237
238             ff_pv->list  = hb_list_init();
239             ff_pv->job   = job;
240
241             if ( audio->config.out.codec == HB_ACODEC_AC3 )
242             {
243                 // ffmpegs audio encoder expect an smpte chan map as input.
244                 // So we need to map the decoders output to smpte.
245                 ff_pv->out_map = &hb_smpte_chan_map;
246             }
247             else
248             {
249                 ff_pv->out_map = &hb_qt_chan_map;
250             }
251             if ( hb_need_downmix( audio->config.in.channel_layout, 
252                                   audio->config.out.mixdown) )
253             {
254                 ff_pv->downmix = hb_downmix_init(audio->config.in.channel_layout, 
255                                               audio->config.out.mixdown);
256                 hb_downmix_set_chan_map( ff_pv->downmix, &hb_smpte_chan_map, ff_pv->out_map );
257             }
258         }
259     }
260
261     return 0;
262 }
263
264 /***********************************************************************
265  * Close
266  ***********************************************************************
267  *
268  **********************************************************************/
269 static void closePrivData( hb_work_private_t ** ppv )
270 {
271     hb_work_private_t * pv = *ppv;
272
273     if ( pv )
274     {
275         if ( pv->job && pv->context && pv->context->codec )
276         {
277             hb_log( "%s-decoder done: %u frames, %u decoder errors, %u drops",
278                     pv->context->codec->name, pv->nframes, pv->decode_errors,
279                     pv->ndrops );
280         }
281         if ( pv->sws_context )
282         {
283             sws_freeContext( pv->sws_context );
284         }
285         if ( pv->parser )
286         {
287             av_parser_close(pv->parser);
288         }
289         if ( pv->context && pv->context->codec )
290         {
291             hb_avcodec_close( pv->context );
292         }
293         if ( pv->list )
294         {
295             hb_list_close( &pv->list );
296         }
297         if ( pv->buffer )
298         {
299             av_free( pv->buffer );
300             pv->buffer = NULL;
301         }
302         if ( pv->downmix )
303         {
304             hb_downmix_close( &(pv->downmix) );
305         }
306         if ( pv->downmix_buffer )
307         {
308             free( pv->downmix_buffer );
309             pv->downmix_buffer = NULL;
310         }
311         free( pv );
312     }
313     *ppv = NULL;
314 }
315
316 static void decavcodecClose( hb_work_object_t * w )
317 {
318     hb_work_private_t * pv = w->private_data;
319
320     if ( pv )
321     {
322         if ( pv->ff_audio_list != NULL )
323         {
324             hb_work_private_t * ff_pv;
325             while ( ( ff_pv = hb_list_item( pv->list, 0 ) ) != NULL )
326             {
327                 hb_list_rem( pv->ff_audio_list, ff_pv );
328                 closePrivData( &ff_pv );
329             }
330         }
331         closePrivData( &pv );
332         w->private_data = NULL;
333     }
334 }
335
336 static void writeAudioEof( hb_work_object_t * w )
337 {
338     hb_work_private_t * pv = w->private_data;
339     hb_audio_t * audio = w->audio;
340     int i;
341     hb_buffer_t * buf;
342
343     for ( i = 0; i < hb_list_count( audio->priv.ff_audio_list ); i++ )
344     {
345         hb_audio_t *ff_audio = hb_list_item( audio->priv.ff_audio_list, i );
346         hb_work_private_t *ff_pv = hb_list_item( pv->ff_audio_list, i );
347         if ( ff_pv )
348         {
349             buf = hb_buffer_init( 0 );
350             if ( buf )
351             {
352                 while ( !*w->done )
353                 {
354                     if ( hb_fifo_full_wait( ff_audio->priv.fifo_raw ) )
355                     {
356                         hb_fifo_push( ff_audio->priv.fifo_raw, buf );
357                         buf = NULL;
358                         break;
359                     }
360                 }
361                 if ( buf )
362                 {
363                     // w->done == true while waiting
364                     hb_buffer_close( &buf );
365                     break;
366                 }
367             }
368         }
369     }
370 }
371
372 static void writeAudioFifos( hb_work_object_t * w )
373 {
374     hb_work_private_t * pv = w->private_data;
375     hb_audio_t * audio = w->audio;
376     int i;
377     hb_buffer_t * buf;
378
379     for ( i = 0; i < hb_list_count( audio->priv.ff_audio_list ); i++ )
380     {
381         hb_audio_t *ff_audio = hb_list_item( audio->priv.ff_audio_list, i );
382         hb_work_private_t *ff_pv = hb_list_item( pv->ff_audio_list, i );
383         if ( ff_pv )
384         {
385             buf = link_buf_list( ff_pv );
386             if ( buf )
387             {
388                 while ( !*w->done )
389                 {
390                     if ( hb_fifo_full_wait( ff_audio->priv.fifo_raw ) )
391                     {
392                         hb_fifo_push( ff_audio->priv.fifo_raw, buf );
393                         buf = NULL;
394                         break;
395                     }
396                 }
397                 if ( buf )
398                 {
399                     // w->done == true while waiting
400                     hb_buffer_close( &buf );
401                     break;
402                 }
403             }
404         }
405     }
406 }
407
408 /***********************************************************************
409  * Work
410  ***********************************************************************
411  *
412  **********************************************************************/
413 static int decavcodecWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
414                     hb_buffer_t ** buf_out )
415 {
416     hb_work_private_t * pv = w->private_data;
417     hb_buffer_t * in = *buf_in;
418
419     if ( in->size <= 0 )
420     {
421         /* EOF on input stream - send it downstream & say that we're done */
422         *buf_out = in;
423         *buf_in = NULL;
424         writeAudioEof( w );
425         return HB_WORK_DONE;
426     }
427
428     *buf_out = NULL;
429
430     if ( in->start < -1 && pv->pts_next <= 0 )
431     {
432         // discard buffers that start before video time 0
433         return HB_WORK_OK;
434     }
435
436     // if the packet has a timestamp use it 
437     if ( in->start != -1 )
438     {
439         pv->pts_next = in->start;
440     }
441
442     int pos, len;
443     for ( pos = 0; pos < in->size; pos += len )
444     {
445         uint8_t *parser_output_buffer;
446         int parser_output_buffer_len;
447         int64_t cur = pv->pts_next;
448
449         if ( pv->parser != NULL )
450         {
451             len = av_parser_parse2( pv->parser, pv->context,
452                     &parser_output_buffer, &parser_output_buffer_len,
453                     in->data + pos, in->size - pos, cur, cur, AV_NOPTS_VALUE );
454         }
455         else
456         {
457             parser_output_buffer = in->data;
458             len = parser_output_buffer_len = in->size;
459         }
460         if (parser_output_buffer_len)
461         {
462             // set the duration on every frame since the stream format can
463             // change (it shouldn't but there's no way to guarantee it).
464             // duration is a scaling factor to go from #bytes in the decoded
465             // frame to frame time (in 90KHz mpeg ticks). 'channels' converts
466             // total samples to per-channel samples. 'sample_rate' converts
467             // per-channel samples to seconds per sample and the 90000
468             // is mpeg ticks per second.
469             if ( pv->context->sample_rate && pv->context->channels )
470             {
471                 pv->duration = 90000. /
472                             (double)( pv->context->sample_rate * pv->context->channels );
473             }
474             decodeAudio( w->audio, pv, parser_output_buffer, parser_output_buffer_len );
475         }
476     }
477     writeAudioFifos( w );
478     *buf_out = link_buf_list( pv );
479     return HB_WORK_OK;
480 }
481
482 static int decavcodecInfo( hb_work_object_t *w, hb_work_info_t *info )
483 {
484     hb_work_private_t *pv = w->private_data;
485
486     memset( info, 0, sizeof(*info) );
487
488     if ( pv && pv->context )
489     {
490         AVCodecContext *context = pv->context;
491         info->bitrate = context->bit_rate;
492         info->rate = context->time_base.num;
493         info->rate_base = context->time_base.den;
494         info->profile = context->profile;
495         info->level = context->level;
496         return 1;
497     }
498     return 0;
499 }
500
501 static int decavcodecBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
502                              hb_work_info_t *info )
503 {
504     hb_work_private_t *pv = w->private_data;
505     int ret = 0;
506
507     memset( info, 0, sizeof(*info) );
508
509     if ( pv && pv->context )
510     {
511         return decavcodecInfo( w, info );
512     }
513     // XXX
514     // We should parse the bitstream to find its parameters but for right
515     // now we just return dummy values if there's a codec that will handle it.
516     AVCodec *codec = avcodec_find_decoder( w->codec_param? w->codec_param :
517                                                            CODEC_ID_MP2 );
518     if ( ! codec )
519     {
520         // there's no ffmpeg codec for this audio type - give up
521         return -1;
522     }
523
524     static char codec_name[64];
525     info->name =  strncpy( codec_name, codec->name, sizeof(codec_name)-1 );
526
527     AVCodecParserContext *parser = av_parser_init( codec->id );
528     AVCodecContext *context = avcodec_alloc_context();
529     hb_avcodec_open( context, codec );
530     uint8_t *buffer = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
531     int out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
532     unsigned char *pbuffer;
533     int pos, pbuffer_size;
534
535     while ( buf && !ret )
536     {
537         pos = 0;
538         while ( pos < buf->size )
539         {
540             int len;
541
542             if (parser != NULL )
543             {
544                 len = av_parser_parse2( parser, context, &pbuffer, 
545                                         &pbuffer_size, buf->data + pos, 
546                                         buf->size - pos, buf->start, 
547                                         buf->start, AV_NOPTS_VALUE );
548             }
549             else
550             {
551                 pbuffer = buf->data;
552                 len = pbuffer_size = buf->size;
553             }
554             pos += len;
555             if ( pbuffer_size > 0 )
556             {
557                 AVPacket avp;
558                 av_init_packet( &avp );
559                 avp.data = pbuffer;
560                 avp.size = pbuffer_size;
561
562                 len = avcodec_decode_audio3( context, (int16_t*)buffer, 
563                                              &out_size, &avp );
564                 if ( len > 0 && context->sample_rate > 0 )
565                 {
566                     info->bitrate = context->bit_rate;
567                     info->rate = context->sample_rate;
568                     info->rate_base = 1;
569                     info->channel_layout = 
570                         hb_ff_layout_xlat(context->channel_layout, 
571                                           context->channels);
572                     ret = 1;
573                     break;
574                 }
575             }
576         }
577         buf = buf->next;
578     }
579
580     av_free( buffer );
581     if ( parser != NULL )
582         av_parser_close( parser );
583     hb_avcodec_close( context );
584     return ret;
585 }
586
587 /* -------------------------------------------------------------
588  * General purpose video decoder using libavcodec
589  */
590
591 static uint8_t *copy_plane( uint8_t *dst, uint8_t* src, int dstride, int sstride,
592                             int h )
593 {
594     if ( dstride == sstride )
595     {
596         memcpy( dst, src, dstride * h );
597         return dst + dstride * h;
598     }
599     int lbytes = dstride <= sstride? dstride : sstride;
600     while ( --h >= 0 )
601     {
602         memcpy( dst, src, lbytes );
603         src += sstride;
604         dst += dstride;
605     }
606     return dst;
607 }
608
609 // copy one video frame into an HB buf. If the frame isn't in our color space
610 // or at least one of its dimensions is odd, use sws_scale to convert/rescale it.
611 // Otherwise just copy the bits.
612 static hb_buffer_t *copy_frame( hb_work_private_t *pv, AVFrame *frame )
613 {
614     AVCodecContext *context = pv->context;
615     int w, h;
616     if ( ! pv->job )
617     {
618         // if the dimensions are odd, drop the lsb since h264 requires that
619         // both width and height be even.
620         w = ( context->width >> 1 ) << 1;
621         h = ( context->height >> 1 ) << 1;
622     }
623     else
624     {
625         w =  pv->job->title->width;
626         h =  pv->job->title->height;
627     }
628     hb_buffer_t *buf = hb_video_buffer_init( w, h );
629     uint8_t *dst = buf->data;
630
631     if ( context->pix_fmt != PIX_FMT_YUV420P || w != context->width ||
632          h != context->height )
633     {
634         // have to convert to our internal color space and/or rescale
635         AVPicture dstpic;
636         avpicture_fill( &dstpic, dst, PIX_FMT_YUV420P, w, h );
637
638         if ( ! pv->sws_context )
639         {
640             pv->sws_context = hb_sws_get_context( context->width, context->height, context->pix_fmt,
641                                               w, h, PIX_FMT_YUV420P,
642                                               SWS_LANCZOS|SWS_ACCURATE_RND);
643         }
644         sws_scale( pv->sws_context, frame->data, frame->linesize, 0, h,
645                    dstpic.data, dstpic.linesize );
646     }
647     else
648     {
649         dst = copy_plane( dst, frame->data[0], w, frame->linesize[0], h );
650         w = (w + 1) >> 1; h = (h + 1) >> 1;
651         dst = copy_plane( dst, frame->data[1], w, frame->linesize[1], h );
652         dst = copy_plane( dst, frame->data[2], w, frame->linesize[2], h );
653     }
654     return buf;
655 }
656
657 static int get_frame_buf( AVCodecContext *context, AVFrame *frame )
658 {
659     hb_work_private_t *pv = context->opaque;
660     frame->pts = pv->pts;
661     pv->pts = -1;
662     return avcodec_default_get_buffer( context, frame );
663 }
664
665 static int reget_frame_buf( AVCodecContext *context, AVFrame *frame )
666 {
667     hb_work_private_t *pv = context->opaque;
668     frame->pts = pv->pts;
669     pv->pts = -1;
670     return avcodec_default_reget_buffer( context, frame );
671 }
672
673 static void log_chapter( hb_work_private_t *pv, int chap_num, int64_t pts )
674 {
675     hb_chapter_t *c;
676
677     if ( !pv->job )
678         return;
679
680     c = hb_list_item( pv->job->title->list_chapter, chap_num - 1 );
681     if ( c && c->title )
682     {
683         hb_log( "%s: \"%s\" (%d) at frame %u time %"PRId64,
684                 pv->context->codec->name, c->title, chap_num, pv->nframes, pts );
685     }
686     else
687     {
688         hb_log( "%s: Chapter %d at frame %u time %"PRId64,
689                 pv->context->codec->name, chap_num, pv->nframes, pts );
690     }
691 }
692
693 static void flushDelayQueue( hb_work_private_t *pv )
694 {
695     hb_buffer_t *buf;
696     int slot = pv->nframes & (HEAP_SIZE-1);
697
698     // flush all the video packets left on our timestamp-reordering delay q
699     while ( ( buf = pv->delayq[slot] ) != NULL )
700     {
701         buf->start = heap_pop( &pv->pts_heap );
702         hb_list_add( pv->list, buf );
703         pv->delayq[slot] = NULL;
704         slot = ( slot + 1 ) & (HEAP_SIZE-1);
705     }
706 }
707
708 #define TOP_FIRST PIC_FLAG_TOP_FIELD_FIRST
709 #define PROGRESSIVE PIC_FLAG_PROGRESSIVE_FRAME
710 #define REPEAT_FIRST PIC_FLAG_REPEAT_FIRST_FIELD
711 #define TB 8
712 #define BT 16
713 #define BT_PROG 32
714 #define BTB_PROG 64
715 #define TB_PROG 128
716 #define TBT_PROG 256
717
718 static void checkCadence( int * cadence, uint16_t flags, int64_t start )
719 {
720     /*  Rotate the cadence tracking. */
721     int i = 0;
722     for(i=11; i > 0; i--)
723     {
724         cadence[i] = cadence[i-1];
725     }
726
727     if ( !(flags & PROGRESSIVE) && !(flags & TOP_FIRST) )
728     {
729         /* Not progressive, not top first...
730            That means it's probably bottom
731            first, 2 fields displayed.
732         */
733         //hb_log("MPEG2 Flag: Bottom field first, 2 fields displayed.");
734         cadence[0] = BT;
735     }
736     else if ( !(flags & PROGRESSIVE) && (flags & TOP_FIRST) )
737     {
738         /* Not progressive, top is first,
739            Two fields displayed.
740         */
741         //hb_log("MPEG2 Flag: Top field first, 2 fields displayed.");
742         cadence[0] = TB;
743     }
744     else if ( (flags & PROGRESSIVE) && !(flags & TOP_FIRST) && !( flags & REPEAT_FIRST )  )
745     {
746         /* Progressive, but noting else.
747            That means Bottom first,
748            2 fields displayed.
749         */
750         //hb_log("MPEG2 Flag: Progressive. Bottom field first, 2 fields displayed.");
751         cadence[0] = BT_PROG;
752     }
753     else if ( (flags & PROGRESSIVE) && !(flags & TOP_FIRST) && ( flags & REPEAT_FIRST )  )
754     {
755         /* Progressive, and repeat. .
756            That means Bottom first,
757            3 fields displayed.
758         */
759         //hb_log("MPEG2 Flag: Progressive repeat. Bottom field first, 3 fields displayed.");
760         cadence[0] = BTB_PROG;
761     }
762     else if ( (flags & PROGRESSIVE) && (flags & TOP_FIRST) && !( flags & REPEAT_FIRST )  )
763     {
764         /* Progressive, top first.
765            That means top first,
766            2 fields displayed.
767         */
768         //hb_log("MPEG2 Flag: Progressive. Top field first, 2 fields displayed.");
769         cadence[0] = TB_PROG;
770     }
771     else if ( (flags & PROGRESSIVE) && (flags & TOP_FIRST) && ( flags & REPEAT_FIRST )  )
772     {
773         /* Progressive, top, repeat.
774            That means top first,
775            3 fields displayed.
776         */
777         //hb_log("MPEG2 Flag: Progressive repeat. Top field first, 3 fields displayed.");
778         cadence[0] = TBT_PROG;
779     }
780
781     if ( (cadence[2] <= TB) && (cadence[1] <= TB) && (cadence[0] > TB) && (cadence[11]) )
782         hb_log("%fs: Video -> Film", (float)start / 90000);
783     if ( (cadence[2] > TB) && (cadence[1] <= TB) && (cadence[0] <= TB) && (cadence[11]) )
784         hb_log("%fs: Film -> Video", (float)start / 90000);
785 }
786
787 /*
788  * Decodes a video frame from the specified raw packet data ('data', 'size', 'sequence').
789  * The output of this function is stored in 'pv->list', which contains a list
790  * of zero or more decoded packets.
791  * 
792  * The returned packets are guaranteed to have their timestamps in the correct order,
793  * even if the original packets decoded by libavcodec have misordered timestamps,
794  * due to the use of 'packed B-frames'.
795  * 
796  * Internally the set of decoded packets may be buffered in 'pv->delayq'
797  * until enough packets have been decoded so that the timestamps can be
798  * correctly rewritten, if this is necessary.
799  */
800 static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size, int sequence )
801 {
802     int got_picture, oldlevel = 0;
803     AVFrame frame;
804     AVPacket avp;
805
806     if ( global_verbosity_level <= 1 )
807     {
808         oldlevel = av_log_get_level();
809         av_log_set_level( AV_LOG_QUIET );
810     }
811
812     av_init_packet( &avp );
813     avp.data = data;
814     avp.size = size;
815     if ( avcodec_decode_video2( pv->context, &frame, &got_picture, &avp ) < 0 )
816     {
817         ++pv->decode_errors;     
818     }
819     if ( global_verbosity_level <= 1 )
820     {
821         av_log_set_level( oldlevel );
822     }
823     if( got_picture )
824     {
825         uint16_t flags = 0;
826
827         // ffmpeg makes it hard to attach a pts to a frame. if the MPEG ES
828         // packet had a pts we handed it to av_parser_parse (if the packet had
829         // no pts we set it to -1 but before the parse we can't distinguish between
830         // the start of a video frame with no pts & an intermediate packet of
831         // some frame which never has a pts). we hope that when parse returns
832         // the frame to us the pts we originally handed it will be in parser->pts.
833         // we put this pts into pv->pts so that when a avcodec_decode_video
834         // finally gets around to allocating an AVFrame to hold the decoded
835         // frame we can stuff that pts into the frame. if all of these relays
836         // worked at this point frame.pts should hold the frame's pts from the
837         // original data stream or -1 if it didn't have one. in the latter case
838         // we generate the next pts in sequence for it.
839         double frame_dur = pv->duration;
840         if ( frame_dur <= 0 )
841         {
842             frame_dur = 90000. * (double)pv->context->time_base.num /
843                         (double)pv->context->time_base.den;
844             pv->duration = frame_dur;
845         }
846         if ( pv->context->ticks_per_frame > 1 )
847         {
848             frame_dur *= 2;
849         }
850         if ( frame.repeat_pict )
851         {
852             frame_dur += frame.repeat_pict * pv->duration;
853         }
854         // XXX Unlike every other video decoder, the Raw decoder doesn't
855         //     use the standard buffer allocation routines so we never
856         //     get to put a PTS in the frame. Do it now.
857         if ( pv->context->codec_id == CODEC_ID_RAWVIDEO )
858         {
859             frame.pts = pv->pts;
860             pv->pts = -1;
861         }
862         // If there was no pts for this frame, assume constant frame rate
863         // video & estimate the next frame time from the last & duration.
864         double pts = frame.pts;
865         if ( pts < 0 )
866         {
867             pts = pv->pts_next;
868         }
869         pv->pts_next = pts + frame_dur;
870
871         if ( frame.top_field_first )
872         {
873             flags |= PIC_FLAG_TOP_FIELD_FIRST;
874         }
875         if ( !frame.interlaced_frame )
876         {
877             flags |= PIC_FLAG_PROGRESSIVE_FRAME;
878         }
879         if ( frame.repeat_pict )
880         {
881             flags |= PIC_FLAG_REPEAT_FIRST_FIELD;
882         }
883
884         hb_buffer_t *buf;
885
886         // if we're doing a scan or this content couldn't have been broken
887         // by Microsoft we don't worry about timestamp reordering
888         if ( ! pv->job || ! pv->brokenByMicrosoft )
889         {
890             buf = copy_frame( pv, &frame );
891             buf->start = pts;
892             buf->sequence = sequence;
893             buf->flags = flags;
894             if ( pv->new_chap && buf->start >= pv->chap_time )
895             {
896                 buf->new_chap = pv->new_chap;
897                 pv->new_chap = 0;
898                 pv->chap_time = 0;
899                 log_chapter( pv, buf->new_chap, buf->start );
900             }
901             else if ( pv->nframes == 0 && pv->job )
902             {
903                 log_chapter( pv, pv->job->chapter_start, buf->start );
904             }
905             checkCadence( pv->cadence, buf->flags, buf->start );
906             hb_list_add( pv->list, buf );
907             ++pv->nframes;
908             return got_picture;
909         }
910
911         // XXX This following probably addresses a libavcodec bug but I don't
912         //     see an easy fix so we workaround it here.
913         //
914         // The M$ 'packed B-frames' atrocity results in decoded frames with
915         // the wrong timestamp. E.g., if there are 2 b-frames the timestamps
916         // we see here will be "2 3 1 5 6 4 ..." instead of "1 2 3 4 5 6".
917         // The frames are actually delivered in the right order but with
918         // the wrong timestamp. To get the correct timestamp attached to
919         // each frame we have a delay queue (longer than the max number of
920         // b-frames) & a sorting heap for the timestamps. As each frame
921         // comes out of the decoder the oldest frame in the queue is removed
922         // and associated with the smallest timestamp. Then the new frame is
923         // added to the queue & its timestamp is pushed on the heap.
924         // This does nothing if the timestamps are correct (i.e., the video
925         // uses a codec that Micro$oft hasn't broken yet) but the frames
926         // get timestamped correctly even when M$ has munged them.
927
928         // remove the oldest picture from the frame queue (if any) &
929         // give it the smallest timestamp from our heap. The queue size
930         // is a power of two so we get the slot of the oldest by masking
931         // the frame count & this will become the slot of the newest
932         // once we've removed & processed the oldest.
933         int slot = pv->nframes & (HEAP_SIZE-1);
934         if ( ( buf = pv->delayq[slot] ) != NULL )
935         {
936             buf->start = heap_pop( &pv->pts_heap );
937
938             if ( pv->new_chap && buf->start >= pv->chap_time )
939             {
940                 buf->new_chap = pv->new_chap;
941                 pv->new_chap = 0;
942                 pv->chap_time = 0;
943                 log_chapter( pv, buf->new_chap, buf->start );
944             }
945             else if ( pv->nframes == 0 && pv->job )
946             {
947                 log_chapter( pv, pv->job->chapter_start, buf->start );
948             }
949             checkCadence( pv->cadence, buf->flags, buf->start );
950             hb_list_add( pv->list, buf );
951         }
952
953         // add the new frame to the delayq & push its timestamp on the heap
954         buf = copy_frame( pv, &frame );
955         buf->sequence = sequence;
956         buf->flags = flags;
957         pv->delayq[slot] = buf;
958         heap_push( &pv->pts_heap, pts );
959
960         ++pv->nframes;
961     }
962
963     return got_picture;
964 }
965
966 static void decodeVideo( hb_work_private_t *pv, uint8_t *data, int size, int sequence,
967                          int64_t pts, int64_t dts )
968 {
969     /*
970      * The following loop is a do..while because we need to handle both
971      * data & the flush at the end (signaled by size=0). At the end there's
972      * generally a frame in the parser & one or more frames in the decoder
973      * (depending on the bframes setting).
974      */
975     int pos = 0;
976     do {
977         uint8_t *pout;
978         int pout_len;
979         int len = av_parser_parse2( pv->parser, pv->context, &pout, &pout_len,
980                                     data + pos, size - pos, pts, dts, 0 );
981         pos += len;
982
983         if ( pout_len > 0 )
984         {
985             pv->pts = pv->parser->pts;
986             decodeFrame( pv, pout, pout_len, sequence );
987         }
988     } while ( pos < size );
989
990     /* the stuff above flushed the parser, now flush the decoder */
991     if ( size <= 0 )
992     {
993         while ( decodeFrame( pv, NULL, 0, sequence ) )
994         {
995         }
996         flushDelayQueue( pv );
997     }
998 }
999
1000 /*
1001  * Removes all packets from 'pv->list', links them together into
1002  * a linked-list, and returns the first packet in the list.
1003  */
1004 static hb_buffer_t *link_buf_list( hb_work_private_t *pv )
1005 {
1006     hb_buffer_t *head = hb_list_item( pv->list, 0 );
1007
1008     if ( head )
1009     {
1010         hb_list_rem( pv->list, head );
1011
1012         hb_buffer_t *last = head, *buf;
1013
1014         while ( ( buf = hb_list_item( pv->list, 0 ) ) != NULL )
1015         {
1016             hb_list_rem( pv->list, buf );
1017             last->next = buf;
1018             last = buf;
1019         }
1020     }
1021     return head;
1022 }
1023
1024
1025 static int decavcodecvInit( hb_work_object_t * w, hb_job_t * job )
1026 {
1027
1028     hb_work_private_t *pv = calloc( 1, sizeof( hb_work_private_t ) );
1029     w->private_data = pv;
1030     pv->job   = job;
1031     pv->list = hb_list_init();
1032
1033     int codec_id = w->codec_param;
1034     pv->parser = av_parser_init( codec_id );
1035     pv->context = avcodec_alloc_context2( CODEC_TYPE_VIDEO );
1036
1037     /* we have to wrap ffmpeg's get_buffer to be able to set the pts (?!) */
1038     pv->context->opaque = pv;
1039     pv->context->get_buffer = get_frame_buf;
1040     pv->context->reget_buffer = reget_frame_buf;
1041
1042     return 0;
1043 }
1044
1045 static int next_hdr( hb_buffer_t *in, int offset )
1046 {
1047     uint8_t *dat = in->data;
1048     uint16_t last2 = 0xffff;
1049     for ( ; in->size - offset > 1; ++offset )
1050     {
1051         if ( last2 == 0 && dat[offset] == 0x01 )
1052             // found an mpeg start code
1053             return offset - 2;
1054
1055         last2 = ( last2 << 8 ) | dat[offset];
1056     }
1057
1058     return -1;
1059 }
1060
1061 static int find_hdr( hb_buffer_t *in, int offset, uint8_t hdr_type )
1062 {
1063     if ( in->size - offset < 4 )
1064         // not enough room for an mpeg start code
1065         return -1;
1066
1067     for ( ; ( offset = next_hdr( in, offset ) ) >= 0; ++offset )
1068     {
1069         if ( in->data[offset+3] == hdr_type )
1070             // found it
1071             break;
1072     }
1073     return offset;
1074 }
1075
1076 static int setup_extradata( hb_work_object_t *w, hb_buffer_t *in )
1077 {
1078     hb_work_private_t *pv = w->private_data;
1079
1080     // we can't call the avstream funcs but the read_header func in the
1081     // AVInputFormat may set up some state in the AVContext. In particular 
1082     // vc1t_read_header allocates 'extradata' to deal with header issues
1083     // related to Microsoft's bizarre engineering notions. We alloc a chunk
1084     // of space to make vc1 work then associate the codec with the context.
1085     if ( w->codec_param != CODEC_ID_VC1 )
1086     {
1087         // we haven't been inflicted with M$ - allocate a little space as
1088         // a marker and return success.
1089         pv->context->extradata_size = 0;
1090         pv->context->extradata = av_malloc(pv->context->extradata_size);
1091         return 0;
1092     }
1093
1094     // find the start and and of the sequence header
1095     int shdr, shdr_end;
1096     if ( ( shdr = find_hdr( in, 0, 0x0f ) ) < 0 )
1097     {
1098         // didn't find start of seq hdr
1099         return 1;
1100     }
1101     if ( ( shdr_end = next_hdr( in, shdr + 4 ) ) < 0 )
1102     {
1103         shdr_end = in->size;
1104     }
1105     shdr_end -= shdr;
1106
1107     // find the start and and of the entry point header
1108     int ehdr, ehdr_end;
1109     if ( ( ehdr = find_hdr( in, 0, 0x0e ) ) < 0 )
1110     {
1111         // didn't find start of entry point hdr
1112         return 1;
1113     }
1114     if ( ( ehdr_end = next_hdr( in, ehdr + 4 ) ) < 0 )
1115     {
1116         ehdr_end = in->size;
1117     }
1118     ehdr_end -= ehdr;
1119
1120     // found both headers - allocate an extradata big enough to hold both
1121     // then copy them into it.
1122     pv->context->extradata_size = shdr_end + ehdr_end;
1123     pv->context->extradata = av_malloc(pv->context->extradata_size + 8);
1124     memcpy( pv->context->extradata, in->data + shdr, shdr_end );
1125     memcpy( pv->context->extradata + shdr_end, in->data + ehdr, ehdr_end );
1126     memset( pv->context->extradata + shdr_end + ehdr_end, 0, 8);
1127     return 0;
1128 }
1129
1130 static int decavcodecvWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
1131                             hb_buffer_t ** buf_out )
1132 {
1133     hb_work_private_t *pv = w->private_data;
1134     hb_buffer_t *in = *buf_in;
1135     int64_t pts = AV_NOPTS_VALUE;
1136     int64_t dts = pts;
1137
1138     *buf_in = NULL;
1139
1140     /* if we got an empty buffer signaling end-of-stream send it downstream */
1141     if ( in->size == 0 )
1142     {
1143         if ( pv->context->codec != NULL )
1144         {
1145             decodeVideo( pv, in->data, in->size, in->sequence, pts, dts );
1146         }
1147         hb_list_add( pv->list, in );
1148         *buf_out = link_buf_list( pv );
1149         return HB_WORK_DONE;
1150     }
1151
1152     // if this is the first frame open the codec (we have to wait for the
1153     // first frame because of M$ VC1 braindamage).
1154     if ( pv->context->extradata == NULL )
1155     {
1156         if ( setup_extradata( w, in ) )
1157         {
1158             // we didn't find the headers needed to set up extradata.
1159             // the codec will abort if we open it so just free the buf
1160             // and hope we eventually get the info we need.
1161             hb_buffer_close( &in );
1162             return HB_WORK_OK;
1163         }
1164         AVCodec *codec = avcodec_find_decoder( w->codec_param );
1165         // There's a mis-feature in ffmpeg that causes the context to be 
1166         // incorrectly initialized the 1st time avcodec_open is called.
1167         // If you close it and open a 2nd time, it finishes the job.
1168         hb_avcodec_open( pv->context, codec );
1169         hb_avcodec_close( pv->context );
1170         hb_avcodec_open( pv->context, codec );
1171     }
1172
1173     if( in->start >= 0 )
1174     {
1175         pts = in->start;
1176         dts = in->renderOffset;
1177     }
1178     if ( in->new_chap )
1179     {
1180         pv->new_chap = in->new_chap;
1181         pv->chap_time = pts >= 0? pts : pv->pts_next;
1182     }
1183     decodeVideo( pv, in->data, in->size, in->sequence, pts, dts );
1184     hb_buffer_close( &in );
1185     *buf_out = link_buf_list( pv );
1186     return HB_WORK_OK;
1187 }
1188
1189 static int decavcodecvInfo( hb_work_object_t *w, hb_work_info_t *info )
1190 {
1191     hb_work_private_t *pv = w->private_data;
1192
1193     memset( info, 0, sizeof(*info) );
1194
1195     if ( pv && pv->context )
1196     {
1197         AVCodecContext *context = pv->context;
1198         info->bitrate = context->bit_rate;
1199         info->width = context->width;
1200         info->height = context->height;
1201
1202         /* ffmpeg gives the frame rate in frames per second while HB wants
1203          * it in units of the 27MHz MPEG clock. */
1204         info->rate = 27000000;
1205         info->rate_base = (int64_t)context->time_base.num * 27000000LL /
1206                           context->time_base.den;
1207         if ( context->ticks_per_frame > 1 )
1208         {
1209             // for ffmpeg 0.5 & later, the H.264 & MPEG-2 time base is
1210             // field rate rather than frame rate so convert back to frames.
1211             info->rate_base *= context->ticks_per_frame;
1212         }
1213
1214         info->pixel_aspect_width = context->sample_aspect_ratio.num;
1215         info->pixel_aspect_height = context->sample_aspect_ratio.den;
1216
1217         /* Sometimes there's no pixel aspect set in the source ffmpeg context
1218          * which appears to come from the video stream. In that case,
1219          * try the pixel aspect in AVStream (which appears to come from
1220          * the container). Else assume a 1:1 PAR. */
1221         if ( info->pixel_aspect_width == 0 ||
1222              info->pixel_aspect_height == 0 )
1223         {
1224             // There will not be an ffmpeg stream if the file is TS
1225             AVStream *st = hb_ffmpeg_avstream( w->codec_param );
1226             info->pixel_aspect_width = st && st->sample_aspect_ratio.num ?
1227                                        st->sample_aspect_ratio.num : 1;
1228             info->pixel_aspect_height = st && st->sample_aspect_ratio.den ?
1229                                         st->sample_aspect_ratio.den : 1;
1230         }
1231         /* ffmpeg returns the Pixel Aspect Ratio (PAR). Handbrake wants the
1232          * Display Aspect Ratio so we convert by scaling by the Storage
1233          * Aspect Ratio (w/h). We do the calc in floating point to get the
1234          * rounding right. */
1235         info->aspect = (double)info->pixel_aspect_width * 
1236                        (double)context->width /
1237                        (double)info->pixel_aspect_height /
1238                        (double)context->height;
1239
1240         info->profile = context->profile;
1241         info->level = context->level;
1242         info->name = context->codec->name;
1243         return 1;
1244     }
1245     return 0;
1246 }
1247
1248 static int decavcodecvBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
1249                              hb_work_info_t *info )
1250 {
1251     return 0;
1252 }
1253
1254 hb_work_object_t hb_decavcodecv =
1255 {
1256     WORK_DECAVCODECV,
1257     "Video decoder (libavcodec)",
1258     decavcodecvInit,
1259     decavcodecvWork,
1260     decavcodecClose,
1261     decavcodecvInfo,
1262     decavcodecvBSInfo
1263 };
1264
1265
1266 // This is a special decoder for ffmpeg streams. The ffmpeg stream reader
1267 // includes a parser and passes information from the parser to the decoder
1268 // via a codec context kept in the AVStream of the reader's AVFormatContext.
1269 // We *have* to use that codec context to decode the stream or we'll get
1270 // garbage. ffmpeg_title_scan put a cookie that can be used to get to that
1271 // codec context in our codec_param.
1272
1273 // this routine gets the appropriate context pointer from the ffmpeg
1274 // stream reader. it can't be called until we get the first buffer because
1275 // we can't guarantee that reader will be called before the our init
1276 // routine and if our init is called first we'll get a pointer to the
1277 // old scan stream (which has already been closed).
1278 static void init_ffmpeg_context( hb_work_object_t *w )
1279 {
1280     hb_work_private_t *pv = w->private_data;
1281     pv->context = hb_ffmpeg_context( w->codec_param );
1282
1283     // during scan the decoder gets closed & reopened which will
1284     // close the codec so reopen it if it's not there
1285     if ( ! pv->context->codec )
1286     {
1287         AVCodec *codec = avcodec_find_decoder( pv->context->codec_id );
1288         hb_avcodec_open( pv->context, codec );
1289     }
1290     // set up our best guess at the frame duration.
1291     // the frame rate in the codec is usually bogus but it's sometimes
1292     // ok in the stream.
1293     AVStream *st = hb_ffmpeg_avstream( w->codec_param );
1294
1295     if ( st->nb_frames && st->duration )
1296     {
1297         // compute the average frame duration from the total number
1298         // of frames & the total duration.
1299         pv->duration = ( (double)st->duration * (double)st->time_base.num ) /
1300                        ( (double)st->nb_frames * (double)st->time_base.den );
1301     }
1302     else
1303     {
1304         // XXX We don't have a frame count or duration so try to use the
1305         // far less reliable time base info in the stream.
1306         // Because the time bases are so screwed up, we only take values
1307         // in the range 8fps - 64fps.
1308         AVRational tb;
1309         if ( st->avg_frame_rate.den * 64L > st->avg_frame_rate.num &&
1310              st->avg_frame_rate.num > st->avg_frame_rate.den * 8L )
1311         {
1312             tb.num = st->avg_frame_rate.den;
1313             tb.den = st->avg_frame_rate.num;
1314         }
1315         else if ( st->time_base.num * 64L > st->time_base.den &&
1316                   st->time_base.den > st->time_base.num * 8L )
1317         {
1318             tb = st->time_base;
1319         }
1320         else if ( st->r_frame_rate.den * 64L > st->r_frame_rate.num &&
1321                   st->r_frame_rate.num > st->r_frame_rate.den * 8L )
1322         {
1323             tb.num = st->r_frame_rate.den;
1324             tb.den = st->r_frame_rate.num;
1325         }
1326         else
1327         {
1328             tb.num = 1001;  /*XXX*/
1329             tb.den = 24000; /*XXX*/
1330         }
1331         pv->duration =  (double)tb.num / (double)tb.den;
1332     }
1333     pv->duration *= 90000.;
1334
1335     // we have to wrap ffmpeg's get_buffer to be able to set the pts (?!)
1336     pv->context->opaque = pv;
1337     pv->context->get_buffer = get_frame_buf;
1338     pv->context->reget_buffer = reget_frame_buf;
1339
1340     // avi, mkv and possibly mp4 containers can contain the M$ VFW packed
1341     // b-frames abortion that messes up frame ordering and timestamps.
1342     // XXX ffmpeg knows which streams are broken but doesn't expose the
1343     //     info externally. We should patch ffmpeg to add a flag to the
1344     //     codec context for this but until then we mark all ffmpeg streams
1345     //     as suspicious.
1346     pv->brokenByMicrosoft = 1;
1347 }
1348
1349 static void prepare_ffmpeg_buffer( hb_buffer_t * in )
1350 {
1351     // ffmpeg requires an extra 8 bytes of zero at the end of the buffer and
1352     // will seg fault in odd, data dependent ways if it's not there. (my guess
1353     // is this is a case of a local performance optimization creating a global
1354     // performance degradation since all the time wasted by extraneous data
1355     // copies & memory zeroing has to be huge compared to the minor reduction
1356     // in inner-loop instructions this affords - modern cpus bottleneck on
1357     // memory bandwidth not instruction bandwidth).
1358     if ( in->size + FF_INPUT_BUFFER_PADDING_SIZE > in->alloc )
1359     {
1360         // have to realloc to add the padding
1361         hb_buffer_realloc( in, in->size + FF_INPUT_BUFFER_PADDING_SIZE );
1362     }
1363     memset( in->data + in->size, 0, FF_INPUT_BUFFER_PADDING_SIZE );
1364 }
1365
1366 static int decavcodecviInit( hb_work_object_t * w, hb_job_t * job )
1367 {
1368
1369     hb_work_private_t *pv = calloc( 1, sizeof( hb_work_private_t ) );
1370     int i;
1371     w->private_data = pv;
1372     pv->job   = job;
1373     pv->list = hb_list_init();
1374     pv->pts_next = -1;
1375     pv->pts = -1;
1376
1377     if ( w->audio != NULL )
1378     {
1379         if ( w->audio->config.out.codec == HB_ACODEC_AC3 )
1380         {
1381             // ffmpegs audio encoder expect an smpte chan map as input.
1382             // So we need to map the decoders output to smpte.
1383             pv->out_map = &hb_smpte_chan_map;
1384         }
1385         else
1386         {
1387             pv->out_map = &hb_qt_chan_map;
1388         }
1389         if ( hb_need_downmix( w->audio->config.in.channel_layout, 
1390                               w->audio->config.out.mixdown) )
1391         {
1392             pv->downmix = hb_downmix_init(w->audio->config.in.channel_layout, 
1393                                           w->audio->config.out.mixdown);
1394             hb_downmix_set_chan_map( pv->downmix, &hb_smpte_chan_map, pv->out_map );
1395         }
1396
1397         pv->ff_audio_list = hb_list_init();
1398         for ( i = 0; i < hb_list_count( w->audio->priv.ff_audio_list ); i++ )
1399         {
1400             hb_work_private_t * ff_pv = calloc( 1, sizeof( hb_work_private_t ) );
1401             hb_list_add( pv->ff_audio_list, ff_pv );
1402
1403             hb_audio_t *audio = hb_list_item( w->audio->priv.ff_audio_list, i );
1404
1405             ff_pv->list  = hb_list_init();
1406             ff_pv->job   = job;
1407
1408             if ( audio->config.out.codec == HB_ACODEC_AC3 )
1409             {
1410                 // ffmpegs audio encoder expect an smpte chan map as input.
1411                 // So we need to map the decoders output to smpte.
1412                 ff_pv->out_map = &hb_smpte_chan_map;
1413             }
1414             else
1415             {
1416                 ff_pv->out_map = &hb_qt_chan_map;
1417             }
1418             if ( hb_need_downmix( audio->config.in.channel_layout, 
1419                                   audio->config.out.mixdown) )
1420             {
1421                 ff_pv->downmix = hb_downmix_init(audio->config.in.channel_layout, 
1422                                               audio->config.out.mixdown);
1423                 hb_downmix_set_chan_map( ff_pv->downmix, &hb_smpte_chan_map, ff_pv->out_map );
1424             }
1425         }
1426     }
1427
1428     return 0;
1429 }
1430
1431 static int decavcodecviWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
1432                              hb_buffer_t ** buf_out )
1433 {
1434     hb_work_private_t *pv = w->private_data;
1435     hb_buffer_t *in = *buf_in;
1436     *buf_in = NULL;
1437
1438     /* if we got an empty buffer signaling end-of-stream send it downstream */
1439     if ( in->size == 0 )
1440     {
1441         /* flush any frames left in the decoder */
1442         while ( pv->context && decodeFrame( pv, NULL, 0, in->sequence ) )
1443         {
1444         }
1445         flushDelayQueue( pv );
1446         hb_list_add( pv->list, in );
1447         *buf_out = link_buf_list( pv );
1448         return HB_WORK_DONE;
1449     }
1450
1451     if ( ! pv->context )
1452     {
1453         init_ffmpeg_context( w );
1454     }
1455
1456     int64_t pts = in->start;
1457     if( pts >= 0 )
1458     {
1459         // use the first timestamp as our 'next expected' pts
1460         if ( pv->pts_next < 0 )
1461         {
1462             pv->pts_next = pts;
1463         }
1464         pv->pts = pts;
1465     }
1466
1467     if ( in->new_chap )
1468     {
1469         pv->new_chap = in->new_chap;
1470         pv->chap_time = pts >= 0? pts : pv->pts_next;
1471     }
1472     prepare_ffmpeg_buffer( in );
1473     decodeFrame( pv, in->data, in->size, in->sequence );
1474     hb_buffer_close( &in );
1475     *buf_out = link_buf_list( pv );
1476     return HB_WORK_OK;
1477 }
1478
1479 static int decavcodecviInfo( hb_work_object_t *w, hb_work_info_t *info )
1480 {
1481     if ( decavcodecvInfo( w, info ) )
1482     {
1483         hb_work_private_t *pv = w->private_data;
1484         if ( ! pv->context )
1485         {
1486             init_ffmpeg_context( w );
1487         }
1488         // we have the frame duration in units of the 90KHz pts clock but
1489         // need it in units of the 27MHz MPEG clock. */
1490         info->rate = 27000000;
1491         info->rate_base = pv->duration * 300.;
1492         return 1;
1493     }
1494     return 0;
1495 }
1496
1497 static hb_buffer_t * downmixAudio( 
1498     hb_audio_t *audio, 
1499     hb_work_private_t *pv, 
1500     int16_t *buffer, 
1501     int channels,
1502     int nsamples )
1503 {
1504     hb_buffer_t * buf = NULL;
1505
1506     if ( pv->downmix )
1507     {
1508         pv->downmix_buffer = realloc(pv->downmix_buffer, nsamples * sizeof(hb_sample_t));
1509         
1510         int i;
1511         for( i = 0; i < nsamples; ++i )
1512         {
1513             pv->downmix_buffer[i] = buffer[i];
1514         }
1515
1516         int n_ch_samples = nsamples / channels;
1517         int out_channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown);
1518
1519         buf = hb_buffer_init( n_ch_samples * out_channels * sizeof(float) );
1520         hb_sample_t *samples = (hb_sample_t *)buf->data;
1521         hb_downmix(pv->downmix, samples, pv->downmix_buffer, n_ch_samples);
1522     }
1523     else
1524     {
1525         buf = hb_buffer_init( nsamples * sizeof(float) );
1526         float *fl32 = (float *)buf->data;
1527         int i;
1528         for( i = 0; i < nsamples; ++i )
1529         {
1530             fl32[i] = buffer[i];
1531         }
1532         int n_ch_samples = nsamples / channels;
1533         hb_layout_remap( &hb_smpte_chan_map, pv->out_map,
1534                          audio->config.in.channel_layout, 
1535                          fl32, n_ch_samples );
1536     }
1537
1538     return buf;
1539 }
1540
1541 static void decodeAudio( hb_audio_t * audio, hb_work_private_t *pv, uint8_t *data, int size )
1542 {
1543     AVCodecContext *context = pv->context;
1544     int pos = 0;
1545     int loop_limit = 256;
1546
1547     while ( pos < size )
1548     {
1549         int16_t *buffer = pv->buffer;
1550         if ( buffer == NULL )
1551         {
1552             pv->buffer = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
1553             buffer = pv->buffer;
1554         }
1555
1556         AVPacket avp;
1557         av_init_packet( &avp );
1558         avp.data = data + pos;
1559         avp.size = size - pos;
1560
1561         int out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
1562         int nsamples;
1563         int len = avcodec_decode_audio3( context, buffer, &out_size, &avp );
1564         if ( len < 0 )
1565         {
1566             return;
1567         }
1568         if ( len == 0 )
1569         {
1570             if ( !(loop_limit--) )
1571                 return;
1572         }
1573         else
1574             loop_limit = 256;
1575
1576         pos += len;
1577         if( out_size > 0 )
1578         {
1579             // We require signed 16-bit ints for the output format. If
1580             // we got something different convert it.
1581             if ( context->sample_fmt != SAMPLE_FMT_S16 )
1582             {
1583                 // Note: av_audio_convert seems to be a work-in-progress but
1584                 //       looks like it will eventually handle general audio
1585                 //       mixdowns which would allow us much more flexibility
1586                 //       in handling multichannel audio in HB. If we were doing
1587                 //       anything more complicated than a one-for-one format
1588                 //       conversion we'd probably want to cache the converter
1589                 //       context in the pv.
1590                 int isamp = av_get_bits_per_sample_fmt( context->sample_fmt ) / 8;
1591                 AVAudioConvert *ctx = av_audio_convert_alloc( SAMPLE_FMT_S16, 1,
1592                                                               context->sample_fmt, 1,
1593                                                               NULL, 0 );
1594                 // get output buffer size (in 2-byte samples) then malloc a buffer
1595                 nsamples = out_size / isamp;
1596                 buffer = av_malloc( nsamples * 2 );
1597
1598                 // we're doing straight sample format conversion which behaves as if
1599                 // there were only one channel.
1600                 const void * const ibuf[6] = { pv->buffer };
1601                 void * const obuf[6] = { buffer };
1602                 const int istride[6] = { isamp };
1603                 const int ostride[6] = { 2 };
1604
1605                 av_audio_convert( ctx, obuf, ostride, ibuf, istride, nsamples );
1606                 av_audio_convert_free( ctx );
1607             }
1608             else
1609             {
1610                 nsamples = out_size / 2;
1611             }
1612
1613             hb_buffer_t * buf;
1614             double pts = pv->pts_next;
1615             double pts_next = pts + nsamples * pv->duration;
1616             buf = downmixAudio( audio, pv, buffer, context->channels, nsamples );
1617             if ( buf )
1618             {
1619                 buf->start = pts;
1620                 buf->stop = pts_next;
1621                 hb_list_add( pv->list, buf );
1622             }
1623
1624             int i;
1625             for ( i = 0; i < hb_list_count( audio->priv.ff_audio_list ); i++ )
1626             {
1627                 hb_audio_t *ff_audio = hb_list_item( audio->priv.ff_audio_list, i );
1628                 hb_work_private_t *ff_pv = hb_list_item( pv->ff_audio_list, i );
1629                 if ( ff_pv )
1630                 {
1631                     buf = downmixAudio( ff_audio, ff_pv, buffer, context->channels, nsamples );
1632                     if ( buf )
1633                     {
1634                         buf->start = pts;
1635                         buf->stop = pts_next;
1636                         hb_list_add( ff_pv->list, buf );
1637                     }
1638                 }
1639             }
1640             pv->pts_next = pts_next;
1641
1642             // if we allocated a buffer for sample format conversion, free it
1643             if ( buffer != pv->buffer )
1644             {
1645                 av_free( buffer );
1646             }
1647         }
1648     }
1649 }
1650
1651 static int decavcodecaiWork( hb_work_object_t *w, hb_buffer_t **buf_in,
1652                     hb_buffer_t **buf_out )
1653 {
1654     if ( (*buf_in)->size <= 0 )
1655     {
1656         /* EOF on input stream - send it downstream & say that we're done */
1657         *buf_out = *buf_in;
1658         *buf_in = NULL;
1659         writeAudioEof( w );
1660         return HB_WORK_DONE;
1661     }
1662
1663     hb_work_private_t *pv = w->private_data;
1664
1665     if ( (*buf_in)->start < -1 && pv->pts_next <= 0 )
1666     {
1667         // discard buffers that start before video time 0
1668         *buf_out = NULL;
1669         return HB_WORK_OK;
1670     }
1671
1672     if ( ! pv->context )
1673     {
1674         init_ffmpeg_context( w );
1675         // duration is a scaling factor to go from #bytes in the decoded
1676         // frame to frame time (in 90KHz mpeg ticks). 'channels' converts
1677         // total samples to per-channel samples. 'sample_rate' converts
1678         // per-channel samples to seconds per sample and the 90000
1679         // is mpeg ticks per second.
1680         pv->duration = 90000. /
1681                     (double)( pv->context->sample_rate * pv->context->channels );
1682     }
1683     hb_buffer_t *in = *buf_in;
1684
1685     // if the packet has a timestamp use it if we don't have a timestamp yet
1686     // or if there's been a timing discontinuity of more than 100ms.
1687     if ( in->start >= 0 &&
1688          ( pv->pts_next < 0 || ( in->start - pv->pts_next ) > 90*100 ) )
1689     {
1690         pv->pts_next = in->start;
1691     }
1692     prepare_ffmpeg_buffer( in );
1693     decodeAudio( w->audio, pv, in->data, in->size );
1694     writeAudioFifos( w );
1695     *buf_out = link_buf_list( pv );
1696
1697     return HB_WORK_OK;
1698 }
1699
1700 hb_work_object_t hb_decavcodecvi =
1701 {
1702     WORK_DECAVCODECVI,
1703     "Video decoder (ffmpeg streams)",
1704     decavcodecviInit,
1705     decavcodecviWork,
1706     decavcodecClose,
1707     decavcodecviInfo,
1708     decavcodecvBSInfo
1709 };
1710
1711 hb_work_object_t hb_decavcodecai =
1712 {
1713     WORK_DECAVCODECAI,
1714     "Audio decoder (ffmpeg streams)",
1715     decavcodecviInit,
1716     decavcodecaiWork,
1717     decavcodecClose,
1718     decavcodecInfo,
1719     decavcodecBSInfo
1720 };