OSDN Git Service

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