26 #define BITSTREAM_READER_LE
75 #define HISTORY_SIZE (64 * 1024)
81 unsigned reference_pos, match_length,
delta,
pos = 0;
82 uint8_t history[64 * 1024];
85 uint8_t token = bytestream2_get_byte(gb);
86 unsigned num_literals = token >> 4;
88 if (num_literals == 15) {
89 unsigned char current;
91 current = bytestream2_get_byte(gb);
92 num_literals += current;
93 }
while (current == 255);
100 while (num_literals-- > 0) {
101 history[
pos++] = bytestream2_get_byte(gb);
112 delta = bytestream2_get_le16(gb);
115 match_length = 4 + (token & 0x0F);
116 if (match_length == 4 + 0x0F) {
120 current = bytestream2_get_byte(gb);
121 match_length += current;
122 }
while (current == 255);
126 if (
pos >= reference_pos + match_length || reference_pos >=
pos + match_length) {
127 memcpy(history +
pos, history + reference_pos, match_length);
130 while (match_length-- > 0)
131 history[
pos++] = history[reference_pos++];
134 while (match_length-- > 0) {
135 history[
pos++] = history[reference_pos++];
151 unsigned uncompressed_size)
156 int ylinesize, ulinesize, vlinesize, alinesize;
157 uint16_t *dsty, *dstu, *dstv, *dsta;
160 s->texture_size_x = bytestream2_get_le32(gb);
161 s->texture_size_y = bytestream2_get_le32(gb);
167 s->uv_offset_data_offset = bytestream2_get_le32(gb);
168 if (
s->uv_offset_data_offset >= UINT_MAX / 4)
170 s->uv_offset_data_offset *= 4;
171 if (
s->uv_offset_data_offset >= uncompressed_size)
174 s->y_control_data_offset = bytestream2_get_le32(gb);
175 if (
s->y_control_data_offset >= UINT_MAX / 4)
177 s->y_control_data_offset *= 4;
178 if (
s->y_control_data_offset >= uncompressed_size)
181 s->a_control_word_offset = bytestream2_get_le32(gb);
182 if (
s->a_control_word_offset >= UINT_MAX / 4)
184 s->a_control_word_offset *= 4;
185 if (
s->a_control_word_offset >= uncompressed_size)
188 s->uv_data_offset = bytestream2_get_le32(gb);
189 if (
s->uv_data_offset >= UINT_MAX / 4)
191 s->uv_data_offset *= 4;
192 if (
s->uv_data_offset >= uncompressed_size)
195 s->y_data_size = bytestream2_get_le32(gb);
196 if (
s->y_data_size >= UINT_MAX / 4)
199 s->a_data_offset = bytestream2_get_le32(gb);
200 if (
s->a_data_offset >= UINT_MAX / 4)
202 s->a_data_offset *= 4;
203 if (
s->a_data_offset >= uncompressed_size)
206 s->a_count_size = bytestream2_get_le32(gb);
207 if (
s->a_count_size >= UINT_MAX / 4)
209 s->a_count_size *= 4;
210 if (
s->a_count_size >= uncompressed_size)
213 s->data_end = bytestream2_get_le32(gb);
214 if (
s->data_end > uncompressed_size)
218 if (
s->data_end <=
s->y_data_size)
220 s->y_data_offset =
s->data_end -
s->y_data_size;
221 if (
s->y_data_offset <=
s->a_data_offset)
223 s->uv_count_offset =
s->y_data_offset -
s->a_data_offset;
236 dsty = (uint16_t *)p->
data[0];
237 dsta = (uint16_t *)p->
data[3];
241 for (
int y = 0; y < avctx->
height; y += 4) {
242 const unsigned row_offset = bytestream2_get_le32(&
rgb);
247 for (
int x = 0; x < avctx->
width; x += 4) {
248 unsigned item = bytestream2_get_le32(gb);
249 unsigned y_min = item & 4095;
250 unsigned y_max = (item >> 12) & 4095;
251 unsigned y_diff = y_max - y_min;
254 control[0] = (item >> 24) & 3;
255 control[1] = (item >> 26) & 3;
256 control[2] = (item >> 28) & 3;
257 control[3] = (item >> 30) & 3;
259 for (
int i = 0;
i < 4;
i++) {
260 const int nb_bits = control[
i] + 1;
261 const int div = (1 << nb_bits) - 1;
262 const int add = div - 1;
271 dsty += 4 * ylinesize;
277 if (
s->uv_count_offset ==
s->a_control_word_offset) {
278 for (
int y = 0; y < avctx->
height; y++) {
279 for (
int x = 0; x < avctx->
width; x++)
287 for (
int y = 0; y < avctx->
height; y += 16) {
288 for (
int x = 0; x < avctx->
width; x += 16) {
289 unsigned m = bytestream2_get_le32(gb);
290 unsigned offset = bytestream2_get_le32(gb);
291 unsigned alpha0, alpha1;
294 if (
offset >= UINT_MAX / 4)
301 control = bytestream2_get_le64(&dgb);
302 alpha0 = control & 0xFF;
303 alpha1 = (control >> 8) & 0xFF;
304 control = control >> 16;
306 for (
int by = 0; by < 4; by++) {
307 for (
int bx = 0; bx < 4; bx++) {
310 for (
int i = 0;
i < 4;
i++) {
311 for (
int j = 0; j < 4; j++) {
312 dsta[x + (
i + by * 4) * alinesize + bx * 4 + j] = 0;
317 for (
int i = 0;
i < 4;
i++) {
318 for (
int j = 0; j < 4; j++) {
319 dsta[x + (
i + by * 4) * alinesize + bx * 4 + j] = 4095;
324 for (
int i = 0;
i < 4;
i++) {
325 for (
int j = 0; j < 4; j++) {
326 dsta[x + (
i + by * 4) * alinesize + bx * 4 + j] = (alpha0 + (alpha1 - alpha0) * (control & 7)) << 4;
340 dsta += 16 * alinesize;
346 dstu = (uint16_t *)p->
data[1];
347 dstv = (uint16_t *)p->
data[2];
351 for (
int y = 0; y < avctx->
height; y += 16) {
352 for (
int x = 0; x < avctx->
width; x += 16) {
353 unsigned offset = bytestream2_get_le32(&
rgb) * 4;
354 int u[16][16] = { 0 }, v[16][16] = { 0 };
355 int u0,
v0, u1, v1, udif, vdif;
356 unsigned escape, is8x8, loc;
360 is8x8 = bytestream2_get_le16(&dgb);
361 escape = bytestream2_get_le16(&dgb);
363 if (escape == 0 && is8x8 == 0) {
364 u0 = bytestream2_get_byte(&dgb);
365 v0 = bytestream2_get_byte(&dgb);
366 u1 = bytestream2_get_byte(&dgb);
367 v1 = bytestream2_get_byte(&dgb);
368 loc = bytestream2_get_le32(&dgb);
369 u0 = (u0 << 4) | (u0 & 0xF);
370 v0 = (
v0 << 4) | (
v0 & 0xF);
371 u1 = (u1 << 4) | (u1 & 0xF);
372 v1 = (v1 << 4) | (v1 & 0xF);
376 for (
int i = 0;
i < 16;
i += 4) {
377 for (
int j = 0; j < 16; j += 4) {
378 for (
int ii = 0; ii < 4; ii++) {
379 for (
int jj = 0; jj < 4; jj++) {
380 u[
i + ii][j + jj] = u0 + ((udif * (
int)(loc & 3) + 2) / 3);
381 v[
i + ii][j + jj] =
v0 + ((vdif * (
int)(loc & 3) + 2) / 3);
389 for (
int i = 0;
i < 16;
i += 8) {
390 for (
int j = 0; j < 16; j += 8) {
392 u0 = bytestream2_get_byte(&dgb);
393 v0 = bytestream2_get_byte(&dgb);
394 u1 = bytestream2_get_byte(&dgb);
395 v1 = bytestream2_get_byte(&dgb);
396 loc = bytestream2_get_le32(&dgb);
397 u0 = (u0 << 4) | (u0 & 0xF);
398 v0 = (
v0 << 4) | (
v0 & 0xF);
399 u1 = (u1 << 4) | (u1 & 0xF);
400 v1 = (v1 << 4) | (v1 & 0xF);
404 for (
int ii = 0; ii < 8; ii += 2) {
405 for (
int jj = 0; jj < 8; jj += 2) {
406 for (
int iii = 0; iii < 2; iii++) {
407 for (
int jjj = 0; jjj < 2; jjj++) {
408 u[
i + ii + iii][j + jj + jjj] = u0 + ((udif * (
int)(loc & 3) + 2) / 3);
409 v[
i + ii + iii][j + jj + jjj] =
v0 + ((vdif * (
int)(loc & 3) + 2) / 3);
417 for (
int ii = 0; ii < 8; ii += 4) {
418 for (
int jj = 0; jj < 8; jj += 4) {
419 u0 = bytestream2_get_byte(&dgb);
420 v0 = bytestream2_get_byte(&dgb);
421 u1 = bytestream2_get_byte(&dgb);
422 v1 = bytestream2_get_byte(&dgb);
423 loc = bytestream2_get_le32(&dgb);
424 u0 = (u0 << 4) | (u0 & 0xF);
425 v0 = (
v0 << 4) | (
v0 & 0xF);
426 u1 = (u1 << 4) | (u1 & 0xF);
427 v1 = (v1 << 4) | (v1 & 0xF);
431 for (
int iii = 0; iii < 4; iii++) {
432 for (
int jjj = 0; jjj < 4; jjj++) {
433 u[
i + ii + iii][j + jj + jjj] = u0 + ((udif * (
int)(loc & 3) + 2) / 3);
434 v[
i + ii + iii][j + jj + jjj] =
v0 + ((vdif * (
int)(loc & 3) + 2) / 3);
448 for (
int i = 0;
i < 16;
i++) {
449 for (
int j = 0; j < 16; j++) {
450 dstu[x +
i * ulinesize + j] =
u[
i][j];
451 dstv[x +
i * vlinesize + j] = v[
i][j];
456 dstu += 16 * ulinesize;
457 dstv += 16 * vlinesize;
469 unsigned uncompressed_size;
472 if (avpkt->
size <= 40)
477 if (bytestream2_get_le32(gb) !=
MKBETAG(
'N',
'L',
'C',
'1'))
480 uncompressed_size = bytestream2_get_le32(gb);
481 s->compressed_size = bytestream2_get_le32(gb);
482 s->format = bytestream2_get_le32(gb);
487 if (
s->format == 0) {
492 if (uncompressed_size >
s->lzf_size)
496 }
else if (
s->format == 1) {
502 if (!
s->uncompressed_buffer)
508 if (
ret != uncompressed_size)
531 s->uncompressed_size = 0;