【蓝牙sbc协议】sbc源码阅读笔记(三)——数据读写过程

sbc_编码过程详解


编码部分源码

// sbcenc.c
static void encode(char *filename, int subbands, int bitpool, int joint,
				int dualchannel, int snr, int blocks, bool msbc)
{
     
	struct au_header au_hdr;
	sbc_t sbc;
	int fd, size, srate, codesize, nframes;
	ssize_t encoded;
	ssize_t len;

	// 数据初始化
  ···

  // codesize = subbands * blocks *channels * 2
	codesize = sbc_get_codesize(&sbc);
  // read(fd, input, BE_INT(au_hdr.hdr_size) - len)
	nframes = sizeof(input) / codesize;
	while (1) {
     
		unsigned char *inp, *outp;
		/* read data for up to 'nframes' frames of input data */
		size = read(fd, input, codesize * nframes);
		if (size < 0) {
     
			/* Something really bad happened */
			perror("Can't read audio data");
			break;
		}
		if (size < codesize) {
     
			/* Not enough data for encoding even a single frame */
			break;
		}
		/* encode all the data from the input buffer in a loop */
		inp = input;
		outp = output;
		while (size >= codesize) {
     
			len = sbc_encode(&sbc, inp, codesize,
				outp, sizeof(output) - (outp - output),
				&encoded);
			if (len != codesize || encoded <= 0) {
     
				fprintf(stderr,
					"sbc_encode fail, len=%zd, encoded=%lu\n",
					len, (unsigned long) encoded);
				break;
			}
			size -= len;
			inp += len;
			outp += encoded;
		}
		len = write(fileno(stdout), output, outp - output);
		if (len != outp - output) {
     
			perror("Can't write SBC output");
			break;
		}
		if (size != 0) {
     
			/*
			 * sbc_encode failure has been detected earlier or end
			 * of file reached (have trailing partial data which is
			 * insufficient to encode SBC frame)
			 */
			break;
		}
	}

	sbc_finish(&sbc);
  ···
}

其中codesize为编码的长度,通过sbc_get_codesize求出,具体过程为:

// sbc.c
SBC_EXPORT size_t sbc_get_codesize(sbc_t *sbc)
{
     
	uint16_t subbands, channels, blocks;
	struct sbc_priv *priv;

	priv = sbc->priv;
	if (!priv->init) {
     
		subbands = sbc->subbands ? 8 : 4;
		if (priv->msbc)
			blocks = MSBC_BLOCKS;
		else
			blocks = 4 + (sbc->blocks * 4);
		channels = sbc->mode == SBC_MODE_MONO ? 1 : 2;
	} else {
     
		subbands = priv->frame.subbands;
		blocks = priv->frame.blocks;
		channels = priv->frame.channels;
	}
	
	return subbands * blocks * channels * 2;
}

然后求出数据帧数nframes,其中input为跳过头部的额外字节:

nframes = sizeof(input) / codesize;

// input
#define BUF_SIZE 32768
static unsigned char input[BUF_SIZE];
/* Skip extra bytes of the header if any */
// #define BE_INT(v)		bswap_32(v)
// hdr.size: size of header (min 24)
// len = read(fd, &au_hdr, sizeof(au_hdr));
read(fd, input, BE_INT(au_hdr.hdr_size) - len);

接下来开始进入读取数据部分,读取全部nframe帧的数据:

/* read data for up to 'nframes' frames of input data */
size = read(fd, input, codesize * nframes);

然后在循环中对输入缓冲区内的所有数据进行编码,从下面代码可以看出,对输入的数据进行逐帧编码,将编码后的数据写入output

/* encode all the data from the input buffer in a loop */
inp = input;
outp = output;
while (size >= codesize) {
     
	len = sbc_encode(&sbc, inp, codesize,
		outp, sizeof(output) - (outp - output),
		&encoded);
	if (len != codesize || encoded <= 0) {
     
		fprintf(stderr,
			"sbc_encode fail, len=%zd, encoded=%lu\n",
			len, (unsigned long) encoded);
		break;
	}
	size -= len;
	inp += len;
	outp += encoded;
}
len = write(fileno(stdout), output, outp - output);
if (len != outp - output) {
     
	perror("Can't write SBC output");
	break;
}
if (size != 0) {
     
	 /*
		* sbc_encode failure has been detected earlier or end
		* of file reached (have trailing partial data which is
		* insufficient to encode SBC frame)
		*/
	break;
}

以上就是编码及写入的整体过程,接下来研究具体的编码过程,也就是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)

待补充内容:

  • sbc_encode详解

你可能感兴趣的:(音频处理,sbc协议,蓝牙sbc,sbc源码,sbc读写,源码笔记)