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
25 * QT SMC Video Decoder by Mike Melanson (melanson@pcisys.net)
26 * For more information about the SMC format, visit:
27 * http://www.pcisys.net/~melanson/codecs/
29 * The SMC decoder outputs PAL8 colorspace data.
45 #define COLORS_PER_TABLE 256
47 typedef struct SmcContext {
49 AVCodecContext *avctx;
56 /* SMC color tables */
57 unsigned char color_pairs[COLORS_PER_TABLE * CPAIR];
58 unsigned char color_quads[COLORS_PER_TABLE * CQUAD];
59 unsigned char color_octets[COLORS_PER_TABLE * COCTET];
63 #define GET_BLOCK_COUNT() \
64 (opcode & 0x10) ? (1 + s->buf[stream_ptr++]) : 1 + (opcode & 0x0F);
66 #define ADVANCE_BLOCK() \
69 if (pixel_ptr >= width) \
72 row_ptr += stride * 4; \
75 if (total_blocks < 0) \
77 av_log(s->avctx, AV_LOG_INFO, "warning: block counter just went negative (this should not happen)\n"); \
82 static void smc_decode_stream(SmcContext *s)
84 int width = s->avctx->width;
85 int height = s->avctx->height;
86 int stride = s->frame.linesize[0];
92 unsigned int color_flags;
93 unsigned int color_flags_a;
94 unsigned int color_flags_b;
95 unsigned int flag_mask;
97 unsigned char *pixels = s->frame.data[0];
99 int image_size = height * s->frame.linesize[0];
102 int pixel_x, pixel_y;
103 int row_inc = stride - 4;
106 int prev_block_ptr1, prev_block_ptr2;
109 int color_table_index; /* indexes to color pair, quad, or octet tables */
112 int color_pair_index = 0;
113 int color_quad_index = 0;
114 int color_octet_index = 0;
116 /* make the palette available */
117 memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
118 if (s->avctx->palctrl->palette_changed) {
119 s->frame.palette_has_changed = 1;
120 s->avctx->palctrl->palette_changed = 0;
123 chunk_size = AV_RB32(&s->buf[stream_ptr]) & 0x00FFFFFF;
125 if (chunk_size != s->size)
126 av_log(s->avctx, AV_LOG_INFO, "warning: MOV chunk size != encoded chunk size (%d != %d); using MOV chunk size\n",
127 chunk_size, s->size);
129 chunk_size = s->size;
130 total_blocks = ((s->avctx->width + 3) / 4) * ((s->avctx->height + 3) / 4);
132 /* traverse through the blocks */
133 while (total_blocks) {
135 /* make sure stream ptr hasn't gone out of bounds */
136 if (stream_ptr > chunk_size) {
137 av_log(s->avctx, AV_LOG_INFO, "SMC decoder just went out of bounds (stream ptr = %d, chunk size = %d)\n",
138 stream_ptr, chunk_size);
141 /* make sure the row pointer hasn't gone wild */
142 if (row_ptr >= image_size) {
143 av_log(s->avctx, AV_LOG_INFO, "SMC decoder just went out of bounds (row ptr = %d, height = %d)\n",
144 row_ptr, image_size);
148 opcode = s->buf[stream_ptr++];
149 switch (opcode & 0xF0) {
153 n_blocks = GET_BLOCK_COUNT();
159 /* repeat last block n times */
162 n_blocks = GET_BLOCK_COUNT();
165 if ((row_ptr == 0) && (pixel_ptr == 0)) {
166 av_log(s->avctx, AV_LOG_INFO, "encountered repeat block opcode (%02X) but no blocks rendered yet\n",
171 /* figure out where the previous block started */
174 (row_ptr - s->avctx->width * 4) + s->avctx->width - 4;
176 prev_block_ptr1 = row_ptr + pixel_ptr - 4;
179 block_ptr = row_ptr + pixel_ptr;
180 prev_block_ptr = prev_block_ptr1;
181 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
182 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
183 pixels[block_ptr++] = pixels[prev_block_ptr++];
185 block_ptr += row_inc;
186 prev_block_ptr += row_inc;
192 /* repeat previous pair of blocks n times */
195 n_blocks = GET_BLOCK_COUNT();
199 if ((row_ptr == 0) && (pixel_ptr < 2 * 4)) {
200 av_log(s->avctx, AV_LOG_INFO, "encountered repeat block opcode (%02X) but not enough blocks rendered yet\n",
205 /* figure out where the previous 2 blocks started */
207 prev_block_ptr1 = (row_ptr - s->avctx->width * 4) +
208 s->avctx->width - 4 * 2;
209 else if (pixel_ptr == 4)
210 prev_block_ptr1 = (row_ptr - s->avctx->width * 4) + row_inc;
212 prev_block_ptr1 = row_ptr + pixel_ptr - 4 * 2;
215 prev_block_ptr2 = (row_ptr - s->avctx->width * 4) + row_inc;
217 prev_block_ptr2 = row_ptr + pixel_ptr - 4;
221 block_ptr = row_ptr + pixel_ptr;
223 prev_block_ptr = prev_block_ptr2;
225 prev_block_ptr = prev_block_ptr1;
226 prev_block_flag = !prev_block_flag;
228 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
229 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
230 pixels[block_ptr++] = pixels[prev_block_ptr++];
232 block_ptr += row_inc;
233 prev_block_ptr += row_inc;
239 /* 1-color block encoding */
242 n_blocks = GET_BLOCK_COUNT();
243 pixel = s->buf[stream_ptr++];
246 block_ptr = row_ptr + pixel_ptr;
247 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
248 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
249 pixels[block_ptr++] = pixel;
251 block_ptr += row_inc;
257 /* 2-color block encoding */
260 n_blocks = (opcode & 0x0F) + 1;
262 /* figure out which color pair to use to paint the 2-color block */
263 if ((opcode & 0xF0) == 0x80) {
264 /* fetch the next 2 colors from bytestream and store in next
265 * available entry in the color pair table */
266 for (i = 0; i < CPAIR; i++) {
267 pixel = s->buf[stream_ptr++];
268 color_table_index = CPAIR * color_pair_index + i;
269 s->color_pairs[color_table_index] = pixel;
271 /* this is the base index to use for this block */
272 color_table_index = CPAIR * color_pair_index;
275 if (color_pair_index == COLORS_PER_TABLE)
276 color_pair_index = 0;
278 color_table_index = CPAIR * s->buf[stream_ptr++];
281 color_flags = AV_RB16(&s->buf[stream_ptr]);
284 block_ptr = row_ptr + pixel_ptr;
285 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
286 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
287 if (color_flags & flag_mask)
288 pixel = color_table_index + 1;
290 pixel = color_table_index;
292 pixels[block_ptr++] = s->color_pairs[pixel];
294 block_ptr += row_inc;
300 /* 4-color block encoding */
303 n_blocks = (opcode & 0x0F) + 1;
305 /* figure out which color quad to use to paint the 4-color block */
306 if ((opcode & 0xF0) == 0xA0) {
307 /* fetch the next 4 colors from bytestream and store in next
308 * available entry in the color quad table */
309 for (i = 0; i < CQUAD; i++) {
310 pixel = s->buf[stream_ptr++];
311 color_table_index = CQUAD * color_quad_index + i;
312 s->color_quads[color_table_index] = pixel;
314 /* this is the base index to use for this block */
315 color_table_index = CQUAD * color_quad_index;
318 if (color_quad_index == COLORS_PER_TABLE)
319 color_quad_index = 0;
321 color_table_index = CQUAD * s->buf[stream_ptr++];
324 color_flags = AV_RB32(&s->buf[stream_ptr]);
326 /* flag mask actually acts as a bit shift count here */
328 block_ptr = row_ptr + pixel_ptr;
329 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
330 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
331 pixel = color_table_index +
332 ((color_flags >> flag_mask) & 0x03);
334 pixels[block_ptr++] = s->color_quads[pixel];
336 block_ptr += row_inc;
342 /* 8-color block encoding */
345 n_blocks = (opcode & 0x0F) + 1;
347 /* figure out which color octet to use to paint the 8-color block */
348 if ((opcode & 0xF0) == 0xC0) {
349 /* fetch the next 8 colors from bytestream and store in next
350 * available entry in the color octet table */
351 for (i = 0; i < COCTET; i++) {
352 pixel = s->buf[stream_ptr++];
353 color_table_index = COCTET * color_octet_index + i;
354 s->color_octets[color_table_index] = pixel;
356 /* this is the base index to use for this block */
357 color_table_index = COCTET * color_octet_index;
360 if (color_octet_index == COLORS_PER_TABLE)
361 color_octet_index = 0;
363 color_table_index = COCTET * s->buf[stream_ptr++];
367 For this input of 6 hex bytes:
369 Mangle it to this output:
370 flags_a = xx012456, flags_b = xx89A37B
372 /* build the color flags */
373 color_flags_a = color_flags_b = 0;
375 (s->buf[stream_ptr + 0] << 16) |
376 ((s->buf[stream_ptr + 1] & 0xF0) << 8) |
377 ((s->buf[stream_ptr + 2] & 0xF0) << 4) |
378 ((s->buf[stream_ptr + 2] & 0x0F) << 4) |
379 ((s->buf[stream_ptr + 3] & 0xF0) >> 4);
381 (s->buf[stream_ptr + 4] << 16) |
382 ((s->buf[stream_ptr + 5] & 0xF0) << 8) |
383 ((s->buf[stream_ptr + 1] & 0x0F) << 8) |
384 ((s->buf[stream_ptr + 3] & 0x0F) << 4) |
385 (s->buf[stream_ptr + 5] & 0x0F);
388 color_flags = color_flags_a;
389 /* flag mask actually acts as a bit shift count here */
391 block_ptr = row_ptr + pixel_ptr;
392 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
393 /* reload flags at third row (iteration pixel_y == 2) */
395 color_flags = color_flags_b;
398 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
399 pixel = color_table_index +
400 ((color_flags >> flag_mask) & 0x07);
402 pixels[block_ptr++] = s->color_octets[pixel];
404 block_ptr += row_inc;
410 /* 16-color block encoding (every pixel is a different color) */
412 n_blocks = (opcode & 0x0F) + 1;
415 block_ptr = row_ptr + pixel_ptr;
416 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
417 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
418 pixels[block_ptr++] = s->buf[stream_ptr++];
420 block_ptr += row_inc;
427 av_log(s->avctx, AV_LOG_INFO, "0xF0 opcode seen in SMC chunk (contact the developers)\n");
433 static int smc_decode_init(AVCodecContext *avctx)
435 SmcContext *s = (SmcContext *)avctx->priv_data;
438 avctx->pix_fmt = PIX_FMT_PAL8;
439 avctx->has_b_frames = 0;
440 dsputil_init(&s->dsp, avctx);
442 s->frame.data[0] = NULL;
447 static int smc_decode_frame(AVCodecContext *avctx,
448 void *data, int *data_size,
449 uint8_t *buf, int buf_size)
451 SmcContext *s = (SmcContext *)avctx->priv_data;
456 s->frame.reference = 1;
457 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
458 FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
459 if (avctx->reget_buffer(avctx, &s->frame)) {
460 av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
464 smc_decode_stream(s);
466 *data_size = sizeof(AVFrame);
467 *(AVFrame*)data = s->frame;
469 /* always report that the buffer was completely consumed */
473 static int smc_decode_end(AVCodecContext *avctx)
475 SmcContext *s = (SmcContext *)avctx->priv_data;
477 if (s->frame.data[0])
478 avctx->release_buffer(avctx, &s->frame);
483 AVCodec smc_decoder = {