25 #define BITSTREAM_READER_LE
73 #define HISTORY_SIZE (64 * 1024)
79 unsigned reference_pos,
delta,
pos = 0;
80 uint8_t history[64 * 1024];
84 uint8_t token = bytestream2_get_byte(gb);
85 int num_literals = token >> 4;
87 if (num_literals == 15) {
88 unsigned char current;
90 current = bytestream2_get_byte(gb);
91 num_literals += current;
92 }
while (current == 255);
99 while (num_literals-- > 0) {
100 history[
pos++] = bytestream2_get_byte(gb);
111 delta = bytestream2_get_le16(gb);
114 match_length = 4 + (token & 0x0F);
115 if (match_length == 4 + 0x0F) {
119 current = bytestream2_get_byte(gb);
120 match_length += current;
121 }
while (current == 255);
125 if (
pos >= reference_pos + match_length || reference_pos >=
pos + match_length) {
126 memcpy(history +
pos, history + reference_pos, match_length);
129 while (match_length-- > 0)
130 history[
pos++] = history[reference_pos++];
133 while (match_length-- > 0) {
134 history[
pos++] = history[reference_pos++];
150 unsigned uncompressed_size)
155 int ylinesize, ulinesize, vlinesize, alinesize;
156 uint16_t *dsty, *dstu, *dstv, *dsta;
159 s->texture_size_x = bytestream2_get_le32(gb);
160 s->texture_size_y = bytestream2_get_le32(gb);
166 s->uv_offset_data_offset = bytestream2_get_le32(gb);
167 if (
s->uv_offset_data_offset >= UINT_MAX / 4)
169 s->uv_offset_data_offset *= 4;
170 if (
s->uv_offset_data_offset >= uncompressed_size)
173 s->y_control_data_offset = bytestream2_get_le32(gb);
174 if (
s->y_control_data_offset >= UINT_MAX / 4)
176 s->y_control_data_offset *= 4;
177 if (
s->y_control_data_offset >= uncompressed_size)
180 s->a_control_word_offset = bytestream2_get_le32(gb);
181 if (
s->a_control_word_offset >= UINT_MAX / 4)
183 s->a_control_word_offset *= 4;
184 if (
s->a_control_word_offset >= uncompressed_size)
187 s->uv_data_offset = bytestream2_get_le32(gb);
188 if (
s->uv_data_offset >= UINT_MAX / 4)
190 s->uv_data_offset *= 4;
191 if (
s->uv_data_offset >= uncompressed_size)
194 s->y_data_size = bytestream2_get_le32(gb);
195 if (
s->y_data_size >= UINT_MAX / 4)
198 s->a_data_offset = bytestream2_get_le32(gb);
199 if (
s->a_data_offset >= UINT_MAX / 4)
201 s->a_data_offset *= 4;
202 if (
s->a_data_offset >= uncompressed_size)
205 s->a_count_size = bytestream2_get_le32(gb);
206 if (
s->a_count_size >= UINT_MAX / 4)
208 s->a_count_size *= 4;
209 if (
s->a_count_size >= uncompressed_size)
212 s->data_end = bytestream2_get_le32(gb);
213 if (
s->data_end > uncompressed_size)
217 if (
s->data_end <=
s->y_data_size)
219 s->y_data_offset =
s->data_end -
s->y_data_size;
220 if (
s->y_data_offset <=
s->a_data_offset)
222 s->uv_count_offset =
s->y_data_offset -
s->a_data_offset;
235 dsty = (uint16_t *)p->
data[0];
236 dsta = (uint16_t *)p->
data[3];
240 for (
int y = 0; y < avctx->
height; y += 4) {
241 const unsigned row_offset = bytestream2_get_le32(&
rgb);
248 for (
int x = 0; x < avctx->
width; x += 4) {
249 unsigned item = bytestream2_get_le32(gb);
250 unsigned y_min = item & 4095;
251 unsigned y_max = (item >> 12) & 4095;
252 unsigned y_diff = y_max - y_min;
255 control[0] = (item >> 24) & 3;
256 control[1] = (item >> 26) & 3;
257 control[2] = (item >> 28) & 3;
258 control[3] = (item >> 30) & 3;
260 for (
int i = 0;
i < 4;
i++) {
261 const int nb_bits = control[
i] + 1;
262 const int div = (1 << nb_bits) - 1;
263 const int add = div - 1;
272 dsty += 4 * ylinesize;
278 if (
s->uv_count_offset ==
s->a_control_word_offset) {
279 for (
int y = 0; y < avctx->
height; y++) {
280 for (
int x = 0; x < avctx->
width; x++)
288 for (
int y = 0; y < avctx->
height; y += 16) {
289 for (
int x = 0; x < avctx->
width; x += 16) {
290 unsigned m = bytestream2_get_le32(gb);
291 unsigned offset = bytestream2_get_le32(gb);
292 unsigned alpha0, alpha1;
295 if (
offset >= UINT_MAX / 4)
302 control = bytestream2_get_le64(&dgb);
303 alpha0 = control & 0xFF;
304 alpha1 = (control >> 8) & 0xFF;
305 control = control >> 16;
307 for (
int by = 0; by < 4; by++) {
308 for (
int bx = 0; bx < 4; bx++) {
311 for (
int i = 0;
i < 4;
i++) {
312 for (
int j = 0; j < 4; j++) {
313 dsta[x + (
i + by * 4) * alinesize + bx * 4 + j] = 0;
318 for (
int i = 0;
i < 4;
i++) {
319 for (
int j = 0; j < 4; j++) {
320 dsta[x + (
i + by * 4) * alinesize + bx * 4 + j] = 4095;
325 for (
int i = 0;
i < 4;
i++) {
326 for (
int j = 0; j < 4; j++) {
327 dsta[x + (
i + by * 4) * alinesize + bx * 4 + j] = (alpha0 + (alpha1 - alpha0) * (control & 7)) << 4;
341 dsta += 16 * alinesize;
347 dstu = (uint16_t *)p->
data[1];
348 dstv = (uint16_t *)p->
data[2];
352 for (
int y = 0; y < avctx->
height; y += 16) {
353 for (
int x = 0; x < avctx->
width; x += 16) {
354 unsigned offset = bytestream2_get_le32(&
rgb) * 4;
355 int u[16][16] = { 0 }, v[16][16] = { 0 };
356 int u0,
v0, u1, v1, udif, vdif;
357 unsigned escape, is8x8, loc;
361 is8x8 = bytestream2_get_le16(&dgb);
362 escape = bytestream2_get_le16(&dgb);
364 if (escape == 0 && is8x8 == 0) {
365 u0 = bytestream2_get_byte(&dgb);
366 v0 = bytestream2_get_byte(&dgb);
367 u1 = bytestream2_get_byte(&dgb);
368 v1 = bytestream2_get_byte(&dgb);
369 loc = bytestream2_get_le32(&dgb);
370 u0 = (u0 << 4) | (u0 & 0xF);
371 v0 = (
v0 << 4) | (
v0 & 0xF);
372 u1 = (u1 << 4) | (u1 & 0xF);
373 v1 = (v1 << 4) | (v1 & 0xF);
377 for (
int i = 0;
i < 16;
i += 4) {
378 for (
int j = 0; j < 16; j += 4) {
379 for (
int ii = 0; ii < 4; ii++) {
380 for (
int jj = 0; jj < 4; jj++) {
381 u[
i + ii][j + jj] = u0 + ((udif * (
int)(loc & 3) + 2) / 3);
382 v[
i + ii][j + jj] =
v0 + ((vdif * (
int)(loc & 3) + 2) / 3);
390 for (
int i = 0;
i < 16;
i += 8) {
391 for (
int j = 0; j < 16; j += 8) {
393 u0 = bytestream2_get_byte(&dgb);
394 v0 = bytestream2_get_byte(&dgb);
395 u1 = bytestream2_get_byte(&dgb);
396 v1 = bytestream2_get_byte(&dgb);
397 loc = bytestream2_get_le32(&dgb);
398 u0 = (u0 << 4) | (u0 & 0xF);
399 v0 = (
v0 << 4) | (
v0 & 0xF);
400 u1 = (u1 << 4) | (u1 & 0xF);
401 v1 = (v1 << 4) | (v1 & 0xF);
405 for (
int ii = 0; ii < 8; ii += 2) {
406 for (
int jj = 0; jj < 8; jj += 2) {
407 for (
int iii = 0; iii < 2; iii++) {
408 for (
int jjj = 0; jjj < 2; jjj++) {
409 u[
i + ii + iii][j + jj + jjj] = u0 + ((udif * (
int)(loc & 3) + 2) / 3);
410 v[
i + ii + iii][j + jj + jjj] =
v0 + ((vdif * (
int)(loc & 3) + 2) / 3);
418 for (
int ii = 0; ii < 8; ii += 4) {
419 for (
int jj = 0; jj < 8; jj += 4) {
420 u0 = bytestream2_get_byte(&dgb);
421 v0 = bytestream2_get_byte(&dgb);
422 u1 = bytestream2_get_byte(&dgb);
423 v1 = bytestream2_get_byte(&dgb);
424 loc = bytestream2_get_le32(&dgb);
425 u0 = (u0 << 4) | (u0 & 0xF);
426 v0 = (
v0 << 4) | (
v0 & 0xF);
427 u1 = (u1 << 4) | (u1 & 0xF);
428 v1 = (v1 << 4) | (v1 & 0xF);
432 for (
int iii = 0; iii < 4; iii++) {
433 for (
int jjj = 0; jjj < 4; jjj++) {
434 u[
i + ii + iii][j + jj + jjj] = u0 + ((udif * (
int)(loc & 3) + 2) / 3);
435 v[
i + ii + iii][j + jj + jjj] =
v0 + ((vdif * (
int)(loc & 3) + 2) / 3);
449 for (
int i = 0;
i < 16;
i++) {
450 for (
int j = 0; j < 16; j++) {
451 dstu[x +
i * ulinesize + j] =
u[
i][j];
452 dstv[x +
i * vlinesize + j] = v[
i][j];
457 dstu += 16 * ulinesize;
458 dstv += 16 * vlinesize;
470 unsigned uncompressed_size;
473 if (avpkt->
size <= 40)
478 if (bytestream2_get_le32(gb) !=
MKBETAG(
'N',
'L',
'C',
'1'))
481 uncompressed_size = bytestream2_get_le32(gb);
482 s->compressed_size = bytestream2_get_le32(gb);
483 s->format = bytestream2_get_le32(gb);
488 if (
s->format == 0) {
493 if (uncompressed_size >
s->lzf_size)
497 }
else if (
s->format == 1) {
503 if (!
s->uncompressed_buffer)
509 if (
ret != uncompressed_size)
532 s->uncompressed_size = 0;