简体   繁体   English

音频MP2编码和解码可产生一半数据C ++和FFMPEG

[英]Audio MP2 encoding and decoding producing half data C++ and FFMPEG

I have 16 bit PCM data with stereo setup which I grab from a microphone. 我有16位PCM数据,带有立体声设置,可以从麦克风中获取。

Once I get the data I encode it using the following encoder settings 一旦获得数据,便使用以下编码器设置对其进行编码

AVCodec* audio_codec = avcodec_find_encoder(AV_CODEC_ID_MP2);

AVCodecContext* audio_codec_ctx = avcodec_alloc_context3(audio_codec);
audio_codec_ctx->bit_rate = 64000;
audio_codec_ctx->channels = 2;
audio_codec_ctx->channel_layout = AV_CH_LAYOUT_STEREO;
audio_codec_ctx->sample_rate = 44100;
audio_codec_ctx->sample_fmt = AV_SAMPLE_FMT_S16;

When I pass the audio data into the encoder I see that it takes 4608 bytes of data each time and encodes it correctly to MP2 data. 当我将音频数据传递到编码器时,我看到它每次都占用4608字节的数据并将其正确编码为MP2数据。 PCM data grabbed by the microphone is 88320 bytes and the encoder takes 4608 bytes each time and compresses it. 麦克风捕获的PCM数据为88320字节,编码器每次占用4608字节并将其压缩。

If I take each 4608 byte section that has been encoded and pass it through a decoder with the same settings as above but with a decoder. 如果我获取每个已编码的4608字节节,然后将其通过与上述设置相同但带有解码器的解码器。

AVCodecID audio_codec_id = AV_CODEC_ID_MP2;
AVCodec * audio_decodec = avcodec_find_decoder(audio_codec_id);

audio_decodecContext = avcodec_alloc_context3(audio_decodec); 
audio_decodecContext->bit_rate = 64000;
audio_decodecContext->channels = 2;
audio_decodecContext->channel_layout = AV_CH_LAYOUT_STEREO;
audio_decodecContext->sample_rate = 44100;
audio_decodecContext->sample_fmt = AV_SAMPLE_FMT_S16;

The decoding works and is successful but when I look at the data size it is exactly half 2034 of what was encoded. 解码有效并且成功,但是当我查看数据大小时,它恰好是编码的2034的一半。 I dont understand why that would be. 我不明白为什么会这样。 I would of imagined I would get 4608 considering the encoder and decoder are the same. 考虑到编码器和解码器相同,我会想象得到4608。

Can anyone shed some light into why this would be happening. 任何人都可以阐明为什么会发生这种情况。 Anything I should be setting? 我应该设置什么?

The requested decoder sample format should be set using audio_decodecContext->request_sample_fmt . 请求的解码器样本格式应使用audio_decodecContext->request_sample_fmt设置。 sample_fmt is set by the decoder itself, and may be different, in which case you should use libswresample to convert between sample formats. sample_fmt由解码器本身设置,并且可能有所不同,在这种情况下,应使用libswresample在样本格式之间进行转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM