1 #include "shn.h"
2 #include "../pcmconv.h"
3 #include "../buffer.h"
4 #include <string.h>
5 #include <math.h>
6
7 /********************************************************
8 Audio Tools, a module and set of tools for manipulating audio data
9 Copyright (C) 2007-2014 Brian Langenberger
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
OpusDecoder_new(PyTypeObject * type,PyObject * args,PyObject * kwds)23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 *******************************************************/
25
26
27 #ifndef STANDALONE
28 PyObject*
29 SHNDecoder_new(PyTypeObject *type,
30 PyObject *args, PyObject *kwds)
31 {
32 decoders_SHNDecoder *self;
33
34 self = (decoders_SHNDecoder *)type->tp_alloc(type, 0);
35
36 return (PyObject *)self;
37 }
38
39 int
40 SHNDecoder_init(decoders_SHNDecoder *self,
41 PyObject *args, PyObject *kwds)
42 {
43 PyObject *file;
44 self->bitstream = NULL;
45 self->stream_finished = 0;
46
47 self->means = aa_int_new();
48 self->previous_samples = aa_int_new();
49
50 /*setup temporary buffers*/
51 self->samples = aa_int_new();
52 self->unshifted = aa_int_new();
53 self->pcm_header = a_int_new();
54 self->pcm_footer = a_int_new();
55
56 if ((self->audiotools_pcm = open_audiotools_pcm()) == NULL)
57 return -1;
58
59 if (!PyArg_ParseTuple(args, "O", &file)) {
60 return -1;
61 } else {
62 Py_INCREF(file);
63 }
OpusDecoders_dealloc(decoders_OpusDecoder * self)64
65 /*open the shn file*/
66 self->bitstream = br_open_external(
67 file,
68 BS_BIG_ENDIAN,
69 4096,
70 (ext_read_f)br_read_python,
71 (ext_setpos_f)bs_setpos_python,
72 (ext_getpos_f)bs_getpos_python,
73 (ext_free_pos_f)bs_free_pos_python,
74 (ext_seek_f)bs_fseek_python,
75 (ext_close_f)bs_close_python,
76 (ext_free_f)bs_free_python_decref);
77
OpusDecoder_sample_rate(decoders_OpusDecoder * self,void * closure)78 /*read Shorten header for basic info*/
79 switch (read_shn_header(self, self->bitstream)) {
80 case INVALID_MAGIC_NUMBER:
81 PyErr_SetString(PyExc_ValueError, "invalid magic number");
82 return -1;
83 case INVALID_SHORTEN_VERSION:
84 PyErr_SetString(PyExc_ValueError, "invalid Shorten version");
85 return -1;
86 case UNSUPPORTED_FILE_TYPE:
87 PyErr_SetString(PyExc_ValueError, "unsupported Shorten file type");
88 return -1;
89 case IOERROR:
90 PyErr_SetString(PyExc_IOError, "I/O error reading Shorten header");
91 return -1;
92 default:
93 /*mark stream as not closed and ready for reading*/
94 self->closed = 0;
95
96 return 0;
97 }
98 }
99
100 void
101 SHNDecoder_dealloc(decoders_SHNDecoder *self)
OpusDecoder_channel_mask(decoders_OpusDecoder * self,void * closure)102 {
103 self->means->del(self->means);
104 self->previous_samples->del(self->previous_samples);
105 self->samples->del(self->samples);
106 self->unshifted->del(self->unshifted);
107 self->pcm_header->del(self->pcm_header);
108 self->pcm_footer->del(self->pcm_footer);
109
110 Py_XDECREF(self->audiotools_pcm);
111
112 if (self->bitstream != NULL) {
113 self->bitstream->free(self->bitstream);
114 }
115
116 Py_TYPE(self)->tp_free((PyObject*)self);
117 }
118
119 PyObject*
120 SHNDecoder_close(decoders_SHNDecoder* self, PyObject *args)
121 {
122 /*mark stream as closed so more calls to read() generate ValueErrors*/
123 self->closed = 1;
124
125 /*close bitstream for further reading*/
126 self->bitstream->close_internal_stream(self->bitstream);
127
128 Py_INCREF(Py_None);
129 return Py_None;
130 }
131
132 static PyObject*
133 SHNDecoder_enter(decoders_SHNDecoder* self, PyObject *args)
134 {
135 Py_INCREF(self);
136 return (PyObject *)self;
137 }
138
139 static PyObject*
140 SHNDecoder_exit(decoders_SHNDecoder* self, PyObject *args)
141 {
142 self->closed = 1;
143
144 self->bitstream->close_internal_stream(self->bitstream);
145
146 Py_INCREF(Py_None);
147 return Py_None;
148 }
149
150 static PyObject*
151 SHNDecoder_sample_rate(decoders_SHNDecoder *self, void *closure)
152 {
153 return Py_BuildValue("I", self->sample_rate);
154 }
155
156 static PyObject*
157 SHNDecoder_bits_per_sample(decoders_SHNDecoder *self,
158 void *closure)
159 {
160 return Py_BuildValue("I", self->bits_per_sample);
161 }
162
163 static PyObject*
164 SHNDecoder_channels(decoders_SHNDecoder *self, void *closure)
OpusDecoder_read(decoders_OpusDecoder * self,PyObject * args)165 {
166 return Py_BuildValue("I", self->header.channels);
167 }
168
169 static PyObject*
170 SHNDecoder_channel_mask(decoders_SHNDecoder *self, void *closure)
171 {
172 return Py_BuildValue("I", self->channel_mask);
173 }
174
175 PyObject*
176 SHNDecoder_read(decoders_SHNDecoder* self, PyObject *args)
177 {
178 if (self->closed) {
179 PyErr_SetString(PyExc_ValueError, "cannot read closed stream");
180 return NULL;
181 }
182
183 if (self->stream_finished) {
184 return empty_FrameList(self->audiotools_pcm,
185 self->header.channels,
186 self->bits_per_sample);
187 }
188
189 self->unshifted->reset(self->unshifted);
190
191 switch (read_framelist(self, self->unshifted)) {
192 case OK:
193 return aa_int_to_FrameList(self->audiotools_pcm,
194 self->unshifted,
195 self->bits_per_sample);
196 case END_OF_STREAM:
197 return empty_FrameList(self->audiotools_pcm,
198 self->header.channels,
199 self->bits_per_sample);
200 case UNKNOWN_COMMAND:
201 PyErr_SetString(PyExc_ValueError,
202 "unknown command in Shorten stream");
203 return NULL;
204 case IOERROR:
205 PyErr_SetString(PyExc_IOError, "I/O error reading Shorten file");
206 return NULL;
207 default:
208 /*shouldn't get here*/
209 PyErr_SetString(PyExc_ValueError,
210 "unknown value from read_framelist()");
211 return NULL;
212 }
213 }
214
215 static PyObject*
216 SHNDecoder_pcm_split(decoders_SHNDecoder* self, PyObject *args)
217 {
218 #if PY_MAJOR_VERSION >= 3
219 char format[] = "y#y#";
220 #else
221 char format[] = "s#s#";
222 #endif
223
224 if (!setjmp(*br_try(self->bitstream))) {
225 a_int* header = self->pcm_header;
226 a_int* footer = self->pcm_footer;
227 a_int* current = header;
228 uint8_t* header_s;
229 uint8_t* footer_s;
230 PyObject* tuple;
231
232 unsigned command;
233 unsigned i;
234
235 header->reset(header);
236 footer->reset(footer);
237
238 /*walk through file, processing all commands*/
239 do {
240 unsigned energy;
241 unsigned LPC_count;
242 unsigned verbatim_size;
243
244 command = read_unsigned(self->bitstream, COMMAND_SIZE);
245
246 switch (command) {
247 case FN_DIFF0:
248 case FN_DIFF1:
249 case FN_DIFF2:
250 case FN_DIFF3:
251 /*all the DIFF commands have the same structure*/
252 energy = read_unsigned(self->bitstream, ENERGY_SIZE);
253 for (i = 0; i < self->block_length; i++) {
254 skip_signed(self->bitstream, energy);
255 }
256 current = footer;
257 break;
258 case FN_QUIT:
259 self->stream_finished = 1;
260 break;
261 case FN_BLOCKSIZE:
262 self->block_length = read_long(self->bitstream);
263 break;
264 case FN_BITSHIFT:
265 skip_unsigned(self->bitstream, SHIFT_SIZE);
266 break;
267 case FN_QLPC:
268 energy = read_unsigned(self->bitstream, ENERGY_SIZE);
OpusDecoder_close(decoders_OpusDecoder * self,PyObject * args)269 LPC_count = read_unsigned(self->bitstream, LPC_COUNT_SIZE);
270 for (i = 0; i < LPC_count; i++) {
271 skip_signed(self->bitstream, LPC_COEFF_SIZE);
272 }
273 for (i = 0; i < self->block_length; i++) {
274 skip_signed(self->bitstream, energy);
275 }
276 current = footer;
OpusDecoder_enter(decoders_OpusDecoder * self,PyObject * args)277 break;
278 case FN_ZERO:
279 current = footer;
280 break;
281 case FN_VERBATIM:
282 /*any VERBATIM commands have their data appended
283 to header or footer*/
OpusDecoder_exit(decoders_OpusDecoder * self,PyObject * args)284 verbatim_size = read_unsigned(self->bitstream,
285 VERBATIM_CHUNK_SIZE);
286 for (i = 0; i < verbatim_size; i++) {
287 current->append(current,
288 read_unsigned(self->bitstream,
289 VERBATIM_BYTE_SIZE));
290 }
291 break;
292 }
293 } while (command != FN_QUIT);
294
295 br_etry(self->bitstream);
296
297 /*once all commands have been processed,
298 transform the bytes in header and footer to strings*/
299 header_s = malloc(sizeof(uint8_t) * header->len);
300 for (i = 0; i < header->len; i++)
301 header_s[i] = (uint8_t)(header->_[i] & 0xFF);
302
303 footer_s = malloc(sizeof(uint8_t) * footer->len);
304 for (i = 0; i < footer->len; i++)
305 footer_s[i] = (uint8_t)(footer->_[i] & 0xFF);
306
307 /*generate a tuple from the strings*/
308 tuple = Py_BuildValue(format,
309 header_s, header->len,
310 footer_s, footer->len);
311
312 /*deallocate temporary space before returning tuple*/
313 free(header_s);
314 free(footer_s);
315
316 return tuple;
317 } else {
318 br_etry(self->bitstream);
319 PyErr_SetString(PyExc_IOError, "I/O error reading Shorten file");
320 return NULL;
321 }
322 }
323 #endif
324
325 static status
326 read_shn_header(decoders_SHNDecoder* self, BitstreamReader* reader)
327 {
328 if (!setjmp(*br_try(reader))) {
329 uint8_t magic_number[4];
330 unsigned version;
331 unsigned i;
332
333 reader->parse(reader, "4b 8u", magic_number, &version);
334 if (memcmp(magic_number, "ajkg", 4)) {
335 br_etry(reader);
336 return INVALID_MAGIC_NUMBER;
337 }
338 if (version != 2) {
339 br_etry(reader);
340 return INVALID_SHORTEN_VERSION;
341 }
342
343 self->header.file_type = read_long(reader);
344 self->header.channels = read_long(reader);
345 self->block_length = read_long(reader);
346 self->left_shift = 0;
347 self->header.max_LPC = read_long(reader);
348 self->header.mean_count = read_long(reader);
349 reader->skip_bytes(reader, read_long(reader));
350
351 if ((1 <= self->header.file_type) && (self->header.file_type <= 2)) {
352 self->bits_per_sample = 8;
353 self->signed_samples = (self->header.file_type == 1);
354 } else if ((3 <= self->header.file_type) &&
355 (self->header.file_type <= 6)) {
356 self->bits_per_sample = 16;
357 self->signed_samples = ((self->header.file_type == 3) ||
358 (self->header.file_type == 5));
359 } else {
360 br_etry(reader);
361 return UNSUPPORTED_FILE_TYPE;
362 }
363
364 for (i = 0; i < self->header.channels; i++) {
365 a_int* means = self->means->append(self->means);
366 means->mset(means, self->header.mean_count, 0);
367 self->previous_samples->append(self->previous_samples);
368 }
369
370 /*process first instruction for wave/aiff header, if present*/
371 process_iff_header(reader,
372 &(self->sample_rate),
373 &(self->channel_mask));
374
375 br_etry(reader);
376
377 return OK;
378 } else {
379 /*read error in Shorten header*/
380 br_etry(reader);
381 return IOERROR;
382 }
383 }
384
385 static status
386 read_framelist(decoders_SHNDecoder* self, aa_int* framelist)
387 {
388 unsigned c = 0;
389
390 self->samples->reset(self->samples);
391
392 if (!setjmp(*br_try(self->bitstream))) {
393 while (1) {
394 const unsigned command = read_unsigned(self->bitstream,
395 COMMAND_SIZE);
396
397 if (((FN_DIFF0 <= command) && (command <= FN_DIFF3)) ||
398 ((FN_QLPC <= command) && (command <= FN_ZERO))) {
399 /*audio data commands*/
400 a_int* means = self->means->_[c];
401 a_int* previous_samples = self->previous_samples->_[c];
402 a_int* samples = self->samples->append(self->samples);
403 a_int* unshifted = framelist->append(framelist);
404
405 switch (command) {
406 case FN_DIFF0:
407 read_diff0(self->bitstream,
408 self->block_length,
409 means,
410 samples);
411 break;
412 case FN_DIFF1:
413 read_diff1(self->bitstream,
414 self->block_length,
415 previous_samples,
416 samples);
417 break;
418 case FN_DIFF2:
419 read_diff2(self->bitstream,
420 self->block_length,
421 previous_samples,
422 samples);
423 break;
424 case FN_DIFF3:
425 read_diff3(self->bitstream,
426 self->block_length,
427 previous_samples,
428 samples);
429 break;
430 case FN_QLPC:
431 read_qlpc(self->bitstream,
432 self->block_length,
433 previous_samples,
434 means,
435 samples);
436 break;
437 case FN_ZERO:
438 samples->mset(samples, self->block_length, 0);
439 break;
440 default:
441 break; /*can't get here*/
442 }
443
444 /*calculate next mean for given channel*/
445 means->append(means, shnmean(samples));
446 means->tail(means, self->header.mean_count, means);
447
448 /*wrap samples for next set of channels*/
449 samples->tail(samples, MAX(3, self->header.max_LPC),
450 previous_samples);
451
452 /*apply any left shift to channel*/
453 if (self->left_shift) {
454 unsigned i;
455 unshifted->resize_for(unshifted, samples->len);
456 for (i = 0; i < samples->len; i++)
457 a_append(unshifted, samples->_[i] << self->left_shift);
458 } else {
459 samples->copy(samples, unshifted);
460 }
461
462 /*if stream is unsigned, convert unshifted samples to signed*/
463 if (!self->signed_samples) {
464 const int adjustment = 1 << (self->bits_per_sample - 1);
465 unsigned i;
466 for (i = 0; i < unshifted->len; i++)
467 unshifted->_[i] -= adjustment;
468 }
469
470 /*move on to next channel*/
471 c++;
472
473 /*return OK once all channels are constructed*/
474 if (c == self->header.channels) {
475 br_etry(self->bitstream);
476 return OK;
477 }
478 } else if (((FN_QUIT <= command) && (command <= FN_BITSHIFT)) ||
479 (command == FN_VERBATIM)) {
480 unsigned verbatim_size;
481 unsigned i;
482
483 /*non audio commands*/
484 switch (command) {
485 case FN_QUIT:
486 self->stream_finished = 1;
487 br_etry(self->bitstream);
488 return END_OF_STREAM;
489 case FN_BLOCKSIZE:
490 self->block_length = read_long(self->bitstream);
491 break;
492 case FN_BITSHIFT:
493 self->left_shift = read_unsigned(self->bitstream,
494 SHIFT_SIZE);
495 break;
496 case FN_VERBATIM:
497 verbatim_size = read_unsigned(self->bitstream,
498 VERBATIM_CHUNK_SIZE);
499 for (i = 0; i < verbatim_size; i++)
500 skip_unsigned(self->bitstream, VERBATIM_BYTE_SIZE);
501 break;
502 default:
503 break; /*can't get here*/
504 }
505 } else {
506 /*unknown command*/
507 br_etry(self->bitstream);
508 return UNKNOWN_COMMAND;
509 }
510 }
511 } else {
512 br_etry(self->bitstream);
513 return IOERROR;
514 }
515 }
516
517 static void
518 read_diff0(BitstreamReader* bs, unsigned block_length,
519 const a_int* means, a_int* samples)
520 {
521 const int offset = shnmean(means);
522 const unsigned energy = read_unsigned(bs, ENERGY_SIZE);
523 unsigned i;
524
525 samples->reset_for(samples, block_length);
526
527 for (i = 0; i < block_length; i++) {
528 const int residual = read_signed(bs, energy);
529 a_append(samples, residual + offset);
530 }
531 }
532
533 static void
534 read_diff1(BitstreamReader* bs, unsigned block_length,
535 a_int* previous_samples, a_int* samples)
536 {
537 unsigned i;
538 unsigned energy;
539
540 /*ensure "previous_samples" contains at least 1 value*/
541 if (previous_samples->len < 1) {
542 samples->mset(samples, 1 - previous_samples->len, 0);
543 samples->extend(samples, previous_samples);
544 } else {
545 previous_samples->tail(previous_samples, 1, samples);
546 }
547
548 energy = read_unsigned(bs, ENERGY_SIZE);
549
550 /*process the residuals to samples*/
551 samples->resize_for(samples, block_length);
552 for (i = 1; i < (block_length + 1); i++) {
553 const int residual = read_signed(bs, energy);
554 a_append(samples, samples->_[i - 1] + residual);
555 }
556
557 /*truncate samples to block length*/
558 samples->tail(samples, block_length, samples);
559 }
560
561 static void
562 read_diff2(BitstreamReader* bs, unsigned block_length,
563 a_int* previous_samples, a_int* samples)
564 {
565 unsigned i;
566 unsigned energy;
567
568 /*ensure "previous_samples" contains at least 2 values*/
569 if (previous_samples->len < 2) {
570 samples->mset(samples, 2 - previous_samples->len, 0);
571 samples->extend(samples, previous_samples);
572 } else {
573 previous_samples->tail(previous_samples, 2, samples);
574 }
575
576 energy = read_unsigned(bs, ENERGY_SIZE);
577
578 /*process the residuals to samples*/
579 samples->resize_for(samples, block_length);
580 for (i = 2; i < (block_length + 2); i++) {
581 const int residual = read_signed(bs, energy);
582 a_append(samples,
583 (2 * samples->_[i - 1]) - samples->_[i - 2] + residual);
584 }
585
586 /*truncate samples to block length*/
587 samples->tail(samples, block_length, samples);
588 }
589
590 static void
591 read_diff3(BitstreamReader* bs, unsigned block_length,
592 a_int* previous_samples, a_int* samples)
593 {
594 unsigned i;
595 unsigned energy;
596
597 /*ensure "previous_samples" contains at least 3 values*/
598 if (previous_samples->len < 3) {
599 samples->mset(samples, 3 - previous_samples->len, 0);
600 samples->extend(samples, previous_samples);
601 } else {
602 previous_samples->tail(previous_samples, 3, samples);
603 }
604
605 energy = read_unsigned(bs, ENERGY_SIZE);
606
607 /*process the residuals to samples*/
608 samples->resize_for(samples, block_length);
609 for (i = 3; i < (block_length + 3); i++) {
610 const int residual = read_signed(bs, energy);
611 a_append(samples,
612 (3 * (samples->_[i - 1] - samples->_[i - 2])) +
613 samples->_[i - 3] + residual);
614 }
615
616 /*truncate samples to block length*/
617 samples->tail(samples, block_length, samples);
618 }
619
620 static void
621 read_qlpc(BitstreamReader* bs, unsigned block_length,
622 a_int* previous_samples, a_int* means, a_int* samples)
623 {
624 /*read some QLPC setup values*/
625 const int offset = shnmean(means);
626 const unsigned energy = read_unsigned(bs, ENERGY_SIZE);
627 const unsigned LPC_count = read_unsigned(bs, LPC_COUNT_SIZE);
628 a_int* LPC_coeff = a_int_new();
629 a_int* offset_samples = a_int_new();
630 a_int* unoffset_samples = a_int_new();
631
632 if (!setjmp(*br_try(bs))) {
633 int i;
634
635 for (i = 0; i < LPC_count; i++)
636 LPC_coeff->append(LPC_coeff, read_signed(bs, LPC_COEFF_SIZE));
637
638 /*ensure "previous_samples" contains at least "LPC count" values*/
639 if (previous_samples->len < LPC_count) {
640 offset_samples->mset(offset_samples,
641 LPC_count - previous_samples->len, 0);
642 offset_samples->extend(offset_samples, previous_samples);
643 } else {
644 previous_samples->tail(previous_samples, LPC_count, offset_samples);
645 }
646
647 /*process the residuals to unoffset samples*/
648 for (i = 0; i < block_length; i++) {
649 const int residual = read_signed(bs, energy);
650 int sum = 1 << 5;
651 int j;
652 for (j = 0; j < LPC_count; j++) {
653 if ((i - j - 1) < 0) {
654 sum += LPC_coeff->_[j] *
655 (offset_samples->_[LPC_count + (i - j - 1)] -
656 offset);
657 } else {
658 sum += LPC_coeff->_[j] * unoffset_samples->_[i - j - 1];
659 }
660 }
661 unoffset_samples->append(unoffset_samples, (sum >> 5) + residual);
662 }
663
664 /*reapply offset to unoffset samples*/
665 samples->reset_for(samples, unoffset_samples->len);
666 for (i = 0; i < unoffset_samples->len; i++) {
667 a_append(samples, unoffset_samples->_[i] + offset);
668 }
669
670 /*deallocate temporary arrays before returning successfully*/
671 LPC_coeff->del(LPC_coeff);
672 offset_samples->del(offset_samples);
673 unoffset_samples->del(unoffset_samples);
674 br_etry(bs);
675 } else {
676 /*error reading QLPC, so deallocate temporary arrays*/
677 LPC_coeff->del(LPC_coeff);
678 offset_samples->del(offset_samples);
679 unoffset_samples->del(unoffset_samples);
680 br_etry(bs);
681
682 /*before aborting to the next spot on the abort stack*/
683 br_abort(bs);
684 }
685 }
686
687 static int
688 shnmean(const a_int* values)
689 {
690 return ((int)(values->len / 2) + values->sum(values)) / (int)(values->len);
691 }
692
693 static unsigned
694 read_unsigned(BitstreamReader* bs, unsigned count)
695 {
696 const unsigned MSB = bs->read_unary(bs, 1);
697 const unsigned LSB = bs->read(bs, count);
698
699 return (MSB << count) | LSB;
700 }
701
702 static int
703 read_signed(BitstreamReader* bs, unsigned count)
704 {
705 /*1 additional sign bit*/
706 const unsigned u = read_unsigned(bs, count + 1);
707 if (u % 2)
708 return -(u >> 1) - 1;
709 else
710 return u >> 1;
711 }
712
713 unsigned int
714 read_long(BitstreamReader* bs)
715 {
716 return read_unsigned(bs, read_unsigned(bs, 2));
717 }
718
719 void
720 skip_unsigned(BitstreamReader* bs, unsigned int count)
721 {
722 bs->skip_unary(bs, 1);
723 bs->skip(bs, count);
724 }
725
726 void
727 skip_signed(BitstreamReader* bs, unsigned int count)
728 {
729 bs->skip_unary(bs, 1);
730 bs->skip(bs, count + 1);
731 }
732
733 static void
734 process_iff_header(BitstreamReader* bs,
735 unsigned* sample_rate,
736 unsigned* channel_mask)
737 {
738 br_pos_t* command_start;
739 br_pos_t* verbatim_start;
740 unsigned command;
741
742 command_start = bs->getpos(bs);
743 if (!setjmp(*br_try(bs))) {
744 command = read_unsigned(bs, COMMAND_SIZE);
745
746 if (command == FN_VERBATIM) {
747 BitstreamReader* verbatim;
748 unsigned verbatim_size;
749
750 verbatim = read_verbatim(bs, &verbatim_size);
751 verbatim_start = verbatim->getpos(verbatim);
752
753 if (!read_wave_header(verbatim, verbatim_size,
754 sample_rate, channel_mask)) {
755 verbatim_start->del(verbatim_start);
756 verbatim->close(verbatim);
757 bs->setpos(bs, command_start);
758 command_start->del(command_start);
759 br_etry(bs);
760 return;
761 } else {
762 verbatim->setpos(verbatim, verbatim_start);
763 }
764
765 if (!read_aiff_header(verbatim, verbatim_size,
766 sample_rate, channel_mask)) {
767 verbatim_start->del(verbatim_start);
768 verbatim->close(verbatim);
769 bs->setpos(bs, command_start);
770 command_start->del(command_start);
771 br_etry(bs);
772 return;
773 } else {
774 verbatim->setpos(verbatim, verbatim_start);
775 }
776
777 /*neither wave header or aiff header found,
778 so use dummy values again*/
779 verbatim_start->del(verbatim_start);
780 verbatim->close(verbatim);
781 bs->setpos(bs, command_start);
782 command_start->del(command_start);
783 *sample_rate = 44100;
784 *channel_mask = 0;
785 br_etry(bs);
786 return;
787 } else {
788 /*VERBATIM isn't the first command
789 so rewind and set some dummy values*/
790 bs->setpos(bs, command_start);
791 command_start->del(command_start);
792 *sample_rate = 44100;
793 *channel_mask = 0;
794 br_etry(bs);
795 return;
796 }
797 } else {
798 /*wrap IFF chunk reader in try block
799 to unmark the stream prior to bubbling up
800 the read exception to read_shn_header()*/
801 command_start->del(command_start);
802 br_etry(bs);
803 br_abort(bs);
804 }
805 }
806
807 static BitstreamReader*
808 read_verbatim(BitstreamReader* bs, unsigned* verbatim_size)
809 {
810 struct bs_buffer *verbatim = buf_new();
811 if (!setjmp(*br_try(bs))) {
812 unsigned i;
813 BitstreamReader *verbatim_reader;
814
815 /*read block of verbatim bytes to buffer*/
816 *verbatim_size = read_unsigned(bs, VERBATIM_CHUNK_SIZE);
817
818 for (i = 0; i < *verbatim_size; i++) {
819 const unsigned byte = read_unsigned(bs, VERBATIM_BYTE_SIZE) & 0xFF;
820 buf_putc((int)byte, verbatim);
821 }
822 br_etry(bs);
823
824 /*convert buffer to BitstreamReader*/
825 verbatim_reader = br_open_buffer(buf_window_start(verbatim),
826 buf_window_size(verbatim),
827 BS_BIG_ENDIAN);
828
829 /*cleanup temporary buffer before returning reader*/
830 buf_close(verbatim);
831
832 return verbatim_reader;
833 } else {
834 /*I/O error reading from main bitstream
835 so cleanup buffer before re-raising error*/
836 buf_close(verbatim);
837 br_etry(bs);
838 br_abort(bs);
839 return NULL; /*shouldn't get here*/
840 }
841 }
842
843 int
844 read_wave_header(BitstreamReader* bs, unsigned verbatim_size,
845 unsigned* sample_rate, unsigned* channel_mask)
846 {
847 if (!setjmp(*br_try(bs))) {
848 uint8_t RIFF[4];
849 unsigned SIZE;
850 uint8_t WAVE[4];
851
852 bs->set_endianness(bs, BS_LITTLE_ENDIAN);
853 bs->parse(bs, "4b 32u 4b", RIFF, &SIZE, WAVE);
854
855 if (memcmp(RIFF, "RIFF", 4) || memcmp(WAVE, "WAVE", 4)) {
856 br_etry(bs);
857 return 1;
858 } else {
859 verbatim_size -= bs_format_byte_size("4b 32u 4b");
860 }
861
862 while (verbatim_size > 0) {
863 uint8_t chunk_id[4];
864 unsigned chunk_size;
865 bs->parse(bs, "4b 32u", chunk_id, &chunk_size);
866 verbatim_size -= bs_format_byte_size("4b 32u");
867 if (!memcmp(chunk_id, "fmt ", 4)) {
868 /*parse fmt chunk*/
869 unsigned compression;
870 unsigned channels;
871 unsigned bytes_per_second;
872 unsigned block_align;
873 unsigned bits_per_sample;
874
875 bs->parse(bs, "16u 16u 32u 32u 16u 16u",
876 &compression,
877 &channels,
878 sample_rate,
879 &bytes_per_second,
880 &block_align,
881 &bits_per_sample);
882 if (compression == 1) {
883 /*if we have a multi-channel WAVE file
884 that's not WAVEFORMATEXTENSIBLE,
885 assume the channels follow
886 SMPTE/ITU-R recommendations
887 and hope for the best*/
888 switch (channels) {
889 case 1:
890 *channel_mask = 0x4;
891 break;
892 case 2:
893 *channel_mask = 0x3;
894 break;
895 case 3:
896 *channel_mask = 0x7;
897 break;
898 case 4:
899 *channel_mask = 0x33;
900 break;
901 case 5:
902 *channel_mask = 0x37;
903 break;
904 case 6:
905 *channel_mask = 0x3F;
906 break;
907 default:
908 *channel_mask = 0;
909 break;
910 }
911 br_etry(bs);
912 return 0;
913 } else if (compression == 0xFFFE) {
914 unsigned cb_size;
915 unsigned valid_bits_per_sample;
916 uint8_t sub_format[16];
917 bs->parse(bs, "16u 16u 32u 16b",
918 &cb_size,
919 &valid_bits_per_sample,
920 channel_mask,
921 sub_format);
922 if (!memcmp(sub_format,
923 "\x01\x00\x00\x00\x00\x00\x10\x00"
924 "\x80\x00\x00\xaa\x00\x38\x9b\x71", 16)) {
925 br_etry(bs);
926 return 0;
927 } else {
928 /*invalid sub format*/
929 br_etry(bs);
930 return 1;
931 }
932 } else {
933 /*unsupported wave compression*/
934 br_etry(bs);
935 return 1;
936 }
937 } else {
938 if (chunk_size % 2) {
939 /*handle odd-sized chunks*/
940 bs->skip_bytes(bs, chunk_size + 1);
941 verbatim_size -= (chunk_size + 1);
942 } else {
943 bs->skip_bytes(bs, chunk_size);
944 verbatim_size -= chunk_size;
945 }
946 }
947 }
948
949 /*no fmt chunk found in wave header*/
950 br_etry(bs);
951 return 1;
952 } else {
953 /*I/O error bouncing through wave chunks*/
954 br_etry(bs);
955 return 1;
956 }
957 }
958
959 int
960 read_aiff_header(BitstreamReader* bs, unsigned verbatim_size,
961 unsigned* sample_rate, unsigned* channel_mask)
962 {
963 if (!setjmp(*br_try(bs))) {
964 uint8_t FORM[4];
965 unsigned SIZE;
966 uint8_t AIFF[4];
967
968 bs->set_endianness(bs, BS_BIG_ENDIAN);
969 bs->parse(bs, "4b 32u 4b", FORM, &SIZE, AIFF);
970
971 if (memcmp(FORM, "FORM", 4) || memcmp(AIFF, "AIFF", 4)) {
972 br_etry(bs);
973 return 1;
974 } else {
975 verbatim_size -= bs_format_byte_size("4b 32u 4b");
976 }
977
978 while (verbatim_size > 0) {
979 uint8_t chunk_id[4];
980 unsigned chunk_size;
981 bs->parse(bs, "4b 32u", chunk_id, &chunk_size);
982
983 verbatim_size -= bs_format_byte_size("4b 32u");
984 if (!memcmp(chunk_id, "COMM", 4)) {
985 /*parse COMM chunk*/
986
987 unsigned channels;
988 unsigned total_sample_frames;
989 unsigned bits_per_sample;
990
991 bs->parse(bs, "16u 32u 16u",
992 &channels,
993 &total_sample_frames,
994 &bits_per_sample);
995
996 *sample_rate = read_ieee_extended(bs);
997
998 switch (channels) {
999 case 1:
1000 *channel_mask = 0x4;
1001 break;
1002 case 2:
1003 *channel_mask = 0x3;
1004 break;
1005 default:
1006 *channel_mask = 0;
1007 break;
1008 }
1009
1010 br_etry(bs);
1011 return 0;
1012 } else {
1013 if (chunk_size % 2) {
1014 /*handle odd-sized chunks*/
1015 bs->skip_bytes(bs, chunk_size + 1);
1016 verbatim_size -= (chunk_size + 1);
1017 } else {
1018 bs->skip_bytes(bs, chunk_size);
1019 verbatim_size -= chunk_size;
1020 }
1021 }
1022 }
1023
1024 /*no COMM chunk found in aiff header*/
1025 br_etry(bs);
1026 return 1;
1027 } else {
1028 /*I/O error bouncing through aiff chunks*/
1029 br_etry(bs);
1030 return 1;
1031 }
1032 }
1033
1034 int
1035 read_ieee_extended(BitstreamReader* bs)
1036 {
1037 unsigned sign;
1038 unsigned exponent;
1039 uint64_t mantissa;
1040
1041 bs->parse(bs, "1u 15u 64U", &sign, &exponent, &mantissa);
1042 if ((exponent == 0) && (mantissa == 0)) {
1043 return 0;
1044 } else if (exponent == 0x7FFF) {
1045 return INT_MAX;
1046 } else {
1047 const int f = (int)((double)mantissa *
1048 pow(2.0, (double)exponent - 16383 - 63));
1049 if (sign)
1050 return -f;
1051 else
1052 return f;
1053 }
1054 }
1055
1056 #ifdef STANDALONE
1057 #include <errno.h>
1058
1059 int main(int argc, char* argv[]) {
1060 decoders_SHNDecoder decoder;
1061 FILE* file;
1062 aa_int* framelist;
1063 unsigned output_data_size;
1064 unsigned char* output_data;
1065 unsigned bytes_per_sample;
1066 FrameList_int_to_char_converter converter;
1067
1068 if (argc < 2) {
1069 fprintf(stderr, "*** Usage: %s <file.shn>\n", argv[0]);
1070 return 1;
1071 }
1072 if ((file = fopen(argv[1], "rb")) == NULL) {
1073 fprintf(stderr, "*** %s: %s\n", argv[1], strerror(errno));
1074 return 1;
1075 } else {
1076 decoder.bitstream = br_open(file, BS_BIG_ENDIAN);
1077 decoder.stream_finished = 0;
1078
1079 decoder.means = aa_int_new();
1080 decoder.previous_samples = aa_int_new();
1081
1082 decoder.samples = aa_int_new();
1083 decoder.unshifted = aa_int_new();
1084 decoder.pcm_header = a_int_new();
1085 decoder.pcm_footer = a_int_new();
1086
1087 framelist = aa_int_new();
1088
1089 output_data_size = 1;
1090 output_data = malloc(output_data_size);
1091 }
1092
1093 /*read Shorten header for basic info*/
1094 switch (read_shn_header(&decoder, decoder.bitstream)) {
1095 case INVALID_MAGIC_NUMBER:
1096 fprintf(stderr, "invalid magic number");
1097 goto error;
1098 case INVALID_SHORTEN_VERSION:
1099 fprintf(stderr, "invalid Shorten version");
1100 goto error;
1101 case UNSUPPORTED_FILE_TYPE:
1102 fprintf(stderr, "unsupported Shorten file type");
1103 goto error;
1104 case IOERROR:
1105 fprintf(stderr, "I/O error reading Shorten header");
1106 goto error;
1107 default:
1108 bytes_per_sample = decoder.bits_per_sample / 8;
1109 converter = FrameList_get_int_to_char_converter(
1110 decoder.bits_per_sample, 0, 1);
1111 break;
1112 }
1113
1114 while (!decoder.stream_finished) {
1115 unsigned pcm_size;
1116 unsigned channel;
1117 unsigned frame;
1118
1119 framelist->reset(framelist);
1120 /*decode set of commands into a single framelist*/
1121 switch (read_framelist(&decoder, framelist)) {
1122 case OK:
1123 /*convert framelist to string and output it to stdout*/
1124 pcm_size = (bytes_per_sample *
1125 framelist->len *
1126 framelist->_[0]->len);
1127 if (pcm_size > output_data_size) {
1128 output_data_size = pcm_size;
1129 output_data = realloc(output_data, output_data_size);
1130 }
1131 for (channel = 0; channel < framelist->len; channel++) {
1132 const a_int* channel_data = framelist->_[channel];
1133 for (frame = 0; frame < channel_data->len; frame++) {
1134 converter(channel_data->_[frame],
1135 output_data +
1136 ((frame * framelist->len) + channel) *
1137 bytes_per_sample);
1138 }
1139 }
1140
1141 fwrite(output_data, sizeof(unsigned char), pcm_size, stdout);
1142 break;
1143 case END_OF_STREAM:
1144 /*automatically sets stream_finished to true*/
1145 break;
1146 case UNKNOWN_COMMAND:
1147 fprintf(stderr, "unknown command in Shorten stream");
1148 goto error;
1149 case IOERROR:
1150 fprintf(stderr, "I/O error reading Shorten file");
1151 goto error;
1152 default:
1153 /*shouldn't get here*/
1154 fprintf(stderr, "unknown value from read_framelist()");
1155 goto error;
1156 }
1157 }
1158
1159 decoder.bitstream->close(decoder.bitstream);
1160
1161 decoder.means->del(decoder.means);
1162 decoder.previous_samples->del(decoder.previous_samples);
1163 decoder.samples->del(decoder.samples);
1164 decoder.unshifted->del(decoder.unshifted);
1165 decoder.pcm_header->del(decoder.pcm_header);
1166 decoder.pcm_footer->del(decoder.pcm_footer);
1167 framelist->del(framelist);
1168 free(output_data);
1169
1170 return 0;
1171
1172 error:
1173 decoder.bitstream->close(decoder.bitstream);
1174
1175 decoder.means->del(decoder.means);
1176 decoder.previous_samples->del(decoder.previous_samples);
1177 decoder.samples->del(decoder.samples);
1178 decoder.unshifted->del(decoder.unshifted);
1179 decoder.pcm_header->del(decoder.pcm_header);
1180 decoder.pcm_footer->del(decoder.pcm_footer);
1181 framelist->del(framelist);
1182 free(output_data);
1183
1184 return 1;
1185 }
1186 #endif
1187