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);        
	}

}