mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-02-10 06:40:23 +00:00
sbc decoder: merge decoder + encoder
This commit is contained in:
parent
e7a4112819
commit
0c87db9ec5
@ -74,10 +74,10 @@ include ${SBC_ENCODER_ROOT}/Makefile.inc
|
||||
|
||||
SBC_DECODER += \
|
||||
${BTSTACK_ROOT}/src/classic/btstack_sbc_plc.c \
|
||||
${BTSTACK_ROOT}/src/classic/btstack_sbc_decoder_bludroid.c \
|
||||
${BTSTACK_ROOT}/src/classic/btstack_sbc_bludroid.c \
|
||||
|
||||
SBC_ENCODER += \
|
||||
${BTSTACK_ROOT}/src/classic/btstack_sbc_encoder_bludroid.c \
|
||||
${BTSTACK_ROOT}/src/classic/btstack_sbc_bludroid.c \
|
||||
${BTSTACK_ROOT}/src/classic/hfp_msbc.c \
|
||||
|
||||
EXAMPLES = \
|
||||
|
@ -59,7 +59,6 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "btstack.h"
|
||||
#include "btstack_sbc_decoder.h"
|
||||
|
||||
#include "sco_demo_util.h"
|
||||
#ifdef HAVE_POSIX_STDIN
|
||||
|
@ -44,8 +44,7 @@
|
||||
|
||||
#include "sco_demo_util.h"
|
||||
#include "btstack_debug.h"
|
||||
#include "btstack_sbc_decoder.h"
|
||||
#include "btstack_sbc_encoder.h"
|
||||
#include "btstack_sbc.h"
|
||||
#include "hfp_msbc.h"
|
||||
#include "hfp.h"
|
||||
|
||||
|
@ -44,12 +44,112 @@
|
||||
#define __BTSTACK_SBC_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "btstack_sbc_plc.h"
|
||||
|
||||
typedef enum{
|
||||
SBC_MODE_STANDARD,
|
||||
SBC_MODE_mSBC
|
||||
} btstack_sbc_mode_t;
|
||||
|
||||
typedef struct {
|
||||
void * context;
|
||||
void (*handle_pcm_data)(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context);
|
||||
// private
|
||||
void * decoder_state;
|
||||
btstack_sbc_plc_state_t plc_state;
|
||||
btstack_sbc_mode_t mode;
|
||||
|
||||
// summary of processed good, bad and zero frames
|
||||
int good_frames_nr;
|
||||
int bad_frames_nr;
|
||||
int zero_frames_nr;
|
||||
} btstack_sbc_decoder_state_t;
|
||||
|
||||
typedef struct {
|
||||
// private
|
||||
void * encoder_state;
|
||||
btstack_sbc_mode_t mode;
|
||||
} btstack_sbc_encoder_state_t;
|
||||
|
||||
/* API_START */
|
||||
|
||||
/* BTstack SBC decoder */
|
||||
/**
|
||||
* @brief Init SBC decoder
|
||||
* @param state
|
||||
* @param mode
|
||||
* @param callback for decoded PCM data
|
||||
* @param context provided in callback
|
||||
*/
|
||||
|
||||
void btstack_sbc_decoder_init(btstack_sbc_decoder_state_t * state, btstack_sbc_mode_t mode, void (*callback)(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context), void * context);
|
||||
|
||||
/**
|
||||
* @brief Process received SCO data
|
||||
* @param state
|
||||
* @param packet_status_flag from SCO packet: 0 = OK, 1 = possibly invalid data, 2 = no data received, 3 = data partially lost
|
||||
* @param buffer
|
||||
* @param size
|
||||
*/
|
||||
void btstack_sbc_decoder_process_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, uint8_t * buffer, int size);
|
||||
|
||||
/**
|
||||
* @brief Get number of samples per SBC frame
|
||||
*/
|
||||
int btstack_sbc_decoder_num_samples_per_frame(btstack_sbc_decoder_state_t * state);
|
||||
|
||||
/*
|
||||
* @brief Get number of channels
|
||||
*/
|
||||
int btstack_sbc_decoder_num_channels(btstack_sbc_decoder_state_t * state);
|
||||
|
||||
/*
|
||||
* @brief Get sample rate in hz
|
||||
*/
|
||||
int btstack_sbc_decoder_sample_rate(btstack_sbc_decoder_state_t * state);
|
||||
|
||||
|
||||
/* BTstack SBC Encoder */
|
||||
/**
|
||||
* @brief Init SBC encoder
|
||||
* @param state
|
||||
* @param mode
|
||||
* @param blocks
|
||||
* @param subbands
|
||||
* @param allocation_method
|
||||
* @param sample_rate
|
||||
* @param bitpool
|
||||
*/
|
||||
void btstack_sbc_encoder_init(btstack_sbc_encoder_state_t * state, btstack_sbc_mode_t mode,
|
||||
int blocks, int subbands, int allocation_method, int sample_rate, int bitpool);
|
||||
|
||||
/**
|
||||
* @brief Process received PCM data
|
||||
* @param buffer
|
||||
*/
|
||||
void btstack_sbc_encoder_process_data(int16_t * input_buffer);
|
||||
|
||||
/**
|
||||
* @brief Return SBC frame
|
||||
*/
|
||||
uint8_t * btstack_sbc_encoder_sbc_buffer(void);
|
||||
|
||||
/**
|
||||
* @brief Return SBC frame length
|
||||
*/
|
||||
uint16_t btstack_sbc_encoder_sbc_buffer_length(void);
|
||||
|
||||
/**
|
||||
* @brief Return number of audio samples in one PCM frame
|
||||
*/
|
||||
int btstack_sbc_encoder_num_audio_samples(void);
|
||||
|
||||
/* API_END */
|
||||
|
||||
// testing only
|
||||
void btstack_sbc_decoder_test_disable_plc(void);
|
||||
void btstack_sbc_decoder_test_simulate_corrupt_frames(int period);
|
||||
|
||||
#if defined __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -37,7 +37,7 @@
|
||||
|
||||
// *****************************************************************************
|
||||
//
|
||||
// SBC decoder based on Bludroid library
|
||||
// SBC decoder and encoder based on Bludroid library
|
||||
//
|
||||
// *****************************************************************************
|
||||
|
||||
@ -50,26 +50,24 @@
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "btstack_sbc_decoder.h"
|
||||
#include "btstack_sbc.h"
|
||||
#include "btstack_sbc_plc.h"
|
||||
|
||||
#include "oi_codec_sbc.h"
|
||||
#include "oi_assert.h"
|
||||
|
||||
#include "btstack.h"
|
||||
#include "sbc_encoder.h"
|
||||
|
||||
#define SBC_MAX_CHANNELS 2
|
||||
#define DECODER_DATA_SIZE (SBC_MAX_CHANNELS*SBC_MAX_BLOCKS*SBC_MAX_BANDS * 2 + SBC_CODEC_MIN_FILTER_BUFFERS*SBC_MAX_BANDS*SBC_MAX_CHANNELS * 2)
|
||||
#include "btstack.h"
|
||||
|
||||
#define mSBC_SYNCWORD 0xad
|
||||
#define SBC_SYNCWORD 0x9c
|
||||
|
||||
#define SBC_MAX_CHANNELS 2
|
||||
// #define LOG_FRAME_STATUS
|
||||
|
||||
// Testing only - START
|
||||
static int plc_enabled = 1;
|
||||
static int corrupt_frame_period = -1;
|
||||
// Testing - STOP
|
||||
// *****************************************************************************
|
||||
// SBC decoder start
|
||||
#define DECODER_DATA_SIZE (SBC_MAX_CHANNELS*SBC_MAX_BLOCKS*SBC_MAX_BANDS * 2 + SBC_CODEC_MIN_FILTER_BUFFERS*SBC_MAX_BANDS*SBC_MAX_CHANNELS * 2)
|
||||
|
||||
typedef struct {
|
||||
OI_UINT32 bytes_in_frame_buffer;
|
||||
@ -86,10 +84,41 @@ typedef struct {
|
||||
int first_good_frame_found;
|
||||
} bludroid_decoder_state_t;
|
||||
|
||||
|
||||
static btstack_sbc_decoder_state_t * sbc_state_singelton = NULL;
|
||||
static bludroid_decoder_state_t bd_state;
|
||||
|
||||
// Testing only - START
|
||||
static int plc_enabled = 1;
|
||||
static int corrupt_frame_period = -1;
|
||||
// Testing - STOP
|
||||
|
||||
// SBC decoder end
|
||||
// *****************************************************************************
|
||||
|
||||
|
||||
// *****************************************************************************
|
||||
// SBC encoder start
|
||||
|
||||
typedef struct {
|
||||
SBC_ENC_PARAMS context;
|
||||
int num_data_bytes;
|
||||
uint8_t sbc_packet[1000];
|
||||
} bludroid_encoder_state_t;
|
||||
|
||||
static btstack_sbc_encoder_state_t * sbc_encoder_state_singelton = NULL;
|
||||
static bludroid_encoder_state_t bd_encoder_state;
|
||||
|
||||
// SBC encoder start
|
||||
// *****************************************************************************
|
||||
|
||||
|
||||
|
||||
// *****************************************************************************
|
||||
//
|
||||
// SBC decoder based on Bludroid library
|
||||
//
|
||||
// *****************************************************************************
|
||||
|
||||
void btstack_sbc_decoder_test_disable_plc(void){
|
||||
plc_enabled = 0;
|
||||
}
|
||||
@ -364,3 +393,103 @@ void btstack_sbc_decoder_process_data(btstack_sbc_decoder_state_t * state, int p
|
||||
}
|
||||
// printf ("<<-- exit -->>\n");
|
||||
}
|
||||
|
||||
|
||||
// *****************************************************************************
|
||||
//
|
||||
// SBC encoder based on Bludroid library
|
||||
//
|
||||
// *****************************************************************************
|
||||
|
||||
void btstack_sbc_encoder_init(btstack_sbc_encoder_state_t * state, btstack_sbc_mode_t mode,
|
||||
int blocks, int subbands, int allmethod, int sample_rate, int bitpool){
|
||||
|
||||
if (sbc_encoder_state_singelton && sbc_encoder_state_singelton != state ){
|
||||
log_error("SBC encoder: different sbc decoder state is allready registered");
|
||||
}
|
||||
|
||||
sbc_encoder_state_singelton = state;
|
||||
sbc_encoder_state_singelton->mode = mode;
|
||||
|
||||
switch (sbc_encoder_state_singelton->mode){
|
||||
case SBC_MODE_STANDARD:
|
||||
bd_encoder_state.context.s16NumOfBlocks = blocks;
|
||||
bd_encoder_state.context.s16NumOfSubBands = subbands;
|
||||
bd_encoder_state.context.s16AllocationMethod = allmethod;
|
||||
bd_encoder_state.context.s16BitPool = 31;
|
||||
bd_encoder_state.context.mSBCEnabled = 0;
|
||||
|
||||
switch(sample_rate){
|
||||
case 16000: bd_encoder_state.context.s16SamplingFreq = SBC_sf16000; break;
|
||||
case 32000: bd_encoder_state.context.s16SamplingFreq = SBC_sf32000; break;
|
||||
case 44100: bd_encoder_state.context.s16SamplingFreq = SBC_sf44100; break;
|
||||
case 48000: bd_encoder_state.context.s16SamplingFreq = SBC_sf48000; break;
|
||||
default: bd_encoder_state.context.s16SamplingFreq = 0; break;
|
||||
}
|
||||
break;
|
||||
case SBC_MODE_mSBC:
|
||||
bd_encoder_state.context.s16NumOfBlocks = 15;
|
||||
bd_encoder_state.context.s16NumOfSubBands = 8;
|
||||
bd_encoder_state.context.s16AllocationMethod = SBC_LOUDNESS;
|
||||
bd_encoder_state.context.s16BitPool = 26;
|
||||
bd_encoder_state.context.s16ChannelMode = SBC_MONO;
|
||||
bd_encoder_state.context.s16NumOfChannels = 1;
|
||||
bd_encoder_state.context.mSBCEnabled = 1;
|
||||
bd_encoder_state.context.s16SamplingFreq = SBC_sf16000;
|
||||
break;
|
||||
}
|
||||
bd_encoder_state.context.pu8Packet = bd_encoder_state.sbc_packet;
|
||||
|
||||
sbc_encoder_state_singelton->encoder_state = &bd_encoder_state;
|
||||
SBC_ENC_PARAMS * context = &((bludroid_encoder_state_t *)sbc_encoder_state_singelton->encoder_state)->context;
|
||||
SBC_Encoder_Init(context);
|
||||
}
|
||||
|
||||
|
||||
void btstack_sbc_encoder_process_data(int16_t * input_buffer){
|
||||
if (!sbc_state_singelton){
|
||||
log_error("SBC encoder: sbc state is NULL, call btstack_sbc_encoder_init to initialize it");
|
||||
}
|
||||
SBC_ENC_PARAMS * context = &((bludroid_encoder_state_t *)sbc_encoder_state_singelton->encoder_state)->context;
|
||||
context->ps16PcmBuffer = input_buffer;
|
||||
if (context->mSBCEnabled){
|
||||
context->pu8Packet[0] = 0xad;
|
||||
}
|
||||
SBC_Encoder(context);
|
||||
}
|
||||
|
||||
int btstack_sbc_encoder_num_audio_samples(void){
|
||||
SBC_ENC_PARAMS * context = &((bludroid_encoder_state_t *)sbc_encoder_state_singelton->encoder_state)->context;
|
||||
return context->s16NumOfSubBands * context->s16NumOfBlocks * context->s16NumOfChannels;
|
||||
}
|
||||
|
||||
uint8_t * btstack_sbc_encoder_sbc_buffer(void){
|
||||
SBC_ENC_PARAMS * context = &((bludroid_encoder_state_t *)sbc_encoder_state_singelton->encoder_state)->context;
|
||||
return context->pu8Packet;
|
||||
}
|
||||
|
||||
uint16_t btstack_sbc_encoder_sbc_buffer_length(void){
|
||||
SBC_ENC_PARAMS * context = &((bludroid_encoder_state_t *)sbc_encoder_state_singelton->encoder_state)->context;
|
||||
return context->u16PacketLength;
|
||||
}
|
||||
|
||||
// static void btstack_sbc_encoder_dump_context(void){
|
||||
// SBC_ENC_PARAMS * context = &((bludroid_encoder_state_t *)sbc_encoder_state_singelton->encoder_state)->context;
|
||||
|
||||
// printf("Blocks %d\n", context->s16NumOfBlocks);
|
||||
// printf("SubBands %d\n", context->s16NumOfSubBands);
|
||||
// printf("Allocation Method %d\n", context->s16AllocationMethod);
|
||||
// printf("BitPool %d\n", context->s16BitPool);
|
||||
// printf("Channel Mode %d\n", context->s16ChannelMode);
|
||||
|
||||
// printf("Sample Rate ");
|
||||
// switch (context->s16SamplingFreq){
|
||||
// case 0: printf("16000\n"); break;
|
||||
// case 1: printf("32000\n"); break;
|
||||
// case 2: printf("44100\n"); break;
|
||||
// case 3: printf("48000\n"); break;
|
||||
// default: printf("not defined\n"); break;
|
||||
// }
|
||||
// printf("mSBC Enabled %d\n", context->mSBCEnabled);
|
||||
// printf("\n");
|
||||
// }
|
@ -1,114 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2014 BlueKitchen GmbH
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holders nor the names of
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* 4. Any redistribution, use, or modification is done solely for
|
||||
* personal benefit and not for any commercial purpose or for
|
||||
* monetary gain.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
|
||||
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* Please inquire about commercial licensing options at
|
||||
* contact@bluekitchen-gmbh.com
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* btstack_sbc_decoder.h
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __BTSTACK_SBC_DECODER_H
|
||||
#define __BTSTACK_SBC_DECODER_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "btstack_sbc.h"
|
||||
#include "btstack_sbc_plc.h"
|
||||
|
||||
#if defined __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
void * context;
|
||||
void (*handle_pcm_data)(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context);
|
||||
// private
|
||||
void * decoder_state;
|
||||
btstack_sbc_plc_state_t plc_state;
|
||||
btstack_sbc_mode_t mode;
|
||||
|
||||
// summary of processed good, bad and zero frames
|
||||
int good_frames_nr;
|
||||
int bad_frames_nr;
|
||||
int zero_frames_nr;
|
||||
} btstack_sbc_decoder_state_t;
|
||||
|
||||
/* API_START */
|
||||
|
||||
/**
|
||||
* @brief Init SBC decoder
|
||||
* @param state
|
||||
* @param mode
|
||||
* @param callback for decoded PCM data
|
||||
* @param context provided in callback
|
||||
*/
|
||||
|
||||
void btstack_sbc_decoder_init(btstack_sbc_decoder_state_t * state, btstack_sbc_mode_t mode, void (*callback)(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context), void * context);
|
||||
|
||||
/**
|
||||
* @brief Process received SCO data
|
||||
* @param state
|
||||
* @param packet_status_flag from SCO packet: 0 = OK, 1 = possibly invalid data, 2 = no data received, 3 = data partially lost
|
||||
* @param buffer
|
||||
* @param size
|
||||
*/
|
||||
void btstack_sbc_decoder_process_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, uint8_t * buffer, int size);
|
||||
|
||||
/**
|
||||
* @brief Get number of samples per SBC frame
|
||||
*/
|
||||
int btstack_sbc_decoder_num_samples_per_frame(btstack_sbc_decoder_state_t * state);
|
||||
|
||||
/*
|
||||
* @brief Get number of channels
|
||||
*/
|
||||
int btstack_sbc_decoder_num_channels(btstack_sbc_decoder_state_t * state);
|
||||
|
||||
/*
|
||||
* @brief Get sample rate in hz
|
||||
*/
|
||||
int btstack_sbc_decoder_sample_rate(btstack_sbc_decoder_state_t * state);
|
||||
|
||||
/* API_END */
|
||||
|
||||
// testing only
|
||||
void btstack_sbc_decoder_test_disable_plc(void);
|
||||
void btstack_sbc_decoder_test_simulate_corrupt_frames(int period);
|
||||
|
||||
#if defined __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // __BTSTACK_SBC_DECODER_H
|
@ -1,168 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2014 BlueKitchen GmbH
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holders nor the names of
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* 4. Any redistribution, use, or modification is done solely for
|
||||
* personal benefit and not for any commercial purpose or for
|
||||
* monetary gain.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
|
||||
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* Please inquire about commercial licensing options at
|
||||
* contact@bluekitchen-gmbh.com
|
||||
*
|
||||
*/
|
||||
|
||||
// *****************************************************************************
|
||||
//
|
||||
// SBC encoder based on Bludroid library
|
||||
//
|
||||
// *****************************************************************************
|
||||
|
||||
#include "btstack_config.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "sbc_encoder.h"
|
||||
#include "btstack_sbc_encoder.h"
|
||||
#include "btstack.h"
|
||||
|
||||
#define SBC_MAX_CHANNELS 2
|
||||
|
||||
#define mSBC_SYNCWORD 0xad
|
||||
#define SBC_SYNCWORD 0x9c
|
||||
|
||||
// #define LOG_FRAME_STATUS
|
||||
|
||||
typedef struct {
|
||||
SBC_ENC_PARAMS context;
|
||||
int num_data_bytes;
|
||||
uint8_t sbc_packet[1000];
|
||||
} bludroid_encoder_state_t;
|
||||
|
||||
|
||||
static btstack_sbc_encoder_state_t * sbc_state_singelton = NULL;
|
||||
static bludroid_encoder_state_t bd_state;
|
||||
|
||||
void btstack_sbc_encoder_dump_context(void){
|
||||
SBC_ENC_PARAMS * context = &((bludroid_encoder_state_t *)sbc_state_singelton->encoder_state)->context;
|
||||
|
||||
printf("Blocks %d\n", context->s16NumOfBlocks);
|
||||
printf("SubBands %d\n", context->s16NumOfSubBands);
|
||||
printf("Allocation Method %d\n", context->s16AllocationMethod);
|
||||
printf("BitPool %d\n", context->s16BitPool);
|
||||
printf("Channel Mode %d\n", context->s16ChannelMode);
|
||||
|
||||
printf("Sample Rate ");
|
||||
switch (context->s16SamplingFreq){
|
||||
case 0: printf("16000\n"); break;
|
||||
case 1: printf("32000\n"); break;
|
||||
case 2: printf("44100\n"); break;
|
||||
case 3: printf("48000\n"); break;
|
||||
default: printf("not defined\n"); break;
|
||||
}
|
||||
printf("mSBC Enabled %d\n", context->mSBCEnabled);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
int btstack_sbc_encoder_num_audio_samples(void){
|
||||
SBC_ENC_PARAMS * context = &((bludroid_encoder_state_t *)sbc_state_singelton->encoder_state)->context;
|
||||
return context->s16NumOfSubBands * context->s16NumOfBlocks * context->s16NumOfChannels;
|
||||
}
|
||||
|
||||
uint8_t * btstack_sbc_encoder_sbc_buffer(void){
|
||||
SBC_ENC_PARAMS * context = &((bludroid_encoder_state_t *)sbc_state_singelton->encoder_state)->context;
|
||||
return context->pu8Packet;
|
||||
}
|
||||
|
||||
|
||||
uint16_t btstack_sbc_encoder_sbc_buffer_length(void){
|
||||
SBC_ENC_PARAMS * context = &((bludroid_encoder_state_t *)sbc_state_singelton->encoder_state)->context;
|
||||
return context->u16PacketLength;
|
||||
}
|
||||
|
||||
|
||||
void btstack_sbc_encoder_init(btstack_sbc_encoder_state_t * state, btstack_sbc_mode_t mode,
|
||||
int blocks, int subbands, int allmethod, int sample_rate, int bitpool){
|
||||
|
||||
if (sbc_state_singelton && sbc_state_singelton != state ){
|
||||
log_error("SBC encoder: different sbc decoder state is allready registered");
|
||||
}
|
||||
|
||||
sbc_state_singelton = state;
|
||||
sbc_state_singelton->mode = mode;
|
||||
|
||||
switch (sbc_state_singelton->mode){
|
||||
case SBC_MODE_STANDARD:
|
||||
bd_state.context.s16NumOfBlocks = blocks;
|
||||
bd_state.context.s16NumOfSubBands = subbands;
|
||||
bd_state.context.s16AllocationMethod = allmethod;
|
||||
bd_state.context.s16BitPool = 31;
|
||||
bd_state.context.mSBCEnabled = 0;
|
||||
|
||||
switch(sample_rate){
|
||||
case 16000: bd_state.context.s16SamplingFreq = SBC_sf16000; break;
|
||||
case 32000: bd_state.context.s16SamplingFreq = SBC_sf32000; break;
|
||||
case 44100: bd_state.context.s16SamplingFreq = SBC_sf44100; break;
|
||||
case 48000: bd_state.context.s16SamplingFreq = SBC_sf48000; break;
|
||||
default: bd_state.context.s16SamplingFreq = 0; break;
|
||||
}
|
||||
break;
|
||||
case SBC_MODE_mSBC:
|
||||
bd_state.context.s16NumOfBlocks = 15;
|
||||
bd_state.context.s16NumOfSubBands = 8;
|
||||
bd_state.context.s16AllocationMethod = SBC_LOUDNESS;
|
||||
bd_state.context.s16BitPool = 26;
|
||||
bd_state.context.s16ChannelMode = SBC_MONO;
|
||||
bd_state.context.s16NumOfChannels = 1;
|
||||
bd_state.context.mSBCEnabled = 1;
|
||||
bd_state.context.s16SamplingFreq = SBC_sf16000;
|
||||
break;
|
||||
}
|
||||
bd_state.context.pu8Packet = bd_state.sbc_packet;
|
||||
|
||||
sbc_state_singelton->encoder_state = &bd_state;
|
||||
SBC_ENC_PARAMS * context = &((bludroid_encoder_state_t *)sbc_state_singelton->encoder_state)->context;
|
||||
SBC_Encoder_Init(context);
|
||||
}
|
||||
|
||||
|
||||
void btstack_sbc_encoder_process_data(int16_t * input_buffer){
|
||||
if (!sbc_state_singelton){
|
||||
log_error("SBC encoder: sbc state is NULL, call btstack_sbc_encoder_init to initialize it");
|
||||
}
|
||||
SBC_ENC_PARAMS * context = &((bludroid_encoder_state_t *)sbc_state_singelton->encoder_state)->context;
|
||||
context->ps16PcmBuffer = input_buffer;
|
||||
if (context->mSBCEnabled){
|
||||
context->pu8Packet[0] = 0xad;
|
||||
}
|
||||
SBC_Encoder(context);
|
||||
}
|
@ -46,7 +46,7 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "btstack_debug.h"
|
||||
#include "btstack_sbc_encoder.h"
|
||||
#include "btstack_sbc.h"
|
||||
#include "hfp_msbc.h"
|
||||
|
||||
#define MSBC_FRAME_SIZE 57
|
||||
|
@ -48,8 +48,6 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "btstack_sbc_encoder.h"
|
||||
|
||||
#if defined __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
@ -36,40 +36,52 @@
|
||||
*/
|
||||
|
||||
/*
|
||||
* btstack_sbc_encoder.h
|
||||
* sbc_decoder.h
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __BTSTACK_SBC_ENCODER_H
|
||||
#define __BTSTACK_SBC_ENCODER_H
|
||||
#ifndef __SBC_DECODER_H
|
||||
#define __SBC_DECODER_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "btstack_sbc.h"
|
||||
#include "btstack_sbc_plc.h"
|
||||
|
||||
#if defined __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum{
|
||||
SBC_MODE_STANDARD,
|
||||
SBC_MODE_mSBC
|
||||
} sbc_mode_t;
|
||||
|
||||
typedef struct {
|
||||
void * context;
|
||||
void (*handle_pcm_data)(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context);
|
||||
// private
|
||||
void * encoder_state;
|
||||
btstack_sbc_mode_t mode;
|
||||
} btstack_sbc_encoder_state_t;
|
||||
void * decoder_state;
|
||||
sbc_plc_state_t plc_state;
|
||||
sbc_mode_t mode;
|
||||
|
||||
void btstack_sbc_encoder_init(btstack_sbc_encoder_state_t * state, btstack_sbc_mode_t mode,
|
||||
int blocks, int subbands, int allmethod, int sample_rate, int bitpool);
|
||||
// summary of processed good, bad and zero frames
|
||||
int good_frames_nr;
|
||||
int bad_frames_nr;
|
||||
int zero_frames_nr;
|
||||
} sbc_decoder_state_t;
|
||||
|
||||
void btstack_sbc_encoder_process_data(int16_t * input_buffer);
|
||||
void sbc_decoder_init(sbc_decoder_state_t * state, sbc_mode_t mode, void (*callback)(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context), void * context);
|
||||
void sbc_decoder_process_data(sbc_decoder_state_t * state, uint8_t * buffer, int size);
|
||||
|
||||
int sbc_decoder_num_samples_per_frame(sbc_decoder_state_t * state);
|
||||
int sbc_decoder_num_channels(sbc_decoder_state_t * state);
|
||||
int sbc_decoder_sample_rate(sbc_decoder_state_t * state);
|
||||
|
||||
uint8_t * btstack_sbc_encoder_sbc_buffer(void);
|
||||
uint16_t btstack_sbc_encoder_sbc_buffer_length(void);
|
||||
|
||||
int btstack_sbc_encoder_num_audio_samples(void);
|
||||
void btstack_sbc_encoder_dump_context(void);
|
||||
// testing only
|
||||
void sbc_decoder_test_disable_plc(void);
|
||||
void sbc_decoder_test_simulate_corrupt_frames(int period);
|
||||
|
||||
#if defined __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // __BTSTACK_SBC_ENCODER_H
|
||||
#endif // __SBC_DECODER_H
|
@ -9,10 +9,10 @@ include ${SBC_ENCODER_ROOT}/Makefile.inc
|
||||
|
||||
SBC_DECODER += \
|
||||
${BTSTACK_ROOT}/src/classic/btstack_sbc_plc.c \
|
||||
${BTSTACK_ROOT}/src/classic/btstack_sbc_decoder_bludroid.c \
|
||||
${BTSTACK_ROOT}/src/classic/btstack_sbc_bludroid.c \
|
||||
|
||||
SBC_ENCODER += \
|
||||
${BTSTACK_ROOT}/src/classic/btstack_sbc_encoder_bludroid.c \
|
||||
${BTSTACK_ROOT}/src/classic/btstack_sbc_bludroid.c \
|
||||
${BTSTACK_ROOT}/src/classic/hfp_msbc.c \
|
||||
|
||||
SBC_DECODER_OBJ = $(SBC_DECODER:.c=.o)
|
||||
@ -38,10 +38,10 @@ SBC_TESTS = sbc_decoder_test sbc_encoder_test
|
||||
|
||||
all: ${SBC_TESTS}
|
||||
|
||||
sbc_decoder_test: ${SBC_DECODER_OBJ} ${COMMON_OBJ} sbc_decoder_test.o
|
||||
sbc_decoder_test: ${SBC_DECODER_OBJ} ${SBC_ENCODER_OBJ} ${COMMON_OBJ} sbc_decoder_test.o
|
||||
${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@
|
||||
|
||||
sbc_encoder_test: ${SBC_ENCODER_OBJ} ${COMMON_OBJ} sbc_encoder_test.o
|
||||
sbc_encoder_test: ${SBC_DECODER_OBJ} ${SBC_ENCODER_OBJ} ${COMMON_OBJ} sbc_encoder_test.o
|
||||
${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@
|
||||
|
||||
test: all
|
||||
|
@ -53,7 +53,7 @@
|
||||
#include "oi_assert.h"
|
||||
|
||||
#include "btstack.h"
|
||||
#include "btstack_sbc_decoder.h"
|
||||
#include "btstack_sbc.h"
|
||||
|
||||
static uint8_t read_buffer[24];
|
||||
|
||||
|
@ -51,9 +51,9 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "btstack.h"
|
||||
#include "btstack_sbc_encoder.h"
|
||||
#include "hfp_msbc.h"
|
||||
|
||||
#include "hfp_msbc.h"
|
||||
#include "btstack_sbc.h"
|
||||
|
||||
static int num_frames = 0;
|
||||
|
||||
@ -129,9 +129,6 @@ int main (int argc, const char * argv[]){
|
||||
fwrite(output_buffer, 1, sizeof(output_buffer), sbc_fd);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
btstack_sbc_encoder_dump_context();
|
||||
|
||||
printf("Done, frame count %d \n", num_frames);
|
||||
close(wav_fd);
|
||||
|
Loading…
x
Reference in New Issue
Block a user