1 /* $Id: decavcodec.c,v 1.6 2005/03/06 04:08:54 titer Exp $
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. */
7 /* This module is Handbrake's interface to the ffmpeg decoder library
8 (libavcodec & small parts of libavformat). It contains four Handbrake
11 decavcodec connects HB to an ffmpeg audio decoder
12 decavcodecv connects HB to an ffmpeg video decoder
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.).
21 decavcodecai "internal" (incestuous?) version of decavcodec
22 decavcodecvi "internal" (incestuous?) version of decavcodecv
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
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.
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.
64 #include "libavcodec/avcodec.h"
65 #include "libavformat/avformat.h"
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 * );
73 hb_work_object_t hb_decavcodec =
76 "MPGA decoder (libavcodec)",
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];
93 struct hb_work_private_s
96 AVCodecContext *context;
97 AVCodecParserContext *parser;
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
106 uint32_t decode_errors;
107 int brokenByMicrosoft; // video stream may contain packed b-frames
108 hb_buffer_t* delayq[HEAP_SIZE];
113 static int64_t heap_pop( pts_heap_t *heap )
117 if ( heap->nheap <= 0 )
122 // return the top of the heap then put the bottom element on top,
123 // decrease the heap size by one & rebalence the heap.
126 int64_t v = heap->h[heap->nheap--];
128 int child = parent << 1;
129 while ( child <= heap->nheap )
131 // find the smallest of the two children of parent
132 if (child < heap->nheap && heap->h[child] > heap->h[child+1] )
135 if (v <= heap->h[child])
136 // new item is smaller than either child so it's the new parent.
139 // smallest child is smaller than new item so move it up then
140 // check its children.
141 int64_t hp = heap->h[child];
142 heap->h[parent] = hp;
150 static void heap_push( pts_heap_t *heap, int64_t v )
152 if ( heap->nheap < HEAP_SIZE )
157 // stick the new value on the bottom of the heap then bubble it
158 // up to its correct spot.
159 int child = heap->nheap;
161 int parent = child >> 1;
162 if (heap->h[parent] <= v)
165 int64_t hp = heap->h[parent];
173 /***********************************************************************
174 * hb_work_decavcodec_init
175 ***********************************************************************
177 **********************************************************************/
178 static int decavcodecInit( hb_work_object_t * w, hb_job_t * job )
182 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
183 w->private_data = pv;
187 int codec_id = w->codec_param;
190 codec_id = CODEC_ID_MP2;
191 codec = avcodec_find_decoder( codec_id );
192 pv->parser = av_parser_init( codec_id );
194 pv->context = avcodec_alloc_context();
195 avcodec_open( pv->context, codec );
200 /***********************************************************************
202 ***********************************************************************
204 **********************************************************************/
205 static void decavcodecClose( hb_work_object_t * w )
207 hb_work_private_t * pv = w->private_data;
211 if ( pv->job && pv->context && pv->context->codec )
213 hb_log( "%s-decoder done: %u frames, %u decoder errors, %u drops",
214 pv->context->codec->name, pv->nframes, pv->decode_errors,
219 av_parser_close(pv->parser);
221 if ( pv->context && pv->context->codec )
223 avcodec_close( pv->context );
227 hb_list_close( &pv->list );
235 w->private_data = NULL;
239 /***********************************************************************
241 ***********************************************************************
243 **********************************************************************/
244 static int decavcodecWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
245 hb_buffer_t ** buf_out )
247 hb_work_private_t * pv = w->private_data;
248 hb_buffer_t * in = *buf_in, * buf, * last = NULL;
249 int pos, len, out_size, i, uncompressed_len;
250 short buffer[AVCODEC_MAX_AUDIO_FRAME_SIZE];
252 unsigned char *parser_output_buffer;
253 int parser_output_buffer_len;
255 if ( (*buf_in)->size <= 0 )
257 /* EOF on input stream - send it downstream & say that we're done */
265 cur = ( in->start < 0 )? pv->pts_next : in->start;
268 while( pos < in->size )
270 len = av_parser_parse( pv->parser, pv->context,
271 &parser_output_buffer, &parser_output_buffer_len,
272 in->data + pos, in->size - pos, cur, cur );
274 uncompressed_len = 0;
275 if (parser_output_buffer_len)
277 out_size = sizeof(buffer);
278 uncompressed_len = avcodec_decode_audio2( pv->context, buffer,
280 parser_output_buffer,
281 parser_output_buffer_len );
288 buf = hb_buffer_init( 2 * out_size );
290 int sample_size_in_bytes = 2; // Default to 2 bytes
291 switch (pv->context->sample_fmt)
294 sample_size_in_bytes = 2;
296 /* We should handle other formats here - but that needs additional format conversion work below */
297 /* For now we'll just report the error and try to carry on */
299 hb_log("decavcodecWork - Unknown Sample Format from avcodec_decode_audio (%d) !", pv->context->sample_fmt);
304 buf->stop = cur + 90000 * ( out_size / (sample_size_in_bytes * pv->context->channels) ) /
305 pv->context->sample_rate;
309 fl32 = (float *) buf->data;
310 for( i = 0; i < out_size / 2; i++ )
317 last = last->next = buf;
321 *buf_out = last = buf;
333 static int decavcodecInfo( hb_work_object_t *w, hb_work_info_t *info )
335 hb_work_private_t *pv = w->private_data;
337 memset( info, 0, sizeof(*info) );
339 if ( pv && pv->context )
341 AVCodecContext *context = pv->context;
342 info->bitrate = context->bit_rate;
343 info->rate = context->time_base.num;
344 info->rate_base = context->time_base.den;
345 info->profile = context->profile;
346 info->level = context->level;
352 static int decavcodecBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
353 hb_work_info_t *info )
355 hb_work_private_t *pv = w->private_data;
357 memset( info, 0, sizeof(*info) );
359 if ( pv && pv->context )
361 return decavcodecInfo( w, info );
364 // We should parse the bitstream to find its parameters but for right
365 // now we just return dummy values if there's a codec that will handle it.
366 AVCodec *codec = avcodec_find_decoder( w->codec_param? w->codec_param :
370 static char codec_name[64];
372 info->name = strncpy( codec_name, codec->name, sizeof(codec_name)-1 );
373 info->bitrate = 384000;
376 info->channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
382 /* -------------------------------------------------------------
383 * General purpose video decoder using libavcodec
386 static uint8_t *copy_plane( uint8_t *dst, uint8_t* src, int dstride, int sstride,
389 if ( dstride == sstride )
391 memcpy( dst, src, dstride * h );
392 return dst + dstride * h;
394 int lbytes = dstride <= sstride? dstride : sstride;
397 memcpy( dst, src, lbytes );
404 /* Note: assumes frame format is PIX_FMT_YUV420P */
405 static hb_buffer_t *copy_frame( AVCodecContext *context, AVFrame *frame )
407 int w = context->width, h = context->height;
408 hb_buffer_t *buf = hb_buffer_init( w * h * 3 / 2 );
409 uint8_t *dst = buf->data;
411 dst = copy_plane( dst, frame->data[0], w, frame->linesize[0], h );
413 dst = copy_plane( dst, frame->data[1], w, frame->linesize[1], h );
414 dst = copy_plane( dst, frame->data[2], w, frame->linesize[2], h );
419 static int get_frame_buf( AVCodecContext *context, AVFrame *frame )
421 hb_work_private_t *pv = context->opaque;
422 frame->pts = pv->pts;
424 return avcodec_default_get_buffer( context, frame );
427 static void log_chapter( hb_work_private_t *pv, int chap_num, int64_t pts )
429 hb_chapter_t *c = hb_list_item( pv->job->title->list_chapter, chap_num - 1 );
432 hb_log( "%s: \"%s\" (%d) at frame %u time %lld",
433 pv->context->codec->name, c->title, chap_num, pv->nframes, pts );
437 hb_log( "%s: Chapter %d at frame %u time %lld",
438 pv->context->codec->name, chap_num, pv->nframes, pts );
442 static void flushDelayQueue( hb_work_private_t *pv )
445 int slot = pv->nframes & (HEAP_SIZE-1);
447 // flush all the video packets left on our timestamp-reordering delay q
448 while ( ( buf = pv->delayq[slot] ) != NULL )
450 buf->start = heap_pop( &pv->pts_heap );
451 hb_list_add( pv->list, buf );
452 pv->delayq[slot] = NULL;
453 slot = ( slot + 1 ) & (HEAP_SIZE-1);
457 static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size )
462 if ( avcodec_decode_video( pv->context, &frame, &got_picture, data, size ) < 0 )
468 // ffmpeg makes it hard to attach a pts to a frame. if the MPEG ES
469 // packet had a pts we handed it to av_parser_parse (if the packet had
470 // no pts we set it to -1 but before the parse we can't distinguish between
471 // the start of a video frame with no pts & an intermediate packet of
472 // some frame which never has a pts). we hope that when parse returns
473 // the frame to us the pts we originally handed it will be in parser->pts.
474 // we put this pts into pv->pts so that when a avcodec_decode_video
475 // finally gets around to allocating an AVFrame to hold the decoded
476 // frame we can stuff that pts into the frame. if all of these relays
477 // worked at this point frame.pts should hold the frame's pts from the
478 // original data stream or -1 if it didn't have one. in the latter case
479 // we generate the next pts in sequence for it.
480 double frame_dur = pv->duration;
481 if ( frame_dur <= 0 )
483 frame_dur = 90000. * (double)pv->context->time_base.num /
484 (double)pv->context->time_base.den;
485 pv->duration = frame_dur;
487 if ( frame.repeat_pict )
489 frame_dur += frame.repeat_pict * frame_dur * 0.5;
491 // If there was no pts for this frame, assume constant frame rate
492 // video & estimate the next frame time from the last & duration.
493 double pts = frame.pts;
498 pv->pts_next = pts + frame_dur;
502 // if we're doing a scan or this content couldn't have been broken
503 // by Microsoft we don't worry about timestamp reordering
504 if ( ! pv->job || ! pv->brokenByMicrosoft )
506 buf = copy_frame( pv->context, &frame );
508 hb_list_add( pv->list, buf );
513 // XXX This following probably addresses a libavcodec bug but I don't
514 // see an easy fix so we workaround it here.
516 // The M$ 'packed B-frames' atrocity results in decoded frames with
517 // the wrong timestamp. E.g., if there are 2 b-frames the timestamps
518 // we see here will be "2 3 1 5 6 4 ..." instead of "1 2 3 4 5 6".
519 // The frames are actually delivered in the right order but with
520 // the wrong timestamp. To get the correct timestamp attached to
521 // each frame we have a delay queue (longer than the max number of
522 // b-frames) & a sorting heap for the timestamps. As each frame
523 // comes out of the decoder the oldest frame in the queue is removed
524 // and associated with the smallest timestamp. Then the new frame is
525 // added to the queue & its timestamp is pushed on the heap.
526 // This does nothing if the timestamps are correct (i.e., the video
527 // uses a codec that Micro$oft hasn't broken yet) but the frames
528 // get timestamped correctly even when M$ has munged them.
530 // remove the oldest picture from the frame queue (if any) &
531 // give it the smallest timestamp from our heap. The queue size
532 // is a power of two so we get the slot of the oldest by masking
533 // the frame count & this will become the slot of the newest
534 // once we've removed & processed the oldest.
535 int slot = pv->nframes & (HEAP_SIZE-1);
536 if ( ( buf = pv->delayq[slot] ) != NULL )
538 buf->start = heap_pop( &pv->pts_heap );
540 if ( pv->new_chap && buf->start >= pv->chap_time )
542 buf->new_chap = pv->new_chap;
545 log_chapter( pv, buf->new_chap, buf->start );
547 else if ( pv->nframes == 0 )
549 log_chapter( pv, pv->job->chapter_start, buf->start );
551 hb_list_add( pv->list, buf );
554 // add the new frame to the delayq & push its timestamp on the heap
555 pv->delayq[slot] = copy_frame( pv->context, &frame );
556 heap_push( &pv->pts_heap, pts );
564 static void decodeVideo( hb_work_private_t *pv, uint8_t *data, int size,
565 int64_t pts, int64_t dts )
568 * The following loop is a do..while because we need to handle both
569 * data & the flush at the end (signaled by size=0). At the end there's
570 * generally a frame in the parser & one or more frames in the decoder
571 * (depending on the bframes setting).
577 int len = av_parser_parse( pv->parser, pv->context, &pout, &pout_len,
578 data + pos, size - pos, pts, dts );
583 pv->pts = pv->parser->pts;
584 decodeFrame( pv, pout, pout_len );
586 } while ( pos < size );
588 /* the stuff above flushed the parser, now flush the decoder */
591 while ( decodeFrame( pv, NULL, 0 ) )
594 flushDelayQueue( pv );
598 static hb_buffer_t *link_buf_list( hb_work_private_t *pv )
600 hb_buffer_t *head = hb_list_item( pv->list, 0 );
604 hb_list_rem( pv->list, head );
606 hb_buffer_t *last = head, *buf;
608 while ( ( buf = hb_list_item( pv->list, 0 ) ) != NULL )
610 hb_list_rem( pv->list, buf );
619 static int decavcodecvInit( hb_work_object_t * w, hb_job_t * job )
622 hb_work_private_t *pv = calloc( 1, sizeof( hb_work_private_t ) );
623 w->private_data = pv;
625 pv->list = hb_list_init();
627 int codec_id = w->codec_param;
628 pv->parser = av_parser_init( codec_id );
629 pv->context = avcodec_alloc_context2( CODEC_TYPE_VIDEO );
631 /* we have to wrap ffmpeg's get_buffer to be able to set the pts (?!) */
632 pv->context->opaque = pv;
633 pv->context->get_buffer = get_frame_buf;
635 AVCodec *codec = avcodec_find_decoder( codec_id );
637 // we can't call the avstream funcs but the read_header func in the
638 // AVInputFormat may set up some state in the AVContext. In particular
639 // vc1t_read_header allocates 'extradata' to deal with header issues
640 // related to Microsoft's bizarre engineering notions. We alloc a chunk
641 // of space to make vc1 work then associate the codec with the context.
642 pv->context->extradata_size = 32;
643 pv->context->extradata = av_malloc(pv->context->extradata_size);
644 avcodec_open( pv->context, codec );
649 static int decavcodecvWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
650 hb_buffer_t ** buf_out )
652 hb_work_private_t *pv = w->private_data;
653 hb_buffer_t *in = *buf_in;
654 int64_t pts = AV_NOPTS_VALUE;
659 /* if we got an empty buffer signaling end-of-stream send it downstream */
662 decodeVideo( pv, in->data, in->size, pts, dts );
663 hb_list_add( pv->list, in );
664 *buf_out = link_buf_list( pv );
671 dts = in->renderOffset;
675 pv->new_chap = in->new_chap;
676 pv->chap_time = pts >= 0? pts : pv->pts_next;
678 decodeVideo( pv, in->data, in->size, pts, dts );
679 hb_buffer_close( &in );
680 *buf_out = link_buf_list( pv );
684 static int decavcodecvInfo( hb_work_object_t *w, hb_work_info_t *info )
686 hb_work_private_t *pv = w->private_data;
688 memset( info, 0, sizeof(*info) );
690 if ( pv && pv->context )
692 AVCodecContext *context = pv->context;
693 info->bitrate = context->bit_rate;
694 info->width = context->width;
695 info->height = context->height;
697 /* ffmpeg gives the frame rate in frames per second while HB wants
698 * it in units of the 27MHz MPEG clock. */
699 info->rate = 27000000;
700 info->rate_base = (int64_t)context->time_base.num * 27000000LL /
701 context->time_base.den;
703 /* Sometimes there's no pixel aspect set in the source. In that case,
704 assume a 1:1 PAR. Otherwise, preserve the source PAR. */
705 info->pixel_aspect_width = context->sample_aspect_ratio.num ?
706 context->sample_aspect_ratio.num : 1;
707 info->pixel_aspect_height = context->sample_aspect_ratio.den ?
708 context->sample_aspect_ratio.den : 1;
710 /* ffmpeg returns the Pixel Aspect Ratio (PAR). Handbrake wants the
711 * Display Aspect Ratio so we convert by scaling by the Storage
712 * Aspect Ratio (w/h). We do the calc in floating point to get the
714 info->aspect = (double)info->pixel_aspect_width *
715 (double)context->width /
716 (double)info->pixel_aspect_height /
717 (double)context->height;
719 info->profile = context->profile;
720 info->level = context->level;
721 info->name = context->codec->name;
727 static int decavcodecvBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
728 hb_work_info_t *info )
733 hb_work_object_t hb_decavcodecv =
736 "Video decoder (libavcodec)",
745 // This is a special decoder for ffmpeg streams. The ffmpeg stream reader
746 // includes a parser and passes information from the parser to the decoder
747 // via a codec context kept in the AVStream of the reader's AVFormatContext.
748 // We *have* to use that codec context to decode the stream or we'll get
749 // garbage. ffmpeg_title_scan put a cookie that can be used to get to that
750 // codec context in our codec_param.
752 // this routine gets the appropriate context pointer from the ffmpeg
753 // stream reader. it can't be called until we get the first buffer because
754 // we can't guarantee that reader will be called before the our init
755 // routine and if our init is called first we'll get a pointer to the
756 // old scan stream (which has already been closed).
757 static void init_ffmpeg_context( hb_work_object_t *w )
759 hb_work_private_t *pv = w->private_data;
760 pv->context = hb_ffmpeg_context( w->codec_param );
762 // during scan the decoder gets closed & reopened which will
763 // close the codec so reopen it if it's not there
764 if ( ! pv->context->codec )
766 AVCodec *codec = avcodec_find_decoder( pv->context->codec_id );
767 avcodec_open( pv->context, codec );
769 // set up our best guess at the frame duration.
770 // the frame rate in the codec is usually bogus but it's sometimes
772 AVStream *st = hb_ffmpeg_avstream( w->codec_param );
774 if ( st->nb_frames && st->duration )
776 // compute the average frame duration from the total number
777 // of frames & the total duration.
778 pv->duration = ( (double)st->duration * (double)st->time_base.num ) /
779 ( (double)st->nb_frames * (double)st->time_base.den );
783 // XXX We don't have a frame count or duration so try to use the
784 // far less reliable time base info in the stream.
785 // Because the time bases are so screwed up, we only take values
786 // in the range 8fps - 64fps.
788 if ( st->time_base.num * 64 > st->time_base.den &&
789 st->time_base.den > st->time_base.num * 8 )
793 else if ( st->r_frame_rate.den * 64 > st->r_frame_rate.num &&
794 st->r_frame_rate.num > st->r_frame_rate.den * 8 )
796 tb.num = st->r_frame_rate.den;
797 tb.den = st->r_frame_rate.num;
801 tb.num = 1001; /*XXX*/
802 tb.den = 24000; /*XXX*/
804 pv->duration = (double)tb.num / (double)tb.den;
806 pv->duration *= 90000.;
808 // we have to wrap ffmpeg's get_buffer to be able to set the pts (?!)
809 pv->context->opaque = pv;
810 pv->context->get_buffer = get_frame_buf;
812 // avi, mkv and possibly mp4 containers can contain the M$ VFW packed
813 // b-frames abortion that messes up frame ordering and timestamps.
814 // XXX ffmpeg knows which streams are broken but doesn't expose the
815 // info externally. We should patch ffmpeg to add a flag to the
816 // codec context for this but until then we mark all ffmpeg streams
818 pv->brokenByMicrosoft = 1;
821 static void prepare_ffmpeg_buffer( hb_buffer_t * in )
823 // ffmpeg requires an extra 8 bytes of zero at the end of the buffer and
824 // will seg fault in odd, data dependent ways if it's not there. (my guess
825 // is this is a case of a local performance optimization creating a global
826 // performance degradation since all the time wasted by extraneous data
827 // copies & memory zeroing has to be huge compared to the minor reduction
828 // in inner-loop instructions this affords - modern cpus bottleneck on
829 // memory bandwidth not instruction bandwidth).
830 if ( in->size + FF_INPUT_BUFFER_PADDING_SIZE > in->alloc )
832 // have to realloc to add the padding
833 hb_buffer_realloc( in, in->size + FF_INPUT_BUFFER_PADDING_SIZE );
835 memset( in->data + in->size, 0, FF_INPUT_BUFFER_PADDING_SIZE );
838 static int decavcodecviInit( hb_work_object_t * w, hb_job_t * job )
841 hb_work_private_t *pv = calloc( 1, sizeof( hb_work_private_t ) );
842 w->private_data = pv;
844 pv->list = hb_list_init();
850 static int decavcodecviWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
851 hb_buffer_t ** buf_out )
853 hb_work_private_t *pv = w->private_data;
856 init_ffmpeg_context( w );
858 hb_buffer_t *in = *buf_in;
861 /* if we got an empty buffer signaling end-of-stream send it downstream */
864 /* flush any frames left in the decoder */
865 while ( decodeFrame( pv, NULL, 0 ) )
868 flushDelayQueue( pv );
869 hb_list_add( pv->list, in );
870 *buf_out = link_buf_list( pv );
874 int64_t pts = in->start;
877 // use the first timestamp as our 'next expected' pts
878 if ( pv->pts_next < 0 )
887 pv->new_chap = in->new_chap;
888 pv->chap_time = pts >= 0? pts : pv->pts_next;
890 prepare_ffmpeg_buffer( in );
891 decodeFrame( pv, in->data, in->size );
892 hb_buffer_close( &in );
893 *buf_out = link_buf_list( pv );
897 static int decavcodecviInfo( hb_work_object_t *w, hb_work_info_t *info )
899 if ( decavcodecvInfo( w, info ) )
901 hb_work_private_t *pv = w->private_data;
904 init_ffmpeg_context( w );
906 // we have the frame duration in units of the 90KHz pts clock but
907 // need it in units of the 27MHz MPEG clock. */
908 info->rate = 27000000;
909 info->rate_base = pv->duration * 300.;
915 static void decodeAudio( hb_work_private_t *pv, uint8_t *data, int size )
917 AVCodecContext *context = pv->context;
922 int16_t *buffer = pv->buffer;
923 if ( buffer == NULL )
925 // XXX ffmpeg bug workaround
926 // malloc a buffer for the audio decode. On an x86, ffmpeg
927 // uses mmx/sse instructions on this buffer without checking
928 // that it's 16 byte aligned and this will cause an abort if
929 // the buffer is allocated on our stack. Rather than doing
930 // complicated, machine dependent alignment here we use the
931 // fact that malloc returns an aligned pointer on most architectures.
932 pv->buffer = malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
935 int out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
936 int len = avcodec_decode_audio2( context, buffer, &out_size,
937 data + pos, size - pos );
945 hb_buffer_t *buf = hb_buffer_init( 2 * out_size );
947 // convert from bytes to total samples
950 double pts = pv->pts_next;
952 pts += out_size * pv->duration;
956 float *fl32 = (float *)buf->data;
958 for( i = 0; i < out_size; ++i )
962 hb_list_add( pv->list, buf );
967 static int decavcodecaiWork( hb_work_object_t *w, hb_buffer_t **buf_in,
968 hb_buffer_t **buf_out )
970 if ( (*buf_in)->size <= 0 )
972 /* EOF on input stream - send it downstream & say that we're done */
978 hb_work_private_t *pv = w->private_data;
981 init_ffmpeg_context( w );
982 // duration is a scaling factor to go from #bytes in the decoded
983 // frame to frame time (in 90KHz mpeg ticks). 'channels' converts
984 // total samples to per-channel samples. 'sample_rate' converts
985 // per-channel samples to seconds per sample and the 90000
986 // is mpeg ticks per second.
987 pv->duration = 90000. /
988 (double)( pv->context->sample_rate * pv->context->channels );
990 hb_buffer_t *in = *buf_in;
992 // if the packet has a timestamp use it if we don't have a timestamp yet
993 // or if there's been a timing discontinuity of more than 100ms.
994 if ( in->start >= 0 &&
995 ( pv->pts_next < 0 || ( in->start - pv->pts_next ) > 90*100 ) )
997 pv->pts_next = in->start;
999 prepare_ffmpeg_buffer( in );
1000 decodeAudio( pv, in->data, in->size );
1001 *buf_out = link_buf_list( pv );
1006 hb_work_object_t hb_decavcodecvi =
1009 "Video decoder (ffmpeg streams)",
1017 hb_work_object_t hb_decavcodecai =
1020 "Audio decoder (ffmpeg streams)",