OSDN Git Service

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