26 #define BITSTREAM_READER_LE
74 #define HISTORY_SIZE (64 * 1024)
80 unsigned reference_pos, match_length,
delta,
pos = 0;
84 uint8_t token = bytestream2_get_byte(gb);
85 unsigned 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_byte(gb);
112 delta |= (unsigned)bytestream2_get_byte(gb) << 8;
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->uv_count_size = bytestream2_get_le32(gb);
200 if (
s->uv_count_size >= UINT_MAX / 4)
202 s->uv_count_size *= 4;
203 if (
s->uv_count_size >= 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->uv_count_size)
223 s->uv_count_offset =
s->y_data_offset -
s->uv_count_size;
233 dsty = (uint16_t *)p->
data[0];
234 dsta = (uint16_t *)p->
data[3];
238 for (
int y = 0; y < avctx->
height; y += 4) {
239 const unsigned row_offset = bytestream2_get_le32(&rgb);
244 for (
int x = 0; x < avctx->
width; x += 4) {
245 unsigned item = bytestream2_get_le32(gb);
246 unsigned y_min = item & 4095;
247 unsigned y_max = (item >> 12) & 4095;
248 unsigned y_diff = y_max - y_min;
251 control[0] = (item >> 24) & 3;
252 control[1] = (item >> 26) & 3;
253 control[2] = (item >> 28) & 3;
254 control[3] = (item >> 30) & 3;
256 for (
int i = 0;
i < 4;
i++) {
257 const int nb_bits = control[
i] + 1;
258 const int div = (1 << nb_bits) - 1;
259 const int add = div - 1;
261 dsty[x +
i * ylinesize + 0] = av_clip_uintp2(y_min + ((y_diff *
get_bits(&
bit, nb_bits) + add) / div), 12);
262 dsty[x +
i * ylinesize + 1] = av_clip_uintp2(y_min + ((y_diff *
get_bits(&
bit, nb_bits) + add) / div), 12);
263 dsty[x +
i * ylinesize + 2] = av_clip_uintp2(y_min + ((y_diff *
get_bits(&
bit, nb_bits) + add) / div), 12);
264 dsty[x +
i * ylinesize + 3] = av_clip_uintp2(y_min + ((y_diff *
get_bits(&
bit, nb_bits) + add) / div), 12);
268 dsty += 4 * ylinesize;
269 dsta += 4 * alinesize;
277 dstu = (uint16_t *)p->
data[1];
278 dstv = (uint16_t *)p->
data[2];
282 for (
int y = 0; y < avctx->
height; y += 16) {
283 for (
int x = 0; x < avctx->
width; x += 16) {
284 unsigned offset = bytestream2_get_le32(&rgb) * 4;
285 int u[16][16] = { 0 }, v[16][16] = { 0 };
286 int u0,
v0, u1, v1, udif, vdif;
287 unsigned escape, is8x8, loc;
291 is8x8 = bytestream2_get_le16(&dgb);
292 escape = bytestream2_get_le16(&dgb);
294 if (escape == 0 && is8x8 == 0) {
295 u0 = bytestream2_get_byte(&dgb);
296 v0 = bytestream2_get_byte(&dgb);
297 u1 = bytestream2_get_byte(&dgb);
298 v1 = bytestream2_get_byte(&dgb);
299 loc = bytestream2_get_le32(&dgb);
300 u0 = (u0 << 4) | (u0 & 0xF);
301 v0 = (
v0 << 4) | (
v0 & 0xF);
302 u1 = (u1 << 4) | (u1 & 0xF);
303 v1 = (v1 << 4) | (v1 & 0xF);
307 for (
int i = 0;
i < 16;
i += 4) {
308 for (
int j = 0; j < 16; j += 4) {
309 for (
int ii = 0; ii < 4; ii++) {
310 for (
int jj = 0; jj < 4; jj++) {
311 u[
i + ii][j + jj] = u0 + ((udif * (
int)(loc & 3) + 2) / 3);
312 v[
i + ii][j + jj] =
v0 + ((vdif * (
int)(loc & 3) + 2) / 3);
320 for (
int i = 0;
i < 16;
i += 8) {
321 for (
int j = 0; j < 16; j += 8) {
323 u0 = bytestream2_get_byte(&dgb);
324 v0 = bytestream2_get_byte(&dgb);
325 u1 = bytestream2_get_byte(&dgb);
326 v1 = bytestream2_get_byte(&dgb);
327 loc = bytestream2_get_le32(&dgb);
328 u0 = (u0 << 4) | (u0 & 0xF);
329 v0 = (
v0 << 4) | (
v0 & 0xF);
330 u1 = (u1 << 4) | (u1 & 0xF);
331 v1 = (v1 << 4) | (v1 & 0xF);
335 for (
int ii = 0; ii < 8; ii += 2) {
336 for (
int jj = 0; jj < 8; jj += 2) {
337 for (
int iii = 0; iii < 2; iii++) {
338 for (
int jjj = 0; jjj < 2; jjj++) {
339 u[
i + ii + iii][j + jj + jjj] = u0 + ((udif * (
int)(loc & 3) + 2) / 3);
340 v[
i + ii + iii][j + jj + jjj] =
v0 + ((vdif * (
int)(loc & 3) + 2) / 3);
348 for (
int ii = 0; ii < 8; ii += 4) {
349 for (
int jj = 0; jj < 8; jj += 4) {
350 u0 = bytestream2_get_byte(&dgb);
351 v0 = bytestream2_get_byte(&dgb);
352 u1 = bytestream2_get_byte(&dgb);
353 v1 = bytestream2_get_byte(&dgb);
354 loc = bytestream2_get_le32(&dgb);
355 u0 = (u0 << 4) | (u0 & 0xF);
356 v0 = (
v0 << 4) | (
v0 & 0xF);
357 u1 = (u1 << 4) | (u1 & 0xF);
358 v1 = (v1 << 4) | (v1 & 0xF);
362 for (
int iii = 0; iii < 4; iii++) {
363 for (
int jjj = 0; jjj < 4; jjj++) {
364 u[
i + ii + iii][j + jj + jjj] = u0 + ((udif * (
int)(loc & 3) + 2) / 3);
365 v[
i + ii + iii][j + jj + jjj] =
v0 + ((vdif * (
int)(loc & 3) + 2) / 3);
379 for (
int i = 0;
i < 16;
i++) {
380 for (
int j = 0; j < 16; j++) {
381 dstu[x +
i * ulinesize + j] =
u[
i][j];
382 dstv[x +
i * vlinesize + j] = v[
i][j];
387 dstu += 16 * ulinesize;
388 dstv += 16 * vlinesize;
395 void *
data,
int *got_frame,
402 unsigned uncompressed_size;
406 if (avpkt->
size <= 40)
411 if (bytestream2_get_le32(gb) !=
MKBETAG(
'N',
'L',
'C',
'1'))
414 uncompressed_size = bytestream2_get_le32(gb);
415 s->compressed_size = bytestream2_get_le32(gb);
416 s->format = bytestream2_get_le32(gb);
421 if (
s->format == 0) {
426 if (uncompressed_size >
s->lzf_size)
430 }
else if (
s->format == 1) {
433 if (!
s->uncompressed_buffer)
439 if (
ret != uncompressed_size)
462 s->uncompressed_size = 0;