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.
65 #include "libavcodec/audioconvert.h"
67 static void flushDelayQueue( hb_work_private_t *pv );
68 static int decavcodecInit( hb_work_object_t *, hb_job_t * );
69 static int decavcodecWork( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
70 static void decavcodecClose( hb_work_object_t * );
71 static int decavcodecInfo( hb_work_object_t *, hb_work_info_t * );
72 static int decavcodecBSInfo( hb_work_object_t *, const hb_buffer_t *, hb_work_info_t * );
74 hb_work_object_t hb_decavcodec =
77 "MPGA decoder (libavcodec)",
87 // there are nheap items on the heap indexed 1..nheap (i.e., top of
88 // heap is 1). The 0th slot is unused - a marker is put there to check
89 // for overwrite errs.
90 int64_t h[HEAP_SIZE+1];
94 struct hb_work_private_s
97 AVCodecContext *context;
98 AVCodecParserContext *parser;
100 hb_list_t *ff_audio_list;
101 double duration; // frame duration (for video)
102 double pts_next; // next pts we expect to generate
103 int64_t chap_time; // time of next chap mark (if new_chap != 0)
104 int new_chap; // output chapter mark pending
107 uint32_t decode_errors;
108 int brokenByMicrosoft; // video stream may contain packed b-frames
109 hb_buffer_t* delayq[HEAP_SIZE];
112 struct SwsContext *sws_context; // if we have to rescale or convert color space
113 hb_downmix_t *downmix;
114 hb_sample_t *downmix_buffer;
116 hb_chan_map_t *out_map;
119 static void decodeAudio( hb_audio_t * audio, hb_work_private_t *pv, uint8_t *data, int size, int64_t pts );
120 static hb_buffer_t *link_buf_list( hb_work_private_t *pv );
123 static int64_t heap_pop( pts_heap_t *heap )
127 if ( heap->nheap <= 0 )
132 // return the top of the heap then put the bottom element on top,
133 // decrease the heap size by one & rebalence the heap.
136 int64_t v = heap->h[heap->nheap--];
138 int child = parent << 1;
139 while ( child <= heap->nheap )
141 // find the smallest of the two children of parent
142 if (child < heap->nheap && heap->h[child] > heap->h[child+1] )
145 if (v <= heap->h[child])
146 // new item is smaller than either child so it's the new parent.
149 // smallest child is smaller than new item so move it up then
150 // check its children.
151 int64_t hp = heap->h[child];
152 heap->h[parent] = hp;
160 static void heap_push( pts_heap_t *heap, int64_t v )
162 if ( heap->nheap < HEAP_SIZE )
167 // stick the new value on the bottom of the heap then bubble it
168 // up to its correct spot.
169 int child = heap->nheap;
171 int parent = child >> 1;
172 if (heap->h[parent] <= v)
175 int64_t hp = heap->h[parent];
183 /***********************************************************************
184 * hb_work_decavcodec_init
185 ***********************************************************************
187 **********************************************************************/
188 static int decavcodecInit( hb_work_object_t * w, hb_job_t * job )
193 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
194 w->private_data = pv;
197 pv->list = hb_list_init();
199 int codec_id = w->codec_param;
202 codec_id = CODEC_ID_MP2;
204 codec = avcodec_find_decoder( codec_id );
205 pv->parser = av_parser_init( codec_id );
207 pv->context = avcodec_alloc_context();
208 hb_avcodec_open( pv->context, codec );
210 if ( w->audio != NULL )
212 if ( w->audio->config.out.codec == HB_ACODEC_AC3 )
214 // ffmpegs audio encoder expect an smpte chan map as input.
215 // So we need to map the decoders output to smpte.
216 pv->out_map = &hb_smpte_chan_map;
220 pv->out_map = &hb_qt_chan_map;
222 if ( hb_need_downmix( w->audio->config.in.channel_layout,
223 w->audio->config.out.mixdown) )
225 pv->downmix = hb_downmix_init(w->audio->config.in.channel_layout,
226 w->audio->config.out.mixdown);
227 hb_downmix_set_chan_map( pv->downmix, &hb_smpte_chan_map, pv->out_map );
230 pv->ff_audio_list = hb_list_init();
231 for ( i = 0; i < hb_list_count( w->audio->priv.ff_audio_list ); i++ )
233 hb_work_private_t * ff_pv = calloc( 1, sizeof( hb_work_private_t ) );
234 hb_list_add( pv->ff_audio_list, ff_pv );
236 hb_audio_t *audio = hb_list_item( w->audio->priv.ff_audio_list, i );
238 ff_pv->list = hb_list_init();
241 if ( audio->config.out.codec == HB_ACODEC_AC3 )
243 // ffmpegs audio encoder expect an smpte chan map as input.
244 // So we need to map the decoders output to smpte.
245 ff_pv->out_map = &hb_smpte_chan_map;
249 ff_pv->out_map = &hb_qt_chan_map;
251 if ( hb_need_downmix( audio->config.in.channel_layout,
252 audio->config.out.mixdown) )
254 ff_pv->downmix = hb_downmix_init(audio->config.in.channel_layout,
255 audio->config.out.mixdown);
256 hb_downmix_set_chan_map( ff_pv->downmix, &hb_smpte_chan_map, ff_pv->out_map );
264 /***********************************************************************
266 ***********************************************************************
268 **********************************************************************/
269 static void closePrivData( hb_work_private_t ** ppv )
271 hb_work_private_t * pv = *ppv;
275 flushDelayQueue( pv );
277 if ( pv->job && pv->context && pv->context->codec )
279 hb_log( "%s-decoder done: %u frames, %u decoder errors, %u drops",
280 pv->context->codec->name, pv->nframes, pv->decode_errors,
283 if ( pv->sws_context )
285 sws_freeContext( pv->sws_context );
289 av_parser_close(pv->parser);
291 if ( pv->context && pv->context->codec )
293 hb_avcodec_close( pv->context );
297 hb_list_empty( &pv->list );
301 av_free( pv->buffer );
306 hb_downmix_close( &(pv->downmix) );
308 if ( pv->downmix_buffer )
310 free( pv->downmix_buffer );
311 pv->downmix_buffer = NULL;
318 static void decavcodecClose( hb_work_object_t * w )
320 hb_work_private_t * pv = w->private_data;
324 if ( pv->ff_audio_list != NULL )
326 hb_work_private_t * ff_pv;
327 while ( ( ff_pv = hb_list_item( pv->list, 0 ) ) != NULL )
329 hb_list_rem( pv->ff_audio_list, ff_pv );
330 closePrivData( &ff_pv );
333 closePrivData( &pv );
334 w->private_data = NULL;
338 static void writeAudioEof( hb_work_object_t * w )
340 hb_work_private_t * pv = w->private_data;
341 hb_audio_t * audio = w->audio;
345 for ( i = 0; i < hb_list_count( audio->priv.ff_audio_list ); i++ )
347 hb_audio_t *ff_audio = hb_list_item( audio->priv.ff_audio_list, i );
348 hb_work_private_t *ff_pv = hb_list_item( pv->ff_audio_list, i );
351 buf = hb_buffer_init( 0 );
356 if ( hb_fifo_full_wait( ff_audio->priv.fifo_raw ) )
358 hb_fifo_push( ff_audio->priv.fifo_raw, buf );
365 // w->done == true while waiting
366 hb_buffer_close( &buf );
374 static void writeAudioFifos( hb_work_object_t * w )
376 hb_work_private_t * pv = w->private_data;
377 hb_audio_t * audio = w->audio;
381 for ( i = 0; i < hb_list_count( audio->priv.ff_audio_list ); i++ )
383 hb_audio_t *ff_audio = hb_list_item( audio->priv.ff_audio_list, i );
384 hb_work_private_t *ff_pv = hb_list_item( pv->ff_audio_list, i );
387 buf = link_buf_list( ff_pv );
392 if ( hb_fifo_full_wait( ff_audio->priv.fifo_raw ) )
394 hb_fifo_push( ff_audio->priv.fifo_raw, buf );
401 // w->done == true while waiting
402 hb_buffer_close( &buf );
410 /***********************************************************************
412 ***********************************************************************
414 **********************************************************************/
415 static int decavcodecWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
416 hb_buffer_t ** buf_out )
418 hb_work_private_t * pv = w->private_data;
419 hb_buffer_t * in = *buf_in;
423 /* EOF on input stream - send it downstream & say that we're done */
432 if ( in->start < -1 && pv->pts_next <= 0 )
434 // discard buffers that start before video time 0
439 for ( pos = 0; pos < in->size; pos += len )
441 uint8_t *parser_output_buffer;
442 int parser_output_buffer_len;
443 int64_t cur = pv->pts_next;
445 if ( in->start != -1 )
450 if ( pv->parser != NULL )
452 len = av_parser_parse2( pv->parser, pv->context,
453 &parser_output_buffer, &parser_output_buffer_len,
454 in->data + pos, in->size - pos, cur, cur, 0 );
458 parser_output_buffer = in->data;
459 len = parser_output_buffer_len = in->size;
461 if (parser_output_buffer_len)
463 // set the duration on every frame since the stream format can
464 // change (it shouldn't but there's no way to guarantee it).
465 // duration is a scaling factor to go from #bytes in the decoded
466 // frame to frame time (in 90KHz mpeg ticks). 'channels' converts
467 // total samples to per-channel samples. 'sample_rate' converts
468 // per-channel samples to seconds per sample and the 90000
469 // is mpeg ticks per second.
470 if ( pv->context->sample_rate && pv->context->channels )
472 pv->duration = 90000. /
473 (double)( pv->context->sample_rate * pv->context->channels );
475 decodeAudio( w->audio, pv, parser_output_buffer, parser_output_buffer_len, pv->parser->pts );
478 writeAudioFifos( w );
479 *buf_out = link_buf_list( pv );
483 static int decavcodecInfo( hb_work_object_t *w, hb_work_info_t *info )
485 hb_work_private_t *pv = w->private_data;
487 memset( info, 0, sizeof(*info) );
489 if ( pv && pv->context )
491 AVCodecContext *context = pv->context;
492 info->bitrate = context->bit_rate;
493 info->rate = context->time_base.num;
494 info->rate_base = context->time_base.den;
495 info->profile = context->profile;
496 info->level = context->level;
502 static int decavcodecBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
503 hb_work_info_t *info )
505 hb_work_private_t *pv = w->private_data;
508 memset( info, 0, sizeof(*info) );
510 if ( pv && pv->context )
512 return decavcodecInfo( w, info );
515 // We should parse the bitstream to find its parameters but for right
516 // now we just return dummy values if there's a codec that will handle it.
517 AVCodec *codec = avcodec_find_decoder( w->codec_param? w->codec_param :
521 // there's no ffmpeg codec for this audio type - give up
525 static char codec_name[64];
526 info->name = strncpy( codec_name, codec->name, sizeof(codec_name)-1 );
528 AVCodecParserContext *parser = av_parser_init( codec->id );
529 AVCodecContext *context = avcodec_alloc_context();
530 hb_avcodec_open( context, codec );
531 uint8_t *buffer = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
532 int out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
533 unsigned char *pbuffer;
534 int pos, pbuffer_size;
536 while ( buf && !ret )
539 while ( pos < buf->size )
545 len = av_parser_parse2( parser, context, &pbuffer,
546 &pbuffer_size, buf->data + pos,
547 buf->size - pos, buf->start,
553 len = pbuffer_size = buf->size;
556 if ( pbuffer_size > 0 )
559 av_init_packet( &avp );
561 avp.size = pbuffer_size;
563 len = avcodec_decode_audio3( context, (int16_t*)buffer,
565 if ( len > 0 && context->sample_rate > 0 )
567 info->bitrate = context->bit_rate;
568 info->rate = context->sample_rate;
570 info->channel_layout =
571 hb_ff_layout_xlat(context->channel_layout,
582 if ( parser != NULL )
583 av_parser_close( parser );
584 hb_avcodec_close( context );
588 /* -------------------------------------------------------------
589 * General purpose video decoder using libavcodec
592 static uint8_t *copy_plane( uint8_t *dst, uint8_t* src, int dstride, int sstride,
595 if ( dstride == sstride )
597 memcpy( dst, src, dstride * h );
598 return dst + dstride * h;
600 int lbytes = dstride <= sstride? dstride : sstride;
603 memcpy( dst, src, lbytes );
610 // copy one video frame into an HB buf. If the frame isn't in our color space
611 // or at least one of its dimensions is odd, use sws_scale to convert/rescale it.
612 // Otherwise just copy the bits.
613 static hb_buffer_t *copy_frame( hb_work_private_t *pv, AVFrame *frame )
615 AVCodecContext *context = pv->context;
619 // if the dimensions are odd, drop the lsb since h264 requires that
620 // both width and height be even.
621 w = ( context->width >> 1 ) << 1;
622 h = ( context->height >> 1 ) << 1;
626 w = pv->job->title->width;
627 h = pv->job->title->height;
629 hb_buffer_t *buf = hb_video_buffer_init( w, h );
630 uint8_t *dst = buf->data;
632 if ( context->pix_fmt != PIX_FMT_YUV420P || w != context->width ||
633 h != context->height )
635 // have to convert to our internal color space and/or rescale
637 avpicture_fill( &dstpic, dst, PIX_FMT_YUV420P, w, h );
639 if ( ! pv->sws_context )
641 pv->sws_context = hb_sws_get_context( context->width, context->height, context->pix_fmt,
642 w, h, PIX_FMT_YUV420P,
643 SWS_LANCZOS|SWS_ACCURATE_RND);
645 sws_scale( pv->sws_context, frame->data, frame->linesize, 0, h,
646 dstpic.data, dstpic.linesize );
650 dst = copy_plane( dst, frame->data[0], w, frame->linesize[0], h );
651 w = (w + 1) >> 1; h = (h + 1) >> 1;
652 dst = copy_plane( dst, frame->data[1], w, frame->linesize[1], h );
653 dst = copy_plane( dst, frame->data[2], w, frame->linesize[2], h );
658 static int get_frame_buf( AVCodecContext *context, AVFrame *frame )
660 return avcodec_default_get_buffer( context, frame );
663 static int reget_frame_buf( AVCodecContext *context, AVFrame *frame )
665 return avcodec_default_reget_buffer( context, frame );
668 static void log_chapter( hb_work_private_t *pv, int chap_num, int64_t pts )
675 c = hb_list_item( pv->job->title->list_chapter, chap_num - 1 );
678 hb_log( "%s: \"%s\" (%d) at frame %u time %"PRId64,
679 pv->context->codec->name, c->title, chap_num, pv->nframes, pts );
683 hb_log( "%s: Chapter %d at frame %u time %"PRId64,
684 pv->context->codec->name, chap_num, pv->nframes, pts );
688 static void flushDelayQueue( hb_work_private_t *pv )
691 int slot = pv->nframes & (HEAP_SIZE-1);
693 // flush all the video packets left on our timestamp-reordering delay q
694 while ( ( buf = pv->delayq[slot] ) != NULL )
696 buf->start = heap_pop( &pv->pts_heap );
697 hb_list_add( pv->list, buf );
698 pv->delayq[slot] = NULL;
699 slot = ( slot + 1 ) & (HEAP_SIZE-1);
703 #define TOP_FIRST PIC_FLAG_TOP_FIELD_FIRST
704 #define PROGRESSIVE PIC_FLAG_PROGRESSIVE_FRAME
705 #define REPEAT_FIRST PIC_FLAG_REPEAT_FIRST_FIELD
713 static void checkCadence( int * cadence, uint16_t flags, int64_t start )
715 /* Rotate the cadence tracking. */
717 for(i=11; i > 0; i--)
719 cadence[i] = cadence[i-1];
722 if ( !(flags & PROGRESSIVE) && !(flags & TOP_FIRST) )
724 /* Not progressive, not top first...
725 That means it's probably bottom
726 first, 2 fields displayed.
728 //hb_log("MPEG2 Flag: Bottom field first, 2 fields displayed.");
731 else if ( !(flags & PROGRESSIVE) && (flags & TOP_FIRST) )
733 /* Not progressive, top is first,
734 Two fields displayed.
736 //hb_log("MPEG2 Flag: Top field first, 2 fields displayed.");
739 else if ( (flags & PROGRESSIVE) && !(flags & TOP_FIRST) && !( flags & REPEAT_FIRST ) )
741 /* Progressive, but noting else.
742 That means Bottom first,
745 //hb_log("MPEG2 Flag: Progressive. Bottom field first, 2 fields displayed.");
746 cadence[0] = BT_PROG;
748 else if ( (flags & PROGRESSIVE) && !(flags & TOP_FIRST) && ( flags & REPEAT_FIRST ) )
750 /* Progressive, and repeat. .
751 That means Bottom first,
754 //hb_log("MPEG2 Flag: Progressive repeat. Bottom field first, 3 fields displayed.");
755 cadence[0] = BTB_PROG;
757 else if ( (flags & PROGRESSIVE) && (flags & TOP_FIRST) && !( flags & REPEAT_FIRST ) )
759 /* Progressive, top first.
760 That means top first,
763 //hb_log("MPEG2 Flag: Progressive. Top field first, 2 fields displayed.");
764 cadence[0] = TB_PROG;
766 else if ( (flags & PROGRESSIVE) && (flags & TOP_FIRST) && ( flags & REPEAT_FIRST ) )
768 /* Progressive, top, repeat.
769 That means top first,
772 //hb_log("MPEG2 Flag: Progressive repeat. Top field first, 3 fields displayed.");
773 cadence[0] = TBT_PROG;
776 if ( (cadence[2] <= TB) && (cadence[1] <= TB) && (cadence[0] > TB) && (cadence[11]) )
777 hb_log("%fs: Video -> Film", (float)start / 90000);
778 if ( (cadence[2] > TB) && (cadence[1] <= TB) && (cadence[0] <= TB) && (cadence[11]) )
779 hb_log("%fs: Film -> Video", (float)start / 90000);
783 * Decodes a video frame from the specified raw packet data
784 * ('data', 'size', 'sequence').
785 * The output of this function is stored in 'pv->list', which contains a list
786 * of zero or more decoded packets.
788 * The returned packets are guaranteed to have their timestamps in the correct
789 * order, even if the original packets decoded by libavcodec have misordered
790 * timestamps, due to the use of 'packed B-frames'.
792 * Internally the set of decoded packets may be buffered in 'pv->delayq'
793 * until enough packets have been decoded so that the timestamps can be
794 * correctly rewritten, if this is necessary.
796 static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size, int sequence, int64_t pts, int64_t dts )
798 int got_picture, oldlevel = 0;
802 if ( global_verbosity_level <= 1 )
804 oldlevel = av_log_get_level();
805 av_log_set_level( AV_LOG_QUIET );
808 av_init_packet( &avp );
813 if ( avcodec_decode_video2( pv->context, &frame, &got_picture, &avp ) < 0 )
817 if ( global_verbosity_level <= 1 )
819 av_log_set_level( oldlevel );
825 // ffmpeg makes it hard to attach a pts to a frame. if the MPEG ES
826 // packet had a pts we handed it to av_parser_parse (if the packet had
827 // no pts we set it to AV_NOPTS_VALUE, but before the parse we can't
828 // distinguish between the start of a video frame with no pts & an
829 // intermediate packet of some frame which never has a pts). we hope
830 // that when parse returns the frame to us the pts we originally
831 // handed it will be in parser->pts. we put this pts into avp.pts so
832 // that when avcodec_decode_video finally gets around to allocating an
833 // AVFrame to hold the decoded frame, avcodec_default_get_buffer can
834 // stuff that pts into the it. if all of these relays worked at this
835 // point frame.pts should hold the frame's pts from the original data
836 // stream or AV_NOPTS_VALUE if it didn't have one. in the latter case
837 // we generate the next pts in sequence for it.
838 double frame_dur = pv->duration;
839 if ( frame_dur <= 0 )
841 frame_dur = 90000. * (double)pv->context->time_base.num /
842 (double)pv->context->time_base.den;
843 pv->duration = frame_dur;
845 if ( pv->context->ticks_per_frame > 1 )
849 if ( frame.repeat_pict )
851 frame_dur += frame.repeat_pict * pv->duration;
854 // If there was no pts for this frame, assume constant frame rate
855 // video & estimate the next frame time from the last & duration.
857 if (frame.pkt_pts == AV_NOPTS_VALUE)
865 pv->pts_next = pts + frame_dur;
867 if ( frame.top_field_first )
869 flags |= PIC_FLAG_TOP_FIELD_FIRST;
871 if ( !frame.interlaced_frame )
873 flags |= PIC_FLAG_PROGRESSIVE_FRAME;
875 if ( frame.repeat_pict )
877 flags |= PIC_FLAG_REPEAT_FIRST_FIELD;
882 // if we're doing a scan or this content couldn't have been broken
883 // by Microsoft we don't worry about timestamp reordering
884 if ( ! pv->job || ! pv->brokenByMicrosoft )
886 buf = copy_frame( pv, &frame );
888 buf->sequence = sequence;
890 if ( pv->new_chap && buf->start >= pv->chap_time )
892 buf->new_chap = pv->new_chap;
895 log_chapter( pv, buf->new_chap, buf->start );
897 else if ( pv->nframes == 0 && pv->job )
899 log_chapter( pv, pv->job->chapter_start, buf->start );
901 checkCadence( pv->cadence, buf->flags, buf->start );
902 hb_list_add( pv->list, buf );
907 // XXX This following probably addresses a libavcodec bug but I don't
908 // see an easy fix so we workaround it here.
910 // The M$ 'packed B-frames' atrocity results in decoded frames with
911 // the wrong timestamp. E.g., if there are 2 b-frames the timestamps
912 // we see here will be "2 3 1 5 6 4 ..." instead of "1 2 3 4 5 6".
913 // The frames are actually delivered in the right order but with
914 // the wrong timestamp. To get the correct timestamp attached to
915 // each frame we have a delay queue (longer than the max number of
916 // b-frames) & a sorting heap for the timestamps. As each frame
917 // comes out of the decoder the oldest frame in the queue is removed
918 // and associated with the smallest timestamp. Then the new frame is
919 // added to the queue & its timestamp is pushed on the heap.
920 // This does nothing if the timestamps are correct (i.e., the video
921 // uses a codec that Micro$oft hasn't broken yet) but the frames
922 // get timestamped correctly even when M$ has munged them.
924 // remove the oldest picture from the frame queue (if any) &
925 // give it the smallest timestamp from our heap. The queue size
926 // is a power of two so we get the slot of the oldest by masking
927 // the frame count & this will become the slot of the newest
928 // once we've removed & processed the oldest.
929 int slot = pv->nframes & (HEAP_SIZE-1);
930 if ( ( buf = pv->delayq[slot] ) != NULL )
932 buf->start = heap_pop( &pv->pts_heap );
934 if ( pv->new_chap && buf->start >= pv->chap_time )
936 buf->new_chap = pv->new_chap;
939 log_chapter( pv, buf->new_chap, buf->start );
941 else if ( pv->nframes == 0 && pv->job )
943 log_chapter( pv, pv->job->chapter_start, buf->start );
945 checkCadence( pv->cadence, buf->flags, buf->start );
946 hb_list_add( pv->list, buf );
949 // add the new frame to the delayq & push its timestamp on the heap
950 buf = copy_frame( pv, &frame );
951 buf->sequence = sequence;
953 pv->delayq[slot] = buf;
954 heap_push( &pv->pts_heap, pts );
962 static void decodeVideo( hb_work_private_t *pv, uint8_t *data, int size, int sequence, int64_t pts, int64_t dts )
965 * The following loop is a do..while because we need to handle both
966 * data & the flush at the end (signaled by size=0). At the end there's
967 * generally a frame in the parser & one or more frames in the decoder
968 * (depending on the bframes setting).
974 int len = av_parser_parse2( pv->parser, pv->context, &pout, &pout_len,
975 data + pos, size - pos, pts, dts, 0 );
980 decodeFrame( pv, pout, pout_len, sequence, pv->parser->pts, pv->parser->dts );
982 } while ( pos < size );
984 /* the stuff above flushed the parser, now flush the decoder */
987 while ( decodeFrame( pv, NULL, 0, sequence, AV_NOPTS_VALUE, AV_NOPTS_VALUE ) )
990 flushDelayQueue( pv );
995 * Removes all packets from 'pv->list', links them together into
996 * a linked-list, and returns the first packet in the list.
998 static hb_buffer_t *link_buf_list( hb_work_private_t *pv )
1000 hb_buffer_t *head = hb_list_item( pv->list, 0 );
1004 hb_list_rem( pv->list, head );
1006 hb_buffer_t *last = head, *buf;
1008 while ( ( buf = hb_list_item( pv->list, 0 ) ) != NULL )
1010 hb_list_rem( pv->list, buf );
1019 static int decavcodecvInit( hb_work_object_t * w, hb_job_t * job )
1022 hb_work_private_t *pv = calloc( 1, sizeof( hb_work_private_t ) );
1023 w->private_data = pv;
1025 pv->list = hb_list_init();
1027 int codec_id = w->codec_param;
1028 pv->parser = av_parser_init( codec_id );
1029 pv->context = avcodec_alloc_context2( CODEC_TYPE_VIDEO );
1031 /* we have to wrap ffmpeg's get_buffer to be able to set the pts (?!) */
1032 pv->context->opaque = pv;
1033 pv->context->get_buffer = get_frame_buf;
1034 pv->context->reget_buffer = reget_frame_buf;
1039 static int next_hdr( hb_buffer_t *in, int offset )
1041 uint8_t *dat = in->data;
1042 uint16_t last2 = 0xffff;
1043 for ( ; in->size - offset > 1; ++offset )
1045 if ( last2 == 0 && dat[offset] == 0x01 )
1046 // found an mpeg start code
1049 last2 = ( last2 << 8 ) | dat[offset];
1055 static int find_hdr( hb_buffer_t *in, int offset, uint8_t hdr_type )
1057 if ( in->size - offset < 4 )
1058 // not enough room for an mpeg start code
1061 for ( ; ( offset = next_hdr( in, offset ) ) >= 0; ++offset )
1063 if ( in->data[offset+3] == hdr_type )
1070 static int setup_extradata( hb_work_object_t *w, hb_buffer_t *in )
1072 hb_work_private_t *pv = w->private_data;
1074 // we can't call the avstream funcs but the read_header func in the
1075 // AVInputFormat may set up some state in the AVContext. In particular
1076 // vc1t_read_header allocates 'extradata' to deal with header issues
1077 // related to Microsoft's bizarre engineering notions. We alloc a chunk
1078 // of space to make vc1 work then associate the codec with the context.
1079 if ( w->codec_param != CODEC_ID_VC1 )
1081 // we haven't been inflicted with M$ - allocate a little space as
1082 // a marker and return success.
1083 pv->context->extradata_size = 0;
1084 pv->context->extradata = av_malloc(pv->context->extradata_size);
1088 // find the start and and of the sequence header
1090 if ( ( shdr = find_hdr( in, 0, 0x0f ) ) < 0 )
1092 // didn't find start of seq hdr
1095 if ( ( shdr_end = next_hdr( in, shdr + 4 ) ) < 0 )
1097 shdr_end = in->size;
1101 // find the start and and of the entry point header
1103 if ( ( ehdr = find_hdr( in, 0, 0x0e ) ) < 0 )
1105 // didn't find start of entry point hdr
1108 if ( ( ehdr_end = next_hdr( in, ehdr + 4 ) ) < 0 )
1110 ehdr_end = in->size;
1114 // found both headers - allocate an extradata big enough to hold both
1115 // then copy them into it.
1116 pv->context->extradata_size = shdr_end + ehdr_end;
1117 pv->context->extradata = av_malloc(pv->context->extradata_size + 8);
1118 memcpy( pv->context->extradata, in->data + shdr, shdr_end );
1119 memcpy( pv->context->extradata + shdr_end, in->data + ehdr, ehdr_end );
1120 memset( pv->context->extradata + shdr_end + ehdr_end, 0, 8);
1124 static int decavcodecvWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
1125 hb_buffer_t ** buf_out )
1127 hb_work_private_t *pv = w->private_data;
1128 hb_buffer_t *in = *buf_in;
1129 int64_t pts = AV_NOPTS_VALUE;
1134 /* if we got an empty buffer signaling end-of-stream send it downstream */
1135 if ( in->size == 0 )
1137 if ( pv->context->codec != NULL )
1139 decodeVideo( pv, in->data, in->size, in->sequence, pts, dts );
1141 hb_list_add( pv->list, in );
1142 *buf_out = link_buf_list( pv );
1143 return HB_WORK_DONE;
1146 // if this is the first frame open the codec (we have to wait for the
1147 // first frame because of M$ VC1 braindamage).
1148 if ( pv->context->extradata == NULL )
1150 if ( setup_extradata( w, in ) )
1152 // we didn't find the headers needed to set up extradata.
1153 // the codec will abort if we open it so just free the buf
1154 // and hope we eventually get the info we need.
1155 hb_buffer_close( &in );
1158 AVCodec *codec = avcodec_find_decoder( w->codec_param );
1159 // There's a mis-feature in ffmpeg that causes the context to be
1160 // incorrectly initialized the 1st time avcodec_open is called.
1161 // If you close it and open a 2nd time, it finishes the job.
1162 hb_avcodec_open( pv->context, codec );
1163 hb_avcodec_close( pv->context );
1164 hb_avcodec_open( pv->context, codec );
1167 if( in->start >= 0 )
1170 dts = in->renderOffset;
1174 pv->new_chap = in->new_chap;
1175 pv->chap_time = pts >= 0? pts : pv->pts_next;
1177 decodeVideo( pv, in->data, in->size, in->sequence, pts, dts );
1178 hb_buffer_close( &in );
1179 *buf_out = link_buf_list( pv );
1183 static int decavcodecvInfo( hb_work_object_t *w, hb_work_info_t *info )
1185 hb_work_private_t *pv = w->private_data;
1187 memset( info, 0, sizeof(*info) );
1189 if ( pv && pv->context )
1191 AVCodecContext *context = pv->context;
1192 info->bitrate = context->bit_rate;
1193 info->width = context->width;
1194 info->height = context->height;
1196 /* ffmpeg gives the frame rate in frames per second while HB wants
1197 * it in units of the 27MHz MPEG clock. */
1198 info->rate = 27000000;
1199 info->rate_base = (int64_t)context->time_base.num * 27000000LL /
1200 context->time_base.den;
1201 if ( context->ticks_per_frame > 1 )
1203 // for ffmpeg 0.5 & later, the H.264 & MPEG-2 time base is
1204 // field rate rather than frame rate so convert back to frames.
1205 info->rate_base *= context->ticks_per_frame;
1208 info->pixel_aspect_width = context->sample_aspect_ratio.num;
1209 info->pixel_aspect_height = context->sample_aspect_ratio.den;
1211 /* Sometimes there's no pixel aspect set in the source ffmpeg context
1212 * which appears to come from the video stream. In that case,
1213 * try the pixel aspect in AVStream (which appears to come from
1214 * the container). Else assume a 1:1 PAR. */
1215 if ( info->pixel_aspect_width == 0 ||
1216 info->pixel_aspect_height == 0 )
1218 // There will not be an ffmpeg stream if the file is TS
1219 AVStream *st = hb_ffmpeg_avstream( w->codec_param );
1220 info->pixel_aspect_width = st && st->sample_aspect_ratio.num ?
1221 st->sample_aspect_ratio.num : 1;
1222 info->pixel_aspect_height = st && st->sample_aspect_ratio.den ?
1223 st->sample_aspect_ratio.den : 1;
1225 /* ffmpeg returns the Pixel Aspect Ratio (PAR). Handbrake wants the
1226 * Display Aspect Ratio so we convert by scaling by the Storage
1227 * Aspect Ratio (w/h). We do the calc in floating point to get the
1228 * rounding right. */
1229 info->aspect = (double)info->pixel_aspect_width *
1230 (double)context->width /
1231 (double)info->pixel_aspect_height /
1232 (double)context->height;
1234 info->profile = context->profile;
1235 info->level = context->level;
1236 info->name = context->codec->name;
1242 static int decavcodecvBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
1243 hb_work_info_t *info )
1248 hb_work_object_t hb_decavcodecv =
1251 "Video decoder (libavcodec)",
1260 // This is a special decoder for ffmpeg streams. The ffmpeg stream reader
1261 // includes a parser and passes information from the parser to the decoder
1262 // via a codec context kept in the AVStream of the reader's AVFormatContext.
1263 // We *have* to use that codec context to decode the stream or we'll get
1264 // garbage. ffmpeg_title_scan put a cookie that can be used to get to that
1265 // codec context in our codec_param.
1267 // this routine gets the appropriate context pointer from the ffmpeg
1268 // stream reader. it can't be called until we get the first buffer because
1269 // we can't guarantee that reader will be called before the our init
1270 // routine and if our init is called first we'll get a pointer to the
1271 // old scan stream (which has already been closed).
1272 static void init_ffmpeg_context( hb_work_object_t *w )
1274 hb_work_private_t *pv = w->private_data;
1275 pv->context = hb_ffmpeg_context( w->codec_param );
1277 // during scan the decoder gets closed & reopened which will
1278 // close the codec so reopen it if it's not there
1279 if ( ! pv->context->codec )
1281 AVCodec *codec = avcodec_find_decoder( pv->context->codec_id );
1282 hb_avcodec_open( pv->context, codec );
1284 // set up our best guess at the frame duration.
1285 // the frame rate in the codec is usually bogus but it's sometimes
1286 // ok in the stream.
1287 AVStream *st = hb_ffmpeg_avstream( w->codec_param );
1289 if ( st->nb_frames && st->duration )
1291 // compute the average frame duration from the total number
1292 // of frames & the total duration.
1293 pv->duration = ( (double)st->duration * (double)st->time_base.num ) /
1294 ( (double)st->nb_frames * (double)st->time_base.den );
1298 // XXX We don't have a frame count or duration so try to use the
1299 // far less reliable time base info in the stream.
1300 // Because the time bases are so screwed up, we only take values
1301 // in the range 8fps - 64fps.
1303 if ( st->avg_frame_rate.den * 64L > st->avg_frame_rate.num &&
1304 st->avg_frame_rate.num > st->avg_frame_rate.den * 8L )
1306 tb.num = st->avg_frame_rate.den;
1307 tb.den = st->avg_frame_rate.num;
1309 else if ( st->time_base.num * 64L > st->time_base.den &&
1310 st->time_base.den > st->time_base.num * 8L )
1314 else if ( st->r_frame_rate.den * 64L > st->r_frame_rate.num &&
1315 st->r_frame_rate.num > st->r_frame_rate.den * 8L )
1317 tb.num = st->r_frame_rate.den;
1318 tb.den = st->r_frame_rate.num;
1322 tb.num = 1001; /*XXX*/
1323 tb.den = 24000; /*XXX*/
1325 pv->duration = (double)tb.num / (double)tb.den;
1327 pv->duration *= 90000.;
1329 // we have to wrap ffmpeg's get_buffer to be able to set the pts (?!)
1330 pv->context->opaque = pv;
1331 pv->context->get_buffer = get_frame_buf;
1332 pv->context->reget_buffer = reget_frame_buf;
1334 // avi, mkv and possibly mp4 containers can contain the M$ VFW packed
1335 // b-frames abortion that messes up frame ordering and timestamps.
1336 // XXX ffmpeg knows which streams are broken but doesn't expose the
1337 // info externally. We should patch ffmpeg to add a flag to the
1338 // codec context for this but until then we mark all ffmpeg streams
1340 pv->brokenByMicrosoft = 1;
1343 static void prepare_ffmpeg_buffer( hb_buffer_t * in )
1345 // ffmpeg requires an extra 8 bytes of zero at the end of the buffer and
1346 // will seg fault in odd, data dependent ways if it's not there. (my guess
1347 // is this is a case of a local performance optimization creating a global
1348 // performance degradation since all the time wasted by extraneous data
1349 // copies & memory zeroing has to be huge compared to the minor reduction
1350 // in inner-loop instructions this affords - modern cpus bottleneck on
1351 // memory bandwidth not instruction bandwidth).
1352 if ( in->size + FF_INPUT_BUFFER_PADDING_SIZE > in->alloc )
1354 // have to realloc to add the padding
1355 hb_buffer_realloc( in, in->size + FF_INPUT_BUFFER_PADDING_SIZE );
1357 memset( in->data + in->size, 0, FF_INPUT_BUFFER_PADDING_SIZE );
1360 static int decavcodecviInit( hb_work_object_t * w, hb_job_t * job )
1363 hb_work_private_t *pv = calloc( 1, sizeof( hb_work_private_t ) );
1365 w->private_data = pv;
1367 pv->list = hb_list_init();
1370 if ( w->audio != NULL )
1372 if ( w->audio->config.out.codec == HB_ACODEC_AC3 )
1374 // ffmpegs audio encoder expect an smpte chan map as input.
1375 // So we need to map the decoders output to smpte.
1376 pv->out_map = &hb_smpte_chan_map;
1380 pv->out_map = &hb_qt_chan_map;
1382 if ( hb_need_downmix( w->audio->config.in.channel_layout,
1383 w->audio->config.out.mixdown) )
1385 pv->downmix = hb_downmix_init(w->audio->config.in.channel_layout,
1386 w->audio->config.out.mixdown);
1387 hb_downmix_set_chan_map( pv->downmix, &hb_smpte_chan_map, pv->out_map );
1390 pv->ff_audio_list = hb_list_init();
1391 for ( i = 0; i < hb_list_count( w->audio->priv.ff_audio_list ); i++ )
1393 hb_work_private_t * ff_pv = calloc( 1, sizeof( hb_work_private_t ) );
1394 hb_list_add( pv->ff_audio_list, ff_pv );
1396 hb_audio_t *audio = hb_list_item( w->audio->priv.ff_audio_list, i );
1398 ff_pv->list = hb_list_init();
1400 ff_pv->pts_next = -1;
1402 if ( audio->config.out.codec == HB_ACODEC_AC3 )
1404 // ffmpegs audio encoder expect an smpte chan map as input.
1405 // So we need to map the decoders output to smpte.
1406 ff_pv->out_map = &hb_smpte_chan_map;
1410 ff_pv->out_map = &hb_qt_chan_map;
1412 if ( hb_need_downmix( audio->config.in.channel_layout,
1413 audio->config.out.mixdown) )
1415 ff_pv->downmix = hb_downmix_init(audio->config.in.channel_layout,
1416 audio->config.out.mixdown);
1417 hb_downmix_set_chan_map( ff_pv->downmix, &hb_smpte_chan_map, ff_pv->out_map );
1425 static int decavcodecviWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
1426 hb_buffer_t ** buf_out )
1428 hb_work_private_t *pv = w->private_data;
1429 hb_buffer_t *in = *buf_in;
1432 /* if we got an empty buffer signaling end-of-stream send it downstream */
1433 if ( in->size == 0 )
1435 /* flush any frames left in the decoder */
1436 while ( pv->context && decodeFrame( pv, NULL, 0, in->sequence, AV_NOPTS_VALUE, AV_NOPTS_VALUE ) )
1439 flushDelayQueue( pv );
1440 hb_list_add( pv->list, in );
1441 *buf_out = link_buf_list( pv );
1442 return HB_WORK_DONE;
1445 if ( ! pv->context )
1447 init_ffmpeg_context( w );
1450 int64_t pts = in->start;
1453 // use the first timestamp as our 'next expected' pts
1454 if ( pv->pts_next < 0 )
1462 pv->new_chap = in->new_chap;
1463 pv->chap_time = pts >= 0? pts : pv->pts_next;
1465 prepare_ffmpeg_buffer( in );
1466 decodeFrame( pv, in->data, in->size, in->sequence, in->start, in->renderOffset );
1467 hb_buffer_close( &in );
1468 *buf_out = link_buf_list( pv );
1472 static int decavcodecviInfo( hb_work_object_t *w, hb_work_info_t *info )
1474 if ( decavcodecvInfo( w, info ) )
1476 hb_work_private_t *pv = w->private_data;
1477 if ( ! pv->context )
1479 init_ffmpeg_context( w );
1481 // we have the frame duration in units of the 90KHz pts clock but
1482 // need it in units of the 27MHz MPEG clock. */
1483 info->rate = 27000000;
1484 info->rate_base = pv->duration * 300.;
1490 static hb_buffer_t * downmixAudio(
1492 hb_work_private_t *pv,
1497 hb_buffer_t * buf = NULL;
1501 pv->downmix_buffer = realloc(pv->downmix_buffer, nsamples * sizeof(hb_sample_t));
1504 for( i = 0; i < nsamples; ++i )
1506 pv->downmix_buffer[i] = buffer[i];
1509 int n_ch_samples = nsamples / channels;
1510 int out_channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown);
1512 buf = hb_buffer_init( n_ch_samples * out_channels * sizeof(float) );
1513 hb_sample_t *samples = (hb_sample_t *)buf->data;
1514 hb_downmix(pv->downmix, samples, pv->downmix_buffer, n_ch_samples);
1518 buf = hb_buffer_init( nsamples * sizeof(float) );
1519 float *fl32 = (float *)buf->data;
1521 for( i = 0; i < nsamples; ++i )
1523 fl32[i] = buffer[i];
1525 int n_ch_samples = nsamples / channels;
1526 hb_layout_remap( &hb_smpte_chan_map, pv->out_map,
1527 audio->config.in.channel_layout,
1528 fl32, n_ch_samples );
1534 static void decodeAudio( hb_audio_t * audio, hb_work_private_t *pv, uint8_t *data, int size, int64_t pts )
1536 AVCodecContext *context = pv->context;
1538 int loop_limit = 256;
1540 while ( pos < size )
1542 int16_t *buffer = pv->buffer;
1543 if ( buffer == NULL )
1545 pv->buffer = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
1546 buffer = pv->buffer;
1550 av_init_packet( &avp );
1551 avp.data = data + pos;
1552 avp.size = size - pos;
1554 avp.dts = AV_NOPTS_VALUE;
1556 int out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
1558 int len = avcodec_decode_audio3( context, buffer, &out_size, &avp );
1565 if ( !(loop_limit--) )
1574 // We require signed 16-bit ints for the output format. If
1575 // we got something different convert it.
1576 if ( context->sample_fmt != SAMPLE_FMT_S16 )
1578 // Note: av_audio_convert seems to be a work-in-progress but
1579 // looks like it will eventually handle general audio
1580 // mixdowns which would allow us much more flexibility
1581 // in handling multichannel audio in HB. If we were doing
1582 // anything more complicated than a one-for-one format
1583 // conversion we'd probably want to cache the converter
1584 // context in the pv.
1585 int isamp = av_get_bits_per_sample_fmt( context->sample_fmt ) / 8;
1586 AVAudioConvert *ctx = av_audio_convert_alloc( SAMPLE_FMT_S16, 1,
1587 context->sample_fmt, 1,
1589 // get output buffer size (in 2-byte samples) then malloc a buffer
1590 nsamples = out_size / isamp;
1591 buffer = av_malloc( nsamples * 2 );
1593 // we're doing straight sample format conversion which behaves as if
1594 // there were only one channel.
1595 const void * const ibuf[6] = { pv->buffer };
1596 void * const obuf[6] = { buffer };
1597 const int istride[6] = { isamp };
1598 const int ostride[6] = { 2 };
1600 av_audio_convert( ctx, obuf, ostride, ibuf, istride, nsamples );
1601 av_audio_convert_free( ctx );
1605 nsamples = out_size / 2;
1608 if ( pts == AV_NOPTS_VALUE )
1614 double pts_next = pts + nsamples * pv->duration;
1615 buf = downmixAudio( audio, pv, buffer, context->channels, nsamples );
1619 buf->stop = pts_next;
1620 hb_list_add( pv->list, buf );
1624 for ( i = 0; i < hb_list_count( audio->priv.ff_audio_list ); i++ )
1626 hb_audio_t *ff_audio = hb_list_item( audio->priv.ff_audio_list, i );
1627 hb_work_private_t *ff_pv = hb_list_item( pv->ff_audio_list, i );
1630 buf = downmixAudio( ff_audio, ff_pv, buffer, context->channels, nsamples );
1634 buf->stop = pts_next;
1635 hb_list_add( ff_pv->list, buf );
1639 pv->pts_next = pts_next;
1641 // if we allocated a buffer for sample format conversion, free it
1642 if ( buffer != pv->buffer )
1650 static int decavcodecaiWork( hb_work_object_t *w, hb_buffer_t **buf_in,
1651 hb_buffer_t **buf_out )
1653 if ( (*buf_in)->size <= 0 )
1655 /* EOF on input stream - send it downstream & say that we're done */
1659 return HB_WORK_DONE;
1662 hb_work_private_t *pv = w->private_data;
1664 if ( (*buf_in)->start < -1 && pv->pts_next <= 0 )
1666 // discard buffers that start before video time 0
1671 if ( ! pv->context )
1673 init_ffmpeg_context( w );
1674 // duration is a scaling factor to go from #bytes in the decoded
1675 // frame to frame time (in 90KHz mpeg ticks). 'channels' converts
1676 // total samples to per-channel samples. 'sample_rate' converts
1677 // per-channel samples to seconds per sample and the 90000
1678 // is mpeg ticks per second.
1679 pv->duration = 90000. /
1680 (double)( pv->context->sample_rate * pv->context->channels );
1682 hb_buffer_t *in = *buf_in;
1684 // if the packet has a timestamp use it if we don't have a timestamp yet
1685 // or if there's been a timing discontinuity of more than 100ms.
1686 if ( in->start >= 0 &&
1687 ( pv->pts_next < 0 || ( in->start - pv->pts_next ) > 90*100 ) )
1689 pv->pts_next = in->start;
1691 prepare_ffmpeg_buffer( in );
1692 decodeAudio( w->audio, pv, in->data, in->size, pv->pts_next );
1693 writeAudioFifos( w );
1694 *buf_out = link_buf_list( pv );
1699 hb_work_object_t hb_decavcodecvi =
1702 "Video decoder (ffmpeg streams)",
1710 hb_work_object_t hb_decavcodecai =
1713 "Audio decoder (ffmpeg streams)",