OSDN Git Service

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