@@ -78,9 +78,10 @@ enum subbands {
78
78
79
79
#define NB_FILTERS 2
80
80
#define FILTER_TAPS 16
81
+ #define LATENCY_SAMPLES 90
81
82
82
83
struct aptx_filter_signal {
83
- int pos ;
84
+ uint8_t pos ;
84
85
int32_t buffer [2 * FILTER_TAPS ];
85
86
};
86
87
@@ -124,8 +125,10 @@ struct aptx_channel {
124
125
} ;
125
126
126
127
struct aptx_context {
127
- int hd ;
128
- int32_t sync_idx ;
128
+ uint8_t hd ;
129
+ uint8_t sync_idx ;
130
+ uint8_t encode_remaining ;
131
+ uint8_t decode_skip_leading ;
129
132
struct aptx_channel channels [NB_CHANNELS ];
130
133
};
131
134
@@ -900,20 +903,19 @@ static int32_t aptx_quantized_parity(struct aptx_channel *channel)
900
903
901
904
/* For each sample, ensure that the parity of all subbands of all channels
902
905
* is 0 except once every 8 samples where the parity is forced to 1. */
903
- static int aptx_check_parity (struct aptx_channel channels [NB_CHANNELS ], int32_t * idx )
906
+ static int aptx_check_parity (struct aptx_channel channels [NB_CHANNELS ], uint8_t * sync_idx )
904
907
{
905
908
int32_t parity = aptx_quantized_parity (& channels [LEFT ])
906
909
^ aptx_quantized_parity (& channels [RIGHT ]);
910
+ int32_t eighth = * sync_idx == 7 ;
907
911
908
- int eighth = * idx == 7 ;
909
- * idx = (* idx + 1 ) & 7 ;
910
-
912
+ * sync_idx = (* sync_idx + 1 ) & 7 ;
911
913
return parity ^ eighth ;
912
914
}
913
915
914
- static void aptx_insert_sync (struct aptx_channel channels [NB_CHANNELS ], int32_t * idx )
916
+ static void aptx_insert_sync (struct aptx_channel channels [NB_CHANNELS ], uint8_t * sync_idx )
915
917
{
916
- if (aptx_check_parity (channels , idx )) {
918
+ if (aptx_check_parity (channels , sync_idx )) {
917
919
int i ;
918
920
struct aptx_channel * c ;
919
921
static const int map [] = { 1 , 2 , 0 , 3 };
@@ -1022,6 +1024,10 @@ static int aptx_decode_samples(struct aptx_context *ctx,
1022
1024
}
1023
1025
1024
1026
1027
+ int aptx_major = OPENAPTX_MAJOR ;
1028
+ int aptx_minor = OPENAPTX_MINOR ;
1029
+ int aptx_patch = OPENAPTX_PATCH ;
1030
+
1025
1031
struct aptx_context * aptx_init (int hd )
1026
1032
{
1027
1033
struct aptx_context * ctx ;
@@ -1043,6 +1049,8 @@ void aptx_reset(struct aptx_context *ctx)
1043
1049
hd = ctx -> hd ;
1044
1050
memset (ctx , 0 , sizeof (* ctx ));
1045
1051
ctx -> hd = hd ;
1052
+ ctx -> decode_skip_leading = (LATENCY_SAMPLES + 3 )/4 ;
1053
+ ctx -> encode_remaining = (LATENCY_SAMPLES + 3 )/4 ;
1046
1054
1047
1055
for (chan = 0 ; chan < NB_CHANNELS ; chan ++ ) {
1048
1056
struct aptx_channel * channel = & ctx -> channels [chan ];
@@ -1071,7 +1079,7 @@ size_t aptx_encode(struct aptx_context *ctx, const unsigned char *input, size_t
1071
1079
for (ipos = 0 , opos = 0 ; ipos + 3 * NB_CHANNELS * 4 <= input_size && opos + sample_size <= output_size ; opos += sample_size ) {
1072
1080
for (sample = 0 ; sample < 4 ; sample ++ ) {
1073
1081
for (channel = 0 ; channel < NB_CHANNELS ; channel ++ , ipos += 3 ) {
1074
- /* samples need to contain 24bit signed intger stored as 32bit signed integers */
1082
+ /* samples need to contain 24bit signed integer stored as 32bit signed integers */
1075
1083
/* last int8_t --> uint32_t cast propagates sign bit for 32bit integer */
1076
1084
samples [channel ][sample ] = ((uint32_t )input [ipos + 0 ] << 0 ) |
1077
1085
((uint32_t )input [ipos + 1 ] << 8 ) |
@@ -1085,6 +1093,31 @@ size_t aptx_encode(struct aptx_context *ctx, const unsigned char *input, size_t
1085
1093
return ipos ;
1086
1094
}
1087
1095
1096
+ int aptx_encode_finish (struct aptx_context * ctx , unsigned char * output , size_t output_size , size_t * written )
1097
+ {
1098
+ const int32_t samples [NB_CHANNELS ][4 ] = { };
1099
+ int sample_size ;
1100
+ size_t opos ;
1101
+
1102
+ sample_size = ctx -> hd ? 6 : 4 ;
1103
+
1104
+ if (ctx -> encode_remaining == 0 ) {
1105
+ * written = 0 ;
1106
+ return 1 ;
1107
+ }
1108
+
1109
+ for (opos = 0 ; ctx -> encode_remaining > 0 && opos + sample_size <= output_size ; ctx -> encode_remaining -- , opos += sample_size )
1110
+ aptx_encode_samples (ctx , samples , output + opos );
1111
+
1112
+ * written = opos ;
1113
+
1114
+ if (ctx -> encode_remaining > 0 )
1115
+ return 0 ;
1116
+
1117
+ aptx_reset (ctx );
1118
+ return 1 ;
1119
+ }
1120
+
1088
1121
size_t aptx_decode (struct aptx_context * ctx , const unsigned char * input , size_t input_size , unsigned char * output , size_t output_size , size_t * written )
1089
1122
{
1090
1123
int32_t samples [NB_CHANNELS ][4 ];
@@ -1097,7 +1130,14 @@ size_t aptx_decode(struct aptx_context *ctx, const unsigned char *input, size_t
1097
1130
for (ipos = 0 , opos = 0 ; ipos + sample_size <= input_size && opos + 3 * NB_CHANNELS * 4 <= output_size ; ipos += sample_size ) {
1098
1131
if (aptx_decode_samples (ctx , input + ipos , samples ))
1099
1132
break ;
1100
- for (sample = 0 ; sample < 4 ; sample ++ ) {
1133
+ sample = 0 ;
1134
+ if (ctx -> decode_skip_leading > 0 ) {
1135
+ ctx -> decode_skip_leading -- ;
1136
+ if (ctx -> decode_skip_leading > 0 )
1137
+ continue ;
1138
+ sample = LATENCY_SAMPLES %4 ;
1139
+ }
1140
+ for (; sample < 4 ; sample ++ ) {
1101
1141
for (channel = 0 ; channel < NB_CHANNELS ; channel ++ , opos += 3 ) {
1102
1142
/* samples contain 24bit signed integers stored as 32bit signed integers */
1103
1143
/* we do not need to care about negative integers specially as they have 24. bit set */
0 commit comments