44 #define COLORS_PER_TABLE 256
61 #define GET_BLOCK_COUNT() \
62 (opcode & 0x10) ? (1 + bytestream2_get_byte(&s->gb)) : 1 + (opcode & 0x0F);
64 #define ADVANCE_BLOCK() \
67 if (pixel_ptr >= width) \
70 row_ptr += stride * 4; \
73 if (total_blocks < !!n_blocks) \
75 av_log(s->avctx, AV_LOG_INFO, "warning: block counter just went negative (this should not happen)\n"); \
82 int width =
s->avctx->width;
84 int stride =
s->frame->linesize[0];
90 unsigned int color_flags;
91 unsigned int color_flags_a;
92 unsigned int color_flags_b;
93 unsigned int flag_mask;
95 unsigned char *
const pixels =
s->frame->data[0];
97 int image_size =
height *
s->frame->linesize[0];
100 int pixel_x, pixel_y;
104 int prev_block_ptr1, prev_block_ptr2;
107 int color_table_index;
110 int color_pair_index = 0;
111 int color_quad_index = 0;
112 int color_octet_index = 0;
118 chunk_size = bytestream2_get_be24(&
s->gb);
119 if (chunk_size != buf_size)
120 av_log(
s->avctx,
AV_LOG_INFO,
"warning: MOV chunk size != encoded chunk size (%d != %d); using MOV chunk size\n",
121 chunk_size, buf_size);
123 chunk_size = buf_size;
124 total_blocks = ((
s->avctx->width + 3) / 4) * ((
s->avctx->height + 3) / 4);
127 while (total_blocks) {
130 if (row_ptr >= image_size) {
131 av_log(
s->avctx,
AV_LOG_INFO,
"SMC decoder just went out of bounds (row ptr = %d, height = %d)\n",
132 row_ptr, image_size);
140 opcode = bytestream2_get_byte(&
s->gb);
141 switch (opcode & 0xF0) {
157 if ((row_ptr == 0) && (pixel_ptr == 0)) {
158 av_log(
s->avctx,
AV_LOG_INFO,
"encountered repeat block opcode (%02X) but no blocks rendered yet\n",
166 (row_ptr -
s->avctx->width * 4) +
s->avctx->width - 4;
168 prev_block_ptr1 = row_ptr + pixel_ptr - 4;
171 block_ptr = row_ptr + pixel_ptr;
172 prev_block_ptr = prev_block_ptr1;
173 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
174 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
175 pixels[block_ptr++] = pixels[prev_block_ptr++];
177 block_ptr += row_inc;
178 prev_block_ptr += row_inc;
191 if ((row_ptr == 0) && (pixel_ptr < 2 * 4)) {
192 av_log(
s->avctx,
AV_LOG_INFO,
"encountered repeat block opcode (%02X) but not enough blocks rendered yet\n",
199 prev_block_ptr1 = (row_ptr -
s->avctx->width * 4) +
200 s->avctx->width - 4 * 2;
201 else if (pixel_ptr == 4)
202 prev_block_ptr1 = (row_ptr -
s->avctx->width * 4) + row_inc;
204 prev_block_ptr1 = row_ptr + pixel_ptr - 4 * 2;
207 prev_block_ptr2 = (row_ptr -
s->avctx->width * 4) + row_inc;
209 prev_block_ptr2 = row_ptr + pixel_ptr - 4;
213 block_ptr = row_ptr + pixel_ptr;
215 prev_block_ptr = prev_block_ptr2;
217 prev_block_ptr = prev_block_ptr1;
218 prev_block_flag = !prev_block_flag;
220 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
221 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
222 pixels[block_ptr++] = pixels[prev_block_ptr++];
224 block_ptr += row_inc;
225 prev_block_ptr += row_inc;
235 pixel = bytestream2_get_byte(&
s->gb);
238 block_ptr = row_ptr + pixel_ptr;
239 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
240 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
241 pixels[block_ptr++] =
pixel;
243 block_ptr += row_inc;
252 n_blocks = (opcode & 0x0F) + 1;
255 if ((opcode & 0xF0) == 0x80) {
259 pixel = bytestream2_get_byte(&
s->gb);
260 color_table_index =
CPAIR * color_pair_index +
i;
261 s->color_pairs[color_table_index] =
pixel;
264 color_table_index =
CPAIR * color_pair_index;
268 color_pair_index = 0;
270 color_table_index =
CPAIR * bytestream2_get_byte(&
s->gb);
273 color_flags = bytestream2_get_be16(&
s->gb);
275 block_ptr = row_ptr + pixel_ptr;
276 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
277 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
278 if (color_flags & flag_mask)
279 pixel = color_table_index + 1;
281 pixel = color_table_index;
283 pixels[block_ptr++] =
s->color_pairs[
pixel];
285 block_ptr += row_inc;
294 n_blocks = (opcode & 0x0F) + 1;
297 if ((opcode & 0xF0) == 0xA0) {
301 pixel = bytestream2_get_byte(&
s->gb);
302 color_table_index =
CQUAD * color_quad_index +
i;
303 s->color_quads[color_table_index] =
pixel;
306 color_table_index =
CQUAD * color_quad_index;
310 color_quad_index = 0;
312 color_table_index =
CQUAD * bytestream2_get_byte(&
s->gb);
315 color_flags = bytestream2_get_be32(&
s->gb);
318 block_ptr = row_ptr + pixel_ptr;
319 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
320 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
321 pixel = color_table_index +
322 ((color_flags >> flag_mask) & 0x03);
324 pixels[block_ptr++] =
s->color_quads[
pixel];
326 block_ptr += row_inc;
335 n_blocks = (opcode & 0x0F) + 1;
338 if ((opcode & 0xF0) == 0xC0) {
342 pixel = bytestream2_get_byte(&
s->gb);
343 color_table_index =
COCTET * color_octet_index +
i;
344 s->color_octets[color_table_index] =
pixel;
347 color_table_index =
COCTET * color_octet_index;
351 color_octet_index = 0;
353 color_table_index =
COCTET * bytestream2_get_byte(&
s->gb);
363 int val1 = bytestream2_get_be16(&
s->gb);
364 int val2 = bytestream2_get_be16(&
s->gb);
365 int val3 = bytestream2_get_be16(&
s->gb);
366 color_flags_a = ((val1 & 0xFFF0) << 8) | (val2 >> 4);
367 color_flags_b = ((val3 & 0xFFF0) << 8) |
368 ((val1 & 0x0F) << 8) | ((val2 & 0x0F) << 4) | (val3 & 0x0F);
370 color_flags = color_flags_a;
373 block_ptr = row_ptr + pixel_ptr;
374 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
377 color_flags = color_flags_b;
380 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
381 pixel = color_table_index +
382 ((color_flags >> flag_mask) & 0x07);
384 pixels[block_ptr++] =
s->color_octets[
pixel];
386 block_ptr += row_inc;
394 n_blocks = (opcode & 0x0F) + 1;
397 block_ptr = row_ptr + pixel_ptr;
398 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
399 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
400 pixels[block_ptr++] = bytestream2_get_byte(&
s->gb);
402 block_ptr += row_inc;
432 void *
data,
int *got_frame,
436 int buf_size = avpkt->
size;
441 int total_blocks = ((
s->avctx->width + 3) / 4) * ((
s->avctx->height + 3) / 4);
443 if (total_blocks / 1024 > avpkt->
size)
452 s->frame->palette_has_changed = 1;