OSDN Git Service

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