+ if ( in->start < -1 && pv->pts_next <= 0 )
+ {
+ // discard buffers that start before video time 0
+ return HB_WORK_OK;
+ }
+
+ // if the packet has a timestamp use it
+ if ( in->start != -1 )
+ {
+ pv->pts_next = in->start;
+ }
+
+ int pos, len;
+ for ( pos = 0; pos < in->size; pos += len )
+ {
+ uint8_t *parser_output_buffer;
+ int parser_output_buffer_len;
+ int64_t cur = pv->pts_next;
+
+ if ( pv->parser != NULL )
+ {
+ len = av_parser_parse2( pv->parser, pv->context,
+ &parser_output_buffer, &parser_output_buffer_len,
+ in->data + pos, in->size - pos, cur, cur, AV_NOPTS_VALUE );
+ }
+ else
+ {
+ parser_output_buffer = in->data;
+ len = parser_output_buffer_len = in->size;
+ }
+ if (parser_output_buffer_len)
+ {
+ // set the duration on every frame since the stream format can
+ // change (it shouldn't but there's no way to guarantee it).
+ // duration is a scaling factor to go from #bytes in the decoded
+ // frame to frame time (in 90KHz mpeg ticks). 'channels' converts
+ // total samples to per-channel samples. 'sample_rate' converts
+ // per-channel samples to seconds per sample and the 90000
+ // is mpeg ticks per second.
+ if ( pv->context->sample_rate && pv->context->channels )
+ {
+ pv->duration = 90000. /
+ (double)( pv->context->sample_rate * pv->context->channels );
+ }
+ decodeAudio( w->audio, pv, parser_output_buffer, parser_output_buffer_len );
+ }
+ }
+ *buf_out = link_buf_list( pv );
+ return HB_WORK_OK;
+}
+
+static int decavcodecInfo( hb_work_object_t *w, hb_work_info_t *info )
+{
+ hb_work_private_t *pv = w->private_data;
+
+ memset( info, 0, sizeof(*info) );
+
+ if ( pv && pv->context )
+ {
+ AVCodecContext *context = pv->context;
+ info->bitrate = context->bit_rate;
+ info->rate = context->time_base.num;
+ info->rate_base = context->time_base.den;
+ info->profile = context->profile;
+ info->level = context->level;
+ return 1;
+ }
+ return 0;
+}
+
+static int decavcodecBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
+ hb_work_info_t *info )
+{
+ hb_work_private_t *pv = w->private_data;
+ int ret = 0;
+
+ memset( info, 0, sizeof(*info) );
+
+ if ( pv && pv->context )
+ {
+ return decavcodecInfo( w, info );
+ }
+ // XXX
+ // We should parse the bitstream to find its parameters but for right
+ // now we just return dummy values if there's a codec that will handle it.
+ AVCodec *codec = avcodec_find_decoder( w->codec_param? w->codec_param :
+ CODEC_ID_MP2 );
+ if ( ! codec )
+ {
+ // there's no ffmpeg codec for this audio type - give up
+ return -1;
+ }
+
+ static char codec_name[64];
+ info->name = strncpy( codec_name, codec->name, sizeof(codec_name)-1 );
+
+ AVCodecParserContext *parser = av_parser_init( codec->id );
+ AVCodecContext *context = avcodec_alloc_context();
+ hb_avcodec_open( context, codec );
+ uint8_t *buffer = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
+ int out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
+ unsigned char *pbuffer;
+ int pos, pbuffer_size;
+
+ while ( buf && !ret )
+ {
+ pos = 0;
+ while ( pos < buf->size )
+ {
+ int len;
+
+ if (parser != NULL )
+ {
+ len = av_parser_parse2( parser, context, &pbuffer,
+ &pbuffer_size, buf->data + pos,
+ buf->size - pos, buf->start,
+ buf->start, AV_NOPTS_VALUE );
+ }
+ else
+ {
+ pbuffer = buf->data;
+ len = pbuffer_size = buf->size;
+ }
+ pos += len;
+ if ( pbuffer_size > 0 )
+ {
+ AVPacket avp;
+ av_init_packet( &avp );
+ avp.data = pbuffer;
+ avp.size = pbuffer_size;
+
+ len = avcodec_decode_audio3( context, (int16_t*)buffer,
+ &out_size, &avp );
+ if ( len > 0 && context->sample_rate > 0 )
+ {
+ info->bitrate = context->bit_rate;
+ info->rate = context->sample_rate;
+ info->rate_base = 1;
+ info->channel_layout =
+ hb_ff_layout_xlat(context->channel_layout,
+ context->channels);
+ ret = 1;
+ break;
+ }
+ }
+ }
+ buf = buf->next;
+ }
+
+ av_free( buffer );
+ if ( parser != NULL )
+ av_parser_close( parser );
+ hb_avcodec_close( context );
+ return ret;
+}
+
+/* -------------------------------------------------------------
+ * General purpose video decoder using libavcodec
+ */
+
+static uint8_t *copy_plane( uint8_t *dst, uint8_t* src, int dstride, int sstride,
+ int h )
+{
+ if ( dstride == sstride )
+ {
+ memcpy( dst, src, dstride * h );
+ return dst + dstride * h;
+ }
+ int lbytes = dstride <= sstride? dstride : sstride;
+ while ( --h >= 0 )
+ {
+ memcpy( dst, src, lbytes );
+ src += sstride;
+ dst += dstride;
+ }
+ return dst;
+}
+
+// copy one video frame into an HB buf. If the frame isn't in our color space
+// or at least one of its dimensions is odd, use sws_scale to convert/rescale it.
+// Otherwise just copy the bits.
+static hb_buffer_t *copy_frame( hb_work_private_t *pv, AVFrame *frame )
+{
+ AVCodecContext *context = pv->context;
+ int w, h;
+ if ( ! pv->job )
+ {
+ // if the dimensions are odd, drop the lsb since h264 requires that
+ // both width and height be even.
+ w = ( context->width >> 1 ) << 1;
+ h = ( context->height >> 1 ) << 1;
+ }
+ else
+ {
+ w = pv->job->title->width;
+ h = pv->job->title->height;
+ }
+ hb_buffer_t *buf = hb_video_buffer_init( w, h );
+ uint8_t *dst = buf->data;
+
+ if ( context->pix_fmt != PIX_FMT_YUV420P || w != context->width ||
+ h != context->height )
+ {
+ // have to convert to our internal color space and/or rescale
+ AVPicture dstpic;
+ avpicture_fill( &dstpic, dst, PIX_FMT_YUV420P, w, h );
+
+ if ( ! pv->sws_context )
+ {
+ pv->sws_context = sws_getContext( context->width, context->height, context->pix_fmt,
+ w, h, PIX_FMT_YUV420P,
+ SWS_LANCZOS|SWS_ACCURATE_RND,
+ NULL, NULL, NULL );
+ }
+ sws_scale( pv->sws_context, frame->data, frame->linesize, 0, h,
+ dstpic.data, dstpic.linesize );
+ }
+ else
+ {
+ dst = copy_plane( dst, frame->data[0], w, frame->linesize[0], h );
+ w = (w + 1) >> 1; h = (h + 1) >> 1;
+ dst = copy_plane( dst, frame->data[1], w, frame->linesize[1], h );
+ dst = copy_plane( dst, frame->data[2], w, frame->linesize[2], h );
+ }
+ return buf;
+}
+
+static int get_frame_buf( AVCodecContext *context, AVFrame *frame )
+{
+ hb_work_private_t *pv = context->opaque;
+ frame->pts = pv->pts;
+ pv->pts = -1;
+ return avcodec_default_get_buffer( context, frame );
+}
+
+static int reget_frame_buf( AVCodecContext *context, AVFrame *frame )
+{
+ hb_work_private_t *pv = context->opaque;
+ frame->pts = pv->pts;
+ pv->pts = -1;
+ return avcodec_default_reget_buffer( context, frame );
+}
+
+static void log_chapter( hb_work_private_t *pv, int chap_num, int64_t pts )
+{
+ hb_chapter_t *c = hb_list_item( pv->job->title->list_chapter, chap_num - 1 );
+ if ( c && c->title )
+ {
+ hb_log( "%s: \"%s\" (%d) at frame %u time %"PRId64,
+ pv->context->codec->name, c->title, chap_num, pv->nframes, pts );
+ }
+ else
+ {
+ hb_log( "%s: Chapter %d at frame %u time %"PRId64,
+ pv->context->codec->name, chap_num, pv->nframes, pts );
+ }
+}
+
+static void flushDelayQueue( hb_work_private_t *pv )
+{
+ hb_buffer_t *buf;
+ int slot = pv->nframes & (HEAP_SIZE-1);
+
+ // flush all the video packets left on our timestamp-reordering delay q
+ while ( ( buf = pv->delayq[slot] ) != NULL )
+ {
+ buf->start = heap_pop( &pv->pts_heap );
+ hb_list_add( pv->list, buf );
+ pv->delayq[slot] = NULL;
+ slot = ( slot + 1 ) & (HEAP_SIZE-1);
+ }
+}
+
+/*
+ * Decodes a video frame from the specified raw packet data ('data', 'size', 'sequence').
+ * The output of this function is stored in 'pv->list', which contains a list
+ * of zero or more decoded packets.
+ *
+ * The returned packets are guaranteed to have their timestamps in the correct order,
+ * even if the original packets decoded by libavcodec have misordered timestamps,
+ * due to the use of 'packed B-frames'.
+ *
+ * Internally the set of decoded packets may be buffered in 'pv->delayq'
+ * until enough packets have been decoded so that the timestamps can be
+ * correctly rewritten, if this is necessary.
+ */
+static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size, int sequence )
+{
+ int got_picture, oldlevel = 0;
+ AVFrame frame;
+ AVPacket avp;
+
+ if ( global_verbosity_level <= 1 )
+ {
+ oldlevel = av_log_get_level();
+ av_log_set_level( AV_LOG_QUIET );
+ }
+
+ av_init_packet( &avp );
+ avp.data = data;
+ avp.size = size;
+ if ( avcodec_decode_video2( pv->context, &frame, &got_picture, &avp ) < 0 )
+ {
+ ++pv->decode_errors;
+ }
+ if ( global_verbosity_level <= 1 )
+ {
+ av_log_set_level( oldlevel );
+ }
+ if( got_picture )
+ {
+ // ffmpeg makes it hard to attach a pts to a frame. if the MPEG ES
+ // packet had a pts we handed it to av_parser_parse (if the packet had
+ // no pts we set it to -1 but before the parse we can't distinguish between
+ // the start of a video frame with no pts & an intermediate packet of
+ // some frame which never has a pts). we hope that when parse returns
+ // the frame to us the pts we originally handed it will be in parser->pts.
+ // we put this pts into pv->pts so that when a avcodec_decode_video
+ // finally gets around to allocating an AVFrame to hold the decoded
+ // frame we can stuff that pts into the frame. if all of these relays
+ // worked at this point frame.pts should hold the frame's pts from the
+ // original data stream or -1 if it didn't have one. in the latter case
+ // we generate the next pts in sequence for it.
+ double frame_dur = pv->duration;
+ if ( frame_dur <= 0 )
+ {
+ frame_dur = 90000. * (double)pv->context->time_base.num /
+ (double)pv->context->time_base.den;
+ pv->duration = frame_dur;
+ }
+ if ( frame.repeat_pict )
+ {
+ frame_dur += frame.repeat_pict * frame_dur * 0.5;
+ }
+ // XXX Unlike every other video decoder, the Raw decoder doesn't
+ // use the standard buffer allocation routines so we never
+ // get to put a PTS in the frame. Do it now.
+ if ( pv->context->codec_id == CODEC_ID_RAWVIDEO )
+ {
+ frame.pts = pv->pts;
+ pv->pts = -1;
+ }
+ // If there was no pts for this frame, assume constant frame rate
+ // video & estimate the next frame time from the last & duration.
+ double pts = frame.pts;
+ if ( pts < 0 )
+ {
+ pts = pv->pts_next;
+ }
+ pv->pts_next = pts + frame_dur;
+
+ hb_buffer_t *buf;
+
+ // if we're doing a scan or this content couldn't have been broken
+ // by Microsoft we don't worry about timestamp reordering
+ if ( ! pv->job || ! pv->brokenByMicrosoft )
+ {
+ buf = copy_frame( pv, &frame );
+ buf->start = pts;
+ buf->sequence = sequence;
+ hb_list_add( pv->list, buf );
+ ++pv->nframes;
+ return got_picture;
+ }
+
+ // XXX This following probably addresses a libavcodec bug but I don't
+ // see an easy fix so we workaround it here.
+ //
+ // The M$ 'packed B-frames' atrocity results in decoded frames with
+ // the wrong timestamp. E.g., if there are 2 b-frames the timestamps
+ // we see here will be "2 3 1 5 6 4 ..." instead of "1 2 3 4 5 6".
+ // The frames are actually delivered in the right order but with
+ // the wrong timestamp. To get the correct timestamp attached to
+ // each frame we have a delay queue (longer than the max number of
+ // b-frames) & a sorting heap for the timestamps. As each frame
+ // comes out of the decoder the oldest frame in the queue is removed
+ // and associated with the smallest timestamp. Then the new frame is
+ // added to the queue & its timestamp is pushed on the heap.
+ // This does nothing if the timestamps are correct (i.e., the video
+ // uses a codec that Micro$oft hasn't broken yet) but the frames
+ // get timestamped correctly even when M$ has munged them.
+
+ // remove the oldest picture from the frame queue (if any) &
+ // give it the smallest timestamp from our heap. The queue size
+ // is a power of two so we get the slot of the oldest by masking
+ // the frame count & this will become the slot of the newest
+ // once we've removed & processed the oldest.
+ int slot = pv->nframes & (HEAP_SIZE-1);
+ if ( ( buf = pv->delayq[slot] ) != NULL )
+ {
+ buf->start = heap_pop( &pv->pts_heap );
+
+ if ( pv->new_chap && buf->start >= pv->chap_time )
+ {
+ buf->new_chap = pv->new_chap;
+ pv->new_chap = 0;
+ pv->chap_time = 0;
+ log_chapter( pv, buf->new_chap, buf->start );
+ }
+ else if ( pv->nframes == 0 )
+ {
+ log_chapter( pv, pv->job->chapter_start, buf->start );
+ }
+ hb_list_add( pv->list, buf );
+ }
+
+ // add the new frame to the delayq & push its timestamp on the heap
+ buf = copy_frame( pv, &frame );
+ buf->sequence = sequence;
+ pv->delayq[slot] = buf;
+ heap_push( &pv->pts_heap, pts );
+
+ ++pv->nframes;
+ }
+
+ return got_picture;
+}
+
+static void decodeVideo( hb_work_private_t *pv, uint8_t *data, int size, int sequence,
+ int64_t pts, int64_t dts )
+{
+ /*
+ * The following loop is a do..while because we need to handle both
+ * data & the flush at the end (signaled by size=0). At the end there's
+ * generally a frame in the parser & one or more frames in the decoder
+ * (depending on the bframes setting).
+ */
+ int pos = 0;
+ do {
+ uint8_t *pout;
+ int pout_len;
+ int len = av_parser_parse2( pv->parser, pv->context, &pout, &pout_len,
+ data + pos, size - pos, pts, dts, AV_NOPTS_VALUE );
+ pos += len;
+
+ if ( pout_len > 0 )
+ {
+ pv->pts = pv->parser->pts;
+ decodeFrame( pv, pout, pout_len, sequence );
+ }
+ } while ( pos < size );
+
+ /* the stuff above flushed the parser, now flush the decoder */
+ if ( size <= 0 )
+ {
+ while ( decodeFrame( pv, NULL, 0, sequence ) )
+ {
+ }
+ flushDelayQueue( pv );
+ }
+}
+
+/*
+ * Removes all packets from 'pv->list', links them together into
+ * a linked-list, and returns the first packet in the list.
+ */
+static hb_buffer_t *link_buf_list( hb_work_private_t *pv )
+{
+ hb_buffer_t *head = hb_list_item( pv->list, 0 );
+
+ if ( head )
+ {
+ hb_list_rem( pv->list, head );
+
+ hb_buffer_t *last = head, *buf;
+
+ while ( ( buf = hb_list_item( pv->list, 0 ) ) != NULL )
+ {
+ hb_list_rem( pv->list, buf );
+ last->next = buf;
+ last = buf;
+ }
+ }
+ return head;
+}
+
+
+static int decavcodecvInit( hb_work_object_t * w, hb_job_t * job )
+{
+
+ hb_work_private_t *pv = calloc( 1, sizeof( hb_work_private_t ) );
+ w->private_data = pv;
+ pv->job = job;
+ pv->list = hb_list_init();
+
+ int codec_id = w->codec_param;
+ pv->parser = av_parser_init( codec_id );
+ pv->context = avcodec_alloc_context2( CODEC_TYPE_VIDEO );
+
+ /* we have to wrap ffmpeg's get_buffer to be able to set the pts (?!) */
+ pv->context->opaque = pv;
+ pv->context->get_buffer = get_frame_buf;
+ pv->context->reget_buffer = reget_frame_buf;
+
+ return 0;
+}
+
+static int next_hdr( hb_buffer_t *in, int offset )
+{
+ uint8_t *dat = in->data;
+ uint16_t last2 = 0xffff;
+ for ( ; in->size - offset > 1; ++offset )
+ {
+ if ( last2 == 0 && dat[offset] == 0x01 )
+ // found an mpeg start code
+ return offset - 2;
+
+ last2 = ( last2 << 8 ) | dat[offset];
+ }
+
+ return -1;
+}
+
+static int find_hdr( hb_buffer_t *in, int offset, uint8_t hdr_type )
+{
+ if ( in->size - offset < 4 )
+ // not enough room for an mpeg start code
+ return -1;
+
+ for ( ; ( offset = next_hdr( in, offset ) ) >= 0; ++offset )
+ {
+ if ( in->data[offset+3] == hdr_type )
+ // found it
+ break;
+ }
+ return offset;
+}
+
+static int setup_extradata( hb_work_object_t *w, hb_buffer_t *in )
+{
+ hb_work_private_t *pv = w->private_data;
+
+ // we can't call the avstream funcs but the read_header func in the
+ // AVInputFormat may set up some state in the AVContext. In particular
+ // vc1t_read_header allocates 'extradata' to deal with header issues
+ // related to Microsoft's bizarre engineering notions. We alloc a chunk
+ // of space to make vc1 work then associate the codec with the context.
+ if ( w->codec_param != CODEC_ID_VC1 )
+ {
+ // we haven't been inflicted with M$ - allocate a little space as
+ // a marker and return success.
+ pv->context->extradata_size = 16;
+ pv->context->extradata = av_malloc(pv->context->extradata_size);
+ return 0;
+ }
+
+ // find the start and and of the sequence header
+ int shdr, shdr_end;
+ if ( ( shdr = find_hdr( in, 0, 0x0f ) ) < 0 )
+ {
+ // didn't find start of seq hdr
+ return 1;
+ }
+ if ( ( shdr_end = next_hdr( in, shdr + 4 ) ) < 0 )
+ {
+ shdr_end = in->size;
+ }
+ shdr_end -= shdr;
+
+ // find the start and and of the entry point header
+ int ehdr, ehdr_end;
+ if ( ( ehdr = find_hdr( in, 0, 0x0e ) ) < 0 )
+ {
+ // didn't find start of entry point hdr
+ return 1;
+ }
+ if ( ( ehdr_end = next_hdr( in, ehdr + 4 ) ) < 0 )