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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
|
/*
* Resolume DXV decoder
* Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
* Copyright (C) 2018 Paul B Mahol
*
* This file is part of FFmpeg.
*
* FFmpeg 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.
*
* FFmpeg 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 FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
#include "libavutil/imgutils.h"
#include "mathops.h"
#include "avcodec.h"
#include "bytestream.h"
#include "codec_internal.h"
#include "lzf.h"
#include "texturedsp.h"
#include "thread.h"
typedef struct DXVContext {
TextureDSPContext texdsp;
GetByteContext gbc;
uint8_t *tex_data; // Compressed texture
uint8_t *ctex_data; // Compressed texture
int tex_rat; // Compression ratio
int tex_step; // Distance between blocks
int ctex_step; // Distance between blocks
int64_t tex_size; // Texture size
int64_t ctex_size; // Texture size
/* Optimal number of slices for parallel decoding */
int slice_count;
uint8_t *op_data[4]; // Opcodes
int64_t op_size[4]; // Opcodes size
int texture_block_w;
int texture_block_h;
int ctexture_block_w;
int ctexture_block_h;
/* Pointer to the selected decompression function */
int (*tex_funct)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block);
int (*tex_funct_planar[2])(uint8_t *plane0, ptrdiff_t stride0,
uint8_t *plane1, ptrdiff_t stride1,
const uint8_t *block);
} DXVContext;
static void decompress_indices(uint8_t *dst, const uint8_t *src)
{
int block, i;
for (block = 0; block < 2; block++) {
int tmp = AV_RL24(src);
/* Unpack 8x3 bit from last 3 byte block */
for (i = 0; i < 8; i++)
dst[i] = (tmp >> (i * 3)) & 0x7;
src += 3;
dst += 8;
}
}
static int extract_component(int yo0, int yo1, int code)
{
int yo;
if (yo0 == yo1) {
yo = yo0;
} else if (code == 0) {
yo = yo0;
} else if (code == 1) {
yo = yo1;
} else {
if (yo0 > yo1) {
yo = (uint8_t) (((8 - code) * yo0 +
(code - 1) * yo1) / 7);
} else {
if (code == 6) {
yo = 0;
} else if (code == 7) {
yo = 255;
} else {
yo = (uint8_t) (((6 - code) * yo0 +
(code - 1) * yo1) / 5);
}
}
}
return yo;
}
static int cocg_block(uint8_t *plane0, ptrdiff_t stride0,
uint8_t *plane1, ptrdiff_t stride1,
const uint8_t *block)
{
uint8_t co_indices[16];
uint8_t cg_indices[16];
uint8_t co0 = *(block);
uint8_t co1 = *(block + 1);
uint8_t cg0 = *(block + 8);
uint8_t cg1 = *(block + 9);
int x, y;
decompress_indices(co_indices, block + 2);
decompress_indices(cg_indices, block + 10);
for (y = 0; y < 4; y++) {
for (x = 0; x < 4; x++) {
int co_code = co_indices[x + y * 4];
int cg_code = cg_indices[x + y * 4];
plane0[x] = extract_component(cg0, cg1, cg_code);
plane1[x] = extract_component(co0, co1, co_code);
}
plane0 += stride0;
plane1 += stride1;
}
return 16;
}
static void yao_subblock(uint8_t *dst, uint8_t *yo_indices,
ptrdiff_t stride, const uint8_t *block)
{
uint8_t yo0 = *(block);
uint8_t yo1 = *(block + 1);
int x, y;
decompress_indices(yo_indices, block + 2);
for (y = 0; y < 4; y++) {
for (x = 0; x < 4; x++) {
int yo_code = yo_indices[x + y * 4];
dst[x] = extract_component(yo0, yo1, yo_code);
}
dst += stride;
}
}
static int yo_block(uint8_t *dst, ptrdiff_t stride,
uint8_t *unused0, ptrdiff_t unused1,
const uint8_t *block)
{
uint8_t yo_indices[16];
yao_subblock(dst, yo_indices, stride, block);
yao_subblock(dst + 4, yo_indices, stride, block + 8);
yao_subblock(dst + 8, yo_indices, stride, block + 16);
yao_subblock(dst + 12, yo_indices, stride, block + 24);
return 32;
}
static int yao_block(uint8_t *plane0, ptrdiff_t stride0,
uint8_t *plane3, ptrdiff_t stride1,
const uint8_t *block)
{
uint8_t yo_indices[16];
uint8_t a_indices[16];
yao_subblock(plane0, yo_indices, stride0, block);
yao_subblock(plane3, a_indices, stride1, block + 8);
yao_subblock(plane0 + 4, yo_indices, stride0, block + 16);
yao_subblock(plane3 + 4, a_indices, stride1, block + 24);
yao_subblock(plane0 + 8, yo_indices, stride0, block + 32);
yao_subblock(plane3 + 8, a_indices, stride1, block + 40);
yao_subblock(plane0 + 12, yo_indices, stride0, block + 48);
yao_subblock(plane3 + 12, a_indices, stride1, block + 56);
return 64;
}
static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
int slice, int thread_nb)
{
const DXVContext *ctx = avctx->priv_data;
AVFrame *frame = arg;
const uint8_t *d = ctx->tex_data;
int w_block = avctx->coded_width / ctx->texture_block_w;
int h_block = avctx->coded_height / ctx->texture_block_h;
int x, y;
int start_slice, end_slice;
start_slice = h_block * slice / ctx->slice_count;
end_slice = h_block * (slice + 1) / ctx->slice_count;
if (ctx->tex_funct) {
for (y = start_slice; y < end_slice; y++) {
uint8_t *p = frame->data[0] + y * frame->linesize[0] * ctx->texture_block_h;
int off = y * w_block;
for (x = 0; x < w_block; x++) {
ctx->tex_funct(p + x * 4 * ctx->texture_block_w, frame->linesize[0],
d + (off + x) * ctx->tex_step);
}
}
} else {
const uint8_t *c = ctx->ctex_data;
for (y = start_slice; y < end_slice; y++) {
uint8_t *p0 = frame->data[0] + y * frame->linesize[0] * ctx->texture_block_h;
uint8_t *p3 = ctx->tex_step != 64 ? NULL : frame->data[3] + y * frame->linesize[3] * ctx->texture_block_h;
int off = y * w_block;
for (x = 0; x < w_block; x++) {
ctx->tex_funct_planar[0](p0 + x * ctx->texture_block_w, frame->linesize[0],
p3 != NULL ? p3 + x * ctx->texture_block_w : NULL, frame->linesize[3],
d + (off + x) * ctx->tex_step);
}
}
w_block = (avctx->coded_width / 2) / ctx->ctexture_block_w;
h_block = (avctx->coded_height / 2) / ctx->ctexture_block_h;
start_slice = h_block * slice / ctx->slice_count;
end_slice = h_block * (slice + 1) / ctx->slice_count;
for (y = start_slice; y < end_slice; y++) {
uint8_t *p0 = frame->data[1] + y * frame->linesize[1] * ctx->ctexture_block_h;
uint8_t *p1 = frame->data[2] + y * frame->linesize[2] * ctx->ctexture_block_h;
int off = y * w_block;
for (x = 0; x < w_block; x++) {
ctx->tex_funct_planar[1](p0 + x * ctx->ctexture_block_w, frame->linesize[1],
p1 + x * ctx->ctexture_block_w, frame->linesize[2],
c + (off + x) * ctx->ctex_step);
}
}
}
return 0;
}
/* This scheme addresses already decoded elements depending on 2-bit status:
* 0 -> copy new element
* 1 -> copy one element from position -x
* 2 -> copy one element from position -(get_byte() + 2) * x
* 3 -> copy one element from position -(get_16le() + 0x102) * x
* x is always 2 for dxt1 and 4 for dxt5. */
#define CHECKPOINT(x) \
do { \
if (state == 0) { \
if (bytestream2_get_bytes_left(gbc) < 4) \
return AVERROR_INVALIDDATA; \
value = bytestream2_get_le32(gbc); \
state = 16; \
} \
op = value & 0x3; \
value >>= 2; \
state--; \
switch (op) { \
case 1: \
idx = x; \
break; \
case 2: \
idx = (bytestream2_get_byte(gbc) + 2) * x; \
if (idx > pos) { \
av_log(avctx, AV_LOG_ERROR, "idx %d > %d\n", idx, pos); \
return AVERROR_INVALIDDATA; \
} \
break; \
case 3: \
idx = (bytestream2_get_le16(gbc) + 0x102) * x; \
if (idx > pos) { \
av_log(avctx, AV_LOG_ERROR, "idx %d > %d\n", idx, pos); \
return AVERROR_INVALIDDATA; \
} \
break; \
} \
} while(0)
static int dxv_decompress_dxt1(AVCodecContext *avctx)
{
DXVContext *ctx = avctx->priv_data;
GetByteContext *gbc = &ctx->gbc;
uint32_t value, prev, op;
int idx = 0, state = 0;
int pos = 2;
/* Copy the first two elements */
AV_WL32(ctx->tex_data, bytestream2_get_le32(gbc));
AV_WL32(ctx->tex_data + 4, bytestream2_get_le32(gbc));
/* Process input until the whole texture has been filled */
while (pos + 2 <= ctx->tex_size / 4) {
CHECKPOINT(2);
/* Copy two elements from a previous offset or from the input buffer */
if (op) {
prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
AV_WL32(ctx->tex_data + 4 * pos, prev);
pos++;
prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
AV_WL32(ctx->tex_data + 4 * pos, prev);
pos++;
} else {
CHECKPOINT(2);
if (op)
prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
else
prev = bytestream2_get_le32(gbc);
AV_WL32(ctx->tex_data + 4 * pos, prev);
pos++;
CHECKPOINT(2);
if (op)
prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
else
prev = bytestream2_get_le32(gbc);
AV_WL32(ctx->tex_data + 4 * pos, prev);
pos++;
}
}
return 0;
}
typedef struct OpcodeTable {
int16_t next;
uint8_t val1;
uint8_t val2;
} OpcodeTable;
static int fill_ltable(GetByteContext *gb, uint32_t *table, int *nb_elements)
{
unsigned half = 512, bits = 1023, left = 1024, input, mask;
int value, counter = 0, rshift = 10, lshift = 30;
mask = bytestream2_get_le32(gb) >> 2;
while (left) {
if (counter >= 256)
return AVERROR_INVALIDDATA;
value = bits & mask;
left -= bits & mask;
mask >>= rshift;
lshift -= rshift;
table[counter++] = value;
if (lshift < 16) {
if (bytestream2_get_bytes_left(gb) <= 0)
return AVERROR_INVALIDDATA;
input = bytestream2_get_le16(gb);
mask += input << lshift;
lshift += 16;
}
if (left < half) {
half >>= 1;
bits >>= 1;
rshift--;
}
}
for (; !table[counter - 1]; counter--)
if (counter <= 0)
return AVERROR_INVALIDDATA;
*nb_elements = counter;
if (counter < 256)
memset(&table[counter], 0, 4 * (256 - counter));
if (lshift >= 16)
bytestream2_seek(gb, -2, SEEK_CUR);
return 0;
}
static int fill_optable(unsigned *table0, OpcodeTable *table1, int nb_elements)
{
unsigned table2[256] = { 0 };
unsigned x = 0;
int val0, val1, i, j = 2, k = 0;
table2[0] = table0[0];
for (i = 0; i < nb_elements - 1; i++, table2[i] = val0) {
val0 = table0[i + 1] + table2[i];
}
if (!table2[0]) {
do {
k++;
} while (!table2[k]);
}
j = 2;
for (i = 1024; i > 0; i--) {
for (table1[x].val1 = k; k < 256 && j > table2[k]; k++);
x = (x - 383) & 0x3FF;
j++;
}
if (nb_elements > 0)
memcpy(&table2[0], table0, 4 * nb_elements);
for (i = 0; i < 1024; i++) {
val0 = table1[i].val1;
val1 = table2[val0];
table2[val0]++;
x = 31 - ff_clz(val1);
if (x > 10)
return AVERROR_INVALIDDATA;
table1[i].val2 = 10 - x;
table1[i].next = (val1 << table1[i].val2) - 1024;
}
return 0;
}
static int get_opcodes(GetByteContext *gb, uint32_t *table, uint8_t *dst, int op_size, int nb_elements)
{
OpcodeTable optable[1024];
int sum, x, val, lshift, rshift, ret, i, idx;
int64_t size_in_bits;
unsigned endoffset, newoffset, offset;
unsigned next;
const uint8_t *src = gb->buffer;
ret = fill_optable(table, optable, nb_elements);
if (ret < 0)
return ret;
size_in_bits = bytestream2_get_le32(gb);
endoffset = ((size_in_bits + 7) >> 3) - 4;
if (endoffset <= 0 || bytestream2_get_bytes_left(gb) < endoffset)
return AVERROR_INVALIDDATA;
offset = endoffset;
next = AV_RL32(src + endoffset);
rshift = (((size_in_bits & 0xFF) - 1) & 7) + 15;
lshift = 32 - rshift;
idx = (next >> rshift) & 0x3FF;
for (i = 0; i < op_size; i++) {
dst[i] = optable[idx].val1;
val = optable[idx].val2;
sum = val + lshift;
x = (next << lshift) >> 1 >> (31 - val);
newoffset = offset - (sum >> 3);
lshift = sum & 7;
idx = x + optable[idx].next;
offset = newoffset;
if (offset > endoffset)
return AVERROR_INVALIDDATA;
next = AV_RL32(src + offset);
}
bytestream2_skip(gb, (size_in_bits + 7 >> 3) - 4);
return 0;
}
static int dxv_decompress_opcodes(GetByteContext *gb, void *dstp, size_t op_size)
{
int pos = bytestream2_tell(gb);
int flag = bytestream2_peek_byte(gb);
if ((flag & 3) == 0) {
bytestream2_skip(gb, 1);
bytestream2_get_buffer(gb, dstp, op_size);
} else if ((flag & 3) == 1) {
bytestream2_skip(gb, 1);
memset(dstp, bytestream2_get_byte(gb), op_size);
} else {
uint32_t table[256];
int ret, elements = 0;
ret = fill_ltable(gb, table, &elements);
if (ret < 0)
return ret;
ret = get_opcodes(gb, table, dstp, op_size, elements);
if (ret < 0)
return ret;
}
return bytestream2_tell(gb) - pos;
}
static int dxv_decompress_cgo(DXVContext *ctx, GetByteContext *gb,
uint8_t *tex_data, int tex_size,
uint8_t *op_data, int *oindex,
int op_size,
uint8_t **dstp, int *statep,
uint8_t **tab0, uint8_t **tab1,
int offset)
{
uint8_t *dst = *dstp;
uint8_t *tptr0, *tptr1, *tptr3;
int oi = *oindex;
int state = *statep;
int opcode, v, vv;
if (state <= 0) {
if (oi >= op_size)
return AVERROR_INVALIDDATA;
opcode = op_data[oi++];
if (!opcode) {
v = bytestream2_get_byte(gb);
if (v == 255) {
do {
if (bytestream2_get_bytes_left(gb) <= 0)
return AVERROR_INVALIDDATA;
opcode = bytestream2_get_le16(gb);
v += opcode;
} while (opcode == 0xFFFF);
}
AV_WL32(dst, AV_RL32(dst - (8 + offset)));
AV_WL32(dst + 4, AV_RL32(dst - (4 + offset)));
state = v + 4;
goto done;
}
switch (opcode) {
case 1:
AV_WL32(dst, AV_RL32(dst - (8 + offset)));
AV_WL32(dst + 4, AV_RL32(dst - (4 + offset)));
break;
case 2:
vv = (8 + offset) * (bytestream2_get_le16(gb) + 1);
if (vv < 0 || vv > dst - tex_data)
return AVERROR_INVALIDDATA;
tptr0 = dst - vv;
v = AV_RL32(tptr0);
AV_WL32(dst, AV_RL32(tptr0));
AV_WL32(dst + 4, AV_RL32(tptr0 + 4));
tab0[0x9E3779B1 * (uint16_t)v >> 24] = dst;
tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
break;
case 3:
AV_WL32(dst, bytestream2_get_le32(gb));
AV_WL32(dst + 4, bytestream2_get_le32(gb));
tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
break;
case 4:
tptr3 = tab1[bytestream2_get_byte(gb)];
if (!tptr3)
return AVERROR_INVALIDDATA;
AV_WL16(dst, bytestream2_get_le16(gb));
AV_WL16(dst + 2, AV_RL16(tptr3));
dst[4] = tptr3[2];
AV_WL16(dst + 5, bytestream2_get_le16(gb));
dst[7] = bytestream2_get_byte(gb);
tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
break;
case 5:
tptr3 = tab1[bytestream2_get_byte(gb)];
if (!tptr3)
return AVERROR_INVALIDDATA;
AV_WL16(dst, bytestream2_get_le16(gb));
AV_WL16(dst + 2, bytestream2_get_le16(gb));
dst[4] = bytestream2_get_byte(gb);
AV_WL16(dst + 5, AV_RL16(tptr3));
dst[7] = tptr3[2];
tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
break;
case 6:
tptr0 = tab1[bytestream2_get_byte(gb)];
if (!tptr0)
return AVERROR_INVALIDDATA;
tptr1 = tab1[bytestream2_get_byte(gb)];
if (!tptr1)
return AVERROR_INVALIDDATA;
AV_WL16(dst, bytestream2_get_le16(gb));
AV_WL16(dst + 2, AV_RL16(tptr0));
dst[4] = tptr0[2];
AV_WL16(dst + 5, AV_RL16(tptr1));
dst[7] = tptr1[2];
tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
break;
case 7:
v = (8 + offset) * (bytestream2_get_le16(gb) + 1);
if (v < 0 || v > dst - tex_data)
return AVERROR_INVALIDDATA;
tptr0 = dst - v;
AV_WL16(dst, bytestream2_get_le16(gb));
AV_WL16(dst + 2, AV_RL16(tptr0 + 2));
AV_WL32(dst + 4, AV_RL32(tptr0 + 4));
tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
break;
case 8:
tptr1 = tab0[bytestream2_get_byte(gb)];
if (!tptr1)
return AVERROR_INVALIDDATA;
AV_WL16(dst, AV_RL16(tptr1));
AV_WL16(dst + 2, bytestream2_get_le16(gb));
AV_WL32(dst + 4, bytestream2_get_le32(gb));
tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
break;
case 9:
tptr1 = tab0[bytestream2_get_byte(gb)];
if (!tptr1)
return AVERROR_INVALIDDATA;
tptr3 = tab1[bytestream2_get_byte(gb)];
if (!tptr3)
return AVERROR_INVALIDDATA;
AV_WL16(dst, AV_RL16(tptr1));
AV_WL16(dst + 2, AV_RL16(tptr3));
dst[4] = tptr3[2];
AV_WL16(dst + 5, bytestream2_get_le16(gb));
dst[7] = bytestream2_get_byte(gb);
tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
break;
case 10:
tptr1 = tab0[bytestream2_get_byte(gb)];
if (!tptr1)
return AVERROR_INVALIDDATA;
tptr3 = tab1[bytestream2_get_byte(gb)];
if (!tptr3)
return AVERROR_INVALIDDATA;
AV_WL16(dst, AV_RL16(tptr1));
AV_WL16(dst + 2, bytestream2_get_le16(gb));
dst[4] = bytestream2_get_byte(gb);
AV_WL16(dst + 5, AV_RL16(tptr3));
dst[7] = tptr3[2];
tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
break;
case 11:
tptr0 = tab0[bytestream2_get_byte(gb)];
if (!tptr0)
return AVERROR_INVALIDDATA;
tptr3 = tab1[bytestream2_get_byte(gb)];
if (!tptr3)
return AVERROR_INVALIDDATA;
tptr1 = tab1[bytestream2_get_byte(gb)];
if (!tptr1)
return AVERROR_INVALIDDATA;
AV_WL16(dst, AV_RL16(tptr0));
AV_WL16(dst + 2, AV_RL16(tptr3));
dst[4] = tptr3[2];
AV_WL16(dst + 5, AV_RL16(tptr1));
dst[7] = tptr1[2];
break;
case 12:
tptr1 = tab0[bytestream2_get_byte(gb)];
if (!tptr1)
return AVERROR_INVALIDDATA;
v = (8 + offset) * (bytestream2_get_le16(gb) + 1);
if (v < 0 || v > dst - tex_data)
return AVERROR_INVALIDDATA;
tptr0 = dst - v;
AV_WL16(dst, AV_RL16(tptr1));
AV_WL16(dst + 2, AV_RL16(tptr0 + 2));
AV_WL32(dst + 4, AV_RL32(tptr0 + 4));
tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
break;
case 13:
AV_WL16(dst, AV_RL16(dst - (8 + offset)));
AV_WL16(dst + 2, bytestream2_get_le16(gb));
AV_WL32(dst + 4, bytestream2_get_le32(gb));
tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
break;
case 14:
tptr3 = tab1[bytestream2_get_byte(gb)];
if (!tptr3)
return AVERROR_INVALIDDATA;
AV_WL16(dst, AV_RL16(dst - (8 + offset)));
AV_WL16(dst + 2, AV_RL16(tptr3));
dst[4] = tptr3[2];
AV_WL16(dst + 5, bytestream2_get_le16(gb));
dst[7] = bytestream2_get_byte(gb);
tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
break;
case 15:
tptr3 = tab1[bytestream2_get_byte(gb)];
if (!tptr3)
return AVERROR_INVALIDDATA;
AV_WL16(dst, AV_RL16(dst - (8 + offset)));
AV_WL16(dst + 2, bytestream2_get_le16(gb));
dst[4] = bytestream2_get_byte(gb);
AV_WL16(dst + 5, AV_RL16(tptr3));
dst[7] = tptr3[2];
tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
break;
case 16:
tptr3 = tab1[bytestream2_get_byte(gb)];
if (!tptr3)
return AVERROR_INVALIDDATA;
tptr1 = tab1[bytestream2_get_byte(gb)];
if (!tptr1)
return AVERROR_INVALIDDATA;
AV_WL16(dst, AV_RL16(dst - (8 + offset)));
AV_WL16(dst + 2, AV_RL16(tptr3));
dst[4] = tptr3[2];
AV_WL16(dst + 5, AV_RL16(tptr1));
dst[7] = tptr1[2];
break;
case 17:
v = (8 + offset) * (bytestream2_get_le16(gb) + 1);
if (v < 0 || v > dst - tex_data)
return AVERROR_INVALIDDATA;
AV_WL16(dst, AV_RL16(dst - (8 + offset)));
AV_WL16(dst + 2, AV_RL16(&dst[-v + 2]));
AV_WL32(dst + 4, AV_RL32(&dst[-v + 4]));
tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
break;
default:
break;
}
} else {
done:
AV_WL32(dst, AV_RL32(dst - (8 + offset)));
AV_WL32(dst + 4, AV_RL32(dst - (4 + offset)));
state--;
}
if (dst - tex_data + 8 > tex_size)
return AVERROR_INVALIDDATA;
dst += 8;
*oindex = oi;
*dstp = dst;
*statep = state;
return 0;
}
static int dxv_decompress_cocg(DXVContext *ctx, GetByteContext *gb,
uint8_t *tex_data, int tex_size,
uint8_t *op_data0, uint8_t *op_data1,
int max_op_size0, int max_op_size1)
{
uint8_t *dst, *tab2[256] = { 0 }, *tab0[256] = { 0 }, *tab3[256] = { 0 }, *tab1[256] = { 0 };
int op_offset = bytestream2_get_le32(gb);
unsigned op_size0 = bytestream2_get_le32(gb);
unsigned op_size1 = bytestream2_get_le32(gb);
int data_start = bytestream2_tell(gb);
int skip0, skip1, oi0 = 0, oi1 = 0;
int ret, state0 = 0, state1 = 0;
if (op_offset < 12 || op_offset - 12 > bytestream2_get_bytes_left(gb))
return AVERROR_INVALIDDATA;
dst = tex_data;
bytestream2_skip(gb, op_offset - 12);
if (op_size0 > max_op_size0)
return AVERROR_INVALIDDATA;
skip0 = dxv_decompress_opcodes(gb, op_data0, op_size0);
if (skip0 < 0)
return skip0;
if (op_size1 > max_op_size1)
return AVERROR_INVALIDDATA;
skip1 = dxv_decompress_opcodes(gb, op_data1, op_size1);
if (skip1 < 0)
return skip1;
bytestream2_seek(gb, data_start, SEEK_SET);
AV_WL32(dst, bytestream2_get_le32(gb));
AV_WL32(dst + 4, bytestream2_get_le32(gb));
AV_WL32(dst + 8, bytestream2_get_le32(gb));
AV_WL32(dst + 12, bytestream2_get_le32(gb));
tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFF) >> 24] = dst + 2;
tab2[0x9E3779B1 * AV_RL16(dst + 8) >> 24] = dst + 8;
tab3[0x9E3779B1 * (AV_RL32(dst + 10) & 0xFFFFFF) >> 24] = dst + 10;
dst += 16;
while (dst + 10 < tex_data + tex_size) {
ret = dxv_decompress_cgo(ctx, gb, tex_data, tex_size, op_data0, &oi0, op_size0,
&dst, &state0, tab0, tab1, 8);
if (ret < 0)
return ret;
ret = dxv_decompress_cgo(ctx, gb, tex_data, tex_size, op_data1, &oi1, op_size1,
&dst, &state1, tab2, tab3, 8);
if (ret < 0)
return ret;
}
bytestream2_seek(gb, data_start - 12 + op_offset + skip0 + skip1, SEEK_SET);
return 0;
}
static int dxv_decompress_yo(DXVContext *ctx, GetByteContext *gb,
uint8_t *tex_data, int tex_size,
uint8_t *op_data, int max_op_size)
{
int op_offset = bytestream2_get_le32(gb);
unsigned op_size = bytestream2_get_le32(gb);
int data_start = bytestream2_tell(gb);
uint8_t *dst, *table0[256] = { 0 }, *table1[256] = { 0 };
int ret, state = 0, skip, oi = 0, v, vv;
if (op_offset < 8 || op_offset - 8 > bytestream2_get_bytes_left(gb))
return AVERROR_INVALIDDATA;
dst = tex_data;
bytestream2_skip(gb, op_offset - 8);
if (op_size > max_op_size)
return AVERROR_INVALIDDATA;
skip = dxv_decompress_opcodes(gb, op_data, op_size);
if (skip < 0)
return skip;
bytestream2_seek(gb, data_start, SEEK_SET);
v = bytestream2_get_le32(gb);
AV_WL32(dst, v);
vv = bytestream2_get_le32(gb);
table0[0x9E3779B1 * (uint16_t)v >> 24] = dst;
AV_WL32(dst + 4, vv);
table1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFF) >> 24] = dst + 2;
dst += 8;
while (dst < tex_data + tex_size) {
ret = dxv_decompress_cgo(ctx, gb, tex_data, tex_size, op_data, &oi, op_size,
&dst, &state, table0, table1, 0);
if (ret < 0)
return ret;
}
bytestream2_seek(gb, data_start + op_offset + skip - 8, SEEK_SET);
return 0;
}
static int dxv_decompress_ycg6(AVCodecContext *avctx)
{
DXVContext *ctx = avctx->priv_data;
GetByteContext *gb = &ctx->gbc;
int ret;
ret = dxv_decompress_yo(ctx, gb, ctx->tex_data, ctx->tex_size,
ctx->op_data[0], ctx->op_size[0]);
if (ret < 0)
return ret;
return dxv_decompress_cocg(ctx, gb, ctx->ctex_data, ctx->ctex_size,
ctx->op_data[1], ctx->op_data[2],
ctx->op_size[1], ctx->op_size[2]);
}
static int dxv_decompress_yg10(AVCodecContext *avctx)
{
DXVContext *ctx = avctx->priv_data;
GetByteContext *gb = &ctx->gbc;
int ret;
ret = dxv_decompress_cocg(ctx, gb, ctx->tex_data, ctx->tex_size,
ctx->op_data[0], ctx->op_data[3],
ctx->op_size[0], ctx->op_size[3]);
if (ret < 0)
return ret;
return dxv_decompress_cocg(ctx, gb, ctx->ctex_data, ctx->ctex_size,
ctx->op_data[1], ctx->op_data[2],
ctx->op_size[1], ctx->op_size[2]);
}
static int dxv_decompress_dxt5(AVCodecContext *avctx)
{
DXVContext *ctx = avctx->priv_data;
GetByteContext *gbc = &ctx->gbc;
uint32_t value, op, prev;
int idx, state = 0;
int pos = 4;
int run = 0;
int probe, check;
/* Copy the first four elements */
AV_WL32(ctx->tex_data + 0, bytestream2_get_le32(gbc));
AV_WL32(ctx->tex_data + 4, bytestream2_get_le32(gbc));
AV_WL32(ctx->tex_data + 8, bytestream2_get_le32(gbc));
AV_WL32(ctx->tex_data + 12, bytestream2_get_le32(gbc));
/* Process input until the whole texture has been filled */
while (pos + 2 <= ctx->tex_size / 4) {
if (run) {
run--;
prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
AV_WL32(ctx->tex_data + 4 * pos, prev);
pos++;
prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
AV_WL32(ctx->tex_data + 4 * pos, prev);
pos++;
} else {
if (bytestream2_get_bytes_left(gbc) < 1)
return AVERROR_INVALIDDATA;
if (state == 0) {
value = bytestream2_get_le32(gbc);
state = 16;
}
op = value & 0x3;
value >>= 2;
state--;
switch (op) {
case 0:
/* Long copy */
check = bytestream2_get_byte(gbc) + 1;
if (check == 256) {
do {
probe = bytestream2_get_le16(gbc);
check += probe;
} while (probe == 0xFFFF);
}
while (check && pos + 4 <= ctx->tex_size / 4) {
prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
AV_WL32(ctx->tex_data + 4 * pos, prev);
pos++;
prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
AV_WL32(ctx->tex_data + 4 * pos, prev);
pos++;
prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
AV_WL32(ctx->tex_data + 4 * pos, prev);
pos++;
prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
AV_WL32(ctx->tex_data + 4 * pos, prev);
pos++;
check--;
}
/* Restart (or exit) the loop */
continue;
break;
case 1:
/* Load new run value */
run = bytestream2_get_byte(gbc);
if (run == 255) {
do {
probe = bytestream2_get_le16(gbc);
run += probe;
} while (probe == 0xFFFF);
}
/* Copy two dwords from previous data */
prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
AV_WL32(ctx->tex_data + 4 * pos, prev);
pos++;
prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
AV_WL32(ctx->tex_data + 4 * pos, prev);
pos++;
break;
case 2:
/* Copy two dwords from a previous index */
idx = 8 + 4 * bytestream2_get_le16(gbc);
if (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4)
return AVERROR_INVALIDDATA;
prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
AV_WL32(ctx->tex_data + 4 * pos, prev);
pos++;
prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
AV_WL32(ctx->tex_data + 4 * pos, prev);
pos++;
break;
case 3:
/* Copy two dwords from input */
prev = bytestream2_get_le32(gbc);
AV_WL32(ctx->tex_data + 4 * pos, prev);
pos++;
prev = bytestream2_get_le32(gbc);
AV_WL32(ctx->tex_data + 4 * pos, prev);
pos++;
break;
}
}
CHECKPOINT(4);
if (pos + 2 > ctx->tex_size / 4)
return AVERROR_INVALIDDATA;
/* Copy two elements from a previous offset or from the input buffer */
if (op) {
if (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4)
return AVERROR_INVALIDDATA;
prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
AV_WL32(ctx->tex_data + 4 * pos, prev);
pos++;
prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
AV_WL32(ctx->tex_data + 4 * pos, prev);
pos++;
} else {
CHECKPOINT(4);
if (op && (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4))
return AVERROR_INVALIDDATA;
if (op)
prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
else
prev = bytestream2_get_le32(gbc);
AV_WL32(ctx->tex_data + 4 * pos, prev);
pos++;
CHECKPOINT(4);
if (op)
prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
else
prev = bytestream2_get_le32(gbc);
AV_WL32(ctx->tex_data + 4 * pos, prev);
pos++;
}
}
return 0;
}
static int dxv_decompress_lzf(AVCodecContext *avctx)
{
DXVContext *ctx = avctx->priv_data;
return ff_lzf_uncompress(&ctx->gbc, &ctx->tex_data, &ctx->tex_size);
}
static int dxv_decompress_raw(AVCodecContext *avctx)
{
DXVContext *ctx = avctx->priv_data;
GetByteContext *gbc = &ctx->gbc;
if (bytestream2_get_bytes_left(gbc) < ctx->tex_size)
return AVERROR_INVALIDDATA;
bytestream2_get_buffer(gbc, ctx->tex_data, ctx->tex_size);
return 0;
}
static int dxv_decode(AVCodecContext *avctx, AVFrame *frame,
int *got_frame, AVPacket *avpkt)
{
DXVContext *ctx = avctx->priv_data;
GetByteContext *gbc = &ctx->gbc;
int (*decompress_tex)(AVCodecContext *avctx);
const char *msgcomp, *msgtext;
uint32_t tag;
int version_major, version_minor = 0;
int size = 0, old_type = 0;
int ret;
bytestream2_init(gbc, avpkt->data, avpkt->size);
ctx->texture_block_h = 4;
ctx->texture_block_w = 4;
avctx->pix_fmt = AV_PIX_FMT_RGBA;
avctx->colorspace = AVCOL_SPC_RGB;
ctx->tex_funct = NULL;
ctx->tex_funct_planar[0] = NULL;
ctx->tex_funct_planar[1] = NULL;
tag = bytestream2_get_le32(gbc);
switch (tag) {
case MKBETAG('D', 'X', 'T', '1'):
decompress_tex = dxv_decompress_dxt1;
ctx->tex_funct = ctx->texdsp.dxt1_block;
ctx->tex_rat = 8;
ctx->tex_step = 8;
msgcomp = "DXTR1";
msgtext = "DXT1";
break;
case MKBETAG('D', 'X', 'T', '5'):
decompress_tex = dxv_decompress_dxt5;
/* DXV misnomers DXT5, alpha is premultiplied so use DXT4 instead */
ctx->tex_funct = ctx->texdsp.dxt4_block;
ctx->tex_rat = 4;
ctx->tex_step = 16;
msgcomp = "DXTR5";
msgtext = "DXT5";
break;
case MKBETAG('Y', 'C', 'G', '6'):
decompress_tex = dxv_decompress_ycg6;
ctx->tex_funct_planar[0] = yo_block;
ctx->tex_funct_planar[1] = cocg_block;
ctx->tex_rat = 8;
ctx->tex_step = 32;
ctx->ctex_step = 16;
msgcomp = "YOCOCG6";
msgtext = "YCG6";
ctx->ctex_size = avctx->coded_width * avctx->coded_height / 4;
ctx->texture_block_h = 4;
ctx->texture_block_w = 16;
ctx->ctexture_block_h = 4;
ctx->ctexture_block_w = 4;
avctx->pix_fmt = AV_PIX_FMT_YUV420P;
avctx->colorspace = AVCOL_SPC_YCOCG;
break;
case MKBETAG('Y', 'G', '1', '0'):
decompress_tex = dxv_decompress_yg10;
ctx->tex_funct_planar[0] = yao_block;
ctx->tex_funct_planar[1] = cocg_block;
ctx->tex_rat = 4;
ctx->tex_step = 64;
ctx->ctex_step = 16;
msgcomp = "YAOCOCG10";
msgtext = "YG10";
ctx->ctex_size = avctx->coded_width * avctx->coded_height / 4;
ctx->texture_block_h = 4;
ctx->texture_block_w = 16;
ctx->ctexture_block_h = 4;
ctx->ctexture_block_w = 4;
avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
avctx->colorspace = AVCOL_SPC_YCOCG;
break;
default:
/* Old version does not have a real header, just size and type. */
size = tag & 0x00FFFFFF;
old_type = tag >> 24;
version_major = (old_type & 0x0F) - 1;
if (old_type & 0x80) {
msgcomp = "RAW";
decompress_tex = dxv_decompress_raw;
} else {
msgcomp = "LZF";
decompress_tex = dxv_decompress_lzf;
}
if (old_type & 0x40) {
msgtext = "DXT5";
ctx->tex_funct = ctx->texdsp.dxt5_block;
ctx->tex_step = 16;
} else if (old_type & 0x20 || version_major == 1) {
msgtext = "DXT1";
ctx->tex_funct = ctx->texdsp.dxt1_block;
ctx->tex_step = 8;
} else {
av_log(avctx, AV_LOG_ERROR, "Unsupported header (0x%08"PRIX32")\n.", tag);
return AVERROR_INVALIDDATA;
}
ctx->tex_rat = 1;
break;
}
ctx->slice_count = av_clip(avctx->thread_count, 1,
avctx->coded_height / FFMAX(ctx->texture_block_h,
ctx->ctexture_block_h));
/* New header is 12 bytes long. */
if (!old_type) {
version_major = bytestream2_get_byte(gbc) - 1;
version_minor = bytestream2_get_byte(gbc);
/* Encoder copies texture data when compression is not advantageous. */
if (bytestream2_get_byte(gbc)) {
msgcomp = "RAW";
ctx->tex_rat = 1;
decompress_tex = dxv_decompress_raw;
}
bytestream2_skip(gbc, 1); // unknown
size = bytestream2_get_le32(gbc);
}
av_log(avctx, AV_LOG_DEBUG,
"%s compression with %s texture (version %d.%d)\n",
msgcomp, msgtext, version_major, version_minor);
if (size != bytestream2_get_bytes_left(gbc)) {
av_log(avctx, AV_LOG_ERROR,
"Incomplete or invalid file (header %d, left %u).\n",
size, bytestream2_get_bytes_left(gbc));
return AVERROR_INVALIDDATA;
}
ctx->tex_size = avctx->coded_width * avctx->coded_height * 4 / ctx->tex_rat;
ret = av_reallocp(&ctx->tex_data, ctx->tex_size + AV_INPUT_BUFFER_PADDING_SIZE);
if (ret < 0)
return ret;
if (ctx->ctex_size) {
int i;
ctx->op_size[0] = avctx->coded_width * avctx->coded_height / 16;
ctx->op_size[1] = avctx->coded_width * avctx->coded_height / 32;
ctx->op_size[2] = avctx->coded_width * avctx->coded_height / 32;
ctx->op_size[3] = avctx->coded_width * avctx->coded_height / 16;
ret = av_reallocp(&ctx->ctex_data, ctx->ctex_size + AV_INPUT_BUFFER_PADDING_SIZE);
if (ret < 0)
return ret;
for (i = 0; i < 4; i++) {
ret = av_reallocp(&ctx->op_data[i], ctx->op_size[i]);
if (ret < 0)
return ret;
}
}
/* Decompress texture out of the intermediate compression. */
ret = decompress_tex(avctx);
if (ret < 0)
return ret;
{
int w_block = avctx->coded_width / ctx->texture_block_w;
int h_block = avctx->coded_height / ctx->texture_block_h;
if (w_block * h_block * ctx->tex_step > ctx->tex_size * 8LL)
return AVERROR_INVALIDDATA;
}
ret = ff_thread_get_buffer(avctx, frame, 0);
if (ret < 0)
return ret;
/* Now decompress the texture with the standard functions. */
avctx->execute2(avctx, decompress_texture_thread,
frame, NULL, ctx->slice_count);
/* Frame is ready to be output. */
frame->pict_type = AV_PICTURE_TYPE_I;
frame->flags |= AV_FRAME_FLAG_KEY;
*got_frame = 1;
return avpkt->size;
}
static int dxv_init(AVCodecContext *avctx)
{
DXVContext *ctx = avctx->priv_data;
int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
avctx->width, avctx->height);
return ret;
}
/* Codec requires 16x16 alignment. */
avctx->coded_width = FFALIGN(avctx->width, 16);
avctx->coded_height = FFALIGN(avctx->height, 16);
ff_texturedsp_init(&ctx->texdsp);
return 0;
}
static int dxv_close(AVCodecContext *avctx)
{
DXVContext *ctx = avctx->priv_data;
av_freep(&ctx->tex_data);
av_freep(&ctx->ctex_data);
av_freep(&ctx->op_data[0]);
av_freep(&ctx->op_data[1]);
av_freep(&ctx->op_data[2]);
av_freep(&ctx->op_data[3]);
return 0;
}
const FFCodec ff_dxv_decoder = {
.p.name = "dxv",
CODEC_LONG_NAME("Resolume DXV"),
.p.type = AVMEDIA_TYPE_VIDEO,
.p.id = AV_CODEC_ID_DXV,
.init = dxv_init,
FF_CODEC_DECODE_CB(dxv_decode),
.close = dxv_close,
.priv_data_size = sizeof(DXVContext),
.p.capabilities = AV_CODEC_CAP_DR1 |
AV_CODEC_CAP_SLICE_THREADS |
AV_CODEC_CAP_FRAME_THREADS,
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
};
|