2 * Quicktime Graphics (SMC) Video Decoder
3 * Copyright (C) 2003 the ffmpeg project
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * QT SMC Video Decoder by Mike Melanson (melanson@pcisys.net)
25 * For more information about the SMC format, visit:
26 * http://www.pcisys.net/~melanson/codecs/
28 * The SMC decoder outputs PAL8 colorspace data.
43 #define COLORS_PER_TABLE 256
45 typedef struct SmcContext {
47 AVCodecContext *avctx;
54 /* SMC color tables */
55 unsigned char color_pairs[COLORS_PER_TABLE * CPAIR];
56 unsigned char color_quads[COLORS_PER_TABLE * CQUAD];
57 unsigned char color_octets[COLORS_PER_TABLE * COCTET];
61 #define GET_BLOCK_COUNT() \
62 (opcode & 0x10) ? (1 + s->buf[stream_ptr++]) : 1 + (opcode & 0x0F);
64 #define ADVANCE_BLOCK() \
67 if (pixel_ptr >= width) \
70 row_ptr += stride * 4; \
73 if (total_blocks < 0) \
75 av_log(s->avctx, AV_LOG_INFO, "warning: block counter just went negative (this should not happen)\n"); \
80 static void smc_decode_stream(SmcContext *s)
82 int width = s->avctx->width;
83 int height = s->avctx->height;
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 *pixels = s->frame.data[0];
97 int image_size = height * s->frame.linesize[0];
100 int pixel_x, pixel_y;
101 int row_inc = stride - 4;
104 int prev_block_ptr1, prev_block_ptr2;
107 int color_table_index; /* indexes to color pair, quad, or octet tables */
110 int color_pair_index = 0;
111 int color_quad_index = 0;
112 int color_octet_index = 0;
114 /* make the palette available */
115 memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
116 if (s->avctx->palctrl->palette_changed) {
117 s->frame.palette_has_changed = 1;
118 s->avctx->palctrl->palette_changed = 0;
121 chunk_size = AV_RB32(&s->buf[stream_ptr]) & 0x00FFFFFF;
123 if (chunk_size != s->size)
124 av_log(s->avctx, AV_LOG_INFO, "warning: MOV chunk size != encoded chunk size (%d != %d); using MOV chunk size\n",
125 chunk_size, s->size);
127 chunk_size = s->size;
128 total_blocks = ((s->avctx->width + 3) / 4) * ((s->avctx->height + 3) / 4);
130 /* traverse through the blocks */
131 while (total_blocks) {
133 /* make sure stream ptr hasn't gone out of bounds */
134 if (stream_ptr > chunk_size) {
135 av_log(s->avctx, AV_LOG_INFO, "SMC decoder just went out of bounds (stream ptr = %d, chunk size = %d)\n",
136 stream_ptr, chunk_size);
139 /* make sure the row pointer hasn't gone wild */
140 if (row_ptr >= image_size) {
141 av_log(s->avctx, AV_LOG_INFO, "SMC decoder just went out of bounds (row ptr = %d, height = %d)\n",
142 row_ptr, image_size);
146 opcode = s->buf[stream_ptr++];
147 switch (opcode & 0xF0) {
151 n_blocks = GET_BLOCK_COUNT();
157 /* repeat last block n times */
160 n_blocks = GET_BLOCK_COUNT();
163 if ((row_ptr == 0) && (pixel_ptr == 0)) {
164 av_log(s->avctx, AV_LOG_INFO, "encountered repeat block opcode (%02X) but no blocks rendered yet\n",
169 /* figure out where the previous block started */
172 (row_ptr - s->avctx->width * 4) + s->avctx->width - 4;
174 prev_block_ptr1 = row_ptr + pixel_ptr - 4;
177 block_ptr = row_ptr + pixel_ptr;
178 prev_block_ptr = prev_block_ptr1;
179 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
180 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
181 pixels[block_ptr++] = pixels[prev_block_ptr++];
183 block_ptr += row_inc;
184 prev_block_ptr += row_inc;
190 /* repeat previous pair of blocks n times */
193 n_blocks = GET_BLOCK_COUNT();
197 if ((row_ptr == 0) && (pixel_ptr < 2 * 4)) {
198 av_log(s->avctx, AV_LOG_INFO, "encountered repeat block opcode (%02X) but not enough blocks rendered yet\n",
203 /* figure out where the previous 2 blocks started */
205 prev_block_ptr1 = (row_ptr - s->avctx->width * 4) +
206 s->avctx->width - 4 * 2;
207 else if (pixel_ptr == 4)
208 prev_block_ptr1 = (row_ptr - s->avctx->width * 4) + row_inc;
210 prev_block_ptr1 = row_ptr + pixel_ptr - 4 * 2;
213 prev_block_ptr2 = (row_ptr - s->avctx->width * 4) + row_inc;
215 prev_block_ptr2 = row_ptr + pixel_ptr - 4;
219 block_ptr = row_ptr + pixel_ptr;
221 prev_block_ptr = prev_block_ptr2;
223 prev_block_ptr = prev_block_ptr1;
224 prev_block_flag = !prev_block_flag;
226 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
227 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
228 pixels[block_ptr++] = pixels[prev_block_ptr++];
230 block_ptr += row_inc;
231 prev_block_ptr += row_inc;
237 /* 1-color block encoding */
240 n_blocks = GET_BLOCK_COUNT();
241 pixel = s->buf[stream_ptr++];
244 block_ptr = row_ptr + pixel_ptr;
245 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
246 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
247 pixels[block_ptr++] = pixel;
249 block_ptr += row_inc;
255 /* 2-color block encoding */
258 n_blocks = (opcode & 0x0F) + 1;
260 /* figure out which color pair to use to paint the 2-color block */
261 if ((opcode & 0xF0) == 0x80) {
262 /* fetch the next 2 colors from bytestream and store in next
263 * available entry in the color pair table */
264 for (i = 0; i < CPAIR; i++) {
265 pixel = s->buf[stream_ptr++];
266 color_table_index = CPAIR * color_pair_index + i;
267 s->color_pairs[color_table_index] = pixel;
269 /* this is the base index to use for this block */
270 color_table_index = CPAIR * color_pair_index;
273 if (color_pair_index == COLORS_PER_TABLE)
274 color_pair_index = 0;
276 color_table_index = CPAIR * s->buf[stream_ptr++];
279 color_flags = AV_RB16(&s->buf[stream_ptr]);
282 block_ptr = row_ptr + pixel_ptr;
283 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
284 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
285 if (color_flags & flag_mask)
286 pixel = color_table_index + 1;
288 pixel = color_table_index;
290 pixels[block_ptr++] = s->color_pairs[pixel];
292 block_ptr += row_inc;
298 /* 4-color block encoding */
301 n_blocks = (opcode & 0x0F) + 1;
303 /* figure out which color quad to use to paint the 4-color block */
304 if ((opcode & 0xF0) == 0xA0) {
305 /* fetch the next 4 colors from bytestream and store in next
306 * available entry in the color quad table */
307 for (i = 0; i < CQUAD; i++) {
308 pixel = s->buf[stream_ptr++];
309 color_table_index = CQUAD * color_quad_index + i;
310 s->color_quads[color_table_index] = pixel;
312 /* this is the base index to use for this block */
313 color_table_index = CQUAD * color_quad_index;
316 if (color_quad_index == COLORS_PER_TABLE)
317 color_quad_index = 0;
319 color_table_index = CQUAD * s->buf[stream_ptr++];
322 color_flags = AV_RB32(&s->buf[stream_ptr]);
324 /* flag mask actually acts as a bit shift count here */
326 block_ptr = row_ptr + pixel_ptr;
327 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
328 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
329 pixel = color_table_index +
330 ((color_flags >> flag_mask) & 0x03);
332 pixels[block_ptr++] = s->color_quads[pixel];
334 block_ptr += row_inc;
340 /* 8-color block encoding */
343 n_blocks = (opcode & 0x0F) + 1;
345 /* figure out which color octet to use to paint the 8-color block */
346 if ((opcode & 0xF0) == 0xC0) {
347 /* fetch the next 8 colors from bytestream and store in next
348 * available entry in the color octet table */
349 for (i = 0; i < COCTET; i++) {
350 pixel = s->buf[stream_ptr++];
351 color_table_index = COCTET * color_octet_index + i;
352 s->color_octets[color_table_index] = pixel;
354 /* this is the base index to use for this block */
355 color_table_index = COCTET * color_octet_index;
358 if (color_octet_index == COLORS_PER_TABLE)
359 color_octet_index = 0;
361 color_table_index = COCTET * s->buf[stream_ptr++];
365 For this input of 6 hex bytes:
367 Mangle it to this output:
368 flags_a = xx012456, flags_b = xx89A37B
370 /* build the color flags */
371 color_flags_a = color_flags_b = 0;
373 (s->buf[stream_ptr + 0] << 16) |
374 ((s->buf[stream_ptr + 1] & 0xF0) << 8) |
375 ((s->buf[stream_ptr + 2] & 0xF0) << 4) |
376 ((s->buf[stream_ptr + 2] & 0x0F) << 4) |
377 ((s->buf[stream_ptr + 3] & 0xF0) >> 4);
379 (s->buf[stream_ptr + 4] << 16) |
380 ((s->buf[stream_ptr + 5] & 0xF0) << 8) |
381 ((s->buf[stream_ptr + 1] & 0x0F) << 8) |
382 ((s->buf[stream_ptr + 3] & 0x0F) << 4) |
383 (s->buf[stream_ptr + 5] & 0x0F);
386 color_flags = color_flags_a;
387 /* flag mask actually acts as a bit shift count here */
389 block_ptr = row_ptr + pixel_ptr;
390 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
391 /* reload flags at third row (iteration pixel_y == 2) */
393 color_flags = color_flags_b;
396 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
397 pixel = color_table_index +
398 ((color_flags >> flag_mask) & 0x07);
400 pixels[block_ptr++] = s->color_octets[pixel];
402 block_ptr += row_inc;
408 /* 16-color block encoding (every pixel is a different color) */
410 n_blocks = (opcode & 0x0F) + 1;
413 block_ptr = row_ptr + pixel_ptr;
414 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
415 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
416 pixels[block_ptr++] = s->buf[stream_ptr++];
418 block_ptr += row_inc;
425 av_log(s->avctx, AV_LOG_INFO, "0xF0 opcode seen in SMC chunk (contact the developers)\n");
431 static int smc_decode_init(AVCodecContext *avctx)
433 SmcContext *s = avctx->priv_data;
436 avctx->pix_fmt = PIX_FMT_PAL8;
437 dsputil_init(&s->dsp, avctx);
439 s->frame.data[0] = NULL;
444 static int smc_decode_frame(AVCodecContext *avctx,
445 void *data, int *data_size,
446 uint8_t *buf, int buf_size)
448 SmcContext *s = avctx->priv_data;
453 s->frame.reference = 1;
454 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
455 FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
456 if (avctx->reget_buffer(avctx, &s->frame)) {
457 av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
461 smc_decode_stream(s);
463 *data_size = sizeof(AVFrame);
464 *(AVFrame*)data = s->frame;
466 /* always report that the buffer was completely consumed */
470 static int smc_decode_end(AVCodecContext *avctx)
472 SmcContext *s = avctx->priv_data;
474 if (s->frame.data[0])
475 avctx->release_buffer(avctx, &s->frame);
480 AVCodec smc_decoder = {