46 #define COLORS_PER_TABLE 256
63 #define GET_BLOCK_COUNT() \
64 (opcode & 0x10) ? (1 + bytestream2_get_byte(gb)) : 1 + (opcode & 0x0F);
66 #define ADVANCE_BLOCK() \
69 if (pixel_ptr >= width) \
72 row_ptr += stride * 4; \
75 if (total_blocks < !!n_blocks) \
77 av_log(s->avctx, AV_LOG_ERROR, "block counter just went negative (this should not happen)\n"); \
78 return AVERROR_INVALIDDATA; \
85 int width =
s->avctx->width;
87 int stride =
s->frame->linesize[0];
93 unsigned int color_flags;
94 unsigned int color_flags_a;
95 unsigned int color_flags_b;
96 unsigned int flag_mask;
98 uint8_t *
const pixels =
s->frame->data[0];
100 int image_size =
height *
s->frame->linesize[0];
103 int pixel_x, pixel_y;
107 int prev_block_ptr1, prev_block_ptr2;
110 int color_table_index;
113 int color_pair_index = 0;
114 int color_quad_index = 0;
115 int color_octet_index = 0;
121 chunk_size = bytestream2_get_be24(gb);
122 if (chunk_size != buf_size)
123 av_log(
s->avctx,
AV_LOG_WARNING,
"MOV chunk size != encoded chunk size (%d != %d); using MOV chunk size\n",
124 chunk_size, buf_size);
126 chunk_size = buf_size;
127 total_blocks = ((
s->avctx->width + 3) / 4) * ((
s->avctx->height + 3) / 4);
130 while (total_blocks) {
133 if (row_ptr >= image_size) {
135 row_ptr, image_size);
143 opcode = bytestream2_get_byteu(gb);
144 switch (opcode & 0xF0) {
160 if ((row_ptr == 0) && (pixel_ptr == 0)) {
161 av_log(
s->avctx,
AV_LOG_ERROR,
"encountered repeat block opcode (%02X) but no blocks rendered yet\n",
169 (row_ptr -
s->avctx->width * 4) +
s->avctx->width - 4;
171 prev_block_ptr1 = row_ptr + pixel_ptr - 4;
174 block_ptr = row_ptr + pixel_ptr;
175 prev_block_ptr = prev_block_ptr1;
176 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
177 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
178 pixels[block_ptr++] = pixels[prev_block_ptr++];
180 block_ptr += row_inc;
181 prev_block_ptr += row_inc;
194 if ((row_ptr == 0) && (pixel_ptr < 2 * 4)) {
195 av_log(
s->avctx,
AV_LOG_ERROR,
"encountered repeat block opcode (%02X) but not enough blocks rendered yet\n",
202 prev_block_ptr1 = (row_ptr -
s->avctx->width * 4) +
203 s->avctx->width - 4 * 2;
204 else if (pixel_ptr == 4)
205 prev_block_ptr1 = (row_ptr -
s->avctx->width * 4) + row_inc;
207 prev_block_ptr1 = row_ptr + pixel_ptr - 4 * 2;
210 prev_block_ptr2 = (row_ptr -
s->avctx->width * 4) + row_inc;
212 prev_block_ptr2 = row_ptr + pixel_ptr - 4;
216 block_ptr = row_ptr + pixel_ptr;
218 prev_block_ptr = prev_block_ptr2;
220 prev_block_ptr = prev_block_ptr1;
221 prev_block_flag = !prev_block_flag;
223 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
224 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
225 pixels[block_ptr++] = pixels[prev_block_ptr++];
227 block_ptr += row_inc;
228 prev_block_ptr += row_inc;
238 pixel = bytestream2_get_byte(gb);
241 block_ptr = row_ptr + pixel_ptr;
242 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
243 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
244 pixels[block_ptr++] =
pixel;
246 block_ptr += row_inc;
255 n_blocks = (opcode & 0x0F) + 1;
258 if ((opcode & 0xF0) == 0x80) {
262 pixel = bytestream2_get_byte(gb);
263 color_table_index =
CPAIR * color_pair_index +
i;
264 s->color_pairs[color_table_index] =
pixel;
267 color_table_index =
CPAIR * color_pair_index;
271 color_pair_index = 0;
273 color_table_index =
CPAIR * bytestream2_get_byte(gb);
276 color_flags = bytestream2_get_be16(gb);
278 block_ptr = row_ptr + pixel_ptr;
279 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
280 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
281 if (color_flags & flag_mask)
282 pixel = color_table_index + 1;
284 pixel = color_table_index;
286 pixels[block_ptr++] =
s->color_pairs[
pixel];
288 block_ptr += row_inc;
297 n_blocks = (opcode & 0x0F) + 1;
300 if ((opcode & 0xF0) == 0xA0) {
304 pixel = bytestream2_get_byte(gb);
305 color_table_index =
CQUAD * color_quad_index +
i;
306 s->color_quads[color_table_index] =
pixel;
309 color_table_index =
CQUAD * color_quad_index;
313 color_quad_index = 0;
315 color_table_index =
CQUAD * bytestream2_get_byte(gb);
318 color_flags = bytestream2_get_be32(gb);
321 block_ptr = row_ptr + pixel_ptr;
322 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
323 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
324 pixel = color_table_index +
325 ((color_flags >> flag_mask) & 0x03);
327 pixels[block_ptr++] =
s->color_quads[
pixel];
329 block_ptr += row_inc;
338 n_blocks = (opcode & 0x0F) + 1;
341 if ((opcode & 0xF0) == 0xC0) {
345 pixel = bytestream2_get_byte(gb);
346 color_table_index =
COCTET * color_octet_index +
i;
347 s->color_octets[color_table_index] =
pixel;
350 color_table_index =
COCTET * color_octet_index;
354 color_octet_index = 0;
356 color_table_index =
COCTET * bytestream2_get_byte(gb);
366 int val1 = bytestream2_get_be16(gb);
367 int val2 = bytestream2_get_be16(gb);
368 int val3 = bytestream2_get_be16(gb);
369 color_flags_a = ((val1 & 0xFFF0) << 8) | (val2 >> 4);
370 color_flags_b = ((val3 & 0xFFF0) << 8) |
371 ((val1 & 0x0F) << 8) | ((val2 & 0x0F) << 4) | (val3 & 0x0F);
373 color_flags = color_flags_a;
376 block_ptr = row_ptr + pixel_ptr;
377 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
380 color_flags = color_flags_b;
383 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
384 pixel = color_table_index +
385 ((color_flags >> flag_mask) & 0x07);
387 pixels[block_ptr++] =
s->color_octets[
pixel];
389 block_ptr += row_inc;
398 n_blocks = (opcode & 0x0F) + 1;
401 block_ptr = row_ptr + pixel_ptr;
402 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
403 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
404 pixels[block_ptr++] = bytestream2_get_byte(gb);
406 block_ptr += row_inc;
434 const uint8_t *buf = avpkt->
data;
435 int buf_size = avpkt->
size;
438 int total_blocks = ((
s->avctx->width + 3) / 4) * ((
s->avctx->height + 3) / 4);
440 if (total_blocks / 1024 > avpkt->
size)