| 1 | /* |
| 2 | * Open Source implementation of Audio Processing Technology codec (aptX) |
| 3 | * Copyright (C) 2018-2020 Pali Rohár <[email protected]> |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #ifndef OPENAPTX_H |
| 21 | #define OPENAPTX_H |
| 22 | |
| 23 | #define OPENAPTX_MAJOR 0 |
| 24 | #define OPENAPTX_MINOR 2 |
| 25 | #define OPENAPTX_PATCH 0 |
| 26 | |
| 27 | #include <stddef.h> |
| 28 | |
| 29 | extern const int aptx_major; |
| 30 | extern const int aptx_minor; |
| 31 | extern const int aptx_patch; |
| 32 | |
| 33 | struct aptx_context; |
| 34 | |
| 35 | /* |
| 36 | * Initialize context for aptX codec and reset it. |
| 37 | * hd = 0 process aptX codec |
| 38 | * hd = 1 process aptX HD codec |
| 39 | */ |
| 40 | struct aptx_context *aptx_init(int hd); |
| 41 | |
| 42 | /* |
| 43 | * Reset internal state, predictor and parity sync of aptX context. |
| 44 | * It is needed when going to encode or decode a new stream. |
| 45 | */ |
| 46 | void aptx_reset(struct aptx_context *ctx); |
| 47 | |
| 48 | /* |
| 49 | * Free aptX context initialized by aptx_init(). |
| 50 | */ |
| 51 | void aptx_finish(struct aptx_context *ctx); |
| 52 | |
| 53 | /* |
| 54 | * Encodes sequence of 4 raw 24bit signed stereo samples from input buffer with |
| 55 | * size input_size to aptX audio samples into output buffer with output_size. |
| 56 | * Return value indicates processed length from input buffer and to written |
| 57 | * pointer is stored length of encoded aptX audio samples in output buffer. |
| 58 | * Therefore input buffer must contain sequence of the 24 bytes in format |
| 59 | * LLLRRRLLLRRRLLLRRRLLLRRR (L-left, R-right) and output buffer would contain |
| 60 | * encoded sequence of either four bytes (LLRR) of aptX or six bytes (LLLRRR) |
| 61 | * of aptX HD. |
| 62 | */ |
| 63 | size_t aptx_encode(struct aptx_context *ctx, |
| 64 | const unsigned char *input, |
| 65 | size_t input_size, |
| 66 | unsigned char *output, |
| 67 | size_t output_size, |
| 68 | size_t *written); |
| 69 | |
| 70 | /* |
| 71 | * Finish encoding of current stream and reset internal state to be ready for |
| 72 | * encoding or decoding a new stream. Due to aptX latency, last 90 samples |
| 73 | * (rounded to 92) will be filled by this finish function. When output buffer is |
| 74 | * too small, this function returns zero, fills buffer only partially, does not |
| 75 | * reset internal state and subsequent calls continue filling output buffer. |
| 76 | * When output buffer is large enough, then function returns non-zero value. |
| 77 | * In both cases into written pointer is stored length of encoded samples. |
| 78 | */ |
| 79 | int aptx_encode_finish(struct aptx_context *ctx, |
| 80 | unsigned char *output, |
| 81 | size_t output_size, |
| 82 | size_t *written); |
| 83 | |
| 84 | /* |
| 85 | * Decodes aptX audio samples in input buffer with size input_size to sequence |
| 86 | * of raw 24bit signed stereo samples into output buffer with size output_size. |
| 87 | * Return value indicates processed length from input buffer and to written |
| 88 | * pointer is stored length of decoded output samples in output buffer. |
| 89 | * Input buffer must contain seqeunce of four bytes (LLRR) of aptX or six |
| 90 | * bytes (LLLRRR) of aptX HD samples and output buffer would contain decoded |
| 91 | * sequence of 24 bytes in format LLLRRRLLLRRRLLLRRRLLLRRR (L-left, R-right) |
| 92 | * for one aptX sample. Due to aptX latency, output buffer starts filling |
| 93 | * after 90 samples. When parity check fails then this function stops decoding |
| 94 | * and returns processed length of input buffer. To detect such failure it is |
| 95 | * needed to compare return value and input_size. Note that if you have a |
| 96 | * finite stream then the last two decoded samples from the last decode call |
| 97 | * does not contain any meaningful value. They are present just because aptX |
| 98 | * samples are rounded to the multiple by four and latency is 90 samples so |
| 99 | * last 2 samples are just padding. |
| 100 | */ |
| 101 | size_t aptx_decode(struct aptx_context *ctx, |
| 102 | const unsigned char *input, |
| 103 | size_t input_size, |
| 104 | unsigned char *output, |
| 105 | size_t output_size, |
| 106 | size_t *written); |
| 107 | |
| 108 | /* |
| 109 | * Auto synchronization variant of aptx_decode() function suitable for partially |
| 110 | * corrupted continuous stream in which some bytes are missing. All arguments, |
| 111 | * including return value have same meaning as for aptx_decode() function. The |
| 112 | * only difference is that there is no restriction for size of input buffer, |
| 113 | * output buffer must have space for decoding whole input buffer plus space for |
| 114 | * one additional decoded sample (24 bytes) and the last difference is that this |
| 115 | * function continue to decode even when parity check fails. When decoding fails |
| 116 | * this function starts searching for next bytes from the input buffer which |
| 117 | * have valid parity check (to be synchronized) and then starts decoding again. |
| 118 | * Into synced pointer is stored 1 if at the end of processing is decoder fully |
| 119 | * synchronized (in non-error state, with valid parity check) or is stored 0 if |
| 120 | * decoder is unsynchronized (in error state, without valid parity check). Into |
| 121 | * dropped pointer is stored number of dropped (not decoded) bytes which were |
| 122 | * already processed. Functions aptx_decode() and aptx_decode_sync() should not |
| 123 | * be mixed together. |
| 124 | */ |
| 125 | size_t aptx_decode_sync(struct aptx_context *ctx, |
| 126 | const unsigned char *input, |
| 127 | size_t input_size, |
| 128 | unsigned char *output, |
| 129 | size_t output_size, |
| 130 | size_t *written, |
| 131 | int *synced, |
| 132 | size_t *dropped); |
| 133 | |
| 134 | /* |
| 135 | * Finish decoding of current auto synchronization stream and reset internal |
| 136 | * state to be ready for encoding or decoding a new stream. This function |
| 137 | * returns number of unprocessed cached bytes which would have been processed |
| 138 | * by next aptx_decode_sync() call, therefore in time of calling this function |
| 139 | * it is number of dropped input bytes. |
| 140 | */ |
| 141 | size_t aptx_decode_sync_finish(struct aptx_context *ctx); |
| 142 | |
| 143 | #endif |