void AudioDecoder::DecodeThread()
{
AudioFrame frame;
int len;
int got_frame;
int n;
if (_audio_pkt.data != NULL)
av_free_packet(&_audio_pkt);
if(!_pkt_queue->Pop(&_audio_pkt))
return;
_audio_pkt_tmp.data = _audio_pkt.data;
_audio_pkt_tmp.size = _audio_pkt.size;
while(_audio_pkt_tmp.size > 0)
{
len = avcodec_decode_audio4(_audio_stream->codec, _av_frame, &got_frame, &_audio_pkt_tmp);
if (len < 0)
{
LOG "invalid audio packet or frame. Skipping packet"EL;
break;
}
_audio_pkt_tmp.data += len;
_audio_pkt_tmp.size -= len;
if (!got_frame)
continue;
// 1. copying decoded data into AudioFrame
frame.size = av_samples_get_buffer_size(NULL, _audio_stream->codec->channels, _av_frame->nb_samples,_audio_stream->codec->sample_fmt, 1);
// frame.size= _av_frame->linesize[0];
LOG "frame size " << frame.size EL;
frame.data = fast_aligned(uint8_t,16,frame.size);
memcpy(frame.data,_av_frame->data[0],frame.size);
frame.pts = _pts;
n = 2 * _audio_stream->codec->channels;
frame.duration = frame.size*1000 / (n * _audio_stream->codec->sample_rate);
_pts += frame.duration;
// 2. save the frame
if (!_frame_queue->Push(frame))
return;
avcodec_get_frame_defaults(_av_frame);
}
}
dm9pZCBBdWRpb0RlY29kZXI6OkRlY29kZVRocmVhZCgpCnsKICAgIEF1ZGlvRnJhbWUJCWZyYW1lOwoJaW50CQkJCWxlbjsKICAgIGludCBnb3RfZnJhbWU7CiAgICBpbnQgbjsKCglpZiAoX2F1ZGlvX3BrdC5kYXRhICE9IE5VTEwpCgkJYXZfZnJlZV9wYWNrZXQoJl9hdWRpb19wa3QpOwoKCWlmKCFfcGt0X3F1ZXVlLT5Qb3AoJl9hdWRpb19wa3QpKQoJCXJldHVybjsKCglfYXVkaW9fcGt0X3RtcC5kYXRhID0gX2F1ZGlvX3BrdC5kYXRhOwoJX2F1ZGlvX3BrdF90bXAuc2l6ZSA9IF9hdWRpb19wa3Quc2l6ZTsKICAgIAoJd2hpbGUoX2F1ZGlvX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