Debian Codegraph @

openaptxdec.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
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>
29
30 static unsigned char input_buffer[512*6];
31 static unsigned char output_buffer[512*3*2*6+3*2*4];
32
33 int main(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_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 (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
55 fprintf(stderr, "aptX decoder utility %d.%d.%d (using libopenaptx %d.%d.%d)\n", OPENAPTX_MAJOR, OPENAPTX_MINOR, OPENAPTX_PATCH, aptx_major, aptx_minor, aptx_patch);
56 fprintf(stderr, "\n");
57 fprintf(stderr, "This utility decodes aptX or aptX HD audio stream\n");
58 fprintf(stderr, "from stdin to a raw 24 bit signed stereo on stdout\n");
59 fprintf(stderr, "\n");
60 fprintf(stderr, "When input is damaged it tries to synchronize and recover\n");
61 fprintf(stderr, "\n");
62 fprintf(stderr, "Non-zero return value indicates that input was damaged\n");
63 fprintf(stderr, "and some bytes from input aptX audio stream were dropped\n");
64 fprintf(stderr, "\n");
65 fprintf(stderr, "Usage:\n");
66 fprintf(stderr, " %s [options]\n", argv[0]);
67 fprintf(stderr, "\n");
68 fprintf(stderr, "Options:\n");
69 fprintf(stderr, " -h, --help Display this help\n");
70 fprintf(stderr, " --hd Decode from aptX HD\n");
71 fprintf(stderr, "\n");
72 fprintf(stderr, "Examples:\n");
•73 fprintf(stderr, "\n");
74 fprintf(stderr, " %s < sample.aptx > sample.s24le\n", argv[0]);
•75 fprintf(stderr, "\n");
76 fprintf(stderr, " %s --hd < sample.aptxhd > sample.s24le\n", argv[0]);
•77 fprintf(stderr, "\n");
78 fprintf(stderr, " %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 (strcmp(argv[i], "--hd") == 0) {
81 hd = 1;
82 } else {
83 fprintf(stderr, "%s: Invalid option %s\n", argv[0], argv[i]);
84 return 1;
85 }
86 }
87
88 ctx = aptx_init(hd);
89 if (!ctx) {
90 fprintf(stderr, "%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 = fread(input_buffer, 1, 6, stdin);
99 if (length >= 4 && memcmp(input_buffer, "\x4b\xbf\x4b\xbf", 4) == 0) {
100 if (hd)
101 fprintf(stderr, "%s: Input looks like start of aptX audio stream (not aptX HD), try without --hd\n", argv[0]);
102 } else if (length >= 6 && memcmp(input_buffer, "\x73\xbe\xff\x73\xbe\xff", 6) == 0) {
103 if (!hd)
104 fprintf(stderr, "%s: Input looks like start of aptX HD audio stream, try with --hd\n", argv[0]);
105 } else {
106 if (length >= 4 && memcmp(input_buffer, "\x6b\xbf\x6b\xbf", 4) == 0)
107 fprintf(stderr, "%s: Input looks like start of standard aptX audio stream, which is not supported yet\n", argv[0]);
108 else
109 fprintf(stderr, "%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_sync(ctx, input_buffer, length, output_buffer, sizeof(output_buffer), &written, &synced, &dropped);
117
118 /* Check all possible states of synced, syncing and dropped status */
119 if (!synced) {
120 if (!syncing) {
121 fprintf(stderr, "%s: aptX decoding failed, synchronizing\n", argv[0]);
122 syncing = 1;
123 ret = 1;
124 }
125 if (dropped) {
126 fprintf(stderr, "%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 fprintf(stderr, "%s: aptX decoding failed, synchronizing\n", argv[0]);
132 syncing = 1;
133 ret = 1;
134 }
135 } else {
136 if (dropped) {
137 if (!syncing)
138 fprintf(stderr, "%s: aptX decoding failed, synchronizing\n", argv[0]);
139 fprintf(stderr, "%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 fprintf(stderr, "%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 fprintf(stderr, "%s: aptX decoding failed\n", argv[0]);
152 ret = 1;
153 break;
154 }
155
156 if (!feof(stdin)) {
157 length = fread(input_buffer, 1, sizeof(input_buffer), stdin);
158 if (ferror(stdin)) {
159 fprintf(stderr, "%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 && !ferror(stdin) && written >= 6*2)
169 written -= 6*2;
170
171 if (written > 0) {
172 if (fwrite(output_buffer, 1, written, stdout) != written) {
173 fprintf(stderr, "%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_finish(ctx);
181 if (dropped && !syncing) {
182 fprintf(stderr, "%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 fprintf(stderr, "%s: aptX synchronization failed\n", argv[0]);
186 ret = 1;
187 }
188
189 aptx_finish(ctx);
190 return ret;
191 }