【蓝牙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;
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

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

(0)
上一篇 2025年10月25日 下午5:22
下一篇 2025年10月25日 下午6:01


相关推荐

  • 三门问题详解(附C语言实现)

    三门问题详解(附C语言实现)三门问题 违背直觉的概率现象 nbsp nbsp 三门问题 MontyHallpro 亦称为蒙提霍尔问题 蒙特霍问题或蒙提霍尔悖论 大致出自美国的电视游戏节目 Let sMakeaDeal 问题名字来自该节目的主持人蒙提 霍尔 MontyHall 参赛者会看见三扇关闭了的门 其中一扇的后面有一辆汽车 选中后面有车的那扇门可赢得该汽车 另外两扇门后面则各藏有一只山羊 当参赛者选定了一扇门 但未去开启它的时候 节目主持人开启剩下两扇门的其中一扇 露出其中一只山羊 主持人其后会问参赛者要

    2026年3月19日
    1
  • 怎么让笔记本变路由器,亲身试验可用,不用下第三方软件

    怎么让笔记本变路由器,亲身试验可用,不用下第三方软件怎么让笔记本变路由器,亲身试验可用,不用下第三方软件

    2022年4月23日
    59
  • django的drf_简述django请求生命周期

    django的drf_简述django请求生命周期前言一般我们写完序列化以后,我们就会开始写视图了,drf中我们一般使用CBV的方式,也就是类视图的方式,最基础的我们会使用fromrest_framework.viewsimportAPIVi

    2022年7月31日
    6
  • AI智能体“龙虾”爆火背后藏风险:养了10天的“龙虾”在3000人群里“泄了密”

    AI智能体“龙虾”爆火背后藏风险:养了10天的“龙虾”在3000人群里“泄了密”

    2026年3月12日
    2
  • uniapp fonticon 字体图标的使用

    uniapp fonticon 字体图标的使用uni app 使用字体图标 iconfont HRM2454 的博客 CSDN 博客上面文章 补充一下 把 css 样式的路径加上

    2026年3月17日
    2
  • 8个常用的python办公室自动化技巧

    8个常用的python办公室自动化技巧平时在公司做数据分析的时候 也会用 python 做些办公自动化的工作 领导昨天说别人 3 个小时的活我们已经可以 3 分钟完成了 O O 本文就给大家介绍几个我用到的办公室自动化技巧 文章目录 1Word 文档 doc 转 docx2 文字地址批量转经纬度 3 经纬度计算距离 4 百度经纬度转高德经纬度 5Excel 文件批量合并 6Word 文件批量转 pdf7 批量读取 word 中表格数据 8 用 outlook 批量发邮件 1Word 文档 doc 转 docx 去年想参赛一个数据比赛 里面的数据都是 doc 格式 想

    2026年3月20日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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