25 #define BITSTREAM_READER_LE
73 #define HISTORY_SIZE (64 * 1024)
79 unsigned reference_pos, match_length,
delta,
pos = 0;
80 uint8_t history[64 * 1024];
83 uint8_t token = bytestream2_get_byte(gb);
84 unsigned num_literals = token >> 4;
86 if (num_literals == 15) {
87 unsigned char current;
89 current = bytestream2_get_byte(gb);
90 num_literals += current;
91 }
while (current == 255);
98 while (num_literals-- > 0) {
99 history[
pos++] = bytestream2_get_byte(gb);
110 delta = bytestream2_get_le16(gb);
113 match_length = 4 + (token & 0x0F);
114 if (match_length == 4 + 0x0F) {
118 current = bytestream2_get_byte(gb);
119 match_length += current;
120 }
while (current == 255);
124 if (
pos >= reference_pos + match_length || reference_pos >=
pos + match_length) {
125 memcpy(history +
pos, history + reference_pos, match_length);
128 while (match_length-- > 0)
129 history[
pos++] = history[reference_pos++];
132 while (match_length-- > 0) {
133 history[
pos++] = history[reference_pos++];
149 unsigned uncompressed_size)
154 int ylinesize, ulinesize, vlinesize, alinesize;
155 uint16_t *dsty, *dstu, *dstv, *dsta;
158 s->texture_size_x = bytestream2_get_le32(gb);
159 s->texture_size_y = bytestream2_get_le32(gb);
165 s->uv_offset_data_offset = bytestream2_get_le32(gb);
166 if (
s->uv_offset_data_offset >= UINT_MAX / 4)
168 s->uv_offset_data_offset *= 4;
169 if (
s->uv_offset_data_offset >= uncompressed_size)
172 s->y_control_data_offset = bytestream2_get_le32(gb);
173 if (
s->y_control_data_offset >= UINT_MAX / 4)
175 s->y_control_data_offset *= 4;
176 if (
s->y_control_data_offset >= uncompressed_size)
179 s->a_control_word_offset = bytestream2_get_le32(gb);
180 if (
s->a_control_word_offset >= UINT_MAX / 4)
182 s->a_control_word_offset *= 4;
183 if (
s->a_control_word_offset >= uncompressed_size)
186 s->uv_data_offset = bytestream2_get_le32(gb);
187 if (
s->uv_data_offset >= UINT_MAX / 4)
189 s->uv_data_offset *= 4;
190 if (
s->uv_data_offset >= uncompressed_size)
193 s->y_data_size = bytestream2_get_le32(gb);
194 if (
s->y_data_size >= UINT_MAX / 4)
197 s->a_data_offset = bytestream2_get_le32(gb);
198 if (
s->a_data_offset >= UINT_MAX / 4)
200 s->a_data_offset *= 4;
201 if (
s->a_data_offset >= uncompressed_size)
204 s->a_count_size = bytestream2_get_le32(gb);
205 if (
s->a_count_size >= UINT_MAX / 4)
207 s->a_count_size *= 4;
208 if (
s->a_count_size >= uncompressed_size)
211 s->data_end = bytestream2_get_le32(gb);
212 if (
s->data_end > uncompressed_size)
216 if (
s->data_end <=
s->y_data_size)
218 s->y_data_offset =
s->data_end -
s->y_data_size;
219 if (
s->y_data_offset <=
s->a_data_offset)
221 s->uv_count_offset =
s->y_data_offset -
s->a_data_offset;
234 dsty = (uint16_t *)p->
data[0];
235 dsta = (uint16_t *)p->
data[3];
239 for (
int y = 0; y < avctx->
height; y += 4) {
240 const unsigned row_offset = bytestream2_get_le32(&
rgb);
245 for (
int x = 0; x < avctx->
width; x += 4) {
246 unsigned item = bytestream2_get_le32(gb);
247 unsigned y_min = item & 4095;
248 unsigned y_max = (item >> 12) & 4095;
249 unsigned y_diff = y_max - y_min;
252 control[0] = (item >> 24) & 3;
253 control[1] = (item >> 26) & 3;
254 control[2] = (item >> 28) & 3;
255 control[3] = (item >> 30) & 3;
257 for (
int i = 0;
i < 4;
i++) {
258 const int nb_bits = control[
i] + 1;
259 const int div = (1 << nb_bits) - 1;
260 const int add = div - 1;
269 dsty += 4 * ylinesize;
275 if (
s->uv_count_offset ==
s->a_control_word_offset) {
276 for (
int y = 0; y < avctx->
height; y++) {
277 for (
int x = 0; x < avctx->
width; x++)
285 for (
int y = 0; y < avctx->
height; y += 16) {
286 for (
int x = 0; x < avctx->
width; x += 16) {
287 unsigned m = bytestream2_get_le32(gb);
288 unsigned offset = bytestream2_get_le32(gb);
289 unsigned alpha0, alpha1;
292 if (
offset >= UINT_MAX / 4)
299 control = bytestream2_get_le64(&dgb);
300 alpha0 = control & 0xFF;
301 alpha1 = (control >> 8) & 0xFF;
302 control = control >> 16;
304 for (
int by = 0; by < 4; by++) {
305 for (
int bx = 0; bx < 4; bx++) {
308 for (
int i = 0;
i < 4;
i++) {
309 for (
int j = 0; j < 4; j++) {
310 dsta[x + (
i + by * 4) * alinesize + bx * 4 + j] = 0;
315 for (
int i = 0;
i < 4;
i++) {
316 for (
int j = 0; j < 4; j++) {
317 dsta[x + (
i + by * 4) * alinesize + bx * 4 + j] = 4095;
322 for (
int i = 0;
i < 4;
i++) {
323 for (
int j = 0; j < 4; j++) {
324 dsta[x + (
i + by * 4) * alinesize + bx * 4 + j] = (alpha0 + (alpha1 - alpha0) * (control & 7)) << 4;
338 dsta += 16 * alinesize;
344 dstu = (uint16_t *)p->
data[1];
345 dstv = (uint16_t *)p->
data[2];
349 for (
int y = 0; y < avctx->
height; y += 16) {
350 for (
int x = 0; x < avctx->
width; x += 16) {
351 unsigned offset = bytestream2_get_le32(&
rgb) * 4;
352 int u[16][16] = { 0 }, v[16][16] = { 0 };
353 int u0,
v0, u1, v1, udif, vdif;
354 unsigned escape, is8x8, loc;
358 is8x8 = bytestream2_get_le16(&dgb);
359 escape = bytestream2_get_le16(&dgb);
361 if (escape == 0 && is8x8 == 0) {
362 u0 = bytestream2_get_byte(&dgb);
363 v0 = bytestream2_get_byte(&dgb);
364 u1 = bytestream2_get_byte(&dgb);
365 v1 = bytestream2_get_byte(&dgb);
366 loc = bytestream2_get_le32(&dgb);
367 u0 = (u0 << 4) | (u0 & 0xF);
368 v0 = (
v0 << 4) | (
v0 & 0xF);
369 u1 = (u1 << 4) | (u1 & 0xF);
370 v1 = (v1 << 4) | (v1 & 0xF);
374 for (
int i = 0;
i < 16;
i += 4) {
375 for (
int j = 0; j < 16; j += 4) {
376 for (
int ii = 0; ii < 4; ii++) {
377 for (
int jj = 0; jj < 4; jj++) {
378 u[
i + ii][j + jj] = u0 + ((udif * (
int)(loc & 3) + 2) / 3);
379 v[
i + ii][j + jj] =
v0 + ((vdif * (
int)(loc & 3) + 2) / 3);
387 for (
int i = 0;
i < 16;
i += 8) {
388 for (
int j = 0; j < 16; j += 8) {
390 u0 = bytestream2_get_byte(&dgb);
391 v0 = bytestream2_get_byte(&dgb);
392 u1 = bytestream2_get_byte(&dgb);
393 v1 = bytestream2_get_byte(&dgb);
394 loc = bytestream2_get_le32(&dgb);
395 u0 = (u0 << 4) | (u0 & 0xF);
396 v0 = (
v0 << 4) | (
v0 & 0xF);
397 u1 = (u1 << 4) | (u1 & 0xF);
398 v1 = (v1 << 4) | (v1 & 0xF);
402 for (
int ii = 0; ii < 8; ii += 2) {
403 for (
int jj = 0; jj < 8; jj += 2) {
404 for (
int iii = 0; iii < 2; iii++) {
405 for (
int jjj = 0; jjj < 2; jjj++) {
406 u[
i + ii + iii][j + jj + jjj] = u0 + ((udif * (
int)(loc & 3) + 2) / 3);
407 v[
i + ii + iii][j + jj + jjj] =
v0 + ((vdif * (
int)(loc & 3) + 2) / 3);
415 for (
int ii = 0; ii < 8; ii += 4) {
416 for (
int jj = 0; jj < 8; jj += 4) {
417 u0 = bytestream2_get_byte(&dgb);
418 v0 = bytestream2_get_byte(&dgb);
419 u1 = bytestream2_get_byte(&dgb);
420 v1 = bytestream2_get_byte(&dgb);
421 loc = bytestream2_get_le32(&dgb);
422 u0 = (u0 << 4) | (u0 & 0xF);
423 v0 = (
v0 << 4) | (
v0 & 0xF);
424 u1 = (u1 << 4) | (u1 & 0xF);
425 v1 = (v1 << 4) | (v1 & 0xF);
429 for (
int iii = 0; iii < 4; iii++) {
430 for (
int jjj = 0; jjj < 4; jjj++) {
431 u[
i + ii + iii][j + jj + jjj] = u0 + ((udif * (
int)(loc & 3) + 2) / 3);
432 v[
i + ii + iii][j + jj + jjj] =
v0 + ((vdif * (
int)(loc & 3) + 2) / 3);
446 for (
int i = 0;
i < 16;
i++) {
447 for (
int j = 0; j < 16; j++) {
448 dstu[x +
i * ulinesize + j] =
u[
i][j];
449 dstv[x +
i * vlinesize + j] = v[
i][j];
454 dstu += 16 * ulinesize;
455 dstv += 16 * vlinesize;
467 unsigned uncompressed_size;
470 if (avpkt->
size <= 40)
475 if (bytestream2_get_le32(gb) !=
MKBETAG(
'N',
'L',
'C',
'1'))
478 uncompressed_size = bytestream2_get_le32(gb);
479 s->compressed_size = bytestream2_get_le32(gb);
480 s->format = bytestream2_get_le32(gb);
485 if (
s->format == 0) {
490 if (uncompressed_size >
s->lzf_size)
494 }
else if (
s->format == 1) {
500 if (!
s->uncompressed_buffer)
506 if (
ret != uncompressed_size)
529 s->uncompressed_size = 0;