|
| 1 | +// Synthetic Vorbis only: no game files, encoder, or audio device required. |
| 2 | +// Exercise type-2 residues beyond the stereo bound and decode with a caller-owned arena. |
| 3 | +#include <assert.h> |
| 4 | +#include <math.h> |
| 5 | +#include <stdio.h> |
| 6 | +#include <stdlib.h> |
| 7 | +#include <string.h> |
| 8 | +#define STB_VORBIS_NO_STDIO |
| 9 | +#define STB_VORBIS_NO_INTEGER_CONVERSION |
| 10 | +#include "../../third_party/stb_vorbis/stb_vorbis.c" |
| 11 | + |
| 12 | +typedef struct { unsigned char data[8192]; int bits; } packet; |
| 13 | +typedef struct { unsigned char data[32768]; int size, seq; } stream; |
| 14 | +static void bits(packet *p, unsigned value, int n) { |
| 15 | + assert(p->bits + n <= (int)sizeof(p->data) * 8); |
| 16 | + for (int i = 0; i < n; i++, p->bits++) |
| 17 | + p->data[p->bits / 8] |= ((value >> i) & 1) << (p->bits % 8); |
| 18 | +} |
| 19 | +static packet header(int type) { |
| 20 | + packet p = {0}; bits(&p, type, 8); |
| 21 | + for (int i = 0; i < 6; i++) bits(&p, "vorbis"[i], 8); |
| 22 | + return p; |
| 23 | +} |
| 24 | +static void page(stream *s, packet *p, int flags, unsigned granule) { |
| 25 | + int len = (p->bits + 7) / 8, segments = len / 255 + 1; |
| 26 | + int start = s->size, size = 27 + segments + len; |
| 27 | + assert(start + size <= (int)sizeof(s->data)); |
| 28 | + unsigned char *d = s->data + start; |
| 29 | + memcpy(d, "OggS", 4); d[5] = flags; |
| 30 | + for (int i = 0; i < 4; i++) { d[6+i] = granule >> (8*i); d[18+i] = s->seq >> (8*i); } |
| 31 | + d[14] = 1; d[26] = segments; |
| 32 | + for (int i = 0; i < segments; i++) d[27+i] = i+1 == segments ? len % 255 : 255; |
| 33 | + memcpy(d + 27 + segments, p->data, len); |
| 34 | + unsigned crc = 0; |
| 35 | + for (int i = 0; i < size; i++) crc = crc32_update(crc, d[i]); |
| 36 | + for (int i = 0; i < 4; i++) d[22+i] = crc >> (8*i); |
| 37 | + s->size += size; s->seq++; |
| 38 | +} |
| 39 | +static void codebook(packet *p, int lookup) { |
| 40 | + bits(p, 0x564342, 24); bits(p, 1, 16); bits(p, 1, 24); // one dimension, one entry |
| 41 | + bits(p, 0, 1); bits(p, 0, 1); bits(p, 0, 5); // unordered, dense, one-bit code |
| 42 | + bits(p, lookup, 4); |
| 43 | + if (lookup) { |
| 44 | + bits(p, (788u << 21) | 1, 32); bits(p, 0, 32); // minimum 1.0, delta 0.0 |
| 45 | + bits(p, 0, 4); bits(p, 0, 1); bits(p, 0, 1); // one-bit multiplicand, no sequence |
| 46 | + } |
| 47 | +} |
| 48 | +static stream fixture(int channels, int type, int split) { |
| 49 | + stream s = {0}; packet p = header(1); |
| 50 | + bits(&p, 0, 32); bits(&p, channels, 8); bits(&p, 48000, 32); |
| 51 | + for (int i = 0; i < 3; i++) bits(&p, 0, 32); |
| 52 | + bits(&p, 0x66, 8); bits(&p, 1, 8); // both block sizes 64, framing flag |
| 53 | + page(&s, &p, 2, 0); |
| 54 | + p = header(3); bits(&p, 0, 32); bits(&p, 0, 32); bits(&p, 1, 8); page(&s, &p, 0, 0); |
| 55 | + p = header(5); bits(&p, 1, 8); codebook(&p, 0); codebook(&p, 1); |
| 56 | + bits(&p, 0, 6); bits(&p, 0, 16); // one time configuration |
| 57 | + bits(&p, 0, 6); bits(&p, 1, 16); bits(&p, 0, 5); // one floor 1, no partitions |
| 58 | + bits(&p, 0, 2); bits(&p, 5, 4); // multiplier 1, range 32 |
| 59 | + bits(&p, split ? 1 : 0, 6); |
| 60 | + for (int i = 0; i < 1 + split; i++) { |
| 61 | + int ch = split ? (i ? 1 : channels-1) : channels; |
| 62 | + bits(&p, type, 16); bits(&p, 0, 24); |
| 63 | + bits(&p, 32 * (type == 2 ? ch : 1), 24); // complete interleaved vector |
| 64 | + bits(&p, 31, 24); bits(&p, 0, 6); bits(&p, 0, 8); // partition size 32, classbook 0 |
| 65 | + bits(&p, 1, 3); bits(&p, 0, 1); bits(&p, 1, 8); // pass 0 uses vector codebook 1 |
| 66 | + } |
| 67 | + bits(&p, 0, 6); bits(&p, 0, 16); bits(&p, split, 1); |
| 68 | + if (split) bits(&p, 1, 4); |
| 69 | + bits(&p, 0, 1); bits(&p, 0, 2); // no coupling, reserved bits |
| 70 | + if (split) for (int i = 0; i < channels; i++) bits(&p, i == channels-1, 4); |
| 71 | + for (int i = 0; i < 1 + split; i++) { bits(&p, 0, 8); bits(&p, 0, 8); bits(&p, i, 8); } |
| 72 | + bits(&p, 0, 6); bits(&p, 0, 1); bits(&p, 0, 16); bits(&p, 0, 16); bits(&p, 0, 8); |
| 73 | + bits(&p, 1, 1); page(&s, &p, 0, 0); |
| 74 | + for (int frame = 0; frame < 4; frame++) { |
| 75 | + p = (packet){0}; bits(&p, 0, 1); |
| 76 | + for (int i = 0; i < channels; i++) { bits(&p, 1, 1); bits(&p, 255, 8); bits(&p, 255, 8); } |
| 77 | + // Each 32-bin partition consumes one scalar code and 32 vector codes, all zero. |
| 78 | + for (int i = 0; i < channels; i++) { bits(&p, 0, 1); bits(&p, 0, 32); } |
| 79 | + page(&s, &p, frame == 3 ? 4 : 0, frame * 32); |
| 80 | + } |
| 81 | + return s; |
| 82 | +} |
| 83 | +static int decode(stream *s, int push, const stb_vorbis_alloc *arena, float *output) { |
| 84 | + int error = 0, used = 0, offset = 0, total = 0; |
| 85 | + stb_vorbis *f = push ? stb_vorbis_open_pushdata(s->data, s->size, &used, &error, arena) |
| 86 | + : stb_vorbis_open_memory(s->data, s->size, &error, arena); |
| 87 | + assert(f && error == 0); offset = used; |
| 88 | + for (;;) { |
| 89 | + float **pcm = NULL; int frames = 0; |
| 90 | + if (push) { |
| 91 | + if (offset == s->size) break; |
| 92 | + used = stb_vorbis_decode_frame_pushdata(f, s->data + offset, s->size - offset, NULL, &pcm, &frames); |
| 93 | + assert(used || frames); offset += used; |
| 94 | + } else frames = stb_vorbis_get_frame_float(f, NULL, &pcm); |
| 95 | + if (!push && !frames) break; |
| 96 | + assert(total + frames <= 128); |
| 97 | + for (int i = 0; i < frames; i++) for (int c = 0; c < f->channels; c++) { |
| 98 | + float v = pcm[c][i]; assert(isfinite(v)); output[(total+i)*f->channels+c] = v; |
| 99 | + } |
| 100 | + total += frames; |
| 101 | + } |
| 102 | + assert(stb_vorbis_get_error(f) == 0); stb_vorbis_close(f); return total; |
| 103 | +} |
| 104 | +static void check(int channels, int type, int split) { |
| 105 | + stream s = fixture(channels, type, split); int error = 0; |
| 106 | + stb_vorbis *f = stb_vorbis_open_memory(s.data, s.size, &error, NULL); assert(f); |
| 107 | + stb_vorbis_info info = stb_vorbis_get_info(f); |
| 108 | + if (!split) { |
| 109 | + // Feed zero codes through the actual residue decoder: every coefficient must be one. |
| 110 | + unsigned char zeros[512] = {0}, skip[8] = {0}; float values[8][32], *planes[8]; |
| 111 | + for (int c = 0; c < channels; c++) planes[c] = values[c]; |
| 112 | + f->stream = zeros; f->stream_end = zeros + sizeof zeros; |
| 113 | + f->bytes_in_seg = 255; f->last_seg = TRUE; f->valid_bits = 0; f->acc = 0; |
| 114 | + decode_residue(f, planes, channels, 32, 0, skip); |
| 115 | + for (int c = 0; c < channels; c++) for (int i = 0; i < 32; i++) { |
| 116 | + if (values[c][i] != 1.0f) fprintf(stderr, "type %d, %d channels: coefficient [%d][%d] = %g, expected 1\n", type, channels, c, i, values[c][i]); |
| 117 | + assert(values[c][i] == 1.0f); |
| 118 | + } |
| 119 | + } |
| 120 | + stb_vorbis_close(f); |
| 121 | + unsigned temp = info.temp_memory_required > info.setup_temp_memory_required ? info.temp_memory_required : info.setup_temp_memory_required; |
| 122 | + unsigned bytes = info.setup_memory_required + temp; |
| 123 | + // Use exactly the advertised requirement, rather than hiding an underestimate with a large arena. |
| 124 | + char *memory = malloc(bytes); assert(memory); |
| 125 | + stb_vorbis_alloc arena = { memory, (int)bytes }; |
| 126 | + float reference[128*8], actual[128*8]; int n = decode(&s, 0, NULL, reference); assert(n == 96); |
| 127 | + for (int push = 0; push <= 1; push++) for (int fixed = 0; fixed <= 1; fixed++) { |
| 128 | + int got = decode(&s, push, fixed ? &arena : NULL, actual); assert(got == n); |
| 129 | + double energy = 0; |
| 130 | + for (int i = 0; i < n*channels; i++) { assert(actual[i] == reference[i]); energy += actual[i]*actual[i]; } |
| 131 | + assert(energy > 0); |
| 132 | + } |
| 133 | + free(memory); printf("type %d, %d channels, %d submap(s): heap/arena pull/push passed\n", type, channels, 1+split); fflush(stdout); |
| 134 | +} |
| 135 | +int main(void) { |
| 136 | + crc32_init(); |
| 137 | + for (int type = 0; type <= 2; type++) { check(1, type, 0); check(2, type, 0); } |
| 138 | + check(3, 2, 0); check(6, 2, 0); check(8, 2, 0); check(6, 2, 1); |
| 139 | + return 0; |
| 140 | +} |
0 commit comments