-
Notifications
You must be signed in to change notification settings - Fork 3
/
h4m_audio_decode.c
2569 lines (2338 loc) · 77.7 KB
/
h4m_audio_decode.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include <stddef.h>
// ABI compatibility with HVQM4 1.3 is still kinda broken,
// but native decoding is bit-perfect *shrug*
// 1.5 increased the size of some fields from 16 to 32 bits
// and added per-plane motion vector modes
// define this to be ABI compatible with most HVQM4 1.3 binaries
//#define VERSION_1_3
// define this to be ABI compatible with Frogger Beyond
//#define FROGGER
// some things are per plane (luma, 2x chroma)
#define PLANE_COUNT 3
// some things are only split between luma and chroma
#define LUMA_CHROMA 2
#define LUMA_IDX 0
#define CHROMA_IDX 1
#ifndef NATIVE
#pragma pack(1)
static void bla()
{
fputs("called an uninitialized function pointer\n", stderr);
exit(EXIT_FAILURE);
}
#define SYMBOLT(x, T) T (*p##x)() = (void*)bla;
#include "symbols.inc"
#undef SYMBOLT
#endif
/* .h4m (HVQM4 1.3/1.5) audio decoder 0.4 by flacs/hcs */
//#define VERBOSE_PRINT
/* big endian */
static uint8_t get8(FILE *infile)
{
uint8_t buf[1];
if (1 != fread(buf, 1, 1, infile))
{
fprintf(stderr, "read error at 0x%lx\n", (unsigned long)ftell(infile));
exit(EXIT_FAILURE);
}
return buf[0];
}
static uint16_t read16(void const * buf)
{
uint32_t v = 0;
for (int i = 0; i < 2; i++)
{
v <<= 8;
v |= ((uint8_t const *)buf)[i];
}
return v;
}
static uint16_t get16(FILE * infile)
{
uint8_t buf[2];
if (2 != fread(buf, 1, 2, infile))
{
fprintf(stderr, "read error at 0x%lx\n", (unsigned long)ftell(infile));
exit(EXIT_FAILURE);
}
return read16(buf);
}
static uint32_t read32(void const * buf)
{
uint32_t v = 0;
for (int i = 0; i < 4; i++)
{
v <<= 8;
v |= ((uint8_t const*)buf)[i];
}
return v;
}
static uint32_t get32(FILE * infile)
{
uint8_t buf[4];
if (4 != fread(buf, 1, 4, infile))
{
fprintf(stderr, "read error at 0x%lx\n", (unsigned long)ftell(infile));
exit(EXIT_FAILURE);
}
return read32(buf);
}
static void expect32(uint32_t expected, FILE * infile)
{
uint32_t v = get32(infile);
if (v != expected)
{
fprintf(stderr, "expected 0x%08"PRIx32" at 0x%lx, got 0x%08"PRIx32"\n",
expected, ftell(infile)-4, v);
exit(EXIT_FAILURE);
}
}
static void expect32_imm(uint32_t expected, uint32_t actual, unsigned long offset)
{
if (expected != actual)
{
fprintf(stderr, "expected 0x%08lx to be 0x%08"PRIx32", got 0x%08"PRIx32"\n",
offset, expected, actual);
exit(EXIT_FAILURE);
}
}
static void expect32_text(uint32_t expected, uint32_t actual, char const *name)
{
if (expected != actual)
{
fprintf(stderr, "expected %s to be 0x%08"PRIx32", got 0x%08"PRIx32"\n",
name, expected, actual);
exit(EXIT_FAILURE);
}
}
static void seek_past(uint32_t offset, FILE * infile)
{
if (-1 == fseek(infile, offset, SEEK_CUR))
{
fprintf(stderr, "seek by 0x%x failed\n", offset);
exit(EXIT_FAILURE);
}
}
/* audio decode */
struct audio_state
{
struct
{
int16_t hist;
int8_t idx;
} *ch;
};
const int32_t IMA_Steps[89] =
{
7, 8, 9, 10, 11, 12, 13, 14,
16, 17, 19, 21, 23, 25, 28, 31,
34, 37, 41, 45, 50, 55, 60, 66,
73, 80, 88, 97, 107, 118, 130, 143,
157, 173, 190, 209, 230, 253, 279, 307,
337, 371, 408, 449, 494, 544, 598, 658,
724, 796, 876, 963, 1060, 1166, 1282, 1411,
1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024,
3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484,
7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794,
32767
};
const int8_t IMA_IndexTable[16] =
{
-1, -1, -1, -1, 2, 4, 6, 8,
-1, -1, -1, -1, 2, 4, 6, 8
};
static int16_t clamp16(int32_t v)
{
if (v > INT16_MAX) return INT16_MAX;
else if (v < INT16_MIN) return INT16_MIN;
return v;
}
__attribute__((unused))
static void decode_audio(struct audio_state *state, int first_aud, uint32_t sample_count, FILE *infile, FILE *outfile, int channels)
{
int16_t * samples = samples = malloc(sample_count*sizeof(int16_t)*channels);
uint32_t t = 0, i = 0;
if (first_aud)
{
for (int c = channels - 1; c >= 0; c--)
{
state->ch[c].hist = get8(infile);
state->ch[c].hist <<= 8;
uint8_t b = get8(infile);
state->ch[c].hist |= (b & 0x80);
state->ch[c].idx = b & 0x7f;
if (state->ch[c].idx > 88)
{
fprintf(stderr, "invalid step index (%d) at 0x%lx\n", state->ch[c].idx, ftell(infile));
exit(EXIT_FAILURE);
}
}
for (int c = 0; c < channels; c++)
{
samples[t++] = state->ch[c].hist;
}
i ++;
}
uint8_t b;
int bitsleft = 0;
for (; i < sample_count; i++)
{
for (int c = channels-1; c >= 0; c--)
{
if (bitsleft == 0)
{
b = get8(infile);
bitsleft = 8;
}
int32_t ima_step = IMA_Steps[state->ch[c].idx];
int32_t ima_delta = ima_step >> 3;
if (b & 0x10) ima_delta += ima_step >> 2;
if (b & 0x20) ima_delta += ima_step >> 1;
if (b & 0x40) ima_delta += ima_step;
if (b & 0x80) state->ch[c].hist = clamp16(state->ch[c].hist - ima_delta);
else state->ch[c].hist = clamp16(state->ch[c].hist + ima_delta);
state->ch[c].idx += IMA_IndexTable[(b&0xf0)>>4];
if (state->ch[c].idx > 88) state->ch[c].idx = 88;
if (state->ch[c].idx < 0) state->ch[c].idx = 0;
b <<= 4;
bitsleft -= 4;
}
for (int c = 0; c < channels; c++)
{
samples[t++] = state->ch[c].hist;
}
}
if (channels)
{
if (sample_count != fwrite(samples, sizeof(int16_t)*channels, sample_count, outfile))
{
perror("fwrite");
exit(EXIT_FAILURE);
}
}
free(samples);
}
/* video stuff */
static int32_t divTable[0x10];
static int32_t mcdivTable[0x200];
static void init_global_constants(void)
{
divTable[0] = 0;
mcdivTable[0] = 0;
for (int i = 1; i < 0x10; ++i)
divTable[i] = 0x1000 / (i * 16) * 16;
for (int i = 1; i < 0x200; ++i)
mcdivTable[i] = 0x1000 / i;
}
static void HVQM4InitDecoder(void)
{
init_global_constants();
}
// 4x4 block of single value
static void dcBlock(uint8_t *dst, uint32_t stride, uint8_t value)
{
for (int y = 0; y < 4; ++y)
for (int x = 0; x < 4; ++x)
dst[y * stride + x] = value;
}
static uint8_t saturate(int32_t x)
{
return x < 0 ? 0 : x > 0xFF ? 0xFF : x;
}
static uint8_t sat_mean8(uint32_t u)
{
return saturate((u + 4) / 8);
}
// 4x4 block
static void WeightImBlock(uint8_t *dst, uint32_t stride, uint8_t value, uint8_t top, uint8_t bottom, uint8_t left, uint8_t right)
{
/*
+---+---+---+
| | T | |
+---+---+---+
| L | D | R |
+---+---+---+
| | B | |
+---+---+---+
*/
int32_t tmb = top - bottom;
int32_t lmr = left - right;
int32_t vph = tmb + lmr;
int32_t vmh = tmb - lmr;
int32_t v2 = value * 2;
int32_t v8 = value * 8;
int32_t tpl = (top + left ) - v2;
int32_t tpr = (top + right) - v2;
int32_t bpr = (bottom + right) - v2;
int32_t bpl = (bottom + left ) - v2;
int32_t tml = top - left;
int32_t tmr = top - right;
int32_t bmr = bottom - right;
int32_t bml = bottom - left;
// V:
// 6 8 8 6
// 8 10 10 8
// 8 10 10 8
// 6 8 8 6
//
// T:
// 2 2 2 2
// 0 0 0 0
// -1 -1 -1 -1
// -1 -1 -1 -1
//
// B/L/R: like T but rotated accordingly
// (6*V + 2*T - B + 2*L - R + 4) / 8
// (8*V + 2*T - B - R + 4) / 8
// (8*V + 2*T - B - L + 4) / 8
// (6*V + 2*T - B - L + 2*R + 4) / 8
dst[0] = sat_mean8(v8 + vph + tpl);
dst[1] = sat_mean8(v8 + vph + tml);
dst[2] = sat_mean8(v8 + vmh + tmr);
dst[3] = sat_mean8(v8 + vmh + tpr);
dst += stride;
// ( 8*V - B + 2*L - R + 4) / 8
// (10*V - B - R + 4) / 8
// (10*V - B - L + 4) / 8
// ( 8*V - B - L + 2*R + 4) / 8
dst[0] = sat_mean8(v8 + vph - tml);
dst[1] = sat_mean8(v8 - bpr );
dst[2] = sat_mean8(v8 - bpl );
dst[3] = sat_mean8(v8 + vmh - tmr);
dst += stride;
// ( 8*V - T + 2*L - R + 4) / 8
// (10*V - T - R + 4) / 8
// (10*V - T - L
dst[0] = sat_mean8(v8 - vmh - bml);
dst[1] = sat_mean8(v8 - tpr );
dst[2] = sat_mean8(v8 - tpl );
dst[3] = sat_mean8(v8 - vph - bmr);
dst += stride;
dst[0] = sat_mean8(v8 - vmh + bpl);
dst[1] = sat_mean8(v8 - vmh + bml);
dst[2] = sat_mean8(v8 - vph + bmr);
dst[3] = sat_mean8(v8 - vph + bpr);
}
typedef struct
{
uint32_t pos;
int32_t root;
#if defined(VERSION_1_3) && !defined(FROGGER)
uint16_t array[2][0x200];
#else
uint32_t array[2][0x200];
#endif
} Tree;
#ifndef NATIVE
#if defined(VERSION_1_3) && !defined(FROGGER)
_Static_assert(sizeof(Tree) == 0x808, "sizeof(Tree) is incorrect");
#else
_Static_assert(sizeof(Tree) == 0x1008, "sizeof(Tree) is incorrect");
#endif
#endif
typedef struct
{
#if defined(VERSION_1_3) && !defined(FROGGER)
void const *ptr; // 0-3
void const *start; // 4-7 seems to be unused which is probably why it was removed in 1.5
uint32_t size; // 8-B
uint8_t value; // C
uint8_t bit; // D
uint8_t pad[2]; // E-F
#else
void const *ptr; // 0-3
uint32_t size; // 4-7
uint32_t value; // 8-B
int32_t bit; // C-F
#endif
} BitBuffer;
#ifndef NATIVE
_Static_assert(sizeof(BitBuffer) == 0x10, "sizeof(BitBuffer) is incorrect");
#endif
typedef struct
{
BitBuffer buf; // 0-F
Tree *tree; // 0x10-0x13
} BitBufferWithTree;
#ifndef NATIVE
_Static_assert(sizeof(BitBufferWithTree) == 0x14, "sizeof(BitBufferWithTree) is incorrect");
#endif
typedef struct
{
uint8_t value;
uint8_t type;
} BlockData;
typedef struct
{
// size: 0x38 (56)
BlockData *border; // 0-3 beginning of the plane including the border
BlockData *payload; // 4-7 beginning of the non-border plane data
uint16_t h_blocks; // 8-9
uint16_t v_blocks; // A-B
uint16_t h_blocks_safe; // C-D
uint16_t v_blocks_safe; // E-F
// offsets of PBs within one MCB
// +---+---+
// | 0 | 3 |
// +---+---+
// | 1 | 2 |
// +---+---+
uint16_t mcb_offset[4]; // 10-17
// same for samples within one PB
uint32_t pb_offset[4]; // 18-27
uint16_t width_in_samples; // 28-29
uint16_t height_in_samples; // 2A-2B
uint32_t size_in_samples; // 2C-2F
uint8_t width_shift; // 30
uint8_t height_shift; // 31
uint8_t pb_per_mcb_x; // 32 1..2
uint8_t pb_per_mcb_y; // 33 1..2
uint8_t blocks_per_mcb; // 34 1..4
uint8_t padding[3]; // 35-37
} HVQPlaneDesc;
#ifndef NATIVE
_Static_assert(sizeof(HVQPlaneDesc) == 0x38, "sizeof(HVQPlaneDesc) is incorrect");
#endif
typedef struct
{
// size: 0x6CD8 (plane data comes right after)
HVQPlaneDesc planes[PLANE_COUNT]; // 0x00 - 0xA8
Tree trees[6];
BitBufferWithTree dc_values[PLANE_COUNT]; // DC values
BitBufferWithTree dc_rle[PLANE_COUNT]; // DC run lengths
BitBufferWithTree bufTree0[PLANE_COUNT];
BitBufferWithTree basis_num[LUMA_CHROMA];
BitBufferWithTree basis_num_run[LUMA_CHROMA];
BitBuffer fixvl[PLANE_COUNT]; // uncompressed high-entropy data
BitBufferWithTree mv_h; // horizontal motion vectors
BitBufferWithTree mv_v; // vertical motion vectors
BitBufferWithTree mcb_proc; // macroblock proc
BitBufferWithTree mcb_type; // macroblock type
uint16_t h_nest_size;
uint16_t v_nest_size;
uint8_t is_landscape; // FIXME: check what happens for square video
uint8_t nest_data[70 * 38]; // 1.3: 0x3261 1.5: 0x6261
#if defined(VERSION_1_3) && !defined(FROGGER)
uint8_t padding;
uint16_t dc_max; // 0x3CC6
uint16_t dc_min; // 0x3CC8
#else
uint8_t padding[3];
uint32_t dc_max; // 0x6CC8
uint32_t dc_min; // 0x6CCC
#endif
uint8_t unk_shift; // 1.3: 0x3CCA 1.5: 0x6CD0
uint8_t dc_shift; // 0x6CD1
// number of residual bits to read from mv_h/mv_v,
// one setting for each of past and future
uint8_t mc_residual_bits_h[2]; // 0x6CD2
uint8_t mc_residual_bits_v[2]; // 0x6CD4
#if !defined(VERSION_1_3) || defined(FROGGER)
uint8_t maybe_padding[2]; // 0x6CD6-0x6CD7
#endif
} VideoState;
#ifndef NATIVE
#if defined(VERSION_1_3) && !defined(FROGGER)
_Static_assert(sizeof(VideoState) == 0x3CD0, "sizeof(VideoState) is incorrect");
#else
_Static_assert(sizeof(VideoState) == 0x6CD8, "sizeof(VideoState) is incorrect");
#endif
#endif
typedef struct
{
VideoState *state; // 0-3
uint16_t width; // 4-5
uint16_t height; // 6-7
uint8_t h_samp; // 8
uint8_t v_samp; // 9
} SeqObj;
typedef struct
{
SeqObj seqobj;
void *past;
void *present;
void *future;
} Player;
typedef struct
{
uint16_t hres;
uint16_t vres;
uint8_t h_samp;
uint8_t v_samp;
uint8_t video_mode;
} VideoInfo;
// copy uncompressed 4x4 block
static void OrgBlock(VideoState *state, uint8_t *dst, uint32_t dst_stride, uint32_t plane_idx)
{
BitBuffer *buf = &state->fixvl[plane_idx];
for (int y = 0; y < 4; ++y)
for (int x = 0; x < 4; ++x)
dst[y * dst_stride + x] = *(uint8_t*)buf->ptr++;
}
// can use FFmpeg's get_bits1()/get_bits(..., 8) for this
static int16_t getBit(BitBuffer *buf)
{
#if defined(VERSION_1_3) && !defined(FROGGER)
int32_t bit = buf->bit;
if (bit == 0)
{
buf->value = *(uint8_t const*)buf->ptr;
buf->ptr += 1;
bit = 0x80;
}
buf->bit = bit >> 1;
return buf->value & bit ? 1 : 0;
#else
int32_t bit = buf->bit;
if (bit < 0)
{
buf->value = read32(buf->ptr);
buf->ptr += 4;
bit = 31;
}
buf->bit = bit - 1;
return (buf->value >> bit) & 1;
#endif
}
static uint8_t getByte(BitBuffer *buf)
{
#if defined(VERSION_1_3) && !defined(FROGGER)
uint8_t value = 0;
for (int i = 7; i >= 0; --i)
value |= getBit(buf) << i;
#else
uint32_t value = buf->value;
int32_t bit = buf->bit;
if (bit < 7)
{
buf->value = read32(buf->ptr);
buf->ptr += 4;
value <<= 7 - bit;
value |= buf->value >> (bit + 25);
bit += 24;
}
else
{
value >>= bit - 7;
bit -= 8;
}
buf->bit = bit;
return value & 0xFF;
#endif
}
static uint32_t readTree_signed;
static uint32_t readTree_scale;
static int16_t _readTree(Tree *dst, BitBuffer *src)
{
if (getBit(src) == 0)
{
// leaf node
uint8_t byte = getByte(src);
int16_t symbol = byte;
if (readTree_signed && byte > 0x7F)
symbol = (int8_t)byte;
symbol <<= readTree_scale;
dst->array[0][byte] = symbol;
return byte;
}
else
{
// recurse
uint32_t pos = dst->pos++;
// read the 0 side of the tree
dst->array[0][pos] = (uint32_t)_readTree(dst, src);
// read the 1 side of the tree
dst->array[1][pos] = (uint32_t)_readTree(dst, src);
return (int16_t)pos;
}
}
static void readTree(BitBufferWithTree *buf, uint32_t is_signed, uint32_t scale)
{
readTree_signed = is_signed;
readTree_scale = scale;
Tree *tree = buf->tree;
tree->pos = 0x100;
if (buf->buf.size == 0)
tree->root = 0;
else
tree->root = _readTree(tree, &buf->buf);
}
static uint32_t decodeHuff(BitBufferWithTree *buf)
{
Tree *tree = buf->tree;
int32_t pos = tree->root;
while (pos >= 0x100)
pos = tree->array[getBit(&buf->buf)][pos];
return tree->array[0][pos];
}
// used for DC values
static int32_t decodeSOvfSym(BitBufferWithTree *buf, int32_t min, int32_t max)
{
int32_t sum = 0;
int32_t value;
do
{
value = decodeHuff(buf);
sum += value;
} while (value <= min || value >= max);
return sum;
}
// used for MCB proc/type, max is always 255
static int32_t decodeUOvfSym(BitBufferWithTree *buf, int32_t max)
{
int32_t sum = 0;
int32_t value;
do
{
value = decodeHuff(buf);
sum += value;
} while (value >= max);
return sum;
}
static uint32_t GetAotBasis(VideoState *state, uint8_t basis_out[4][4], int32_t *sum, uint8_t const *nest_data, uint32_t nest_stride, uint32_t plane_idx)
{
BitBuffer *buf = &state->fixvl[plane_idx];
// the nest size 70x38 is chosen to allow for
// 6/5-bit coordinates (0..63 x 0..31) + largest sampling pattern (6x6) = 0..69 x 0..37
// 0x003F: offset70 : 6
// 0x07C0: offset38 : 5
// 0x0800: stride70 : 1
// 0x1000: stride38 : 1
// 0x6000: offset : 2
// 0x8000: negated : 1
uint16_t bits = read16(buf->ptr);
buf->ptr += 2;
// compute the offset inside the nest
uint32_t x_stride, y_stride;
uint32_t offset70 = bits & 0x3F;
uint32_t offset38 = (bits >> 6) & 0x1F;
uint32_t stride70 = (bits >> 11) & 1;
uint32_t stride38 = (bits >> 12) & 1;
if (state->is_landscape)
{
nest_data += nest_stride * offset38 + offset70;
x_stride = 1 << stride70;
y_stride = nest_stride << stride38;
}
else
{
nest_data += nest_stride * offset70 + offset38;
x_stride = 1 << stride38;
y_stride = nest_stride << stride70;
}
// copy basis vector from the nest
uint8_t min = nest_data[0];
uint8_t max = nest_data[0];
for (int y = 0; y < 4; ++y)
{
for (int x = 0; x < 4; ++x)
{
uint8_t nest_value = nest_data[y * y_stride + x * x_stride];
basis_out[y][x] = nest_value;
min = nest_value < min ? nest_value : min;
max = nest_value > max ? nest_value : max;
}
}
*sum += decodeHuff(&state->bufTree0[plane_idx]);
int32_t inverse = divTable[max - min];
if (bits & 0x8000)
inverse = -inverse;
int32_t offset = (bits >> 13) & 3;
return (*sum + offset) * inverse;
}
static uint32_t GetMCAotBasis(VideoState *state, uint8_t basis_out[4][4], int32_t *sum, uint8_t const *nest_data, uint32_t nest_stride, uint32_t plane_idx)
{
// the only difference to GetAotBasis() seems to be the ">> 4 & 0xF"
BitBuffer *buf = &state->fixvl[plane_idx];
uint16_t bits = read16(buf->ptr);
buf->ptr += 2;
uint32_t step, stride;
uint32_t big = bits & 0x3F;
uint32_t small = (bits >> 6) & 0x1F;
if (state->is_landscape)
{
nest_data += nest_stride * small + big;
step = 1 << ((bits >> 11) & 1);
stride = nest_stride << ((bits >> 12) & 1);
}
else
{
nest_data += nest_stride * big + small;
step = 1 << ((bits >> 12) & 1);
stride = nest_stride << ((bits >> 11) & 1);
}
uint8_t min, max;
min = max = (nest_data[0] >> 4) & 0xF; // !
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
uint8_t nest_value = (nest_data[i * stride + j * step] >> 4) & 0xF; // !
basis_out[i][j] = nest_value;
min = nest_value < min ? nest_value : min;
max = nest_value > max ? nest_value : max;
}
}
*sum += decodeHuff(&state->bufTree0[plane_idx]);
int32_t inverse = divTable[max - min];
if (bits & 0x8000)
inverse = -inverse;
int32_t foo = (bits >> 13) & 3;
return (*sum + foo) * inverse;
}
static int32_t GetAotSum(VideoState *state, int32_t result[4][4], uint8_t num_bases, uint8_t const *nest_data, uint32_t nest_stride, uint32_t plane_idx)
{
for (int y = 0; y < 4; ++y)
for (int x = 0; x < 4; ++x)
result[y][x] = 0;
uint8_t basis[4][4];
int32_t temp = 0;
for (int k = 0; k < num_bases; ++k)
{
uint32_t factor = GetAotBasis(state, basis, &temp, nest_data, nest_stride, plane_idx);
for (int y = 0; y < 4; ++y)
for (int x = 0; x < 4; ++x)
result[y][x] += factor * basis[y][x];
}
int32_t sum = 0;
for (int y = 0; y < 4; ++y)
for (int x = 0; x < 4; ++x)
sum += result[y][x];
int32_t mean = sum >> 4;
return mean;
}
static int32_t GetMCAotSum(VideoState *state, int32_t result[4][4], uint8_t num_bases, uint8_t const *nest_data, uint32_t nest_stride, uint32_t plane_idx)
{
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
result[i][j] = 0;
uint8_t byte_result[4][4];
int32_t temp = 0;
for (int k = 0; k < num_bases; ++k)
{
uint32_t factor = GetMCAotBasis(state, byte_result, &temp, nest_data, nest_stride, plane_idx);
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
result[i][j] += factor * byte_result[i][j];
}
int32_t sum = 0;
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
sum += result[i][j];
int32_t mean = sum >> 4;
return mean;
}
static void HVQM4InitSeqObj(SeqObj *seqobj, VideoInfo *videoinfo)
{
seqobj->width = videoinfo->hres;
seqobj->height = videoinfo->vres;
// TODO: find better names for these, uv_h_step/uv_v_step?
seqobj->h_samp = videoinfo->h_samp;
seqobj->v_samp = videoinfo->v_samp;
}
static uint32_t HVQM4BuffSize(SeqObj *seqobj)
{
uint32_t h_blocks = seqobj->width / 4;
uint32_t v_blocks = seqobj->height / 4;
uint32_t y_blocks = (h_blocks + 2) * (v_blocks + 2);
uint32_t uv_h_blocks = seqobj->h_samp == 2 ? h_blocks / 2 : h_blocks;
uint32_t uv_v_blocks = seqobj->v_samp == 2 ? v_blocks / 2 : v_blocks;
uint32_t uv_blocks = (uv_h_blocks + 2) * (uv_v_blocks + 2);
uint32_t total = (y_blocks + uv_blocks * 2) * sizeof(uint16_t);
return sizeof(VideoState) + total;
}
// h_samp/v_samp: pixels per sample
static void setHVQPlaneDesc(SeqObj *seqobj, uint8_t plane_idx, uint8_t h_samp, uint8_t v_samp)
{
HVQPlaneDesc *plane = &seqobj->state->planes[plane_idx];
plane->width_shift = h_samp == 2 ? 1 : 0;
plane->width_in_samples = seqobj->width >> plane->width_shift;
plane->height_shift = v_samp == 2 ? 1 : 0;
plane->height_in_samples = seqobj->height >> plane->height_shift;
plane->size_in_samples = plane->width_in_samples * plane->height_in_samples;
// pixels per 2x2 block
plane->pb_per_mcb_x = 2 >> plane->width_shift; // 1..2
plane->pb_per_mcb_y = 2 >> plane->height_shift; // 1..2
plane->blocks_per_mcb = plane->pb_per_mcb_x * plane->pb_per_mcb_y; // 1..4
// number of 4x4 blocks
plane->h_blocks = seqobj->width / (h_samp * 4);
plane->v_blocks = seqobj->height / (v_samp * 4);
// number of 4x4 blocks + border
plane->h_blocks_safe = plane->h_blocks + 2;
plane->v_blocks_safe = plane->v_blocks + 2;
// offset of blocks in MCB
plane->mcb_offset[0] = 0;
plane->mcb_offset[1] = plane->h_blocks_safe;
plane->mcb_offset[2] = plane->h_blocks_safe + 1;
plane->mcb_offset[3] = 1;
plane->pb_offset[0] = 0;
plane->pb_offset[1] = plane->width_in_samples << 2;
plane->pb_offset[2] = (plane->width_in_samples << 2) + 4;
plane->pb_offset[3] = 4;
}
// HACK: assumes 4:2:0
__attribute__((unused))
static void dumpYUV(Player *player, char const *path)
{
FILE *f = fopen(path, "wb+");
uint32_t w = player->seqobj.width, h = player->seqobj.height;
fprintf(f, "P5\n%u %u\n255\n", w, h * 2);
uint8_t const *p = player->present;
for (int plane = 0; plane < 2; ++plane)
{
for (uint32_t i = 0; i < h; ++i)
{
for (uint32_t j = 0; j < w; ++j)
{
if (plane == 0 || j < w / 2)
fputc(*p++, f);
else
fputc(0, f);
}
}
}
fclose(f);
}
// HACK: assumes 4:2:0, assumes JPEG color space
static uint8_t clamp255(float f)
{
return f < 0 ? 0 : f > 255 ? 255 : (uint8_t)f;
}
static void dumpRGB(Player *player, char const *path)
{
FILE *f = fopen(path, "wb+");
uint32_t w = player->seqobj.width, h = player->seqobj.height;
fprintf(f, "P6\n%u %u\n255\n", w, h);
uint8_t const *yp = player->present;
uint8_t const *up = yp + w*h;
uint8_t const *vp = up + w*h/4;
uint8_t *rgb = malloc(w * h * 3);
uint8_t *ptr = rgb;
for (uint32_t i = 0; i < h; ++i)
{
for (uint32_t j = 0; j < w; ++j)
{
float y = yp[i * w + j];
float u = up[i/2 * w/2 + j/2];
float v = vp[i/2 * w/2 + j/2];
*ptr++ = clamp255(y + 1.402f*(v - 128.f));
*ptr++ = clamp255(y - 0.34414f*(u - 128.f) - 0.71414f*(v - 128.f));
*ptr++ = clamp255(y + 1.772f*(u - 128.f));
}
}
fwrite(rgb, w*h*3, 1, f);
fclose(f);
free(rgb);
}
__attribute__((unused))
static void dumpPlanes(VideoState *state, char const *prefix)
{
for (int plane_idx = 0; plane_idx < PLANE_COUNT; ++plane_idx)
{
char path[128];
snprintf(path, 128, "%s_%c.ppm", prefix, "yuv"[plane_idx]);
FILE *f = fopen(path, "wb+");
HVQPlaneDesc *plane = &state->planes[plane_idx];
fprintf(f, "P5\n%u %u\n255\n", plane->h_blocks_safe, plane->v_blocks_safe);
uint8_t const *p = (uint8_t const*)plane->border;
for (int i = 0; i < plane->v_blocks_safe; ++i)
{
for (int j = 0; j < plane->h_blocks_safe; ++j)
{
fputc(*p++, f);
++p;
}
}
fclose(f);
}
}
static void set_border(BlockData *dst)
{
dst->value = 0x7F;
dst->type = 0xFF;
}
static void HVQM4SetBuffer(SeqObj *seqobj, void *workbuff)
{
VideoState *state = workbuff;
seqobj->state = state;
setHVQPlaneDesc(seqobj, 0, 1, 1);
setHVQPlaneDesc(seqobj, 1, seqobj->h_samp, seqobj->v_samp);
setHVQPlaneDesc(seqobj, 2, seqobj->h_samp, seqobj->v_samp);
state->is_landscape = seqobj->width >= seqobj->height;
if (state->is_landscape)
{
state->h_nest_size = 70;
state->v_nest_size = 38;
}
else
{
state->h_nest_size = 38;
state->v_nest_size = 70;
}
state->basis_num[0].tree = &state->trees[3];
state->basis_num[1].tree = &state->trees[3];
state->basis_num_run[0].tree = &state->trees[1];
state->basis_num_run[1].tree = &state->trees[1];
state->dc_values[0].tree = &state->trees[0];
state->dc_values[1].tree = &state->trees[0];
state->dc_values[2].tree = &state->trees[0];
state->dc_rle[0].tree = &state->trees[1]; // reuse!
state->dc_rle[1].tree = &state->trees[1]; //
state->dc_rle[2].tree = &state->trees[1]; //
state->bufTree0[0].tree = &state->trees[2];
state->bufTree0[1].tree = &state->trees[2];
state->bufTree0[2].tree = &state->trees[2];
state->mv_h.tree = &state->trees[4];
state->mv_v.tree = &state->trees[4];
state->mcb_proc.tree = &state->trees[5];
state->mcb_type.tree = &state->trees[5];