Debian Codegraph @

openaptxenc.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 encoder 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*3*2*4];
31 static unsigned char output_buffer[512*6];
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 struct aptx_context *ctx;
42
43 #ifdef _WIN32
44 _setmode(_fileno(stdin), _O_BINARY);
45 _setmode(_fileno(stdout), _O_BINARY);
46 #endif
47
48 hd = 0;
49
50 for (i = 1; i < argc; ++i) {
51 if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
52 fprintf(stderr, "aptX encoder utility %d.%d.%d (using libopenaptx %d.%d.%d)\n", OPENAPTX_MAJOR, OPENAPTX_MINOR, OPENAPTX_PATCH, aptx_major, aptx_minor, aptx_patch);
53 fprintf(stderr, "\n");
54 fprintf(stderr, "This utility encodes a raw 24 bit signed stereo\n");
55 fprintf(stderr, "samples from stdin to aptX or aptX HD on stdout\n");
56 fprintf(stderr, "\n");
57 fprintf(stderr, "Usage:\n");
58 fprintf(stderr, " %s [options]\n", argv[0]);
59 fprintf(stderr, "\n");
60 fprintf(stderr, "Options:\n");
61 fprintf(stderr, " -h, --help Display this help\n");
62 fprintf(stderr, " --hd Encode to aptX HD\n");
63 fprintf(stderr, "\n");
64 fprintf(stderr, "Examples:\n");
65 fprintf(stderr, " %s < sample.s24le > sample.aptx\n", argv[0]);
66 fprintf(stderr, " %s --hd < sample.s24le > sample.aptxhd\n", argv[0]);
67 fprintf(stderr, " sox sample.wav -t raw -r 44.1k -L -e s -b 24 -c 2 - | %s > sample.aptx\n", argv[0]);
68 return 1;
69 } else if (strcmp(argv[i], "--hd") == 0) {
70 hd = 1;
71 } else {
72 fprintf(stderr, "%s: Invalid option %s\n", argv[0], argv[i]);
73 return 1;
74 }
75 }
76
77 ctx = aptx_init(hd);
78 if (!ctx) {
79 fprintf(stderr, "%s: Cannot initialize aptX encoder\n", argv[0]);
80 return 1;
81 }
82
83 ret = 0;
84
85 while (!feof(stdin)) {
86 length = fread(input_buffer, 1, sizeof(input_buffer), stdin);
87 if (ferror(stdin)) {
88 fprintf(stderr, "%s: aptX encoding failed to read input data\n", argv[0]);
89 ret = 1;
90 }
91 if (length == 0)
92 break;
93 processed = aptx_encode(ctx, input_buffer, length, output_buffer, sizeof(output_buffer), &written);
94 if (processed != length) {
95 fprintf(stderr, "%s: aptX encoding stopped in the middle of the sample, dropped %lu byte%s\n", argv[0], (unsigned long)(length-processed), (length-processed != 1) ? "s" : "");
96 ret = 1;
97 }
98 if (fwrite(output_buffer, 1, written, stdout) != written) {
99 fprintf(stderr, "%s: aptX encoding failed to write encoded data\n", argv[0]);
100 ret = 1;
101 break;
102 }
103 if (processed != length)
104 break;
105 }
106
107 if (aptx_encode_finish(ctx, output_buffer, sizeof(output_buffer), &written)) {
108 if (fwrite(output_buffer, 1, written, stdout) != written) {
109 fprintf(stderr, "%s: aptX encoding failed to write encoded data\n", argv[0]);
110 ret = 1;
111 }
112 }
113
114 aptx_finish(ctx);
115 return ret;
116 }