| 1 |
/* |
| 2 |
* aptX decoder utility |
| 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 |
#include <stdio.h> |
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#ifdef _WIN32 |
| 24 |
#include <fcntl.h> |
| 25 |
#include <io.h> |
| 26 |
#endif |
| 27 |
|
| 28 |
#include <openaptx.h>cxx . . $ `<file>/openaptx.h`/ |
| 29 |
|
| 30 |
static unsigned char input_buffercxx . . $ input_buffer.[512*6]; |
| 31 |
static unsigned char output_buffercxx . . $ output_buffer.[512*3*2*6+3*2*4]; |
| 32 |
|
| 33 |
int maincxx . . $ main(afcd3168fc1188a4).(int argc, char *argv[]) |
| 34 |
{ |
| 35 |
int i; |
| 36 |
int hd; |
| 37 |
int ret; |
| 38 |
size_t length; |
| 39 |
size_t processed; |
| 40 |
size_t written; |
| 41 |
size_t dropped; |
| 42 |
int synced; |
| 43 |
int syncing; |
| 44 |
struct aptx_contextcxx . . $ aptx_context# *ctx; |
| 45 |
|
| 46 |
#ifdef _WIN32 |
| 47 |
_setmode(_fileno(stdin), _O_BINARY); |
| 48 |
_setmode(_fileno(stdout), _O_BINARY); |
| 49 |
#endif |
| 50 |
|
| 51 |
hd = 0; |
| 52 |
|
| 53 |
for (i = 1; i < argc; ++i) { |
| 54 |
if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(argv[i], "-h") == 0 || strcmpcxx . . $ strcmp(5ae29d726ae57f88).(argv[i], "--help") == 0) { |
| 55 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "aptX decoder utility %d.%d.%d (using libopenaptx %d.%d.%d)\n", OPENAPTX_MAJORcxx . . $ `openaptx.h:23:9`!, OPENAPTX_MINORcxx . . $ `openaptx.h:24:9`!, OPENAPTX_PATCHcxx . . $ `openaptx.h:25:9`!, aptx_majorcxx . . $ aptx_major., aptx_minorcxx . . $ aptx_minor., aptx_patchcxx . . $ aptx_patch.); |
| 56 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "\n"); |
| 57 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "This utility decodes aptX or aptX HD audio stream\n"); |
| 58 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "from stdin to a raw 24 bit signed stereo on stdout\n"); |
| 59 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "\n"); |
| 60 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "When input is damaged it tries to synchronize and recover\n"); |
| 61 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "\n"); |
| 62 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Non-zero return value indicates that input was damaged\n"); |
| 63 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "and some bytes from input aptX audio stream were dropped\n"); |
| 64 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "\n"); |
| 65 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Usage:\n"); |
| 66 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, " %s [options]\n", argv[0]); |
| 67 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "\n"); |
| 68 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Options:\n"); |
| 69 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, " -h, --help Display this help\n"); |
| 70 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, " --hd Decode from aptX HD\n"); |
| 71 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "\n"); |
| 72 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Examples:\n"); |
| •73 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "\n"); |
| 74 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, " %s < sample.aptx > sample.s24le\n", argv[0]); |
| •75 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "\n"); |
| 76 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, " %s --hd < sample.aptxhd > sample.s24le\n", argv[0]); |
| •77 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "\n"); |
| 78 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, " %s < sample.aptx | play -t raw -r 44.1k -L -e s -b 24 -c 2 -\n", argv[0]); |
| 79 |
return 1; |
| 80 |
} else if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(argv[i], "--hd") == 0) { |
| 81 |
hd = 1; |
| 82 |
} else { |
| 83 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: Invalid option %s\n", argv[0], argv[i]); |
| 84 |
return 1; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
ctx = aptx_initcxx . . $ aptx_init(212766b5e5b264ef).(hd); |
| 89 |
if (!ctx) { |
| 90 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: Cannot initialize aptX decoder\n", argv[0]); |
| 91 |
return 1; |
| 92 |
} |
| 93 |
|
| 94 |
/* |
| 95 |
* Try to guess type of input stream based on the first six bytes |
| 96 |
* Encoder produces fixed first sample because aptX predictor has fixed values |
| 97 |
*/ |
| 98 |
length = freadcxx . . $ fread(738fc8ac1d711e58).(input_buffercxx . . $ input_buffer., 1, 6, stdincxx . . $ `<external>/232824719582829767/stdio.h:153:9`!); |
| 99 |
if (length >= 4 && memcmpcxx . . $ memcmp(36f949851efa3055).(input_buffercxx . . $ input_buffer., "\x4b\xbf\x4b\xbf", 4) == 0) { |
| 100 |
if (hd) |
| 101 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: Input looks like start of aptX audio stream (not aptX HD), try without --hd\n", argv[0]); |
| 102 |
} else if (length >= 6 && memcmpcxx . . $ memcmp(36f949851efa3055).(input_buffercxx . . $ input_buffer., "\x73\xbe\xff\x73\xbe\xff", 6) == 0) { |
| 103 |
if (!hd) |
| 104 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: Input looks like start of aptX HD audio stream, try with --hd\n", argv[0]); |
| 105 |
} else { |
| 106 |
if (length >= 4 && memcmpcxx . . $ memcmp(36f949851efa3055).(input_buffercxx . . $ input_buffer., "\x6b\xbf\x6b\xbf", 4) == 0) |
| 107 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: Input looks like start of standard aptX audio stream, which is not supported yet\n", argv[0]); |
| 108 |
else |
| 109 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: Input does not look like start of aptX nor aptX HD audio stream\n", argv[0]); |
| 110 |
} |
| 111 |
|
| 112 |
ret = 0; |
| 113 |
syncing = 0; |
| 114 |
|
| 115 |
while (length > 0) { |
| 116 |
processed = aptx_decode_synccxx . . $ aptx_decode_sync(9bb8d8be730f07ef).(ctx, input_buffercxx . . $ input_buffer., length, output_buffercxx . . $ output_buffer., sizeof(output_buffercxx . . $ output_buffer.), &written, &synced, &dropped); |
| 117 |
|
| 118 |
/* Check all possible states of synced, syncing and dropped status */ |
| 119 |
if (!synced) { |
| 120 |
if (!syncing) { |
| 121 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: aptX decoding failed, synchronizing\n", argv[0]); |
| 122 |
syncing = 1; |
| 123 |
ret = 1; |
| 124 |
} |
| 125 |
if (dropped) { |
| 126 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: aptX synchronization successful, dropped %lu byte%s\n", argv[0], (unsigned long)dropped, (dropped != 1) ? "s" : ""); |
| 127 |
syncing = 0; |
| 128 |
ret = 1; |
| 129 |
} |
| 130 |
if (!syncing) { |
| 131 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: aptX decoding failed, synchronizing\n", argv[0]); |
| 132 |
syncing = 1; |
| 133 |
ret = 1; |
| 134 |
} |
| 135 |
} else { |
| 136 |
if (dropped) { |
| 137 |
if (!syncing) |
| 138 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: aptX decoding failed, synchronizing\n", argv[0]); |
| 139 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: aptX synchronization successful, dropped %lu byte%s\n", argv[0], (unsigned long)dropped, (dropped != 1) ? "s" : ""); |
| 140 |
syncing = 0; |
| 141 |
ret = 1; |
| 142 |
} else if (syncing) { |
| 143 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: aptX synchronization successful\n", argv[0]); |
| 144 |
syncing = 0; |
| 145 |
ret = 1; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/* If we have not decoded all supplied samples then decoding unrecoverable failed */ |
| 150 |
if (processed != length) { |
| 151 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: aptX decoding failed\n", argv[0]); |
| 152 |
ret = 1; |
| 153 |
break; |
| 154 |
} |
| 155 |
|
| 156 |
if (!feofcxx . . $ feof(89bc1efcdac0ca22).(stdincxx . . $ `<external>/232824719582829767/stdio.h:153:9`!)) { |
| 157 |
length = freadcxx . . $ fread(738fc8ac1d711e58).(input_buffercxx . . $ input_buffer., 1, sizeof(input_buffercxx . . $ input_buffer.), stdincxx . . $ `<external>/232824719582829767/stdio.h:153:9`!); |
| 158 |
if (ferrorcxx . . $ ferror(89bc1efcdac0ca22).(stdincxx . . $ `<external>/232824719582829767/stdio.h:153:9`!)) { |
| 159 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: aptX decoding failed to read input data\n", argv[0]); |
| 160 |
ret = 1; |
| 161 |
length = 0; |
| 162 |
} |
| 163 |
} else { |
| 164 |
length = 0; |
| 165 |
} |
| 166 |
|
| 167 |
/* On the end of the input stream last two decoded samples are just padding and not a real data */ |
| 168 |
if (length == 0 && !ferrorcxx . . $ ferror(89bc1efcdac0ca22).(stdincxx . . $ `<external>/232824719582829767/stdio.h:153:9`!) && written >= 6*2) |
| 169 |
written -= 6*2; |
| 170 |
|
| 171 |
if (written > 0) { |
| 172 |
if (fwritecxx . . $ fwrite(ab0609fa07c47d6e).(output_buffercxx . . $ output_buffer., 1, written, stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!) != written) { |
| 173 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: aptX decoding failed to write decoded data\n", argv[0]); |
| 174 |
ret = 1; |
| 175 |
break; |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
dropped = aptx_decode_sync_finishcxx . . $ aptx_decode_sync_finish(102e020e261e8b9e).(ctx); |
| 181 |
if (dropped && !syncing) { |
| 182 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: aptX decoding stopped in the middle of the sample, dropped %lu byte%s\n", argv[0], (unsigned long)dropped, (dropped != 1) ? "s" : ""); |
| 183 |
ret = 1; |
| 184 |
} else if (syncing) { |
| 185 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: aptX synchronization failed\n", argv[0]); |
| 186 |
ret = 1; |
| 187 |
} |
| 188 |
|
| 189 |
aptx_finishcxx . . $ aptx_finish(b3e9d6ffbe9cc111).(ctx); |
| 190 |
return ret; |
| 191 |
} |