OSDN Git Service

- patch a reference picture leak in ffmpeg/libavcodec/mpegvideo.c that caused aborts...
[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 struct hb_work_private_s
85 {
86     hb_job_t             *job;
87     AVCodecContext       *context;
88     AVCodecParserContext *parser;
89     hb_list_t            *list;
90     double               pts_next;  // next pts we expect to generate
91     int64_t              pts;       // (video) pts passing from parser to decoder
92     int64_t              chap_time; // time of next chap mark (if new_chap != 0)
93     int                  new_chap;
94     int                  ignore_pts; // workaround M$ bugs
95     uint32_t             nframes;
96     uint32_t             ndrops;
97     uint32_t             decode_errors;
98     double               duration;  // frame duration (for video)
99 };
100
101
102
103 /***********************************************************************
104  * hb_work_decavcodec_init
105  ***********************************************************************
106  *
107  **********************************************************************/
108 static int decavcodecInit( hb_work_object_t * w, hb_job_t * job )
109 {
110     AVCodec * codec;
111
112     hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
113     w->private_data = pv;
114
115     pv->job   = job;
116
117     int codec_id = w->codec_param;
118     /*XXX*/
119     if ( codec_id == 0 )
120         codec_id = CODEC_ID_MP2;
121     codec = avcodec_find_decoder( codec_id );
122     pv->parser = av_parser_init( codec_id );
123
124     pv->context = avcodec_alloc_context();
125     avcodec_open( pv->context, codec );
126
127     return 0;
128 }
129
130 /***********************************************************************
131  * Close
132  ***********************************************************************
133  *
134  **********************************************************************/
135 static void decavcodecClose( hb_work_object_t * w )
136 {
137     hb_work_private_t * pv = w->private_data;
138     if ( pv->parser )
139         {
140                 av_parser_close(pv->parser);
141         }
142     if ( pv->context && pv->context->codec )
143     {
144         avcodec_close( pv->context );
145     }
146     if ( pv->list )
147     {
148         hb_list_close( &pv->list );
149     }
150 }
151
152 /***********************************************************************
153  * Work
154  ***********************************************************************
155  *
156  **********************************************************************/
157 static int decavcodecWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
158                     hb_buffer_t ** buf_out )
159 {
160     hb_work_private_t * pv = w->private_data;
161     hb_buffer_t * in = *buf_in, * buf, * last = NULL;
162     int   pos, len, out_size, i, uncompressed_len;
163     short buffer[AVCODEC_MAX_AUDIO_FRAME_SIZE];
164     uint64_t cur;
165     unsigned char *parser_output_buffer;
166     int parser_output_buffer_len;
167
168     if ( (*buf_in)->size <= 0 )
169     {
170         /* EOF on input stream - send it downstream & say that we're done */
171         *buf_out = *buf_in;
172         *buf_in = NULL;
173         return HB_WORK_DONE;
174     }
175
176     *buf_out = NULL;
177
178     cur = ( in->start < 0 )? pv->pts_next : in->start;
179
180     pos = 0;
181     while( pos < in->size )
182     {
183         len = av_parser_parse( pv->parser, pv->context,
184                                &parser_output_buffer, &parser_output_buffer_len,
185                                in->data + pos, in->size - pos, cur, cur );
186         out_size = 0;
187         uncompressed_len = 0;
188         if (parser_output_buffer_len)
189         {
190             out_size = sizeof(buffer);
191             uncompressed_len = avcodec_decode_audio2( pv->context, buffer,
192                                                       &out_size,
193                                                       parser_output_buffer,
194                                                       parser_output_buffer_len );
195         }
196         if( out_size )
197         {
198             short * s16;
199             float * fl32;
200
201             buf = hb_buffer_init( 2 * out_size );
202
203             int sample_size_in_bytes = 2;   // Default to 2 bytes
204             switch (pv->context->sample_fmt)
205             {
206               case SAMPLE_FMT_S16:
207                 sample_size_in_bytes = 2;
208                 break;
209               /* We should handle other formats here - but that needs additional format conversion work below */
210               /* For now we'll just report the error and try to carry on */
211               default:
212                 hb_log("decavcodecWork - Unknown Sample Format from avcodec_decode_audio (%d) !", pv->context->sample_fmt);
213                 break;
214             }
215
216             buf->start = cur;
217             buf->stop  = cur + 90000 * ( out_size / (sample_size_in_bytes * pv->context->channels) ) /
218                          pv->context->sample_rate;
219             cur = buf->stop;
220
221             s16  = buffer;
222             fl32 = (float *) buf->data;
223             for( i = 0; i < out_size / 2; i++ )
224             {
225                 fl32[i] = s16[i];
226             }
227
228             if( last )
229             {
230                 last = last->next = buf;
231             }
232             else
233             {
234                 *buf_out = last = buf;
235             }
236         }
237
238         pos += len;
239     }
240
241     pv->pts_next = cur;
242
243     return HB_WORK_OK;
244 }
245
246 static int decavcodecInfo( hb_work_object_t *w, hb_work_info_t *info )
247 {
248     hb_work_private_t *pv = w->private_data;
249
250     memset( info, 0, sizeof(*info) );
251
252     if ( pv && pv->context )
253     {
254         AVCodecContext *context = pv->context;
255         info->bitrate = context->bit_rate;
256         info->rate = context->time_base.num;
257         info->rate_base = context->time_base.den;
258         info->profile = context->profile;
259         info->level = context->level;
260         return 1;
261     }
262     return 0;
263 }
264
265 static int decavcodecBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
266                              hb_work_info_t *info )
267 {
268     hb_work_private_t *pv = w->private_data;
269
270     memset( info, 0, sizeof(*info) );
271
272     if ( pv && pv->context )
273     {
274         return decavcodecInfo( w, info );
275     }
276     // XXX
277     // We should parse the bitstream to find its parameters but for right
278     // now we just return dummy values if there's a codec that will handle it.
279     AVCodec *codec = avcodec_find_decoder( w->codec_param? w->codec_param :
280                                                            CODEC_ID_MP2 );
281     if ( codec )
282     {
283         static char codec_name[64];
284
285         info->name =  strncpy( codec_name, codec->name, sizeof(codec_name)-1 );
286         info->bitrate = 384000;
287         info->rate = 48000;
288         info->rate_base = 1;
289         info->channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
290         return 1;
291     }
292     return -1;
293 }
294
295 /* -------------------------------------------------------------
296  * General purpose video decoder using libavcodec
297  */
298
299 static uint8_t *copy_plane( uint8_t *dst, uint8_t* src, int dstride, int sstride,
300                             int h )
301 {
302     if ( dstride == sstride )
303     {
304         memcpy( dst, src, dstride * h );
305         return dst + dstride * h;
306     }
307     int lbytes = dstride <= sstride? dstride : sstride;
308     while ( --h >= 0 )
309     {
310         memcpy( dst, src, lbytes );
311         src += sstride;
312         dst += dstride;
313     }
314     return dst;
315 }
316
317 /* Note: assumes frame format is PIX_FMT_YUV420P */
318 static hb_buffer_t *copy_frame( AVCodecContext *context, AVFrame *frame )
319 {
320     int w = context->width, h = context->height;
321     hb_buffer_t *buf = hb_buffer_init( w * h * 3 / 2 );
322     uint8_t *dst = buf->data;
323
324     dst = copy_plane( dst, frame->data[0], w, frame->linesize[0], h );
325     w >>= 1; h >>= 1;
326     dst = copy_plane( dst, frame->data[1], w, frame->linesize[1], h );
327     dst = copy_plane( dst, frame->data[2], w, frame->linesize[2], h );
328
329     return buf;
330 }
331
332 static int get_frame_buf( AVCodecContext *context, AVFrame *frame )
333 {
334     hb_work_private_t *pv = context->opaque;
335     frame->pts = pv->pts;
336     pv->pts = -1;
337
338     return avcodec_default_get_buffer( context, frame );
339 }
340
341 static void log_chapter( hb_work_private_t *pv, int chap_num, int64_t pts )
342 {
343     hb_chapter_t *c = hb_list_item( pv->job->title->list_chapter, chap_num - 1 );
344     if ( c && c->title )
345     {
346         hb_log( "%s: \"%s\" (%d) at frame %u time %lld",
347                 pv->context->codec->name, c->title, chap_num, pv->nframes, pts );
348     }
349     else
350     {
351         hb_log( "%s: Chapter %d at frame %u time %lld",
352                 pv->context->codec->name, chap_num, pv->nframes, pts );
353     }
354 }
355
356 static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size )
357 {
358     int got_picture;
359     AVFrame frame;
360
361     if ( avcodec_decode_video( pv->context, &frame, &got_picture, data, size ) < 0 )
362     {
363         ++pv->decode_errors;     
364     }
365     if( got_picture )
366     {
367         // ffmpeg makes it hard to attach a pts to a frame. if the MPEG ES
368         // packet had a pts we handed it to av_parser_parse (if the packet had
369         // no pts we set it to -1 but before the parse we can't distinguish between
370         // the start of a video frame with no pts & an intermediate packet of
371         // some frame which never has a pts). we hope that when parse returns
372         // the frame to us the pts we originally handed it will be in parser->pts.
373         // we put this pts into pv->pts so that when a avcodec_decode_video
374         // finally gets around to allocating an AVFrame to hold the decoded
375         // frame we can stuff that pts into the frame. if all of these relays
376         // worked at this point frame.pts should hold the frame's pts from the
377         // original data stream or -1 if it didn't have one. in the latter case
378         // we generate the next pts in sequence for it.
379         double pts = frame.pts;
380         if ( pts < 0 )
381         {
382             pts = pv->pts_next;
383         }
384         if ( pv->duration == 0 )
385         {
386             pv->duration = 90000. * pv->context->time_base.num /
387                            pv->context->time_base.den;
388         }
389         double frame_dur = pv->duration;
390         frame_dur += frame.repeat_pict * frame_dur * 0.5;
391         pv->pts_next = pts + frame_dur;
392
393         hb_buffer_t *buf = copy_frame( pv->context, &frame );
394         buf->start = pts;
395
396         if ( pv->new_chap && buf->start >= pv->chap_time )
397         {
398             buf->new_chap = pv->new_chap;
399             pv->new_chap = 0;
400             pv->chap_time = 0;
401             if ( pv->job )
402             {
403                 log_chapter( pv, buf->new_chap, buf->start );
404             }
405         }
406         else if ( pv->job && pv->nframes == 0 )
407         {
408             log_chapter( pv, pv->job->chapter_start, buf->start );
409         }
410         hb_list_add( pv->list, buf );
411         ++pv->nframes;
412     }
413     return got_picture;
414 }
415
416 static void decodeVideo( hb_work_private_t *pv, uint8_t *data, int size,
417                          int64_t pts, int64_t dts )
418 {
419     /*
420      * The following loop is a do..while because we need to handle both
421      * data & the flush at the end (signaled by size=0). At the end there's
422      * generally a frame in the parser & one or more frames in the decoder
423      * (depending on the bframes setting).
424      */
425     int pos = 0;
426     do {
427         uint8_t *pout;
428         int pout_len;
429         int len = av_parser_parse( pv->parser, pv->context, &pout, &pout_len,
430                                    data + pos, size - pos, pts, dts );
431         pos += len;
432
433         if ( pout_len > 0 )
434         {
435             pv->pts = pv->parser->pts;
436             decodeFrame( pv, pout, pout_len );
437         }
438     } while ( pos < size );
439
440     /* the stuff above flushed the parser, now flush the decoder */
441     while ( size == 0 && decodeFrame( pv, NULL, 0 ) )
442     {
443     }
444 }
445
446 static hb_buffer_t *link_buf_list( hb_work_private_t *pv )
447 {
448     hb_buffer_t *head = hb_list_item( pv->list, 0 );
449
450     if ( head )
451     {
452         hb_list_rem( pv->list, head );
453
454         hb_buffer_t *last = head, *buf;
455
456         while ( ( buf = hb_list_item( pv->list, 0 ) ) != NULL )
457         {
458             hb_list_rem( pv->list, buf );
459             last->next = buf;
460             last = buf;
461         }
462     }
463     return head;
464 }
465
466
467 static int decavcodecvInit( hb_work_object_t * w, hb_job_t * job )
468 {
469
470     hb_work_private_t *pv = calloc( 1, sizeof( hb_work_private_t ) );
471     w->private_data = pv;
472     pv->job   = job;
473     pv->list = hb_list_init();
474
475     int codec_id = w->codec_param;
476     pv->parser = av_parser_init( codec_id );
477     pv->context = avcodec_alloc_context2( CODEC_TYPE_VIDEO );
478
479     /* we have to wrap ffmpeg's get_buffer to be able to set the pts (?!) */
480     pv->context->opaque = pv;
481     pv->context->get_buffer = get_frame_buf;
482
483     AVCodec *codec = avcodec_find_decoder( codec_id );
484
485     // we can't call the avstream funcs but the read_header func in the
486     // AVInputFormat may set up some state in the AVContext. In particular 
487     // vc1t_read_header allocates 'extradata' to deal with header issues
488     // related to Microsoft's bizarre engineering notions. We alloc a chunk
489     // of space to make vc1 work then associate the codec with the context.
490     pv->context->extradata_size = 32;
491     pv->context->extradata = av_malloc(pv->context->extradata_size);
492     avcodec_open( pv->context, codec );
493
494     return 0;
495 }
496
497 static int decavcodecvWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
498                             hb_buffer_t ** buf_out )
499 {
500     hb_work_private_t *pv = w->private_data;
501     hb_buffer_t *in = *buf_in;
502     int64_t pts = -1;
503     int64_t dts = pts;
504
505     *buf_in = NULL;
506
507     /* if we got an empty buffer signaling end-of-stream send it downstream */
508     if ( in->size == 0 )
509     {
510         decodeVideo( pv, in->data, in->size, pts, dts );
511         hb_list_add( pv->list, in );
512         *buf_out = link_buf_list( pv );
513         hb_log( "%s done: %u frames, %u decoder errors",
514                 pv->context->codec->name, pv->nframes, pv->decode_errors );
515         return HB_WORK_DONE;
516     }
517
518     if( in->start >= 0 )
519     {
520         pts = in->start;
521         dts = in->renderOffset;
522     }
523     if ( in->new_chap )
524     {
525         pv->new_chap = in->new_chap;
526         pv->chap_time = pts >= 0? pts : pv->pts_next;
527     }
528     decodeVideo( pv, in->data, in->size, pts, dts );
529     hb_buffer_close( &in );
530     *buf_out = link_buf_list( pv );
531     return HB_WORK_OK;
532 }
533
534 static int decavcodecvInfo( hb_work_object_t *w, hb_work_info_t *info )
535 {
536     hb_work_private_t *pv = w->private_data;
537
538     memset( info, 0, sizeof(*info) );
539
540     if ( pv && pv->context )
541     {
542         AVCodecContext *context = pv->context;
543         info->bitrate = context->bit_rate;
544         info->width = context->width;
545         info->height = context->height;
546
547         /* ffmpeg gives the frame rate in frames per second while HB wants
548          * it in units of the 27MHz MPEG clock. */
549         info->rate = 27000000;
550         info->rate_base = (int64_t)context->time_base.num * 27000000LL /
551                           context->time_base.den;
552         
553         /* Sometimes there's no pixel aspect set in the source. In that case,
554            assume a 1:1 PAR. Otherwise, preserve the source PAR.             */
555         info->pixel_aspect_width = context->sample_aspect_ratio.num ?
556                                         context->sample_aspect_ratio.num : 1;
557         info->pixel_aspect_height = context->sample_aspect_ratio.den ?
558                                         context->sample_aspect_ratio.den : 1;
559
560         /* ffmpeg returns the Pixel Aspect Ratio (PAR). Handbrake wants the
561          * Display Aspect Ratio so we convert by scaling by the Storage
562          * Aspect Ratio (w/h). We do the calc in floating point to get the
563          * rounding right. */
564         info->aspect = (double)info->pixel_aspect_width * 
565                        (double)context->width /
566                        (double)info->pixel_aspect_height /
567                        (double)context->height;
568
569         info->profile = context->profile;
570         info->level = context->level;
571         info->name = context->codec->name;
572         return 1;
573     }
574     return 0;
575 }
576
577 static int decavcodecvBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
578                              hb_work_info_t *info )
579 {
580     return 0;
581 }
582
583 hb_work_object_t hb_decavcodecv =
584 {
585     WORK_DECAVCODECV,
586     "Video decoder (libavcodec)",
587     decavcodecvInit,
588     decavcodecvWork,
589     decavcodecClose,
590     decavcodecvInfo,
591     decavcodecvBSInfo
592 };
593
594
595 // This is a special decoder for ffmpeg streams. The ffmpeg stream reader
596 // includes a parser and passes information from the parser to the decoder
597 // via a codec context kept in the AVStream of the reader's AVFormatContext.
598 // We *have* to use that codec context to decode the stream or we'll get
599 // garbage. ffmpeg_title_scan put a cookie that can be used to get to that
600 // codec context in our codec_param.
601
602 // this routine gets the appropriate context pointer from the ffmpeg
603 // stream reader. it can't be called until we get the first buffer because
604 // we can't guarantee that reader will be called before the our init
605 // routine and if our init is called first we'll get a pointer to the
606 // old scan stream (which has already been closed).
607 static void init_ffmpeg_context( hb_work_object_t *w )
608 {
609     hb_work_private_t *pv = w->private_data;
610     pv->context = hb_ffmpeg_context( w->codec_param );
611
612     // during scan the decoder gets closed & reopened which will
613     // close the codec so reopen it if it's not there
614     if ( ! pv->context->codec )
615     {
616         AVCodec *codec = avcodec_find_decoder( pv->context->codec_id );
617         avcodec_open( pv->context, codec );
618     }
619     // set up our best guess at the frame duration.
620     // the frame rate in the codec is usually bogus but it's sometimes
621     // ok in the stream.
622     AVStream *st = hb_ffmpeg_avstream( w->codec_param );
623     AVRational tb;
624     // XXX because the time bases are so screwed up, we only take values
625     // in the range 8fps - 64fps.
626     if ( st->time_base.num * 64 > st->time_base.den &&
627          st->time_base.den > st->time_base.num * 8 )
628     {
629         tb = st->time_base;
630     }
631     else if ( st->codec->time_base.num * 64 > st->codec->time_base.den &&
632               st->codec->time_base.den > st->codec->time_base.num * 8 )
633     {
634         tb = st->codec->time_base;
635     }
636     else if ( st->r_frame_rate.den * 64 > st->r_frame_rate.num &&
637               st->r_frame_rate.num > st->r_frame_rate.den * 8 )
638     {
639         tb.num = st->r_frame_rate.den;
640         tb.den = st->r_frame_rate.num;
641     }
642     else
643     {
644         tb.num = 1001;  /*XXX*/
645         tb.den = 30000; /*XXX*/
646     }
647     pv->duration = 90000. * tb.num / tb.den;
648
649     // we have to wrap ffmpeg's get_buffer to be able to set the pts (?!)
650     pv->context->opaque = pv;
651     pv->context->get_buffer = get_frame_buf;
652 }
653
654 static void prepare_ffmpeg_buffer( hb_buffer_t * in )
655 {
656     // ffmpeg requires an extra 8 bytes of zero at the end of the buffer and
657     // will seg fault in odd, data dependent ways if it's not there. (my guess
658     // is this is a case of a local performance optimization creating a global
659     // performance degradation since all the time wasted by extraneous data
660     // copies & memory zeroing has to be huge compared to the minor reduction
661     // in inner-loop instructions this affords - modern cpus bottleneck on
662     // memory bandwidth not instruction bandwidth).
663     if ( in->size + FF_INPUT_BUFFER_PADDING_SIZE > in->alloc )
664     {
665         // have to realloc to add the padding
666         hb_buffer_realloc( in, in->size + FF_INPUT_BUFFER_PADDING_SIZE );
667     }
668     memset( in->data + in->size, 0, FF_INPUT_BUFFER_PADDING_SIZE );
669 }
670
671 static int decavcodecviInit( hb_work_object_t * w, hb_job_t * job )
672 {
673
674     hb_work_private_t *pv = calloc( 1, sizeof( hb_work_private_t ) );
675     w->private_data = pv;
676     pv->job   = job;
677     pv->list = hb_list_init();
678
679     return 0;
680 }
681
682 static int decavcodecviWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
683                              hb_buffer_t ** buf_out )
684 {
685     hb_work_private_t *pv = w->private_data;
686     if ( ! pv->context )
687     {
688         init_ffmpeg_context( w );
689
690         switch ( pv->context->codec_id )
691         {
692             // These are the only formats whose timestamps we'll believe.
693             // All others are treated as CFR (i.e., we take the first timestamp
694             // then generate all the others from the frame rate). The reason for
695             // this is that the M$ encoders are so frigging buggy with garbage
696             // like packed b-frames (vfw divx mpeg4) that believing their timestamps
697             // results in discarding more than half the video frames because they'll
698             // be out of sequence (and attempting to reseqence them doesn't work
699             // because it's the timestamps that are wrong, not the decoded frame
700             // order). All hail Redmond, ancestral home of the rich & stupid.
701             case CODEC_ID_MPEG2VIDEO:
702             case CODEC_ID_RAWVIDEO:
703             case CODEC_ID_H264:
704             case CODEC_ID_VC1:
705                 break;
706
707             default:
708                 pv->ignore_pts = 1;
709                 break;
710         }
711     }
712     hb_buffer_t *in = *buf_in;
713     int64_t pts = -1;
714
715     *buf_in = NULL;
716
717     /* if we got an empty buffer signaling end-of-stream send it downstream */
718     if ( in->size == 0 )
719     {
720         /* flush any frames left in the decoder */
721         while ( decodeFrame( pv, NULL, 0 ) )
722         {
723         }
724         hb_list_add( pv->list, in );
725         *buf_out = link_buf_list( pv );
726         hb_log( "%s done: %u frames, %u decoder errors, %u drops",
727                 pv->context->codec->name, pv->nframes, pv->decode_errors,
728                 pv->ndrops );
729         return HB_WORK_DONE;
730     }
731
732     if( in->start >= 0 )
733     {
734         // use the first timestamp as our 'next expected' pts
735         if ( pv->pts_next <= 0 )
736         {
737             pv->pts_next = in->start;
738         }
739
740         if ( ! pv->ignore_pts )
741         {
742             pts = in->start;
743             if ( pv->pts > 0 )
744             {
745                 hb_log( "overwriting pts %lld with %lld (diff %d)",
746                         pv->pts, pts, pts - pv->pts );
747             }
748             if ( pv->pts_next - pts >= pv->duration )
749             {
750                 // this frame starts more than a frame time before where
751                 // the nominal frame rate says it should - drop it.
752                 // log the first 10 drops so we'll know what's going on.
753                 if ( pv->ndrops++ < 10 )
754                 {
755                     hb_log( "time reversal next %.0f pts %lld (diff %g)",
756                             pv->pts_next, pts, pv->pts_next - pts );
757                 }
758                 hb_buffer_close( &in );
759                 return HB_WORK_OK;
760             }
761             pv->pts = pts;
762         }
763     }
764
765     if ( in->new_chap )
766     {
767         pv->new_chap = in->new_chap;
768         pv->chap_time = pts >= 0? pts : pv->pts_next;
769     }
770     prepare_ffmpeg_buffer( in );
771     decodeFrame( pv, in->data, in->size );
772     hb_buffer_close( &in );
773     *buf_out = link_buf_list( pv );
774     return HB_WORK_OK;
775 }
776
777 static int decavcodecviInfo( hb_work_object_t *w, hb_work_info_t *info )
778 {
779     if ( decavcodecvInfo( w, info ) )
780     {
781         // There are at least three different video frame rates in ffmpeg:
782         //  - time_base in the AVStream
783         //  - time_base in the AVCodecContext
784         //  - r_frame_rate in the AVStream
785         // There's no guidence on which if any of these to believe but the
786         // routine compute_frame_duration tries the stream first then the codec.
787         // In general the codec time base seems bogus & the stream time base is
788         // ok except for wmv's where the stream time base is also bogus but
789         // r_frame_rate is sometimes ok & sometimes a random number.
790         AVStream *st = hb_ffmpeg_avstream( w->codec_param );
791         AVRational tb;
792         // XXX because the time bases are so screwed up, we only take values
793         // in the range 8fps - 64fps.
794         if ( st->time_base.num * 64 > st->time_base.den &&
795              st->time_base.den > st->time_base.num * 8 )
796         {
797             tb = st->time_base;
798         }
799         else if ( st->codec->time_base.num * 64 > st->codec->time_base.den &&
800                   st->codec->time_base.den > st->codec->time_base.num * 8 )
801         {
802             tb = st->codec->time_base;
803         }
804         else if ( st->r_frame_rate.den * 64 > st->r_frame_rate.num &&
805                   st->r_frame_rate.num > st->r_frame_rate.den * 8 )
806         {
807             tb.num = st->r_frame_rate.den;
808             tb.den = st->r_frame_rate.num;
809         }
810         else
811         {
812             tb.num = 1001;  /*XXX*/
813             tb.den = 30000; /*XXX*/
814         }
815
816         // ffmpeg gives the frame rate in frames per second while HB wants
817         // it in units of the 27MHz MPEG clock. */
818         info->rate = 27000000;
819         info->rate_base = (int64_t)tb.num * 27000000LL / tb.den;
820         return 1;
821     }
822     return 0;
823 }
824
825 static void decodeAudio( hb_work_private_t *pv, uint8_t *data, int size )
826 {
827     AVCodecContext *context = pv->context;
828     int pos = 0;
829
830     while ( pos < size )
831     {
832         int16_t buffer[AVCODEC_MAX_AUDIO_FRAME_SIZE];
833         int out_size = sizeof(buffer);
834         int len = avcodec_decode_audio2( context, buffer, &out_size,
835                                          data + pos, size - pos );
836         if ( len <= 0 )
837         {
838             return;
839         }
840         pos += len;
841         if( out_size > 0 )
842         {
843             hb_buffer_t *buf = hb_buffer_init( 2 * out_size );
844
845             double pts = pv->pts_next;
846             buf->start = pts;
847             out_size >>= 1;
848             pts += out_size * pv->duration;
849             buf->stop  = pts;
850             pv->pts_next = pts;
851
852             float *fl32 = (float *)buf->data;
853             int i;
854             for( i = 0; i < out_size; ++i )
855             {
856                 fl32[i] = buffer[i];
857             }
858             hb_list_add( pv->list, buf );
859         }
860     }
861 }
862
863 static int decavcodecaiWork( hb_work_object_t *w, hb_buffer_t **buf_in,
864                     hb_buffer_t **buf_out )
865 {
866     if ( (*buf_in)->size <= 0 )
867     {
868         /* EOF on input stream - send it downstream & say that we're done */
869         *buf_out = *buf_in;
870         *buf_in = NULL;
871         return HB_WORK_DONE;
872     }
873
874     hb_work_private_t *pv = w->private_data;
875     if ( ! pv->context )
876     {
877         init_ffmpeg_context( w );
878         pv->duration = 90000. /
879                     (double)( pv->context->sample_rate * pv->context->channels );
880     }
881     hb_buffer_t *in = *buf_in;
882
883     if ( in->start >= 0 &&
884          ( pv->pts_next < 0 || ( in->start - pv->pts_next ) > 90*100 ) )
885     {
886         pv->pts_next = in->start;
887     }
888     prepare_ffmpeg_buffer( in );
889     decodeAudio( pv, in->data, in->size );
890     *buf_out = link_buf_list( pv );
891
892     return HB_WORK_OK;
893 }
894
895 hb_work_object_t hb_decavcodecvi =
896 {
897     WORK_DECAVCODECVI,
898     "Video decoder (ffmpeg streams)",
899     decavcodecviInit,
900     decavcodecviWork,
901     decavcodecClose,
902     decavcodecviInfo,
903     decavcodecvBSInfo
904 };
905
906 hb_work_object_t hb_decavcodecai =
907 {
908     WORK_DECAVCODECAI,
909     "Audio decoder (ffmpeg streams)",
910     decavcodecviInit,
911     decavcodecaiWork,
912     decavcodecClose,
913     decavcodecInfo,
914     decavcodecBSInfo
915 };