Debian Codegraph @

openaptxdec.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
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, " %s < sample.aptx > sample.s24le\n", argv[0]);
74 fprintf(stderr, " %s --hd < sample.aptxhd > sample.s24le\n", argv[0]);
75 fprintf(stderr, " %s < sample.aptx | play -t raw -r 44.1k -L -e s -b 24 -c 2 -\n", argv[0]);
76 return 1;
77 } else if (strcmp(argv[i], "--hd") == 0) {
78 hd = 1;
79 } else {
80 fprintf(stderr, "%s: Invalid option %s\n", argv[0], argv[i]);
81 return 1;
82 }
83 }
84
85 ctx = aptx_init(hd);
86 if (!ctx) {
87 fprintf(stderr, "%s: Cannot initialize aptX decoder\n", argv[0]);
88 return 1;
89 }
90
91 /*
92 * Try to guess type of input stream based on the first six bytes
93 * Encoder produces fixed first sample because aptX predictor has fixed values
94 */
95 length = fread(input_buffer, 1, 6, stdin);
96 if (length >= 4 && memcmp(input_buffer, "\x4b\xbf\x4b\xbf", 4) == 0) {
97 if (hd)
98 fprintf(stderr, "%s: Input looks like start of aptX audio stream (not aptX HD), try without --hd\n", argv[0]);
99 } else if (length >= 6 && memcmp(input_buffer, "\x73\xbe\xff\x73\xbe\xff", 6) == 0) {
100 if (!hd)
101 fprintf(stderr, "%s: Input looks like start of aptX HD audio stream, try with --hd\n", argv[0]);
102 } else {
103 if (length >= 4 && memcmp(input_buffer, "\x6b\xbf\x6b\xbf", 4) == 0)
104 fprintf(stderr, "%s: Input looks like start of standard aptX audio stream, which is not supported yet\n", argv[0]);
105 else
106 fprintf(stderr, "%s: Input does not look like start of aptX nor aptX HD audio stream\n", argv[0]);
107 }
108
109 ret = 0;
110 syncing = 0;
111
112 while (length > 0) {
113 processed = aptx_decode_sync(ctx, input_buffer, length, output_buffer, sizeof(output_buffer), &written, &synced, &dropped);
114
115 /* Check all possible states of synced, syncing and dropped status */
116 if (!synced) {
117 if (!syncing) {
118 fprintf(stderr, "%s: aptX decoding failed, synchronizing\n", argv[0]);
119 syncing = 1;
120 ret = 1;
121 }
122 if (dropped) {
123 fprintf(stderr, "%s: aptX synchronization successful, dropped %lu byte%s\n", argv[0], (unsigned long)dropped, (dropped != 1) ? "s" : "");
124 syncing = 0;
125 ret = 1;
126 }
127 if (!syncing) {
128 fprintf(stderr, "%s: aptX decoding failed, synchronizing\n", argv[0]);
129 syncing = 1;
130 ret = 1;
131 }
132 } else {
133 if (dropped) {
134 if (!syncing)
135 fprintf(stderr, "%s: aptX decoding failed, synchronizing\n", argv[0]);
136 fprintf(stderr, "%s: aptX synchronization successful, dropped %lu byte%s\n", argv[0], (unsigned long)dropped, (dropped != 1) ? "s" : "");
137 syncing = 0;
138 ret = 1;
139 } else if (syncing) {
140 fprintf(stderr, "%s: aptX synchronization successful\n", argv[0]);
141 syncing = 0;
142 ret = 1;
143 }
144 }
145
146 /* If we have not decoded all supplied samples then decoding unrecoverable failed */
147 if (processed != length) {
148 fprintf(stderr, "%s: aptX decoding failed\n", argv[0]);
149 ret = 1;
150 break;
151 }
152
153 if (!feof(stdin)) {
154 length = fread(input_buffer, 1, sizeof(input_buffer), stdin);
155 if (ferror(stdin)) {
156 fprintf(stderr, "%s: aptX decoding failed to read input data\n", argv[0]);
157 ret = 1;
158 length = 0;
159 }
160 } else {
161 length = 0;
162 }
163
164 /* On the end of the input stream last two decoded samples are just padding and not a real data */
165 if (length == 0 && !ferror(stdin) && written >= 6*2)
166 written -= 6*2;
167
168 if (written > 0) {
169 if (fwrite(output_buffer, 1, written, stdout) != written) {
170 fprintf(stderr, "%s: aptX decoding failed to write decoded data\n", argv[0]);
171 ret = 1;
172 break;
173 }
174 }
175 }
176
177 dropped = aptx_decode_sync_finish(ctx);
178 if (dropped && !syncing) {
179 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" : "");
180 ret = 1;
181 } else if (syncing) {
182 fprintf(stderr, "%s: aptX synchronization failed\n", argv[0]);
183 ret = 1;
184 }
185
186 aptx_finish(ctx);
187 return ret;
188 }