*/
#include "hb.h"
-
-#include "libavcodec/avcodec.h"
-#include "libavformat/avformat.h"
-#include "libswscale/swscale.h"
+#include "hbffmpeg.h"
+#include "libavcodec/audioconvert.h"
static int decavcodecInit( hb_work_object_t *, hb_job_t * );
static int decavcodecWork( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
/*XXX*/
if ( codec_id == 0 )
codec_id = CODEC_ID_MP2;
+
codec = avcodec_find_decoder( codec_id );
pv->parser = av_parser_init( codec_id );
pv->context = avcodec_alloc_context();
- avcodec_open( pv->context, codec );
+ hb_avcodec_open( pv->context, codec );
return 0;
}
}
if ( pv->context && pv->context->codec )
{
- avcodec_close( pv->context );
+ hb_avcodec_close( pv->context );
}
if ( pv->list )
{
}
if ( pv->buffer )
{
- free( pv->buffer );
+ av_free( pv->buffer );
pv->buffer = NULL;
}
free( pv );
hb_work_private_t * pv = w->private_data;
hb_buffer_t * in = *buf_in, * buf, * last = NULL;
int pos, len, out_size, i, uncompressed_len;
- short buffer[AVCODEC_MAX_AUDIO_FRAME_SIZE];
+ short* bufaligned;
uint64_t cur;
unsigned char *parser_output_buffer;
int parser_output_buffer_len;
*buf_out = NULL;
+ if ( in->start < -1 && pv->pts_next <= 0 )
+ {
+ // discard buffers that start before video time 0
+ return HB_WORK_OK;
+ }
+
cur = ( in->start < 0 )? pv->pts_next : in->start;
+ bufaligned = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
pos = 0;
while( pos < in->size )
{
- len = av_parser_parse( pv->parser, pv->context,
- &parser_output_buffer, &parser_output_buffer_len,
- in->data + pos, in->size - pos, cur, cur );
+ 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 );
out_size = 0;
uncompressed_len = 0;
if (parser_output_buffer_len)
{
- out_size = sizeof(buffer);
- uncompressed_len = avcodec_decode_audio2( pv->context, buffer,
- &out_size,
- parser_output_buffer,
- parser_output_buffer_len );
+ AVPacket avp;
+ av_init_packet( &avp );
+ avp.data = parser_output_buffer;
+ avp.size = parser_output_buffer_len;
+
+ out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
+ uncompressed_len = avcodec_decode_audio3( pv->context, bufaligned, &out_size, &avp );
}
if( out_size )
{
pv->context->sample_rate;
cur = buf->stop;
- s16 = buffer;
+ s16 = bufaligned;
fl32 = (float *) buf->data;
for( i = 0; i < out_size / 2; i++ )
{
pv->pts_next = cur;
+ av_free( bufaligned );
return HB_WORK_OK;
}
return 0;
}
+static const int chan2layout[] = {
+ HB_INPUT_CH_LAYOUT_MONO, // We should allow no audio really.
+ HB_INPUT_CH_LAYOUT_MONO,
+ HB_INPUT_CH_LAYOUT_STEREO,
+ HB_INPUT_CH_LAYOUT_2F1R,
+ HB_INPUT_CH_LAYOUT_2F2R,
+ HB_INPUT_CH_LAYOUT_3F2R,
+ HB_INPUT_CH_LAYOUT_4F2R,
+ HB_INPUT_CH_LAYOUT_STEREO,
+ HB_INPUT_CH_LAYOUT_STEREO,
+};
+
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) );
// 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 )
+ if ( ! codec )
{
- static char codec_name[64];
+ // there's no ffmpeg codec for this audio type - give up
+ return -1;
+ }
- info->name = strncpy( codec_name, codec->name, sizeof(codec_name)-1 );
- info->bitrate = 384000;
- info->rate = 48000;
- info->rate_base = 1;
- info->channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
- 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 = 0, pbuffer_size;
+
+ while ( pos < buf->size )
+ {
+ int len = av_parser_parse2( parser, context, &pbuffer, &pbuffer_size,
+ buf->data + pos, buf->size - pos,
+ buf->start, buf->start, AV_NOPTS_VALUE );
+ 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 = chan2layout[context->channels & 7];
+ ret = 1;
+ break;
+ }
+ }
}
- return -1;
+ av_free( buffer );
+ av_parser_close( parser );
+ hb_avcodec_close( context );
+ return ret;
}
/* -------------------------------------------------------------
w = pv->job->title->width;
h = pv->job->title->height;
}
- hb_buffer_t *buf = hb_buffer_init( w * h * 3 / 2 );
+ 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 ||
else
{
dst = copy_plane( dst, frame->data[0], w, frame->linesize[0], h );
- w >>= 1; h >>= 1;
+ 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 );
}
static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size )
{
- int got_picture;
+ int got_picture, oldlevel = 0;
AVFrame frame;
+ AVPacket avp;
- if ( avcodec_decode_video( pv->context, &frame, &got_picture, data, size ) < 0 )
+ 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
{
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;
do {
uint8_t *pout;
int pout_len;
- int len = av_parser_parse( pv->parser, pv->context, &pout, &pout_len,
- data + pos, size - pos, pts, dts );
+ 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->context->opaque = pv;
pv->context->get_buffer = get_frame_buf;
- AVCodec *codec = avcodec_find_decoder( codec_id );
+ 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.
- pv->context->extradata_size = 32;
- pv->context->extradata = av_malloc(pv->context->extradata_size);
- avcodec_open( pv->context, codec );
+ 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 )
+ {
+ ehdr_end = in->size;
+ }
+ ehdr_end -= ehdr;
+
+ // found both headers - allocate an extradata big enough to hold both
+ // then copy them into it.
+ pv->context->extradata_size = shdr_end + ehdr_end;
+ pv->context->extradata = av_malloc(pv->context->extradata_size + 8);
+ memcpy( pv->context->extradata, in->data + shdr, shdr_end );
+ memcpy( pv->context->extradata + shdr_end, in->data + ehdr, ehdr_end );
+ memset( pv->context->extradata + shdr_end + ehdr_end, 0, 8);
return 0;
}
return HB_WORK_DONE;
}
+ // if this is the first frame open the codec (we have to wait for the
+ // first frame because of M$ VC1 braindamage).
+ if ( pv->context->extradata_size == 0 )
+ {
+ if ( setup_extradata( w, in ) )
+ {
+ // we didn't find the headers needed to set up extradata.
+ // the codec will abort if we open it so just free the buf
+ // and hope we eventually get the info we need.
+ hb_buffer_close( &in );
+ return HB_WORK_OK;
+ }
+ AVCodec *codec = avcodec_find_decoder( w->codec_param );
+ // There's a mis-feature in ffmpeg that causes the context to be
+ // incorrectly initialized the 1st time avcodec_open is called.
+ // If you close it and open a 2nd time, it finishes the job.
+ hb_avcodec_open( pv->context, codec );
+ hb_avcodec_close( pv->context );
+ hb_avcodec_open( pv->context, codec );
+ }
+
if( in->start >= 0 )
{
pts = in->start;
info->rate = 27000000;
info->rate_base = (int64_t)context->time_base.num * 27000000LL /
context->time_base.den;
+ if ( context->ticks_per_frame > 1 )
+ {
+ // for ffmpeg 0.5 & later, the H.264 & MPEG-2 time base is
+ // field rate rather than frame rate so convert back to frames.
+ info->rate_base *= context->ticks_per_frame;
+ }
/* Sometimes there's no pixel aspect set in the source. In that case,
assume a 1:1 PAR. Otherwise, preserve the source PAR. */
if ( ! pv->context->codec )
{
AVCodec *codec = avcodec_find_decoder( pv->context->codec_id );
- avcodec_open( pv->context, codec );
+ hb_avcodec_open( pv->context, codec );
}
// set up our best guess at the frame duration.
// the frame rate in the codec is usually bogus but it's sometimes
int16_t *buffer = pv->buffer;
if ( buffer == NULL )
{
- // XXX ffmpeg bug workaround
- // malloc a buffer for the audio decode. On an x86, ffmpeg
- // uses mmx/sse instructions on this buffer without checking
- // that it's 16 byte aligned and this will cause an abort if
- // the buffer is allocated on our stack. Rather than doing
- // complicated, machine dependent alignment here we use the
- // fact that malloc returns an aligned pointer on most architectures.
- pv->buffer = malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
+ pv->buffer = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
buffer = pv->buffer;
}
+
+ AVPacket avp;
+ av_init_packet( &avp );
+ avp.data = data + pos;
+ avp.size = size - pos;
+
int out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
- int len = avcodec_decode_audio2( context, buffer, &out_size,
- data + pos, size - pos );
+ int len = avcodec_decode_audio3( context, buffer, &out_size, &avp );
if ( len <= 0 )
{
return;
pos += len;
if( out_size > 0 )
{
+ // We require signed 16-bit ints for the output format. If
+ // we got something different convert it.
+ if ( context->sample_fmt != SAMPLE_FMT_S16 )
+ {
+ // Note: av_audio_convert seems to be a work-in-progress but
+ // looks like it will eventually handle general audio
+ // mixdowns which would allow us much more flexibility
+ // in handling multichannel audio in HB. If we were doing
+ // anything more complicated than a one-for-one format
+ // conversion we'd probably want to cache the converter
+ // context in the pv.
+ int isamp = av_get_bits_per_sample_format( context->sample_fmt ) / 8;
+ AVAudioConvert *ctx = av_audio_convert_alloc( SAMPLE_FMT_S16, 1,
+ context->sample_fmt, 1,
+ NULL, 0 );
+ // get output buffer size (in 2-byte samples) then malloc a buffer
+ out_size = ( out_size * 2 ) / isamp;
+ buffer = av_malloc( out_size );
+
+ // we're doing straight sample format conversion which behaves as if
+ // there were only one channel.
+ const void * const ibuf[6] = { pv->buffer };
+ void * const obuf[6] = { buffer };
+ const int istride[6] = { isamp };
+ const int ostride[6] = { 2 };
+
+ av_audio_convert( ctx, obuf, ostride, ibuf, istride, out_size >> 1 );
+ av_audio_convert_free( ctx );
+ }
hb_buffer_t *buf = hb_buffer_init( 2 * out_size );
// convert from bytes to total samples
fl32[i] = buffer[i];
}
hb_list_add( pv->list, buf );
+
+ // if we allocated a buffer for sample format conversion, free it
+ if ( buffer != pv->buffer )
+ {
+ av_free( buffer );
+ }
}
}
}
}
hb_work_private_t *pv = w->private_data;
+
+ if ( (*buf_in)->start < -1 && pv->pts_next <= 0 )
+ {
+ // discard buffers that start before video time 0
+ *buf_out = NULL;
+ return HB_WORK_OK;
+ }
+
if ( ! pv->context )
{
init_ffmpeg_context( w );