X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=libhb%2Fdecavcodec.c;h=09f15ecd839360f2cfc721bf440dfa815e63e413;hb=d7389faafa327bb1be3340c34c30f353a8f52960;hp=e083d517e19cc094adde4ee6f6b6ff8c6b8611b4;hpb=a9ba7b2e29125df32483cc79ab0315bcec728d9f;p=handbrake-jp%2Fhandbrake-jp-git.git diff --git a/libhb/decavcodec.c b/libhb/decavcodec.c index e083d517..09f15ecd 100644 --- a/libhb/decavcodec.c +++ b/libhb/decavcodec.c @@ -209,6 +209,7 @@ static int decavcodecInit( hb_work_object_t * w, hb_job_t * job ) { pv->downmix = hb_downmix_init(w->audio->config.in.channel_layout, w->audio->config.out.mixdown); + hb_downmix_set_chan_map( pv->downmix, &hb_smpte_chan_map, &hb_qt_chan_map ); } return 0; @@ -389,44 +390,53 @@ static int decavcodecBSInfo( hb_work_object_t *w, const hb_buffer_t *buf, 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; + int pos, pbuffer_size; - while ( pos < buf->size ) + while ( buf && !ret ) { - 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 + pos = 0; + while ( pos < buf->size ) { - 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; + int len; - len = avcodec_decode_audio3( context, (int16_t*)buffer, &out_size, &avp ); - if ( len > 0 && context->sample_rate > 0 ) + if (parser != NULL ) { - 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; + 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 ); @@ -513,6 +523,14 @@ static int get_frame_buf( AVCodecContext *context, AVFrame *frame ) 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 ); @@ -543,7 +561,20 @@ static void flushDelayQueue( hb_work_private_t *pv ) } } -static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size ) +/* + * 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; @@ -616,6 +647,7 @@ static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size ) { buf = copy_frame( pv, &frame ); buf->start = pts; + buf->sequence = sequence; hb_list_add( pv->list, buf ); ++pv->nframes; return got_picture; @@ -663,7 +695,9 @@ static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size ) } // add the new frame to the delayq & push its timestamp on the heap - pv->delayq[slot] = copy_frame( pv, &frame ); + buf = copy_frame( pv, &frame ); + buf->sequence = sequence; + pv->delayq[slot] = buf; heap_push( &pv->pts_heap, pts ); ++pv->nframes; @@ -672,7 +706,7 @@ static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size ) return got_picture; } -static void decodeVideo( hb_work_private_t *pv, uint8_t *data, int size, +static void decodeVideo( hb_work_private_t *pv, uint8_t *data, int size, int sequence, int64_t pts, int64_t dts ) { /* @@ -692,20 +726,24 @@ static void decodeVideo( hb_work_private_t *pv, uint8_t *data, int size, if ( pout_len > 0 ) { pv->pts = pv->parser->pts; - decodeFrame( pv, pout, pout_len ); + 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 ) ) + 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 ); @@ -742,6 +780,7 @@ static int decavcodecvInit( hb_work_object_t * w, hb_job_t * job ) /* 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; } @@ -844,7 +883,7 @@ static int decavcodecvWork( hb_work_object_t * w, hb_buffer_t ** buf_in, /* if we got an empty buffer signaling end-of-stream send it downstream */ if ( in->size == 0 ) { - decodeVideo( pv, in->data, in->size, pts, dts ); + decodeVideo( pv, in->data, in->size, in->sequence, pts, dts ); hb_list_add( pv->list, in ); *buf_out = link_buf_list( pv ); return HB_WORK_DONE; @@ -881,7 +920,7 @@ static int decavcodecvWork( hb_work_object_t * w, hb_buffer_t ** buf_in, pv->new_chap = in->new_chap; pv->chap_time = pts >= 0? pts : pv->pts_next; } - decodeVideo( pv, in->data, in->size, pts, dts ); + decodeVideo( pv, in->data, in->size, in->sequence, pts, dts ); hb_buffer_close( &in ); *buf_out = link_buf_list( pv ); return HB_WORK_OK; @@ -1006,8 +1045,14 @@ static void init_ffmpeg_context( hb_work_object_t *w ) // Because the time bases are so screwed up, we only take values // in the range 8fps - 64fps. AVRational tb; - if ( st->time_base.num * 64 > st->time_base.den && - st->time_base.den > st->time_base.num * 8 ) + if ( st->avg_frame_rate.den * 64 > st->avg_frame_rate.num && + st->avg_frame_rate.num > st->avg_frame_rate.den * 8 ) + { + tb.num = st->avg_frame_rate.den; + tb.den = st->avg_frame_rate.num; + } + else if ( st->time_base.num * 64 > st->time_base.den && + st->time_base.den > st->time_base.num * 8 ) { tb = st->time_base; } @@ -1029,6 +1074,7 @@ static void init_ffmpeg_context( hb_work_object_t *w ) // 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; // avi, mkv and possibly mp4 containers can contain the M$ VFW packed // b-frames abortion that messes up frame ordering and timestamps. @@ -1072,6 +1118,7 @@ static int decavcodecviInit( hb_work_object_t * w, hb_job_t * job ) { pv->downmix = hb_downmix_init(w->audio->config.in.channel_layout, w->audio->config.out.mixdown); + hb_downmix_set_chan_map( pv->downmix, &hb_smpte_chan_map, &hb_qt_chan_map ); } return 0; @@ -1088,7 +1135,7 @@ static int decavcodecviWork( hb_work_object_t * w, hb_buffer_t ** buf_in, if ( in->size == 0 ) { /* flush any frames left in the decoder */ - while ( pv->context && decodeFrame( pv, NULL, 0 ) ) + while ( pv->context && decodeFrame( pv, NULL, 0, in->sequence ) ) { } flushDelayQueue( pv ); @@ -1119,7 +1166,7 @@ static int decavcodecviWork( hb_work_object_t * w, hb_buffer_t ** buf_in, pv->chap_time = pts >= 0? pts : pv->pts_next; } prepare_ffmpeg_buffer( in ); - decodeFrame( pv, in->data, in->size ); + decodeFrame( pv, in->data, in->size, in->sequence ); hb_buffer_close( &in ); *buf_out = link_buf_list( pv ); return HB_WORK_OK; @@ -1147,6 +1194,7 @@ static void decodeAudio( hb_audio_t * audio, hb_work_private_t *pv, uint8_t *dat { AVCodecContext *context = pv->context; int pos = 0; + int loop_limit = 256; while ( pos < size ) { @@ -1165,10 +1213,18 @@ static void decodeAudio( hb_audio_t * audio, hb_work_private_t *pv, uint8_t *dat int out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; int nsamples; int len = avcodec_decode_audio3( context, buffer, &out_size, &avp ); - if ( len <= 0 ) + if ( len < 0 ) { return; } + if ( len == 0 ) + { + if ( !(loop_limit--) ) + return; + } + else + loop_limit = 256; + pos += len; if( out_size > 0 ) { @@ -1219,9 +1275,6 @@ static void decodeAudio( hb_audio_t * audio, hb_work_private_t *pv, uint8_t *dat } int n_ch_samples = nsamples / context->channels; - hb_layout_remap( hb_smpte_chan_map, pv->downmix_buffer, - audio->config.in.channel_layout, n_ch_samples ); - int channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown); buf = hb_buffer_init( n_ch_samples * channels * sizeof(float) ); @@ -1238,8 +1291,9 @@ static void decodeAudio( hb_audio_t * audio, hb_work_private_t *pv, uint8_t *dat fl32[i] = buffer[i]; } int n_ch_samples = nsamples / context->channels; - hb_layout_remap( hb_smpte_chan_map, fl32, - audio->config.in.channel_layout, n_ch_samples ); + hb_layout_remap( &hb_smpte_chan_map, &hb_qt_chan_map, + audio->config.in.channel_layout, + fl32, n_ch_samples ); } double pts = pv->pts_next;