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 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];
111 struct SwsContext *sws_context; // if we have to rescale or convert color space
112 hb_downmix_t *downmix;
113 hb_sample_t *downmix_buffer;
116 static void decodeAudio( hb_audio_t * audio, hb_work_private_t *pv, uint8_t *data, int size );
117 static hb_buffer_t *link_buf_list( hb_work_private_t *pv );
120 static int64_t heap_pop( pts_heap_t *heap )
124 if ( heap->nheap <= 0 )
129 // return the top of the heap then put the bottom element on top,
130 // decrease the heap size by one & rebalence the heap.
133 int64_t v = heap->h[heap->nheap--];
135 int child = parent << 1;
136 while ( child <= heap->nheap )
138 // find the smallest of the two children of parent
139 if (child < heap->nheap && heap->h[child] > heap->h[child+1] )
142 if (v <= heap->h[child])
143 // new item is smaller than either child so it's the new parent.
146 // smallest child is smaller than new item so move it up then
147 // check its children.
148 int64_t hp = heap->h[child];
149 heap->h[parent] = hp;
157 static void heap_push( pts_heap_t *heap, int64_t v )
159 if ( heap->nheap < HEAP_SIZE )
164 // stick the new value on the bottom of the heap then bubble it
165 // up to its correct spot.
166 int child = heap->nheap;
168 int parent = child >> 1;
169 if (heap->h[parent] <= v)
172 int64_t hp = heap->h[parent];
180 /***********************************************************************
181 * hb_work_decavcodec_init
182 ***********************************************************************
184 **********************************************************************/
185 static int decavcodecInit( hb_work_object_t * w, hb_job_t * job )
189 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
190 w->private_data = pv;
193 pv->list = hb_list_init();
195 int codec_id = w->codec_param;
198 codec_id = CODEC_ID_MP2;
200 codec = avcodec_find_decoder( codec_id );
201 pv->parser = av_parser_init( codec_id );
203 pv->context = avcodec_alloc_context();
204 hb_avcodec_open( pv->context, codec );
206 if ( w->audio != NULL &&
207 hb_need_downmix( w->audio->config.in.channel_layout,
208 w->audio->config.out.mixdown) )
210 pv->downmix = hb_downmix_init(w->audio->config.in.channel_layout,
211 w->audio->config.out.mixdown);
212 hb_downmix_set_chan_map( pv->downmix, &hb_smpte_chan_map, &hb_qt_chan_map );
218 /***********************************************************************
220 ***********************************************************************
222 **********************************************************************/
223 static void decavcodecClose( hb_work_object_t * w )
225 hb_work_private_t * pv = w->private_data;
229 if ( pv->job && pv->context && pv->context->codec )
231 hb_log( "%s-decoder done: %u frames, %u decoder errors, %u drops",
232 pv->context->codec->name, pv->nframes, pv->decode_errors,
235 if ( pv->sws_context )
237 sws_freeContext( pv->sws_context );
241 av_parser_close(pv->parser);
243 if ( pv->context && pv->context->codec )
245 hb_avcodec_close( pv->context );
249 hb_list_close( &pv->list );
253 av_free( pv->buffer );
258 hb_downmix_close( &(pv->downmix) );
260 if ( pv->downmix_buffer )
262 free( pv->downmix_buffer );
263 pv->downmix_buffer = NULL;
266 w->private_data = NULL;
270 /***********************************************************************
272 ***********************************************************************
274 **********************************************************************/
275 static int decavcodecWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
276 hb_buffer_t ** buf_out )
278 hb_work_private_t * pv = w->private_data;
279 hb_buffer_t * in = *buf_in;
283 /* EOF on input stream - send it downstream & say that we're done */
291 if ( in->start < -1 && pv->pts_next <= 0 )
293 // discard buffers that start before video time 0
297 // if the packet has a timestamp use it
298 if ( in->start != -1 )
300 pv->pts_next = in->start;
304 for ( pos = 0; pos < in->size; pos += len )
306 uint8_t *parser_output_buffer;
307 int parser_output_buffer_len;
308 int64_t cur = pv->pts_next;
310 if ( pv->parser != NULL )
312 len = av_parser_parse2( pv->parser, pv->context,
313 &parser_output_buffer, &parser_output_buffer_len,
314 in->data + pos, in->size - pos, cur, cur, AV_NOPTS_VALUE );
318 parser_output_buffer = in->data;
319 len = parser_output_buffer_len = in->size;
321 if (parser_output_buffer_len)
323 // set the duration on every frame since the stream format can
324 // change (it shouldn't but there's no way to guarantee it).
325 // duration is a scaling factor to go from #bytes in the decoded
326 // frame to frame time (in 90KHz mpeg ticks). 'channels' converts
327 // total samples to per-channel samples. 'sample_rate' converts
328 // per-channel samples to seconds per sample and the 90000
329 // is mpeg ticks per second.
330 if ( pv->context->sample_rate && pv->context->channels )
332 pv->duration = 90000. /
333 (double)( pv->context->sample_rate * pv->context->channels );
335 decodeAudio( w->audio, pv, parser_output_buffer, parser_output_buffer_len );
338 *buf_out = link_buf_list( pv );
342 static int decavcodecInfo( hb_work_object_t *w, hb_work_info_t *info )
344 hb_work_private_t *pv = w->private_data;
346 memset( info, 0, sizeof(*info) );
348 if ( pv && pv->context )
350 AVCodecContext *context = pv->context;
351 info->bitrate = context->bit_rate;
352 info->rate = context->time_base.num;
353 info->rate_base = context->time_base.den;
354 info->profile = context->profile;
355 info->level = context->level;
361 static int decavcodecBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
362 hb_work_info_t *info )
364 hb_work_private_t *pv = w->private_data;
367 memset( info, 0, sizeof(*info) );
369 if ( pv && pv->context )
371 return decavcodecInfo( w, info );
374 // We should parse the bitstream to find its parameters but for right
375 // now we just return dummy values if there's a codec that will handle it.
376 AVCodec *codec = avcodec_find_decoder( w->codec_param? w->codec_param :
380 // there's no ffmpeg codec for this audio type - give up
384 static char codec_name[64];
385 info->name = strncpy( codec_name, codec->name, sizeof(codec_name)-1 );
387 AVCodecParserContext *parser = av_parser_init( codec->id );
388 AVCodecContext *context = avcodec_alloc_context();
389 hb_avcodec_open( context, codec );
390 uint8_t *buffer = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
391 int out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
392 unsigned char *pbuffer;
393 int pos, pbuffer_size;
395 while ( buf && !ret )
398 while ( pos < buf->size )
404 len = av_parser_parse2( parser, context, &pbuffer,
405 &pbuffer_size, buf->data + pos,
406 buf->size - pos, buf->start,
407 buf->start, AV_NOPTS_VALUE );
412 len = pbuffer_size = buf->size;
415 if ( pbuffer_size > 0 )
418 av_init_packet( &avp );
420 avp.size = pbuffer_size;
422 len = avcodec_decode_audio3( context, (int16_t*)buffer,
424 if ( len > 0 && context->sample_rate > 0 )
426 info->bitrate = context->bit_rate;
427 info->rate = context->sample_rate;
429 info->channel_layout =
430 hb_ff_layout_xlat(context->channel_layout,
441 if ( parser != NULL )
442 av_parser_close( parser );
443 hb_avcodec_close( context );
447 /* -------------------------------------------------------------
448 * General purpose video decoder using libavcodec
451 static uint8_t *copy_plane( uint8_t *dst, uint8_t* src, int dstride, int sstride,
454 if ( dstride == sstride )
456 memcpy( dst, src, dstride * h );
457 return dst + dstride * h;
459 int lbytes = dstride <= sstride? dstride : sstride;
462 memcpy( dst, src, lbytes );
469 // copy one video frame into an HB buf. If the frame isn't in our color space
470 // or at least one of its dimensions is odd, use sws_scale to convert/rescale it.
471 // Otherwise just copy the bits.
472 static hb_buffer_t *copy_frame( hb_work_private_t *pv, AVFrame *frame )
474 AVCodecContext *context = pv->context;
478 // if the dimensions are odd, drop the lsb since h264 requires that
479 // both width and height be even.
480 w = ( context->width >> 1 ) << 1;
481 h = ( context->height >> 1 ) << 1;
485 w = pv->job->title->width;
486 h = pv->job->title->height;
488 hb_buffer_t *buf = hb_video_buffer_init( w, h );
489 uint8_t *dst = buf->data;
491 if ( context->pix_fmt != PIX_FMT_YUV420P || w != context->width ||
492 h != context->height )
494 // have to convert to our internal color space and/or rescale
496 avpicture_fill( &dstpic, dst, PIX_FMT_YUV420P, w, h );
498 if ( ! pv->sws_context )
500 pv->sws_context = sws_getContext( context->width, context->height, context->pix_fmt,
501 w, h, PIX_FMT_YUV420P,
502 SWS_LANCZOS|SWS_ACCURATE_RND,
505 sws_scale( pv->sws_context, frame->data, frame->linesize, 0, h,
506 dstpic.data, dstpic.linesize );
510 dst = copy_plane( dst, frame->data[0], w, frame->linesize[0], h );
511 w = (w + 1) >> 1; h = (h + 1) >> 1;
512 dst = copy_plane( dst, frame->data[1], w, frame->linesize[1], h );
513 dst = copy_plane( dst, frame->data[2], w, frame->linesize[2], h );
518 static int get_frame_buf( AVCodecContext *context, AVFrame *frame )
520 hb_work_private_t *pv = context->opaque;
521 frame->pts = pv->pts;
523 return avcodec_default_get_buffer( context, frame );
526 static int reget_frame_buf( AVCodecContext *context, AVFrame *frame )
528 hb_work_private_t *pv = context->opaque;
529 frame->pts = pv->pts;
531 return avcodec_default_reget_buffer( context, frame );
534 static void log_chapter( hb_work_private_t *pv, int chap_num, int64_t pts )
536 hb_chapter_t *c = hb_list_item( pv->job->title->list_chapter, chap_num - 1 );
539 hb_log( "%s: \"%s\" (%d) at frame %u time %"PRId64,
540 pv->context->codec->name, c->title, chap_num, pv->nframes, pts );
544 hb_log( "%s: Chapter %d at frame %u time %"PRId64,
545 pv->context->codec->name, chap_num, pv->nframes, pts );
549 static void flushDelayQueue( hb_work_private_t *pv )
552 int slot = pv->nframes & (HEAP_SIZE-1);
554 // flush all the video packets left on our timestamp-reordering delay q
555 while ( ( buf = pv->delayq[slot] ) != NULL )
557 buf->start = heap_pop( &pv->pts_heap );
558 hb_list_add( pv->list, buf );
559 pv->delayq[slot] = NULL;
560 slot = ( slot + 1 ) & (HEAP_SIZE-1);
565 * Decodes a video frame from the specified raw packet data ('data', 'size', 'sequence').
566 * The output of this function is stored in 'pv->list', which contains a list
567 * of zero or more decoded packets.
569 * The returned packets are guaranteed to have their timestamps in the correct order,
570 * even if the original packets decoded by libavcodec have misordered timestamps,
571 * due to the use of 'packed B-frames'.
573 * Internally the set of decoded packets may be buffered in 'pv->delayq'
574 * until enough packets have been decoded so that the timestamps can be
575 * correctly rewritten, if this is necessary.
577 static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size, int sequence )
579 int got_picture, oldlevel = 0;
583 if ( global_verbosity_level <= 1 )
585 oldlevel = av_log_get_level();
586 av_log_set_level( AV_LOG_QUIET );
589 av_init_packet( &avp );
592 if ( avcodec_decode_video2( pv->context, &frame, &got_picture, &avp ) < 0 )
596 if ( global_verbosity_level <= 1 )
598 av_log_set_level( oldlevel );
602 // ffmpeg makes it hard to attach a pts to a frame. if the MPEG ES
603 // packet had a pts we handed it to av_parser_parse (if the packet had
604 // no pts we set it to -1 but before the parse we can't distinguish between
605 // the start of a video frame with no pts & an intermediate packet of
606 // some frame which never has a pts). we hope that when parse returns
607 // the frame to us the pts we originally handed it will be in parser->pts.
608 // we put this pts into pv->pts so that when a avcodec_decode_video
609 // finally gets around to allocating an AVFrame to hold the decoded
610 // frame we can stuff that pts into the frame. if all of these relays
611 // worked at this point frame.pts should hold the frame's pts from the
612 // original data stream or -1 if it didn't have one. in the latter case
613 // we generate the next pts in sequence for it.
614 double frame_dur = pv->duration;
615 if ( frame_dur <= 0 )
617 frame_dur = 90000. * (double)pv->context->time_base.num /
618 (double)pv->context->time_base.den;
619 pv->duration = frame_dur;
621 if ( frame.repeat_pict )
623 frame_dur += frame.repeat_pict * frame_dur * 0.5;
625 // XXX Unlike every other video decoder, the Raw decoder doesn't
626 // use the standard buffer allocation routines so we never
627 // get to put a PTS in the frame. Do it now.
628 if ( pv->context->codec_id == CODEC_ID_RAWVIDEO )
633 // If there was no pts for this frame, assume constant frame rate
634 // video & estimate the next frame time from the last & duration.
635 double pts = frame.pts;
640 pv->pts_next = pts + frame_dur;
644 // if we're doing a scan or this content couldn't have been broken
645 // by Microsoft we don't worry about timestamp reordering
646 if ( ! pv->job || ! pv->brokenByMicrosoft )
648 buf = copy_frame( pv, &frame );
650 buf->sequence = sequence;
651 hb_list_add( pv->list, buf );
656 // XXX This following probably addresses a libavcodec bug but I don't
657 // see an easy fix so we workaround it here.
659 // The M$ 'packed B-frames' atrocity results in decoded frames with
660 // the wrong timestamp. E.g., if there are 2 b-frames the timestamps
661 // we see here will be "2 3 1 5 6 4 ..." instead of "1 2 3 4 5 6".
662 // The frames are actually delivered in the right order but with
663 // the wrong timestamp. To get the correct timestamp attached to
664 // each frame we have a delay queue (longer than the max number of
665 // b-frames) & a sorting heap for the timestamps. As each frame
666 // comes out of the decoder the oldest frame in the queue is removed
667 // and associated with the smallest timestamp. Then the new frame is
668 // added to the queue & its timestamp is pushed on the heap.
669 // This does nothing if the timestamps are correct (i.e., the video
670 // uses a codec that Micro$oft hasn't broken yet) but the frames
671 // get timestamped correctly even when M$ has munged them.
673 // remove the oldest picture from the frame queue (if any) &
674 // give it the smallest timestamp from our heap. The queue size
675 // is a power of two so we get the slot of the oldest by masking
676 // the frame count & this will become the slot of the newest
677 // once we've removed & processed the oldest.
678 int slot = pv->nframes & (HEAP_SIZE-1);
679 if ( ( buf = pv->delayq[slot] ) != NULL )
681 buf->start = heap_pop( &pv->pts_heap );
683 if ( pv->new_chap && buf->start >= pv->chap_time )
685 buf->new_chap = pv->new_chap;
688 log_chapter( pv, buf->new_chap, buf->start );
690 else if ( pv->nframes == 0 )
692 log_chapter( pv, pv->job->chapter_start, buf->start );
694 hb_list_add( pv->list, buf );
697 // add the new frame to the delayq & push its timestamp on the heap
698 buf = copy_frame( pv, &frame );
699 buf->sequence = sequence;
700 pv->delayq[slot] = buf;
701 heap_push( &pv->pts_heap, pts );
709 static void decodeVideo( hb_work_private_t *pv, uint8_t *data, int size, int sequence,
710 int64_t pts, int64_t dts )
713 * The following loop is a do..while because we need to handle both
714 * data & the flush at the end (signaled by size=0). At the end there's
715 * generally a frame in the parser & one or more frames in the decoder
716 * (depending on the bframes setting).
722 int len = av_parser_parse2( pv->parser, pv->context, &pout, &pout_len,
723 data + pos, size - pos, pts, dts, AV_NOPTS_VALUE );
728 pv->pts = pv->parser->pts;
729 decodeFrame( pv, pout, pout_len, sequence );
731 } while ( pos < size );
733 /* the stuff above flushed the parser, now flush the decoder */
736 while ( decodeFrame( pv, NULL, 0, sequence ) )
739 flushDelayQueue( pv );
744 * Removes all packets from 'pv->list', links them together into
745 * a linked-list, and returns the first packet in the list.
747 static hb_buffer_t *link_buf_list( hb_work_private_t *pv )
749 hb_buffer_t *head = hb_list_item( pv->list, 0 );
753 hb_list_rem( pv->list, head );
755 hb_buffer_t *last = head, *buf;
757 while ( ( buf = hb_list_item( pv->list, 0 ) ) != NULL )
759 hb_list_rem( pv->list, buf );
768 static int decavcodecvInit( hb_work_object_t * w, hb_job_t * job )
771 hb_work_private_t *pv = calloc( 1, sizeof( hb_work_private_t ) );
772 w->private_data = pv;
774 pv->list = hb_list_init();
776 int codec_id = w->codec_param;
777 pv->parser = av_parser_init( codec_id );
778 pv->context = avcodec_alloc_context2( CODEC_TYPE_VIDEO );
780 /* we have to wrap ffmpeg's get_buffer to be able to set the pts (?!) */
781 pv->context->opaque = pv;
782 pv->context->get_buffer = get_frame_buf;
783 pv->context->reget_buffer = reget_frame_buf;
788 static int next_hdr( hb_buffer_t *in, int offset )
790 uint8_t *dat = in->data;
791 uint16_t last2 = 0xffff;
792 for ( ; in->size - offset > 1; ++offset )
794 if ( last2 == 0 && dat[offset] == 0x01 )
795 // found an mpeg start code
798 last2 = ( last2 << 8 ) | dat[offset];
804 static int find_hdr( hb_buffer_t *in, int offset, uint8_t hdr_type )
806 if ( in->size - offset < 4 )
807 // not enough room for an mpeg start code
810 for ( ; ( offset = next_hdr( in, offset ) ) >= 0; ++offset )
812 if ( in->data[offset+3] == hdr_type )
819 static int setup_extradata( hb_work_object_t *w, hb_buffer_t *in )
821 hb_work_private_t *pv = w->private_data;
823 // we can't call the avstream funcs but the read_header func in the
824 // AVInputFormat may set up some state in the AVContext. In particular
825 // vc1t_read_header allocates 'extradata' to deal with header issues
826 // related to Microsoft's bizarre engineering notions. We alloc a chunk
827 // of space to make vc1 work then associate the codec with the context.
828 if ( w->codec_param != CODEC_ID_VC1 )
830 // we haven't been inflicted with M$ - allocate a little space as
831 // a marker and return success.
832 pv->context->extradata_size = 16;
833 pv->context->extradata = av_malloc(pv->context->extradata_size);
837 // find the start and and of the sequence header
839 if ( ( shdr = find_hdr( in, 0, 0x0f ) ) < 0 )
841 // didn't find start of seq hdr
844 if ( ( shdr_end = next_hdr( in, shdr + 4 ) ) < 0 )
850 // find the start and and of the entry point header
852 if ( ( ehdr = find_hdr( in, 0, 0x0e ) ) < 0 )
854 // didn't find start of entry point hdr
857 if ( ( ehdr_end = next_hdr( in, ehdr + 4 ) ) < 0 )
863 // found both headers - allocate an extradata big enough to hold both
864 // then copy them into it.
865 pv->context->extradata_size = shdr_end + ehdr_end;
866 pv->context->extradata = av_malloc(pv->context->extradata_size + 8);
867 memcpy( pv->context->extradata, in->data + shdr, shdr_end );
868 memcpy( pv->context->extradata + shdr_end, in->data + ehdr, ehdr_end );
869 memset( pv->context->extradata + shdr_end + ehdr_end, 0, 8);
873 static int decavcodecvWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
874 hb_buffer_t ** buf_out )
876 hb_work_private_t *pv = w->private_data;
877 hb_buffer_t *in = *buf_in;
878 int64_t pts = AV_NOPTS_VALUE;
883 /* if we got an empty buffer signaling end-of-stream send it downstream */
886 decodeVideo( pv, in->data, in->size, in->sequence, pts, dts );
887 hb_list_add( pv->list, in );
888 *buf_out = link_buf_list( pv );
892 // if this is the first frame open the codec (we have to wait for the
893 // first frame because of M$ VC1 braindamage).
894 if ( pv->context->extradata_size == 0 )
896 if ( setup_extradata( w, in ) )
898 // we didn't find the headers needed to set up extradata.
899 // the codec will abort if we open it so just free the buf
900 // and hope we eventually get the info we need.
901 hb_buffer_close( &in );
904 AVCodec *codec = avcodec_find_decoder( w->codec_param );
905 // There's a mis-feature in ffmpeg that causes the context to be
906 // incorrectly initialized the 1st time avcodec_open is called.
907 // If you close it and open a 2nd time, it finishes the job.
908 hb_avcodec_open( pv->context, codec );
909 hb_avcodec_close( pv->context );
910 hb_avcodec_open( pv->context, codec );
916 dts = in->renderOffset;
920 pv->new_chap = in->new_chap;
921 pv->chap_time = pts >= 0? pts : pv->pts_next;
923 decodeVideo( pv, in->data, in->size, in->sequence, pts, dts );
924 hb_buffer_close( &in );
925 *buf_out = link_buf_list( pv );
929 static int decavcodecvInfo( hb_work_object_t *w, hb_work_info_t *info )
931 hb_work_private_t *pv = w->private_data;
933 memset( info, 0, sizeof(*info) );
935 if ( pv && pv->context )
937 AVCodecContext *context = pv->context;
938 info->bitrate = context->bit_rate;
939 info->width = context->width;
940 info->height = context->height;
942 /* ffmpeg gives the frame rate in frames per second while HB wants
943 * it in units of the 27MHz MPEG clock. */
944 info->rate = 27000000;
945 info->rate_base = (int64_t)context->time_base.num * 27000000LL /
946 context->time_base.den;
947 if ( context->ticks_per_frame > 1 )
949 // for ffmpeg 0.5 & later, the H.264 & MPEG-2 time base is
950 // field rate rather than frame rate so convert back to frames.
951 info->rate_base *= context->ticks_per_frame;
954 info->pixel_aspect_width = context->sample_aspect_ratio.num;
955 info->pixel_aspect_height = context->sample_aspect_ratio.den;
957 /* Sometimes there's no pixel aspect set in the source ffmpeg context
958 * which appears to come from the video stream. In that case,
959 * try the pixel aspect in AVStream (which appears to come from
960 * the container). Else assume a 1:1 PAR. */
961 if ( info->pixel_aspect_width == 0 ||
962 info->pixel_aspect_height == 0 )
964 AVStream *st = hb_ffmpeg_avstream( w->codec_param );
965 info->pixel_aspect_width = st->sample_aspect_ratio.num ?
966 st->sample_aspect_ratio.num : 1;
967 info->pixel_aspect_height = st->sample_aspect_ratio.den ?
968 st->sample_aspect_ratio.den : 1;
970 /* ffmpeg returns the Pixel Aspect Ratio (PAR). Handbrake wants the
971 * Display Aspect Ratio so we convert by scaling by the Storage
972 * Aspect Ratio (w/h). We do the calc in floating point to get the
974 info->aspect = (double)info->pixel_aspect_width *
975 (double)context->width /
976 (double)info->pixel_aspect_height /
977 (double)context->height;
979 info->profile = context->profile;
980 info->level = context->level;
981 info->name = context->codec->name;
987 static int decavcodecvBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
988 hb_work_info_t *info )
993 hb_work_object_t hb_decavcodecv =
996 "Video decoder (libavcodec)",
1005 // This is a special decoder for ffmpeg streams. The ffmpeg stream reader
1006 // includes a parser and passes information from the parser to the decoder
1007 // via a codec context kept in the AVStream of the reader's AVFormatContext.
1008 // We *have* to use that codec context to decode the stream or we'll get
1009 // garbage. ffmpeg_title_scan put a cookie that can be used to get to that
1010 // codec context in our codec_param.
1012 // this routine gets the appropriate context pointer from the ffmpeg
1013 // stream reader. it can't be called until we get the first buffer because
1014 // we can't guarantee that reader will be called before the our init
1015 // routine and if our init is called first we'll get a pointer to the
1016 // old scan stream (which has already been closed).
1017 static void init_ffmpeg_context( hb_work_object_t *w )
1019 hb_work_private_t *pv = w->private_data;
1020 pv->context = hb_ffmpeg_context( w->codec_param );
1022 // during scan the decoder gets closed & reopened which will
1023 // close the codec so reopen it if it's not there
1024 if ( ! pv->context->codec )
1026 AVCodec *codec = avcodec_find_decoder( pv->context->codec_id );
1027 hb_avcodec_open( pv->context, codec );
1029 // set up our best guess at the frame duration.
1030 // the frame rate in the codec is usually bogus but it's sometimes
1031 // ok in the stream.
1032 AVStream *st = hb_ffmpeg_avstream( w->codec_param );
1034 if ( st->nb_frames && st->duration )
1036 // compute the average frame duration from the total number
1037 // of frames & the total duration.
1038 pv->duration = ( (double)st->duration * (double)st->time_base.num ) /
1039 ( (double)st->nb_frames * (double)st->time_base.den );
1043 // XXX We don't have a frame count or duration so try to use the
1044 // far less reliable time base info in the stream.
1045 // Because the time bases are so screwed up, we only take values
1046 // in the range 8fps - 64fps.
1048 if ( st->avg_frame_rate.den * 64 > st->avg_frame_rate.num &&
1049 st->avg_frame_rate.num > st->avg_frame_rate.den * 8 )
1051 tb.num = st->avg_frame_rate.den;
1052 tb.den = st->avg_frame_rate.num;
1054 else if ( st->time_base.num * 64 > st->time_base.den &&
1055 st->time_base.den > st->time_base.num * 8 )
1059 else if ( st->r_frame_rate.den * 64 > st->r_frame_rate.num &&
1060 st->r_frame_rate.num > st->r_frame_rate.den * 8 )
1062 tb.num = st->r_frame_rate.den;
1063 tb.den = st->r_frame_rate.num;
1067 tb.num = 1001; /*XXX*/
1068 tb.den = 24000; /*XXX*/
1070 pv->duration = (double)tb.num / (double)tb.den;
1072 pv->duration *= 90000.;
1074 // we have to wrap ffmpeg's get_buffer to be able to set the pts (?!)
1075 pv->context->opaque = pv;
1076 pv->context->get_buffer = get_frame_buf;
1077 pv->context->reget_buffer = reget_frame_buf;
1079 // avi, mkv and possibly mp4 containers can contain the M$ VFW packed
1080 // b-frames abortion that messes up frame ordering and timestamps.
1081 // XXX ffmpeg knows which streams are broken but doesn't expose the
1082 // info externally. We should patch ffmpeg to add a flag to the
1083 // codec context for this but until then we mark all ffmpeg streams
1085 pv->brokenByMicrosoft = 1;
1088 static void prepare_ffmpeg_buffer( hb_buffer_t * in )
1090 // ffmpeg requires an extra 8 bytes of zero at the end of the buffer and
1091 // will seg fault in odd, data dependent ways if it's not there. (my guess
1092 // is this is a case of a local performance optimization creating a global
1093 // performance degradation since all the time wasted by extraneous data
1094 // copies & memory zeroing has to be huge compared to the minor reduction
1095 // in inner-loop instructions this affords - modern cpus bottleneck on
1096 // memory bandwidth not instruction bandwidth).
1097 if ( in->size + FF_INPUT_BUFFER_PADDING_SIZE > in->alloc )
1099 // have to realloc to add the padding
1100 hb_buffer_realloc( in, in->size + FF_INPUT_BUFFER_PADDING_SIZE );
1102 memset( in->data + in->size, 0, FF_INPUT_BUFFER_PADDING_SIZE );
1105 static int decavcodecviInit( hb_work_object_t * w, hb_job_t * job )
1108 hb_work_private_t *pv = calloc( 1, sizeof( hb_work_private_t ) );
1109 w->private_data = pv;
1111 pv->list = hb_list_init();
1115 if ( w->audio != NULL &&
1116 hb_need_downmix( w->audio->config.in.channel_layout,
1117 w->audio->config.out.mixdown) )
1119 pv->downmix = hb_downmix_init(w->audio->config.in.channel_layout,
1120 w->audio->config.out.mixdown);
1121 hb_downmix_set_chan_map( pv->downmix, &hb_smpte_chan_map, &hb_qt_chan_map );
1127 static int decavcodecviWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
1128 hb_buffer_t ** buf_out )
1130 hb_work_private_t *pv = w->private_data;
1131 hb_buffer_t *in = *buf_in;
1134 /* if we got an empty buffer signaling end-of-stream send it downstream */
1135 if ( in->size == 0 )
1137 /* flush any frames left in the decoder */
1138 while ( pv->context && decodeFrame( pv, NULL, 0, in->sequence ) )
1141 flushDelayQueue( pv );
1142 hb_list_add( pv->list, in );
1143 *buf_out = link_buf_list( pv );
1144 return HB_WORK_DONE;
1147 if ( ! pv->context )
1149 init_ffmpeg_context( w );
1152 int64_t pts = in->start;
1155 // use the first timestamp as our 'next expected' pts
1156 if ( pv->pts_next < 0 )
1165 pv->new_chap = in->new_chap;
1166 pv->chap_time = pts >= 0? pts : pv->pts_next;
1168 prepare_ffmpeg_buffer( in );
1169 decodeFrame( pv, in->data, in->size, in->sequence );
1170 hb_buffer_close( &in );
1171 *buf_out = link_buf_list( pv );
1175 static int decavcodecviInfo( hb_work_object_t *w, hb_work_info_t *info )
1177 if ( decavcodecvInfo( w, info ) )
1179 hb_work_private_t *pv = w->private_data;
1180 if ( ! pv->context )
1182 init_ffmpeg_context( w );
1184 // we have the frame duration in units of the 90KHz pts clock but
1185 // need it in units of the 27MHz MPEG clock. */
1186 info->rate = 27000000;
1187 info->rate_base = pv->duration * 300.;
1193 static void decodeAudio( hb_audio_t * audio, hb_work_private_t *pv, uint8_t *data, int size )
1195 AVCodecContext *context = pv->context;
1197 int loop_limit = 256;
1199 while ( pos < size )
1201 int16_t *buffer = pv->buffer;
1202 if ( buffer == NULL )
1204 pv->buffer = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
1205 buffer = pv->buffer;
1209 av_init_packet( &avp );
1210 avp.data = data + pos;
1211 avp.size = size - pos;
1213 int out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
1215 int len = avcodec_decode_audio3( context, buffer, &out_size, &avp );
1222 if ( !(loop_limit--) )
1231 // We require signed 16-bit ints for the output format. If
1232 // we got something different convert it.
1233 if ( context->sample_fmt != SAMPLE_FMT_S16 )
1235 // Note: av_audio_convert seems to be a work-in-progress but
1236 // looks like it will eventually handle general audio
1237 // mixdowns which would allow us much more flexibility
1238 // in handling multichannel audio in HB. If we were doing
1239 // anything more complicated than a one-for-one format
1240 // conversion we'd probably want to cache the converter
1241 // context in the pv.
1242 int isamp = av_get_bits_per_sample_format( context->sample_fmt ) / 8;
1243 AVAudioConvert *ctx = av_audio_convert_alloc( SAMPLE_FMT_S16, 1,
1244 context->sample_fmt, 1,
1246 // get output buffer size (in 2-byte samples) then malloc a buffer
1247 nsamples = out_size / isamp;
1248 buffer = av_malloc( nsamples * 2 );
1250 // we're doing straight sample format conversion which behaves as if
1251 // there were only one channel.
1252 const void * const ibuf[6] = { pv->buffer };
1253 void * const obuf[6] = { buffer };
1254 const int istride[6] = { isamp };
1255 const int ostride[6] = { 2 };
1257 av_audio_convert( ctx, obuf, ostride, ibuf, istride, nsamples );
1258 av_audio_convert_free( ctx );
1262 nsamples = out_size / 2;
1269 pv->downmix_buffer = realloc(pv->downmix_buffer, nsamples * sizeof(hb_sample_t));
1272 for( i = 0; i < nsamples; ++i )
1274 pv->downmix_buffer[i] = buffer[i];
1277 int n_ch_samples = nsamples / context->channels;
1278 int channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown);
1280 buf = hb_buffer_init( n_ch_samples * channels * sizeof(float) );
1281 hb_sample_t *samples = (hb_sample_t *)buf->data;
1282 hb_downmix(pv->downmix, samples, pv->downmix_buffer, n_ch_samples);
1286 buf = hb_buffer_init( nsamples * sizeof(float) );
1287 float *fl32 = (float *)buf->data;
1289 for( i = 0; i < nsamples; ++i )
1291 fl32[i] = buffer[i];
1293 int n_ch_samples = nsamples / context->channels;
1294 hb_layout_remap( &hb_smpte_chan_map, &hb_qt_chan_map,
1295 audio->config.in.channel_layout,
1296 fl32, n_ch_samples );
1299 double pts = pv->pts_next;
1301 pts += nsamples * pv->duration;
1305 hb_list_add( pv->list, buf );
1307 // if we allocated a buffer for sample format conversion, free it
1308 if ( buffer != pv->buffer )
1316 static int decavcodecaiWork( hb_work_object_t *w, hb_buffer_t **buf_in,
1317 hb_buffer_t **buf_out )
1319 if ( (*buf_in)->size <= 0 )
1321 /* EOF on input stream - send it downstream & say that we're done */
1324 return HB_WORK_DONE;
1327 hb_work_private_t *pv = w->private_data;
1329 if ( (*buf_in)->start < -1 && pv->pts_next <= 0 )
1331 // discard buffers that start before video time 0
1336 if ( ! pv->context )
1338 init_ffmpeg_context( w );
1339 // duration is a scaling factor to go from #bytes in the decoded
1340 // frame to frame time (in 90KHz mpeg ticks). 'channels' converts
1341 // total samples to per-channel samples. 'sample_rate' converts
1342 // per-channel samples to seconds per sample and the 90000
1343 // is mpeg ticks per second.
1344 pv->duration = 90000. /
1345 (double)( pv->context->sample_rate * pv->context->channels );
1347 hb_buffer_t *in = *buf_in;
1349 // if the packet has a timestamp use it if we don't have a timestamp yet
1350 // or if there's been a timing discontinuity of more than 100ms.
1351 if ( in->start >= 0 &&
1352 ( pv->pts_next < 0 || ( in->start - pv->pts_next ) > 90*100 ) )
1354 pv->pts_next = in->start;
1356 prepare_ffmpeg_buffer( in );
1357 decodeAudio( w->audio, pv, in->data, in->size );
1358 *buf_out = link_buf_list( pv );
1363 hb_work_object_t hb_decavcodecvi =
1366 "Video decoder (ffmpeg streams)",
1374 hb_work_object_t hb_decavcodecai =
1377 "Audio decoder (ffmpeg streams)",