OSDN Git Service

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