Debian Codegraph @

openaptxenc.c

raw symbols ◀ ▶ SCIP
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, "\n");
66 fprintf(stderr, " %s < sample.s24le > sample.aptx\n", argv[0]);
•67 fprintf(stderr, "\n");
68 fprintf(stderr, " %s --hd < sample.s24le > sample.aptxhd\n", argv[0]);
•69 fprintf(stderr, "\n");
70 fprintf(stderr, " sox sample.wav -t raw -r 44.1k -L -e s -b 24 -c 2 - | %s > sample.aptx\n", argv[0]);
71 return 1;
72 } else if (strcmp(argv[i], "--hd") == 0) {
73 hd = 1;
74 } else {
75 fprintf(stderr, "%s: Invalid option %s\n", argv[0], argv[i]);
76 return 1;
77 }
78 }
79
80 ctx = aptx_init(hd);
81 if (!ctx) {
82 fprintf(stderr, "%s: Cannot initialize aptX encoder\n", argv[0]);
83 return 1;
84 }
85
86 ret = 0;
87
88 while (!feof(stdin)) {
89 length = fread(input_buffer, 1, sizeof(input_buffer), stdin);
90 if (ferror(stdin)) {
91 fprintf(stderr, "%s: aptX encoding failed to read input data\n", argv[0]);
92 ret = 1;
93 }
94 if (length == 0)
95 break;
96 processed = aptx_encode(ctx, input_buffer, length, output_buffer, sizeof(output_buffer), &written);
97 if (processed != length) {
98 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" : "");
99 ret = 1;
100 }
101 if (fwrite(output_buffer, 1, written, stdout) != written) {
102 fprintf(stderr, "%s: aptX encoding failed to write encoded data\n", argv[0]);
103 ret = 1;
104 break;
105 }
106 if (processed != length)
107 break;
108 }
109
110 if (aptx_encode_finish(ctx, output_buffer, sizeof(output_buffer), &written)) {
111 if (fwrite(output_buffer, 1, written, stdout) != written) {
112 fprintf(stderr, "%s: aptX encoding failed to write encoded data\n", argv[0]);
113 ret = 1;
114 }
115 }
116
117 aptx_finish(ctx);
118 return ret;
119 }