【蓝牙sbc协议】sbc源码阅读笔记(四)——sbc_encode函数详解

【蓝牙sbc协议】sbc源码阅读笔记(四)——sbc_encode函数详解sbc_encode函数详解函数定义://sbc.cSBC_EXPORTssize_tsbc_encode(sbc_t*sbc,constvoid*input,size_tinput_len, void*output,size_toutput_len,ssize_t*written){ structsbc_priv*priv; intsamples; ssize_tframelen; int(*sbc_enc_process_input)(int

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

sbc_encode函数详解


函数定义:
// sbc.c
SBC_EXPORT ssize_t sbc_encode(sbc_t *sbc, const void *input, size_t input_len,
void *output, size_t output_len, ssize_t *written)
{ 

struct sbc_priv *priv;
int samples;
ssize_t framelen;
int (*sbc_enc_process_input)(int position,
const uint8_t *pcm, int16_t X[2][SBC_X_BUFFER_SIZE],
int nsamples, int nchannels);
if (!sbc || !input)
return -EIO;
priv = sbc->priv;
if (written)
*written = 0;
// 初始化 priv->frame
// priv->frame 包含了一个未打包的 SBC 数据帧
if (!priv->init) { 

priv->frame.frequency = sbc->frequency;
priv->frame.mode = sbc->mode;
priv->frame.channels = sbc->mode == SBC_MODE_MONO ? 1 : 2;
priv->frame.allocation = sbc->allocation;
priv->frame.subband_mode = sbc->subbands;
priv->frame.subbands = sbc->subbands ? 8 : 4;
priv->frame.block_mode = sbc->blocks;
if (priv->msbc)
priv->frame.blocks = MSBC_BLOCKS;
else
priv->frame.blocks = 4 + (sbc->blocks * 4);
priv->frame.bitpool = sbc->bitpool;
priv->frame.codesize = sbc_get_codesize(sbc);
priv->frame.length = sbc_get_frame_length(sbc);
sbc_encoder_init(priv->msbc, &priv->enc_state, &priv->frame);
priv->init = true;
} else if (priv->frame.bitpool != sbc->bitpool) { 

priv->frame.length = sbc_get_frame_length(sbc);
priv->frame.bitpool = sbc->bitpool;
}
/* input must be large enough to encode a complete frame */
if (input_len < priv->frame.codesize)
return 0;
/* output must be large enough to receive the encoded frame */
if (!output || output_len < priv->frame.length)
return -ENOSPC;
/* Select the needed input data processing function and call it */
if (priv->frame.subbands == 8) { 

if (sbc->endian == SBC_BE)
sbc_enc_process_input =
priv->enc_state.sbc_enc_process_input_8s_be;
else
sbc_enc_process_input =
priv->enc_state.sbc_enc_process_input_8s_le;
} else { 

if (sbc->endian == SBC_BE)
sbc_enc_process_input =
priv->enc_state.sbc_enc_process_input_4s_be;
else
sbc_enc_process_input =
priv->enc_state.sbc_enc_process_input_4s_le;
}
priv->enc_state.position = sbc_enc_process_input(
priv->enc_state.position, (const uint8_t *) input,
priv->enc_state.X, priv->frame.subbands * priv->frame.blocks,
priv->frame.channels);
samples = sbc_analyze_audio(&priv->enc_state, &priv->frame);
if (priv->frame.mode == JOINT_STEREO) { 

int j = priv->enc_state.sbc_calc_scalefactors_j(
priv->frame.sb_sample_f, priv->frame.scale_factor,
priv->frame.blocks, priv->frame.subbands);
framelen = priv->pack_frame(output,
&priv->frame, output_len, j);
} else { 

priv->enc_state.sbc_calc_scalefactors(
priv->frame.sb_sample_f, priv->frame.scale_factor,
priv->frame.blocks, priv->frame.channels,
priv->frame.subbands);
framelen = priv->pack_frame(output,
&priv->frame, output_len, 0);
}
if (written)
*written = framelen;
return samples * priv->frame.channels * 2;
}

在这个函数中,首先进行了变量声明等操作;然后对priv->frame初始化,包含一个未打包的 SBC 数据帧;然后初始化编码器sbc_encoder_init():

