25 #define BITSTREAM_READER_LE
74 #define HISTORY_SIZE (64 * 1024)
80 unsigned reference_pos,
delta,
pos = 0;
81 uint8_t history[64 * 1024];
85 uint8_t token = bytestream2_get_byte(gb);
86 int 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);
103 while (num_literals-- > 0) {
104 history[
pos++] = bytestream2_get_byte(gb);
115 delta = bytestream2_get_le16(gb);
118 match_length = 4 + (token & 0x0F);
119 if (match_length == 4 + 0x0F) {
123 current = bytestream2_get_byte(gb);
124 match_length += current;
125 }
while (current == 255);
129 if (
pos >= reference_pos + match_length || reference_pos >=
pos + match_length) {
130 memcpy(history +
pos, history + reference_pos, match_length);
133 while (match_length-- > 0)
134 history[
pos++] = history[reference_pos++];
137 while (match_length-- > 0) {
138 history[
pos++] = history[reference_pos++];
154 unsigned uncompressed_size)
159 int ylinesize, ulinesize, vlinesize, alinesize;
160 uint16_t *dsty, *dstu, *dstv, *dsta;
163 s->texture_size_x = bytestream2_get_le32(gb);
164 s->texture_size_y = bytestream2_get_le32(gb);
170 s->uv_offset_data_offset = bytestream2_get_le32(gb);
171 if (
s->uv_offset_data_offset >= UINT_MAX / 4)
173 s->uv_offset_data_offset *= 4;
174 if (
s->uv_offset_data_offset >= uncompressed_size)
177 s->y_control_data_offset = bytestream2_get_le32(gb);
178 if (
s->y_control_data_offset >= UINT_MAX / 4)
180 s->y_control_data_offset *= 4;
181 if (
s->y_control_data_offset >= uncompressed_size)
184 s->a_control_word_offset = bytestream2_get_le32(gb);
185 if (
s->a_control_word_offset >= UINT_MAX / 4)
187 s->a_control_word_offset *= 4;
188 if (
s->a_control_word_offset >= uncompressed_size)
191 s->uv_data_offset = bytestream2_get_le32(gb);
192 if (
s->uv_data_offset >= UINT_MAX / 4)
194 s->uv_data_offset *= 4;
195 if (
s->uv_data_offset >= uncompressed_size)
198 s->y_data_size = bytestream2_get_le32(gb);
199 if (
s->y_data_size >= UINT_MAX / 4)
202 s->a_data_offset = bytestream2_get_le32(gb);
203 if (
s->a_data_offset >= UINT_MAX / 4)
205 s->a_data_offset *= 4;
206 if (
s->a_data_offset >= uncompressed_size)
209 s->a_count_size = bytestream2_get_le32(gb);
210 if (
s->a_count_size >= UINT_MAX / 4)
212 s->a_count_size *= 4;
213 if (
s->a_count_size >= uncompressed_size)
216 s->data_end = bytestream2_get_le32(gb);
217 if (
s->data_end > uncompressed_size)
221 if (
s->data_end <=
s->y_data_size)
223 s->y_data_offset =
s->data_end -
s->y_data_size;
224 if (
s->y_data_offset <=
s->a_data_offset)
226 s->uv_count_offset =
s->y_data_offset -
s->a_data_offset;
239 dsty = (uint16_t *)p->
data[0];
240 dsta = (uint16_t *)p->
data[3];
244 for (
int y = 0; y < avctx->
height; y += 4) {
245 const unsigned row_offset = bytestream2_get_le32(&
rgb);
252 for (
int x = 0; x < avctx->
width; x += 4) {
253 unsigned item = bytestream2_get_le32(gb);
254 unsigned y_min = item & 4095;
255 unsigned y_max = (item >> 12) & 4095;
256 unsigned y_diff = y_max - y_min;
259 control[0] = (item >> 24) & 3;
260 control[1] = (item >> 26) & 3;
261 control[2] = (item >> 28) & 3;
262 control[3] = (item >> 30) & 3;
264 for (
int i = 0;
i < 4;
i++) {
265 const int nb_bits = control[
i] + 1;
266 const int div = (1 << nb_bits) - 1;
267 const int add = div - 1;
276 dsty += 4 * ylinesize;
282 if (
s->uv_count_offset ==
s->a_control_word_offset) {
283 for (
int y = 0; y < avctx->
height; y++) {
284 for (
int x = 0; x < avctx->
width; x++)
292 for (
int y = 0; y < avctx->
height; y += 16) {
293 for (
int x = 0; x < avctx->
width; x += 16) {
294 unsigned m = bytestream2_get_le32(gb);
295 unsigned offset = bytestream2_get_le32(gb);
296 unsigned alpha0, alpha1;
299 if (
offset >= UINT_MAX / 4)
306 control = bytestream2_get_le64(&dgb);
307 alpha0 = control & 0xFF;
308 alpha1 = (control >> 8) & 0xFF;
309 control = control >> 16;
311 for (
int by = 0; by < 4; by++) {
312 for (
int bx = 0; bx < 4; bx++) {
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] = 0;
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] = 4095;
329 for (
int i = 0;
i < 4;
i++) {
330 for (
int j = 0; j < 4; j++) {
331 dsta[x + (
i + by * 4) * alinesize + bx * 4 + j] = (alpha0 + (alpha1 - alpha0) * (control & 7)) << 4;
345 dsta += 16 * alinesize;
351 dstu = (uint16_t *)p->
data[1];
352 dstv = (uint16_t *)p->
data[2];
356 for (
int y = 0; y < avctx->
height; y += 16) {
357 for (
int x = 0; x < avctx->
width; x += 16) {
358 unsigned offset = bytestream2_get_le32(&
rgb) * 4;
359 int u[16][16] = { 0 }, v[16][16] = { 0 };
360 int u0, v0, u1, v1, udif, vdif;
361 unsigned escape, is8x8, loc;
365 is8x8 = bytestream2_get_le16(&dgb);
366 escape = bytestream2_get_le16(&dgb);
368 if (escape == 0 && is8x8 == 0) {
369 u0 = bytestream2_get_byte(&dgb);
370 v0 = bytestream2_get_byte(&dgb);
371 u1 = bytestream2_get_byte(&dgb);
372 v1 = bytestream2_get_byte(&dgb);
373 loc = bytestream2_get_le32(&dgb);
374 u0 = (u0 << 4) | (u0 & 0xF);
375 v0 = (v0 << 4) | (v0 & 0xF);
376 u1 = (u1 << 4) | (u1 & 0xF);
377 v1 = (v1 << 4) | (v1 & 0xF);
381 for (
int i = 0;
i < 16;
i += 4) {
382 for (
int j = 0; j < 16; j += 4) {
383 for (
int ii = 0; ii < 4; ii++) {
384 for (
int jj = 0; jj < 4; jj++) {
385 u[
i + ii][j + jj] = u0 + ((udif * (int)(loc & 3) + 2) / 3);
386 v[
i + ii][j + jj] = v0 + ((vdif * (int)(loc & 3) + 2) / 3);
394 for (
int i = 0;
i < 16;
i += 8) {
395 for (
int j = 0; j < 16; j += 8) {
397 u0 = bytestream2_get_byte(&dgb);
398 v0 = bytestream2_get_byte(&dgb);
399 u1 = bytestream2_get_byte(&dgb);
400 v1 = bytestream2_get_byte(&dgb);
401 loc = bytestream2_get_le32(&dgb);
402 u0 = (u0 << 4) | (u0 & 0xF);
403 v0 = (v0 << 4) | (v0 & 0xF);
404 u1 = (u1 << 4) | (u1 & 0xF);
405 v1 = (v1 << 4) | (v1 & 0xF);
409 for (
int ii = 0; ii < 8; ii += 2) {
410 for (
int jj = 0; jj < 8; jj += 2) {
411 for (
int iii = 0; iii < 2; iii++) {
412 for (
int jjj = 0; jjj < 2; jjj++) {
413 u[
i + ii + iii][j + jj + jjj] = u0 + ((udif * (int)(loc & 3) + 2) / 3);
414 v[
i + ii + iii][j + jj + jjj] = v0 + ((vdif * (int)(loc & 3) + 2) / 3);
422 for (
int ii = 0; ii < 8; ii += 4) {
423 for (
int jj = 0; jj < 8; jj += 4) {
424 u0 = bytestream2_get_byte(&dgb);
425 v0 = bytestream2_get_byte(&dgb);
426 u1 = bytestream2_get_byte(&dgb);
427 v1 = bytestream2_get_byte(&dgb);
428 loc = bytestream2_get_le32(&dgb);
429 u0 = (u0 << 4) | (u0 & 0xF);
430 v0 = (v0 << 4) | (v0 & 0xF);
431 u1 = (u1 << 4) | (u1 & 0xF);
432 v1 = (v1 << 4) | (v1 & 0xF);
436 for (
int iii = 0; iii < 4; iii++) {
437 for (
int jjj = 0; jjj < 4; jjj++) {
438 u[
i + ii + iii][j + jj + jjj] = u0 + ((udif * (int)(loc & 3) + 2) / 3);
439 v[
i + ii + iii][j + jj + jjj] = v0 + ((vdif * (int)(loc & 3) + 2) / 3);
453 for (
int i = 0;
i < 16;
i++) {
454 for (
int j = 0; j < 16; j++) {
455 dstu[x +
i * ulinesize + j] =
u[
i][j];
456 dstv[x +
i * vlinesize + j] = v[
i][j];
461 dstu += 16 * ulinesize;
462 dstv += 16 * vlinesize;
474 unsigned uncompressed_size;
477 if (avpkt->
size <= 40)
482 if (bytestream2_get_le32(gb) !=
MKBETAG(
'N',
'L',
'C',
'1'))
485 uncompressed_size = bytestream2_get_le32(gb);
486 s->compressed_size = bytestream2_get_le32(gb);
487 s->format = bytestream2_get_le32(gb);
492 if (
s->format == 0) {
497 if (uncompressed_size >
s->lzf_size)
501 }
else if (
s->format == 1) {
507 if (!
s->uncompressed_buffer)
513 if (
ret != uncompressed_size)
533 s->uncompressed_size = 0;