您的位置:首页 > 其它

音频处理——pcm基础知识与重采样

2016-10-25 10:15 375 查看
转载地址:  blog.csdn.net/timesir/article/details/52904024

1 基本概念1:获取pcm音频帧声道数

AVCodecContext->channels

avframe->channels

printf("av_frame_get_channels = %d\n",av_frame_get_channels(frame));

1.1 声道与布局具有映射关系

audio channels and channel_layout_个叉叉_新浪博客

通过av_get_channel_layout_nb_channels()和av_get_default_channel_layout()这些函数可以得到channels和channellayout的转换。
libavutil中的audioconvert.c定义channellayout和channels的相关map:
channel_layout_map[]
{ "mono", 1, AV_CH_LAYOUT_MONO },
{ "stereo", 2, AV_CH_LAYOUT_STEREO },
{ "2.1", 3, AV_CH_LAYOUT_2POINT1 },
{ "3.0", 3, AV_CH_LAYOUT_SURROUND },
{ "3.0(back)", 3, AV_CH_LAYOUT_2_1 },
{ "4.0", 4, AV_CH_LAYOUT_4POINT0 },
{ "quad", 4, AV_CH_LAYOUT_QUAD },
{ "quad(side)", 4, AV_CH_LAYOUT_2_2 },
{ "3.1", 4, AV_CH_LAYOUT_3POINT1 },
{ "5.0", 5, AV_CH_LAYOUT_5POINT0_BACK },
{ "5.0(side)", 5, AV_CH_LAYOUT_5POINT0 },
{ "4.1", 5, AV_CH_LAYOUT_4POINT1 },
{ "5.1", 6, AV_CH_LAYOUT_5POINT1_BACK },
{ "5.1(side)", 6, AV_CH_LAYOUT_5POINT1 },
{ "6.0", 6, AV_CH_LAYOUT_6POINT0 },
{ "6.0(front)", 6, AV_CH_LAYOUT_6POINT0_FRONT },
{ "hexagonal", 6, AV_CH_LAYOUT_HEXAGONAL },
{ "6.1", 7, AV_CH_LAYOUT_6POINT1 },
{ "6.1", 7, AV_CH_LAYOUT_6POINT1_BACK },
{ "6.1(front)", 7, AV_CH_LAYOUT_6POINT1_FRONT },
{ "7.0", 7, AV_CH_LAYOUT_7POINT0 },
{ "7.0(front)", 7, AV_CH_LAYOUT_7POINT0_FRONT },
{ "7.1", 8, AV_CH_LAYOUT_7POINT1 },
{ "7.1(wide)", 8, AV_CH_LAYOUT_7POINT1_WIDE },
{ "octagonal", 8, AV_CH_LAYOUT_OCTAGONAL },
{ "downmix", 2, AV_CH_LAYOUT_STEREO_DOWNMIX, },

2 基本概念2:获取pcm音频帧每个声道的sample采样点个数

frame_->nb_samples


3 基本概念3:获取pcm音频帧物理存放方式

3.1 获取解码之后的pcm音频帧物理存放方式

frame->format

3.2 ffmpeg支持的音频帧的物理存放方式

FFmpeg: Audio sample formats

打印对应的含义

printf("av_get_sample_fmt_name= %s\n",av_get_sample_fmt_name(frame->format));


3.3 ffmpeg音频帧的物理存放方式含义

3.3.1 第一层意思: 每个采样点数据的物理存储类型

大端小端方式

无符号/有符号

数据位数

8位,16位等

数据类型

整形,浮点类型等

3.3.2 第二层意思: 不同声道的同一采样点是否单独存放

参考链接

PCM数据格式 - taoanran - 开源中国社区
Decode Audio from Memory - C++ - Stack Overflow
audio - What is the difference between AV_SAMPLE_FMT_S16P
and AV_SAMPLE_FMT_S16? - Stack Overflow

两种存放方式 packed和planar
第一种: 多个声道数据交错存放(packed类型,不带字符P)

对于 packed音频(左右声道打包存放), 只有一个数据指针(相当于一个声道)。

所有声道的数据交错排放在frame->data[0](即frame->extended_data[0])地址处

所有声道的数据长度为linesize[0](单位:字节)

地址数据备注
data[0]声道1的采样点0每个采样点数据有int、uint、float,大端小端之分
data[0]+1声道2的采样点0 
data[0]+2声道1的采样点1 
data[0]+3声道2的采样点1 
data[0]+4声道1的采样点2 
data[0]+5声道2的采样点2 
 
data[0]+2i声道1的采样点i 
data[0]+2i+1声道2的采样点i 
比如: AV_SAMPLE_FMT_S16 所有声道的数据放在一个buffer中,左右声道采样点交叉存放,每个采样值为一个signed 16位(范围为-32767 to +32767)。

第二种: 每个声道数据单独存放(planar类型,带字符P)

对于 planar音频(左右声道分开存放),每个声道有自己的数据存放位置。

声道0的起始地址为 frame->data[0](或frame->extended_data[0])

声道1的起始地址为 frame->data[1](或frame->extended_data[1])

声道i的起始地址为 frame->data[i](或frame->extended_data[i])