// sbc.c
static void sbc_encoder_init(bool msbc, struct sbc_encoder_state *state,
const struct sbc_frame *frame)
{ 

memset(&state->X, 0, sizeof(state->X));
state->position = (SBC_X_BUFFER_SIZE - frame->subbands * 9) & ~7;
if (msbc)
state->increment = 1;
else
state->increment = 4;
// 检测CPU功能并设置功能指针
sbc_init_primitives(state);
}

然后选择所需要的输入数据处理函数并调用它:

/* Select the needed input data processing function and call it */
if (priv->frame.subbands == 8) { 

if (sbc->endian == SBC_BE)
sbc_enc_process_input =
priv->enc_state.sbc_enc_process_input_8s_be;
else
sbc_enc_process_input =
priv->enc_state.sbc_enc_process_input_8s_le;
} else { 

if (sbc->endian == SBC_BE)
sbc_enc_process_input =
priv->enc_state.sbc_enc_process_input_4s_be;
else
sbc_enc_process_input =
priv->enc_state.sbc_enc_process_input_4s_le;
}
priv->enc_state.position = sbc_enc_process_input(
priv->enc_state.position, (const uint8_t *) input,
priv->enc_state.X, priv->frame.subbands * priv->frame.blocks,
priv->frame.channels);
samples = sbc_analyze_audio(&priv->enc_state, &priv->frame);
if (priv->frame.mode == JOINT_STEREO) { 

int j = priv->enc_state.sbc_calc_scalefactors_j(
priv->frame.sb_sample_f, priv->frame.scale_factor,
priv->frame.blocks, priv->frame.subbands);
framelen = priv->pack_frame(output,
&priv->frame, output_len, j);
} else { 

priv->enc_state.sbc_calc_scalefactors(
priv->frame.sb_sample_f, priv->frame.scale_factor,
priv->frame.blocks, priv->frame.channels,
priv->frame.subbands);
framelen = priv->pack_frame(output,
&priv->frame, output_len, 0);
}

最后计算编码前后的数据长度,修改编码后的数据长度,并返回编码前的长度:

if (written)
*written = framelen;
return samples * priv->frame.channels * 2;
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/195011.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • vgg网络论文_dna结构综述论文

    vgg网络论文_dna结构综述论文原文地址:https://arxiv.org/pdf/1409.1556.pdfVGG简介:VGG卷积神经网络是牛津大学在2014年提出来的模型。当这个模型被提出时,由于它的简洁性和实用性,马上成为了当时最流行的卷积神经网络模型。它在图像分类和目标检测任务中都表现出非常好的结果。在2014年的ILSVRC比赛中,VGG在Top-5中取得了92.3%的正确率。同年的冠军是googl…

    2022年10月21日
  • 【算法】java 实现数组的反转

    【算法】java 实现数组的反转数组的反转原理跟冒泡排序有点像,都是通过交换位置,只不过数组的反转是交换第一个和最后一个的位置,第二个和倒数第二个的位置,冒泡排序是交互相邻两个的位置.下面看一下具体的代码实现packagetest;/***数组的反转*/publicclassTestDemo{publicstaticvoidmain(String[]args){…

  • fsync函数

    fsync函数fsync函数,将文件数据同步到硬盘功能描述: 系统调用fsync将所有已写入文件描述符fd的数据真正的写道磁盘或者其他下层设备上。用法:#includeintfsync(intfd);#ifdef_POSIX_SYNCHRONIZ

  • js的随机函数_随机生成数字的函数

    js的随机函数_随机生成数字的函数//默认为范围为10,开始值为1functionrandom(range=10,start=1){returnMath.floor(Math.random()*range+start);}

    2022年10月28日
  • DNA和纳米(Nano)Fusion技术的发展趋势

    DNA和纳米(Nano)Fusion技术的发展趋势

  • IntelliJ Idea 2017 注册码 免费激活方法「建议收藏」

    IntelliJ Idea 2017 注册码 免费激活方法「建议收藏」!!!补充版本信息,博友反馈17.3版本之后的不能用。注:如果为2017.3之后的版本,参考评论中23楼提供的博文,最近比较忙,没能补充新版本激活方法。谢谢23楼博友!!!可使用以下两种

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号