forked from foo86/dcadec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcadec.c
463 lines (407 loc) · 13.5 KB
/
dcadec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
* This file is part of libdcadec.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation; either version 2.1 of the License, or (at your
* option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <inttypes.h>
#ifdef _MSC_VER
#include "getopt.h"
#else
#include <unistd.h>
#endif
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <signal.h>
#endif
#include "libdcadec/dca_stream.h"
#include "libdcadec/dca_context.h"
#include "libdcadec/dca_waveout.h"
static void print_help(const char *name)
{
fprintf(stderr,
"Usage: %s [-26bcfhilmnPqSx] <input.dts> [output.wav]\n"
"dcadec is a free DTS Coherent Acoustics decoder. Supported options:\n"
"\n"
"-2 Extract embedded 2.0 downmix if present, otherwise extract 5.1 downmix.\n"
"\n"
"-6 Extract embedded 5.1 downmix.\n"
"\n"
"-b Force fixed point DTS core interpolation. Developer option, degrades sound\n"
" quality.\n"
"\n"
"-c Force decoding of DTS core only without extensions.\n"
"\n"
"-f Use FIR filter for floating point DTS core LFE channel interpolation.\n"
"\n"
"-h Show this help message.\n"
"\n"
"-i Use IIR filter for floating point DTS core LFE channel interpolation.\n"
"\n"
"-l Enable lenient decoding mode. Attempt to recover from errors by skipping\n"
" non-decodable parts of the stream.\n"
"\n"
"-m Write a mono WAV file for each native DTS channel. Output file name must\n"
" include `%%s' sub-string that will be replaced with DTS channel name.\n"
"\n"
"-n No-act mode. Parse DTS bitstream without writing WAV file(s).\n"
"\n"
"-P Disable progress indicator.\n"
"\n"
"-q Be quiet. Disables informational messages and progress indicator. Warnings\n"
" and errors are still printed.\n"
"\n"
"-S Don't strip padding samples for streams within DTS-HD container.\n"
"\n"
"-x Force use of X96 synthesis filter for DTS core interpolation. Developer\n"
" option, degrades sound quality.\n"
"\n"
"When run without output file name argument, prints information about DTS file\n"
"to stdout and exits.\n"
"\n"
"Single dash in place of input or output file name argument means to read from\n"
"stdin or write to stdout, respectively.\n"
"\n"
"dcadec comes with ABSOLUTELY NO WARRANTY. This is free software, and you are\n"
"welcome to redistribute it under certain conditions; see GNU Lesser General\n"
"Public License version 2.1 for details.\n", name);
}
static const char * const spkr_pair_names[] = {
"C", "LR", "LsRs", "LFE",
"Cs", "LhRh", "LsrRsr", "Ch",
"Oh", "LcRc", "LwRw", "LssRss",
"LFE2", "LhsRhs", "Chr", "LhrRhr"
};
static char *make_spkr_mask_str(int mask)
{
static char buf[128];
if (!mask)
return "???";
buf[0] = 0;
for (size_t i = 0; i < sizeof(spkr_pair_names) / sizeof(spkr_pair_names[0]); i++) {
if (mask & (1 << i)) {
if (buf[0])
strcat(buf, " ");
strcat(buf, spkr_pair_names[i]);
}
}
return buf;
}
static void print_info(struct dcadec_context *context, FILE *fp)
{
struct dcadec_exss_info *exss = dcadec_context_get_exss_info(context);
if (exss && exss->profile == DCADEC_PROFILE_DS) {
dcadec_context_free_exss_info(exss);
exss = NULL;
}
if (exss) {
if (exss->profile & DCADEC_PROFILE_HD_MA)
fprintf(fp, "DTS-HD Master Audio");
else if (exss->profile & DCADEC_PROFILE_HD_HRA)
fprintf(fp, "DTS-HD High Resolution Audio");
else if (exss->profile & DCADEC_PROFILE_DS_ES)
fprintf(fp, "DTS-ES Discrete");
else if (exss->profile & DCADEC_PROFILE_DS_96_24)
fprintf(fp, "DTS 96/24");
else if (exss->profile & DCADEC_PROFILE_EXPRESS)
fprintf(fp, "DTS Express");
else
fprintf(fp, "Unknown Extension Profile");
fprintf(fp, ": %d ch (%s), %.f kHz, %d bit\n",
exss->nchannels, make_spkr_mask_str(exss->spkr_mask),
exss->sample_rate / 1000.0f, exss->bits_per_sample);
dcadec_context_free_exss_info(exss);
}
struct dcadec_core_info *core = dcadec_context_get_core_info(context);
if (core) {
if (exss)
fprintf(fp, "(");
fprintf(fp, "DTS Core Audio: %d.%d ch, %.f kHz, %d bit",
core->nchannels, !!core->lfe_present, core->sample_rate / 1000.f,
core->source_pcm_res);
if (core->es_format)
fprintf(fp, ", ES");
if (core->bit_rate > 0)
fprintf(fp, ", %.f kbps", core->bit_rate / 1000.0f);
if (exss)
fprintf(fp, ")");
fprintf(fp, "\n");
dcadec_context_free_core_info(core);
}
}
static bool interrupted;
#ifdef _WIN32
static BOOL WINAPI console_ctrl_handler(DWORD dwCtrlType)
{
(void)dwCtrlType;
interrupted = true;
return TRUE;
}
#else
static void signal_handler(int sig)
{
(void)sig;
interrupted = true;
}
#endif
static void my_log_cb(int level, const char *file, int line,
const char *message, void *cbarg)
{
(void)cbarg;
if (level > DCADEC_LOG_WARNING)
return;
const char *prefix = "UNKNOWN";
switch (level) {
case DCADEC_LOG_ERROR:
prefix = "ERROR";
break;
case DCADEC_LOG_WARNING:
prefix = "WARNING";
break;
}
fprintf(stderr, "%s: %s+%d: %s\n", prefix, file, line, message);
}
int main(int argc, char **argv)
{
int flags = DCADEC_FLAG_STRICT;
int wave_flags = 0;
bool parse_only = false;
bool no_progress = false;
bool quiet = false;
bool no_strip = false;
int opt;
while ((opt = getopt(argc, argv, "26bcfhilmnPqSsx")) != -1) {
switch (opt) {
case '2':
flags |= DCADEC_FLAG_KEEP_DMIX_2CH;
break;
case '6':
flags |= DCADEC_FLAG_KEEP_DMIX_6CH;
break;
case 'b':
flags |= DCADEC_FLAG_CORE_BIT_EXACT;
break;
case 'c':
flags |= DCADEC_FLAG_CORE_ONLY;
break;
case 'f':
flags &= ~DCADEC_FLAG_CORE_LFE_IIR;
flags |= DCADEC_FLAG_CORE_LFE_FIR;
break;
case 'h':
print_help(argv[0]);
return 0;
case 'i':
flags &= ~DCADEC_FLAG_CORE_LFE_FIR;
flags |= DCADEC_FLAG_CORE_LFE_IIR;
break;
case 'l':
flags &= ~DCADEC_FLAG_STRICT;
break;
case 'm':
flags |= DCADEC_FLAG_NATIVE_LAYOUT;
wave_flags |= DCADEC_WAVEOUT_FLAG_MONO;
break;
case 'n':
parse_only = true;
break;
case 'P':
no_progress = true;
break;
case 'q':
quiet = true;
break;
case 'S':
no_strip = true;
break;
case 'x':
flags |= DCADEC_FLAG_CORE_SYNTH_X96;
break;
default:
print_help(argv[0]);
return 1;
}
}
no_progress |= quiet;
if (optind >= argc) {
print_help(argv[0]);
return 1;
}
char *fn = argv[optind];
struct dcadec_stream *stream = dcadec_stream_open(strcmp(fn, "-") ? fn : NULL, 0);
if (!stream) {
fprintf(stderr, "Couldn't open input file\n");
return 1;
}
uint8_t *packet;
size_t size;
int ret;
if ((ret = dcadec_stream_read(stream, &packet, &size)) < 0) {
fprintf(stderr, "Error reading packet: %s\n", dcadec_strerror(ret));
dcadec_stream_close(stream);
return 1;
}
if (ret == 0) {
fprintf(stderr, "This doesn't look like a valid DTS bit stream\n");
dcadec_stream_close(stream);
return 1;
}
struct dcadec_context *context = dcadec_context_create(flags);
if (!context) {
fprintf(stderr, "Couldn't create decoder context\n");
dcadec_stream_close(stream);
return 1;
}
dcadec_context_set_log_cb(context, my_log_cb, NULL);
if ((ret = dcadec_context_parse(context, packet, size)) < 0) {
fprintf(stderr, "Error parsing packet: %s\n", dcadec_strerror(ret));
dcadec_context_destroy(context);
dcadec_stream_close(stream);
return 1;
}
if (ret > 0)
fprintf(stderr, "WARNING: %s at frame 0\n", dcadec_strerror(ret));
if (!quiet)
print_info(context, optind + 1 >= argc ? stdout : stderr);
struct dcadec_waveout *waveout = NULL;
if (!parse_only) {
if (optind + 1 >= argc) {
dcadec_context_destroy(context);
dcadec_stream_close(stream);
return 0;
}
fn = argv[optind + 1];
waveout = dcadec_waveout_open(strcmp(fn, "-") ? fn : NULL, wave_flags);
if (!waveout) {
fprintf(stderr, "Couldn't open output file\n");
dcadec_context_destroy(context);
dcadec_stream_close(stream);
return 1;
}
}
int last_progress = -1;
#ifdef _WIN32
SetConsoleCtrlHandler(&console_ctrl_handler, TRUE);
#else
signal(SIGINT, &signal_handler);
#endif
uint32_t ndelayframes = 0;
uint64_t npcmsamples = UINT64_MAX;
uint64_t ntotalframes = 0;
uint64_t nskippedframes = 0;
uint64_t nlossyframes = 0;
if (!parse_only && !no_strip) {
struct dcadec_stream_info *info = dcadec_stream_get_info(stream);
if (info) {
if (info->nframesamples)
ndelayframes = info->ndelaysamples / info->nframesamples;
if (info->npcmsamples)
npcmsamples = info->npcmsamples;
dcadec_stream_free_info(info);
}
}
if (!quiet) {
const bool core_only = (flags & DCADEC_FLAG_CORE_ONLY);
fprintf(stderr, "%s%s...\n",
waveout ? "Decoding" : "Parsing",
core_only ? " (core only)" : "");
}
while (!interrupted) {
if (waveout) {
int **samples, nsamples, channel_mask, sample_rate, bits_per_sample;
if ((ret = dcadec_context_filter(context, &samples, &nsamples,
&channel_mask, &sample_rate,
&bits_per_sample, NULL)) < 0) {
fprintf(stderr, "Error filtering frame: %s\n", dcadec_strerror(ret));
if (flags & DCADEC_FLAG_STRICT) {
break;
} else {
nskippedframes++;
goto next_packet;
}
}
if (ndelayframes) {
ndelayframes--;
goto next_packet;
}
if (ret > 0) {
fprintf(stderr, "WARNING: %s at frame %" PRIu64 "\n", dcadec_strerror(ret), ntotalframes);
nlossyframes++;
}
if ((uint64_t)nsamples > npcmsamples)
nsamples = (int)npcmsamples;
if ((ret = dcadec_waveout_write(waveout, samples, nsamples,
channel_mask, sample_rate,
bits_per_sample)) < 0) {
fprintf(stderr, "Error writing WAV file: %s\n", dcadec_strerror(ret));
if ((flags & DCADEC_FLAG_STRICT) || ret != -DCADEC_EOUTCHG) {
break;
} else {
nskippedframes++;
goto next_packet;
}
}
npcmsamples -= nsamples;
}
next_packet:
ntotalframes++;
if ((ret = dcadec_stream_read(stream, &packet, &size)) < 0) {
fprintf(stderr, "Error reading packet: %s\n", dcadec_strerror(ret));
break;
}
if (!no_progress) {
int progress = dcadec_stream_progress(stream);
if (progress != last_progress) {
fprintf(stderr, "Progress: %d%%\r", progress);
last_progress = progress;
}
}
if (ret == 0)
break;
if ((ret = dcadec_context_parse(context, packet, size)) < 0) {
fprintf(stderr, "Error parsing packet: %s\n", dcadec_strerror(ret));
if (flags & DCADEC_FLAG_STRICT) {
break;
} else {
nskippedframes++;
goto next_packet;
}
}
if (ret > 0)
fprintf(stderr, "WARNING: %s at frame %" PRIu64 "\n", dcadec_strerror(ret), ntotalframes);
}
if (!quiet) {
if (last_progress != -1)
fprintf(stderr, "\n");
if (interrupted)
fprintf(stderr, "Interrupted.\n");
else if (ret == 0)
fprintf(stderr, "Completed.\n");
if (nskippedframes)
fprintf(stderr, "*** %" PRIu64 " frames skipped ***\n", nskippedframes);
if (nlossyframes)
fprintf(stderr, "*** %" PRIu64 " frames not lossless ***\n", nlossyframes);
}
dcadec_waveout_close(waveout);
dcadec_context_destroy(context);
dcadec_stream_close(stream);
return !!ret;
}