每个声道的数据长度为linesize[0](单位:字节)

实际上ffmpeg在实现的时候,每个声道的数据连续存放,不同声道之间也是连续存放的。

地址声道 
data[0]声道1采样点1
  采样点2
  采样点i
data[1]声道2采样点1
  采样点2
  采样点i
所以 data[i]=data[i-1] + linesize[0]

比如: AV_SAMPLE_FMT_S16P 每个声道的数据放在单独的buffer中,每个采样值为一个signed 16位(范围为-32767 to +32767)。

两者之间的联系

所有声道的数据都是存放在 frame->data[0]开始的一段连续空间中

如果是 packed类型,同一采样点的不同声道数据放到一起,然后存储下一个采样点

如果是 planar类型,同一声道的所有采样点数据放到一起,然后存放下一个声道

判断是否是 planar类型

av_sample_fmt_is_planar(sample_fmt)

两者之间的转换

通过重采样函数进行转换

手动将每个声道的数据交错存放

根据存放方式,分配pcm数据空间(重采样用)
手动分配

int nb_planes;

static uint8_t **audio_dst_data = NULL;

nb_planes = av_sample_fmt_is_planar(audio_dec_ctx->sample_fmt) ? audio_dec_ctx->channels : 1; //如果是 planar类型,需要分配一个指针数组,每个元素指向一个声道

audio_dst_data = av_mallocz(sizeof(uint8_t *) * nb_planes);

if (!audio_dst_data) {

fprintf(stderr, "Could not allocate audio data buffers\n");

ret = AVERROR(ENOMEM);

goto end;

}

ret = av_samples_alloc(audio_dst_data, &audio_dst_linesize, av_frame_get_channels(frame),

frame->nb_samples, frame->format, 1);

if (ret < 0) {

fprintf(stderr, "Could not allocate audio buffer\n");

return AVERROR(ENOMEM);

}

调用接口函数(内部实现,即是上面的函数调用过程)

uint8_t ** audio_data;

src_nb_channels = av_get_channel_layout_nb_channels(src_ch_layout);

ret = av_samples_alloc_array_and_samples(&src_data, &src_linesize, src_nb_channels, //都是定义二重指针 audio_data,注意这里的调用方式

src_nb_samples, src_sample_fmt, 0);

if (ret < 0) {

fprintf(stderr, "Could not allocate source samples\n");

goto end;

}

// 这个函数内部即是上面的过程

int av_samples_alloc_array_and_samples (uint8_t * audio_data,

int * linesize,

int nb_channels,

int nb_samples,

enum AVSampleFormat sample_fmt,

int align

)


3.3.3 获取pcm声道占有的byte空间大小

获取pcm每个声道占有的byte空间大小(可以通过pcm物理数据类型和采样点个数,通道个数推导出)
对于 packed音频(左右声道打包存放)

AVFrame->int linesize[0]值即为打包存放的所有声道的数据字节长度

对于 planar音频(每个声道数据单独存放)

AVFrame->int linesize[0]值即为每个声道的数据字节长度

例如:

frame->format为 FLTP类型(每个sample是float类型的)

frame->nb_samples=2048(每个声道2048个采样点)

推导出: 每个声道占有的byte空间大小为 2048*4=8192

frame->linesize[0]确实等于8192

获取pcm所有声道占有的byte空间大小
对于 packed音频(左右声道打包存放)

linesize[0]值即为所有声道的数据字节长度

对于 packed和planar音频,都可以使用官方函数得出

输入的参数为pcm类型,声道个数,采样点数

<span style="color: rgb(206, 83, 122); "><strong>int</strong></span> <span style="color: rgb(188, 110, 197); "><strong>av_samples_get_buffer_size</strong></span>  <span style="color: rgb(79, 151, 215);">(</span>   <span style="color: rgb(206, 83, 122); "><strong>int</strong></span> *   <span style="color: rgb(117, 144, 219);">linesize</span>,  <span style="color: rgb(42, 161, 174); background-color: rgb(41, 46, 52);">//</span><span style="color: rgb(42, 161, 174); background-color: rgb(41, 46, 52);">主要针对 planar类型</span>
<span style="color: rgb(206, 83, 122); "><strong>int</strong></span>   <span style="color: rgb(117, 144, 219);">nb_channels</span>,
<span style="color: rgb(206, 83, 122); "><strong>int</strong></span>   <span style="color: rgb(117, 144, 219);">nb_samples</span>,
<span style="color: rgb(79, 151, 215); "><strong>enum</strong></span> <span style="color: rgb(206, 83, 122); "><strong>AVSampleFormat</strong></span>   <span style="color: rgb(117, 144, 219);">sample_fmt</span>,
<span style="color: rgb(206, 83, 122); "><strong>int</strong></span>   <span style="color: rgb(117, 144, 219);">align</span>
<span style="color: rgb(79, 151, 215);">)</span>


Parameters

[out] linesize calculated linesize, may be NULL

nb_channels the number of channels

nb_samples the number of samples in a single channel

sample_fmt the sample format

align buffer size alignment (0 = default, 1 = no alignment)

Returns

required buffer size, or negative error code on failure
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: