OSDN Git Service

If we always sort timestamps we can't detect & correct misordering caused by an SCR...
[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
64 #include "libavcodec/avcodec.h"
65 #include "libavformat/avformat.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 };
112
113 static int64_t heap_pop( pts_heap_t *heap )
114 {
115     int64_t result;
116
117     if ( heap->nheap <= 0 )
118     {
119         return -1;
120     }
121
122     // return the top of the heap then put the bottom element on top,
123     // decrease the heap size by one & rebalence the heap.
124     result = heap->h[1];
125
126     int64_t v = heap->h[heap->nheap--];
127     int parent = 1;
128     int child = parent << 1;
129     while ( child <= heap->nheap )
130     {
131         // find the smallest of the two children of parent
132         if (child < heap->nheap && heap->h[child] > heap->h[child+1] )
133             ++child;
134
135         if (v <= heap->h[child])
136             // new item is smaller than either child so it's the new parent.
137             break;
138
139         // smallest child is smaller than new item so move it up then
140         // check its children.
141         int64_t hp = heap->h[child];
142         heap->h[parent] = hp;
143         parent = child;
144         child = parent << 1;
145     }
146     heap->h[parent] = v;
147     return result;
148 }
149
150 static void heap_push( pts_heap_t *heap, int64_t v )
151 {
152     if ( heap->nheap < HEAP_SIZE )
153     {
154         ++heap->nheap;
155     }
156
157     // stick the new value on the bottom of the heap then bubble it
158     // up to its correct spot.
159         int child = heap->nheap;
160         while (child > 1) {
161                 int parent = child >> 1;
162                 if (heap->h[parent] <= v)
163                         break;
164                 // move parent down
165                 int64_t hp = heap->h[parent];
166                 heap->h[child] = hp;
167                 child = parent;
168         }
169         heap->h[child] = v;
170 }
171
172
173 /***********************************************************************
174  * hb_work_decavcodec_init
175  ***********************************************************************
176  *
177  **********************************************************************/
178 static int decavcodecInit( hb_work_object_t * w, hb_job_t * job )
179 {
180     AVCodec * codec;
181
182     hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
183     w->private_data = pv;
184
185     pv->job   = job;
186
187     int codec_id = w->codec_param;
188     /*XXX*/
189     if ( codec_id == 0 )
190         codec_id = CODEC_ID_MP2;
191     codec = avcodec_find_decoder( codec_id );
192     pv->parser = av_parser_init( codec_id );
193
194     pv->context = avcodec_alloc_context();
195     avcodec_open( pv->context, codec );
196
197     return 0;
198 }
199
200 /***********************************************************************
201  * Close
202  ***********************************************************************
203  *
204  **********************************************************************/
205 static void decavcodecClose( hb_work_object_t * w )
206 {
207     hb_work_private_t * pv = w->private_data;
208
209     if ( pv )
210     {
211         if ( pv->job && pv->context && pv->context->codec )
212         {
213             hb_log( "%s-decoder done: %u frames, %u decoder errors, %u drops",
214                     pv->context->codec->name, pv->nframes, pv->decode_errors,
215                     pv->ndrops );
216         }
217         if ( pv->parser )
218         {
219             av_parser_close(pv->parser);
220         }
221         if ( pv->context && pv->context->codec )
222         {
223             avcodec_close( pv->context );
224         }
225         if ( pv->list )
226         {
227             hb_list_close( &pv->list );
228         }
229         if ( pv->buffer )
230         {
231             free( pv->buffer );
232             pv->buffer = NULL;
233         }
234         free( pv );
235         w->private_data = NULL;
236     }
237 }
238
239 /***********************************************************************
240  * Work
241  ***********************************************************************
242  *
243  **********************************************************************/
244 static int decavcodecWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
245                     hb_buffer_t ** buf_out )
246 {
247     hb_work_private_t * pv = w->private_data;
248     hb_buffer_t * in = *buf_in, * buf, * last = NULL;
249     int   pos, len, out_size, i, uncompressed_len;
250     short buffer[AVCODEC_MAX_AUDIO_FRAME_SIZE];
251     uint64_t cur;
252     unsigned char *parser_output_buffer;
253     int parser_output_buffer_len;
254
255     if ( (*buf_in)->size <= 0 )
256     {
257         /* EOF on input stream - send it downstream & say that we're done */
258         *buf_out = *buf_in;
259         *buf_in = NULL;
260         return HB_WORK_DONE;
261     }
262
263     *buf_out = NULL;
264
265     cur = ( in->start < 0 )? pv->pts_next : in->start;
266
267     pos = 0;
268     while( pos < in->size )
269     {
270         len = av_parser_parse( pv->parser, pv->context,
271                                &parser_output_buffer, &parser_output_buffer_len,
272                                in->data + pos, in->size - pos, cur, cur );
273         out_size = 0;
274         uncompressed_len = 0;
275         if (parser_output_buffer_len)
276         {
277             out_size = sizeof(buffer);
278             uncompressed_len = avcodec_decode_audio2( pv->context, buffer,
279                                                       &out_size,
280                                                       parser_output_buffer,
281                                                       parser_output_buffer_len );
282         }
283         if( out_size )
284         {
285             short * s16;
286             float * fl32;
287
288             buf = hb_buffer_init( 2 * out_size );
289
290             int sample_size_in_bytes = 2;   // Default to 2 bytes
291             switch (pv->context->sample_fmt)
292             {
293               case SAMPLE_FMT_S16:
294                 sample_size_in_bytes = 2;
295                 break;
296               /* We should handle other formats here - but that needs additional format conversion work below */
297               /* For now we'll just report the error and try to carry on */
298               default:
299                 hb_log("decavcodecWork - Unknown Sample Format from avcodec_decode_audio (%d) !", pv->context->sample_fmt);
300                 break;
301             }
302
303             buf->start = cur;
304             buf->stop  = cur + 90000 * ( out_size / (sample_size_in_bytes * pv->context->channels) ) /
305                          pv->context->sample_rate;
306             cur = buf->stop;
307
308             s16  = buffer;
309             fl32 = (float *) buf->data;
310             for( i = 0; i < out_size / 2; i++ )
311             {
312                 fl32[i] = s16[i];
313             }
314
315             if( last )
316             {
317                 last = last->next = buf;
318             }
319             else
320             {
321                 *buf_out = last = buf;
322             }
323         }
324
325         pos += len;
326     }
327
328     pv->pts_next = cur;
329
330     return HB_WORK_OK;
331 }
332
333 static int decavcodecInfo( hb_work_object_t *w, hb_work_info_t *info )
334 {
335     hb_work_private_t *pv = w->private_data;
336
337     memset( info, 0, sizeof(*info) );
338
339     if ( pv && pv->context )
340     {
341         AVCodecContext *context = pv->context;
342         info->bitrate = context->bit_rate;
343         info->rate = context->time_base.num;
344         info->rate_base = context->time_base.den;
345         info->profile = context->profile;
346         info->level = context->level;
347         return 1;
348     }
349     return 0;
350 }
351
352 static int decavcodecBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
353                              hb_work_info_t *info )
354 {
355     hb_work_private_t *pv = w->private_data;
356
357     memset( info, 0, sizeof(*info) );
358
359     if ( pv && pv->context )
360     {
361         return decavcodecInfo( w, info );
362     }
363     // XXX
364     // We should parse the bitstream to find its parameters but for right
365     // now we just return dummy values if there's a codec that will handle it.
366     AVCodec *codec = avcodec_find_decoder( w->codec_param? w->codec_param :
367                                                            CODEC_ID_MP2 );
368     if ( codec )
369     {
370         static char codec_name[64];
371
372         info->name =  strncpy( codec_name, codec->name, sizeof(codec_name)-1 );
373         info->bitrate = 384000;
374         info->rate = 48000;
375         info->rate_base = 1;
376         info->channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
377         return 1;
378     }
379     return -1;
380 }
381
382 /* -------------------------------------------------------------
383  * General purpose video decoder using libavcodec
384  */
385
386 static uint8_t *copy_plane( uint8_t *dst, uint8_t* src, int dstride, int sstride,
387                             int h )
388 {
389     if ( dstride == sstride )
390     {
391         memcpy( dst, src, dstride * h );
392         return dst + dstride * h;
393     }
394     int lbytes = dstride <= sstride? dstride : sstride;
395     while ( --h >= 0 )
396     {
397         memcpy( dst, src, lbytes );
398         src += sstride;
399         dst += dstride;
400     }
401     return dst;
402 }
403
404 /* Note: assumes frame format is PIX_FMT_YUV420P */
405 static hb_buffer_t *copy_frame( AVCodecContext *context, AVFrame *frame )
406 {
407     int w = context->width, h = context->height;
408     hb_buffer_t *buf = hb_buffer_init( w * h * 3 / 2 );
409     uint8_t *dst = buf->data;
410
411     dst = copy_plane( dst, frame->data[0], w, frame->linesize[0], h );
412     w >>= 1; h >>= 1;
413     dst = copy_plane( dst, frame->data[1], w, frame->linesize[1], h );
414     dst = copy_plane( dst, frame->data[2], w, frame->linesize[2], h );
415
416     return buf;
417 }
418
419 static int get_frame_buf( AVCodecContext *context, AVFrame *frame )
420 {
421     hb_work_private_t *pv = context->opaque;
422     frame->pts = pv->pts;
423     pv->pts = -1;
424     return avcodec_default_get_buffer( context, frame );
425 }
426
427 static void log_chapter( hb_work_private_t *pv, int chap_num, int64_t pts )
428 {
429     hb_chapter_t *c = hb_list_item( pv->job->title->list_chapter, chap_num - 1 );
430     if ( c && c->title )
431     {
432         hb_log( "%s: \"%s\" (%d) at frame %u time %lld",
433                 pv->context->codec->name, c->title, chap_num, pv->nframes, pts );
434     }
435     else
436     {
437         hb_log( "%s: Chapter %d at frame %u time %lld",
438                 pv->context->codec->name, chap_num, pv->nframes, pts );
439     }
440 }
441
442 static void flushDelayQueue( hb_work_private_t *pv )
443 {
444     hb_buffer_t *buf;
445     int slot = pv->nframes & (HEAP_SIZE-1);
446
447     // flush all the video packets left on our timestamp-reordering delay q
448     while ( ( buf = pv->delayq[slot] ) != NULL )
449     {
450         buf->start = heap_pop( &pv->pts_heap );
451         hb_list_add( pv->list, buf );
452         pv->delayq[slot] = NULL;
453         slot = ( slot + 1 ) & (HEAP_SIZE-1);
454     }
455 }
456
457 static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size )
458 {
459     int got_picture;
460     AVFrame frame;
461
462     if ( avcodec_decode_video( pv->context, &frame, &got_picture, data, size ) < 0 )
463     {
464         ++pv->decode_errors;     
465     }
466     if( got_picture )
467     {
468         // ffmpeg makes it hard to attach a pts to a frame. if the MPEG ES
469         // packet had a pts we handed it to av_parser_parse (if the packet had
470         // no pts we set it to -1 but before the parse we can't distinguish between
471         // the start of a video frame with no pts & an intermediate packet of
472         // some frame which never has a pts). we hope that when parse returns
473         // the frame to us the pts we originally handed it will be in parser->pts.
474         // we put this pts into pv->pts so that when a avcodec_decode_video
475         // finally gets around to allocating an AVFrame to hold the decoded
476         // frame we can stuff that pts into the frame. if all of these relays
477         // worked at this point frame.pts should hold the frame's pts from the
478         // original data stream or -1 if it didn't have one. in the latter case
479         // we generate the next pts in sequence for it.
480         double frame_dur = pv->duration;
481         if ( frame_dur <= 0 )
482         {
483             frame_dur = 90000. * (double)pv->context->time_base.num /
484                         (double)pv->context->time_base.den;
485             pv->duration = frame_dur;
486         }
487         if ( frame.repeat_pict )
488         {
489             frame_dur += frame.repeat_pict * frame_dur * 0.5;
490         }
491         // If there was no pts for this frame, assume constant frame rate
492         // video & estimate the next frame time from the last & duration.
493         double pts = frame.pts;
494         if ( pts < 0 )
495         {
496             pts = pv->pts_next;
497         }
498         pv->pts_next = pts + frame_dur;
499
500         hb_buffer_t *buf;
501
502         // if we're doing a scan or this content couldn't have been broken
503         // by Microsoft we don't worry about timestamp reordering
504         if ( ! pv->job || ! pv->brokenByMicrosoft )
505         {
506             buf = copy_frame( pv->context, &frame );
507             buf->start = pts;
508             hb_list_add( pv->list, buf );
509             ++pv->nframes;
510             return got_picture;
511         }
512
513         // XXX This following probably addresses a libavcodec bug but I don't
514         //     see an easy fix so we workaround it here.
515         //
516         // The M$ 'packed B-frames' atrocity results in decoded frames with
517         // the wrong timestamp. E.g., if there are 2 b-frames the timestamps
518         // we see here will be "2 3 1 5 6 4 ..." instead of "1 2 3 4 5 6".
519         // The frames are actually delivered in the right order but with
520         // the wrong timestamp. To get the correct timestamp attached to
521         // each frame we have a delay queue (longer than the max number of
522         // b-frames) & a sorting heap for the timestamps. As each frame
523         // comes out of the decoder the oldest frame in the queue is removed
524         // and associated with the smallest timestamp. Then the new frame is
525         // added to the queue & its timestamp is pushed on the heap.
526         // This does nothing if the timestamps are correct (i.e., the video
527         // uses a codec that Micro$oft hasn't broken yet) but the frames
528         // get timestamped correctly even when M$ has munged them.
529
530         // remove the oldest picture from the frame queue (if any) &
531         // give it the smallest timestamp from our heap. The queue size
532         // is a power of two so we get the slot of the oldest by masking
533         // the frame count & this will become the slot of the newest
534         // once we've removed & processed the oldest.
535         int slot = pv->nframes & (HEAP_SIZE-1);
536         if ( ( buf = pv->delayq[slot] ) != NULL )
537         {
538             buf->start = heap_pop( &pv->pts_heap );
539
540             if ( pv->new_chap && buf->start >= pv->chap_time )
541             {
542                 buf->new_chap = pv->new_chap;
543                 pv->new_chap = 0;
544                 pv->chap_time = 0;
545                 log_chapter( pv, buf->new_chap, buf->start );
546             }
547             else if ( pv->nframes == 0 )
548             {
549                 log_chapter( pv, pv->job->chapter_start, buf->start );
550             }
551             hb_list_add( pv->list, buf );
552         }
553
554         // add the new frame to the delayq & push its timestamp on the heap
555         pv->delayq[slot] = copy_frame( pv->context, &frame );
556         heap_push( &pv->pts_heap, pts );
557
558         ++pv->nframes;
559     }
560
561     return got_picture;
562 }
563
564 static void decodeVideo( hb_work_private_t *pv, uint8_t *data, int size,
565                          int64_t pts, int64_t dts )
566 {
567     /*
568      * The following loop is a do..while because we need to handle both
569      * data & the flush at the end (signaled by size=0). At the end there's
570      * generally a frame in the parser & one or more frames in the decoder
571      * (depending on the bframes setting).
572      */
573     int pos = 0;
574     do {
575         uint8_t *pout;
576         int pout_len;
577         int len = av_parser_parse( pv->parser, pv->context, &pout, &pout_len,
578                                    data + pos, size - pos, pts, dts );
579         pos += len;
580
581         if ( pout_len > 0 )
582         {
583             pv->pts = pv->parser->pts;
584             decodeFrame( pv, pout, pout_len );
585         }
586     } while ( pos < size );
587
588     /* the stuff above flushed the parser, now flush the decoder */
589     if ( size <= 0 )
590     {
591         while ( decodeFrame( pv, NULL, 0 ) )
592         {
593         }
594         flushDelayQueue( pv );
595     }
596 }
597
598 static hb_buffer_t *link_buf_list( hb_work_private_t *pv )
599 {
600     hb_buffer_t *head = hb_list_item( pv->list, 0 );
601
602     if ( head )
603     {
604         hb_list_rem( pv->list, head );
605
606         hb_buffer_t *last = head, *buf;
607
608         while ( ( buf = hb_list_item( pv->list, 0 ) ) != NULL )
609         {
610             hb_list_rem( pv->list, buf );
611             last->next = buf;
612             last = buf;
613         }
614     }
615     return head;
616 }
617
618
619 static int decavcodecvInit( hb_work_object_t * w, hb_job_t * job )
620 {
621
622     hb_work_private_t *pv = calloc( 1, sizeof( hb_work_private_t ) );
623     w->private_data = pv;
624     pv->job   = job;
625     pv->list = hb_list_init();
626
627     int codec_id = w->codec_param;
628     pv->parser = av_parser_init( codec_id );
629     pv->context = avcodec_alloc_context2( CODEC_TYPE_VIDEO );
630
631     /* we have to wrap ffmpeg's get_buffer to be able to set the pts (?!) */
632     pv->context->opaque = pv;
633     pv->context->get_buffer = get_frame_buf;
634
635     AVCodec *codec = avcodec_find_decoder( codec_id );
636
637     // we can't call the avstream funcs but the read_header func in the
638     // AVInputFormat may set up some state in the AVContext. In particular 
639     // vc1t_read_header allocates 'extradata' to deal with header issues
640     // related to Microsoft's bizarre engineering notions. We alloc a chunk
641     // of space to make vc1 work then associate the codec with the context.
642     pv->context->extradata_size = 32;
643     pv->context->extradata = av_malloc(pv->context->extradata_size);
644     avcodec_open( pv->context, codec );
645
646     return 0;
647 }
648
649 static int decavcodecvWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
650                             hb_buffer_t ** buf_out )
651 {
652     hb_work_private_t *pv = w->private_data;
653     hb_buffer_t *in = *buf_in;
654     int64_t pts = AV_NOPTS_VALUE;
655     int64_t dts = pts;
656
657     *buf_in = NULL;
658
659     /* if we got an empty buffer signaling end-of-stream send it downstream */
660     if ( in->size == 0 )
661     {
662         decodeVideo( pv, in->data, in->size, pts, dts );
663         hb_list_add( pv->list, in );
664         *buf_out = link_buf_list( pv );
665         return HB_WORK_DONE;
666     }
667
668     if( in->start >= 0 )
669     {
670         pts = in->start;
671         dts = in->renderOffset;
672     }
673     if ( in->new_chap )
674     {
675         pv->new_chap = in->new_chap;
676         pv->chap_time = pts >= 0? pts : pv->pts_next;
677     }
678     decodeVideo( pv, in->data, in->size, pts, dts );
679     hb_buffer_close( &in );
680     *buf_out = link_buf_list( pv );
681     return HB_WORK_OK;
682 }
683
684 static int decavcodecvInfo( hb_work_object_t *w, hb_work_info_t *info )
685 {
686     hb_work_private_t *pv = w->private_data;
687
688     memset( info, 0, sizeof(*info) );
689
690     if ( pv && pv->context )
691     {
692         AVCodecContext *context = pv->context;
693         info->bitrate = context->bit_rate;
694         info->width = context->width;
695         info->height = context->height;
696
697         /* ffmpeg gives the frame rate in frames per second while HB wants
698          * it in units of the 27MHz MPEG clock. */
699         info->rate = 27000000;
700         info->rate_base = (int64_t)context->time_base.num * 27000000LL /
701                           context->time_base.den;
702         
703         /* Sometimes there's no pixel aspect set in the source. In that case,
704            assume a 1:1 PAR. Otherwise, preserve the source PAR.             */
705         info->pixel_aspect_width = context->sample_aspect_ratio.num ?
706                                         context->sample_aspect_ratio.num : 1;
707         info->pixel_aspect_height = context->sample_aspect_ratio.den ?
708                                         context->sample_aspect_ratio.den : 1;
709
710         /* ffmpeg returns the Pixel Aspect Ratio (PAR). Handbrake wants the
711          * Display Aspect Ratio so we convert by scaling by the Storage
712          * Aspect Ratio (w/h). We do the calc in floating point to get the
713          * rounding right. */
714         info->aspect = (double)info->pixel_aspect_width * 
715                        (double)context->width /
716                        (double)info->pixel_aspect_height /
717                        (double)context->height;
718
719         info->profile = context->profile;
720         info->level = context->level;
721         info->name = context->codec->name;
722         return 1;
723     }
724     return 0;
725 }
726
727 static int decavcodecvBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
728                              hb_work_info_t *info )
729 {
730     return 0;
731 }
732
733 hb_work_object_t hb_decavcodecv =
734 {
735     WORK_DECAVCODECV,
736     "Video decoder (libavcodec)",
737     decavcodecvInit,
738     decavcodecvWork,
739     decavcodecClose,
740     decavcodecvInfo,
741     decavcodecvBSInfo
742 };
743
744
745 // This is a special decoder for ffmpeg streams. The ffmpeg stream reader
746 // includes a parser and passes information from the parser to the decoder
747 // via a codec context kept in the AVStream of the reader's AVFormatContext.
748 // We *have* to use that codec context to decode the stream or we'll get
749 // garbage. ffmpeg_title_scan put a cookie that can be used to get to that
750 // codec context in our codec_param.
751
752 // this routine gets the appropriate context pointer from the ffmpeg
753 // stream reader. it can't be called until we get the first buffer because
754 // we can't guarantee that reader will be called before the our init
755 // routine and if our init is called first we'll get a pointer to the
756 // old scan stream (which has already been closed).
757 static void init_ffmpeg_context( hb_work_object_t *w )
758 {
759     hb_work_private_t *pv = w->private_data;
760     pv->context = hb_ffmpeg_context( w->codec_param );
761
762     // during scan the decoder gets closed & reopened which will
763     // close the codec so reopen it if it's not there
764     if ( ! pv->context->codec )
765     {
766         AVCodec *codec = avcodec_find_decoder( pv->context->codec_id );
767         avcodec_open( pv->context, codec );
768     }
769     // set up our best guess at the frame duration.
770     // the frame rate in the codec is usually bogus but it's sometimes
771     // ok in the stream.
772     AVStream *st = hb_ffmpeg_avstream( w->codec_param );
773
774     if ( st->nb_frames && st->duration )
775     {
776         // compute the average frame duration from the total number
777         // of frames & the total duration.
778         pv->duration = ( (double)st->duration * (double)st->time_base.num ) /
779                        ( (double)st->nb_frames * (double)st->time_base.den );
780     }
781     else
782     {
783         // XXX We don't have a frame count or duration so try to use the
784         // far less reliable time base info in the stream.
785         // Because the time bases are so screwed up, we only take values
786         // in the range 8fps - 64fps.
787         AVRational tb;
788         if ( st->time_base.num * 64 > st->time_base.den &&
789              st->time_base.den > st->time_base.num * 8 )
790         {
791             tb = st->time_base;
792         }
793         else if ( st->r_frame_rate.den * 64 > st->r_frame_rate.num &&
794                   st->r_frame_rate.num > st->r_frame_rate.den * 8 )
795         {
796             tb.num = st->r_frame_rate.den;
797             tb.den = st->r_frame_rate.num;
798         }
799         else
800         {
801             tb.num = 1001;  /*XXX*/
802             tb.den = 24000; /*XXX*/
803         }
804         pv->duration =  (double)tb.num / (double)tb.den;
805     }
806     pv->duration *= 90000.;
807
808     // we have to wrap ffmpeg's get_buffer to be able to set the pts (?!)
809     pv->context->opaque = pv;
810     pv->context->get_buffer = get_frame_buf;
811
812     // avi, mkv and possibly mp4 containers can contain the M$ VFW packed
813     // b-frames abortion that messes up frame ordering and timestamps.
814     // XXX ffmpeg knows which streams are broken but doesn't expose the
815     //     info externally. We should patch ffmpeg to add a flag to the
816     //     codec context for this but until then we mark all ffmpeg streams
817     //     as suspicious.
818     pv->brokenByMicrosoft = 1;
819 }
820
821 static void prepare_ffmpeg_buffer( hb_buffer_t * in )
822 {
823     // ffmpeg requires an extra 8 bytes of zero at the end of the buffer and
824     // will seg fault in odd, data dependent ways if it's not there. (my guess
825     // is this is a case of a local performance optimization creating a global
826     // performance degradation since all the time wasted by extraneous data
827     // copies & memory zeroing has to be huge compared to the minor reduction
828     // in inner-loop instructions this affords - modern cpus bottleneck on
829     // memory bandwidth not instruction bandwidth).
830     if ( in->size + FF_INPUT_BUFFER_PADDING_SIZE > in->alloc )
831     {
832         // have to realloc to add the padding
833         hb_buffer_realloc( in, in->size + FF_INPUT_BUFFER_PADDING_SIZE );
834     }
835     memset( in->data + in->size, 0, FF_INPUT_BUFFER_PADDING_SIZE );
836 }
837
838 static int decavcodecviInit( hb_work_object_t * w, hb_job_t * job )
839 {
840
841     hb_work_private_t *pv = calloc( 1, sizeof( hb_work_private_t ) );
842     w->private_data = pv;
843     pv->job   = job;
844     pv->list = hb_list_init();
845     pv->pts_next = -1;
846     pv->pts = -1;
847     return 0;
848 }
849
850 static int decavcodecviWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
851                              hb_buffer_t ** buf_out )
852 {
853     hb_work_private_t *pv = w->private_data;
854     if ( ! pv->context )
855     {
856         init_ffmpeg_context( w );
857     }
858     hb_buffer_t *in = *buf_in;
859     *buf_in = NULL;
860
861     /* if we got an empty buffer signaling end-of-stream send it downstream */
862     if ( in->size == 0 )
863     {
864         /* flush any frames left in the decoder */
865         while ( decodeFrame( pv, NULL, 0 ) )
866         {
867         }
868         flushDelayQueue( pv );
869         hb_list_add( pv->list, in );
870         *buf_out = link_buf_list( pv );
871         return HB_WORK_DONE;
872     }
873
874     int64_t pts = in->start;
875     if( pts >= 0 )
876     {
877         // use the first timestamp as our 'next expected' pts
878         if ( pv->pts_next < 0 )
879         {
880             pv->pts_next = pts;
881         }
882         pv->pts = pts;
883     }
884
885     if ( in->new_chap )
886     {
887         pv->new_chap = in->new_chap;
888         pv->chap_time = pts >= 0? pts : pv->pts_next;
889     }
890     prepare_ffmpeg_buffer( in );
891     decodeFrame( pv, in->data, in->size );
892     hb_buffer_close( &in );
893     *buf_out = link_buf_list( pv );
894     return HB_WORK_OK;
895 }
896
897 static int decavcodecviInfo( hb_work_object_t *w, hb_work_info_t *info )
898 {
899     if ( decavcodecvInfo( w, info ) )
900     {
901         hb_work_private_t *pv = w->private_data;
902         if ( ! pv->context )
903         {
904             init_ffmpeg_context( w );
905         }
906         // we have the frame duration in units of the 90KHz pts clock but
907         // need it in units of the 27MHz MPEG clock. */
908         info->rate = 27000000;
909         info->rate_base = pv->duration * 300.;
910         return 1;
911     }
912     return 0;
913 }
914
915 static void decodeAudio( hb_work_private_t *pv, uint8_t *data, int size )
916 {
917     AVCodecContext *context = pv->context;
918     int pos = 0;
919
920     while ( pos < size )
921     {
922         int16_t *buffer = pv->buffer;
923         if ( buffer == NULL )
924         {
925             // XXX ffmpeg bug workaround
926             // malloc a buffer for the audio decode. On an x86, ffmpeg
927             // uses mmx/sse instructions on this buffer without checking
928             // that it's 16 byte aligned and this will cause an abort if
929             // the buffer is allocated on our stack. Rather than doing
930             // complicated, machine dependent alignment here we use the
931             // fact that malloc returns an aligned pointer on most architectures.
932             pv->buffer = malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
933             buffer = pv->buffer;
934         }
935         int out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
936         int len = avcodec_decode_audio2( context, buffer, &out_size,
937                                          data + pos, size - pos );
938         if ( len <= 0 )
939         {
940             return;
941         }
942         pos += len;
943         if( out_size > 0 )
944         {
945             hb_buffer_t *buf = hb_buffer_init( 2 * out_size );
946
947             // convert from bytes to total samples
948             out_size >>= 1;
949
950             double pts = pv->pts_next;
951             buf->start = pts;
952             pts += out_size * pv->duration;
953             buf->stop  = pts;
954             pv->pts_next = pts;
955
956             float *fl32 = (float *)buf->data;
957             int i;
958             for( i = 0; i < out_size; ++i )
959             {
960                 fl32[i] = buffer[i];
961             }
962             hb_list_add( pv->list, buf );
963         }
964     }
965 }
966
967 static int decavcodecaiWork( hb_work_object_t *w, hb_buffer_t **buf_in,
968                     hb_buffer_t **buf_out )
969 {
970     if ( (*buf_in)->size <= 0 )
971     {
972         /* EOF on input stream - send it downstream & say that we're done */
973         *buf_out = *buf_in;
974         *buf_in = NULL;
975         return HB_WORK_DONE;
976     }
977
978     hb_work_private_t *pv = w->private_data;
979     if ( ! pv->context )
980     {
981         init_ffmpeg_context( w );
982         // duration is a scaling factor to go from #bytes in the decoded
983         // frame to frame time (in 90KHz mpeg ticks). 'channels' converts
984         // total samples to per-channel samples. 'sample_rate' converts
985         // per-channel samples to seconds per sample and the 90000
986         // is mpeg ticks per second.
987         pv->duration = 90000. /
988                     (double)( pv->context->sample_rate * pv->context->channels );
989     }
990     hb_buffer_t *in = *buf_in;
991
992     // if the packet has a timestamp use it if we don't have a timestamp yet
993     // or if there's been a timing discontinuity of more than 100ms.
994     if ( in->start >= 0 &&
995          ( pv->pts_next < 0 || ( in->start - pv->pts_next ) > 90*100 ) )
996     {
997         pv->pts_next = in->start;
998     }
999     prepare_ffmpeg_buffer( in );
1000     decodeAudio( pv, in->data, in->size );
1001     *buf_out = link_buf_list( pv );
1002
1003     return HB_WORK_OK;
1004 }
1005
1006 hb_work_object_t hb_decavcodecvi =
1007 {
1008     WORK_DECAVCODECVI,
1009     "Video decoder (ffmpeg streams)",
1010     decavcodecviInit,
1011     decavcodecviWork,
1012     decavcodecClose,
1013     decavcodecviInfo,
1014     decavcodecvBSInfo
1015 };
1016
1017 hb_work_object_t hb_decavcodecai =
1018 {
1019     WORK_DECAVCODECAI,
1020     "Audio decoder (ffmpeg streams)",
1021     decavcodecviInit,
1022     decavcodecaiWork,
1023     decavcodecClose,
1024     decavcodecInfo,
1025     decavcodecBSInfo
1026 };