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.
42 #define COLORS_PER_TABLE 256
44 typedef struct SmcContext {
46 AVCodecContext *avctx;
49 const unsigned char *buf;
52 /* SMC color tables */
53 unsigned char color_pairs[COLORS_PER_TABLE * CPAIR];
54 unsigned char color_quads[COLORS_PER_TABLE * CQUAD];
55 unsigned char color_octets[COLORS_PER_TABLE * COCTET];
59 #define GET_BLOCK_COUNT() \
60 (opcode & 0x10) ? (1 + s->buf[stream_ptr++]) : 1 + (opcode & 0x0F);
62 #define ADVANCE_BLOCK() \
65 if (pixel_ptr >= width) \
68 row_ptr += stride * 4; \
71 if (total_blocks < 0) \
73 av_log(s->avctx, AV_LOG_INFO, "warning: block counter just went negative (this should not happen)\n"); \
78 static void smc_decode_stream(SmcContext *s)
80 int width = s->avctx->width;
81 int height = s->avctx->height;
82 int stride = s->frame.linesize[0];
88 unsigned int color_flags;
89 unsigned int color_flags_a;
90 unsigned int color_flags_b;
91 unsigned int flag_mask;
93 unsigned char *pixels = s->frame.data[0];
95 int image_size = height * s->frame.linesize[0];
99 int row_inc = stride - 4;
102 int prev_block_ptr1, prev_block_ptr2;
105 int color_table_index; /* indexes to color pair, quad, or octet tables */
108 int color_pair_index = 0;
109 int color_quad_index = 0;
110 int color_octet_index = 0;
112 /* make the palette available */
113 memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
114 if (s->avctx->palctrl->palette_changed) {
115 s->frame.palette_has_changed = 1;
116 s->avctx->palctrl->palette_changed = 0;
119 chunk_size = AV_RB32(&s->buf[stream_ptr]) & 0x00FFFFFF;
121 if (chunk_size != s->size)
122 av_log(s->avctx, AV_LOG_INFO, "warning: MOV chunk size != encoded chunk size (%d != %d); using MOV chunk size\n",
123 chunk_size, s->size);
125 chunk_size = s->size;
126 total_blocks = ((s->avctx->width + 3) / 4) * ((s->avctx->height + 3) / 4);
128 /* traverse through the blocks */
129 while (total_blocks) {
131 /* make sure stream ptr hasn't gone out of bounds */
132 if (stream_ptr > chunk_size) {
133 av_log(s->avctx, AV_LOG_INFO, "SMC decoder just went out of bounds (stream ptr = %d, chunk size = %d)\n",
134 stream_ptr, chunk_size);
137 /* make sure the row pointer hasn't gone wild */
138 if (row_ptr >= image_size) {
139 av_log(s->avctx, AV_LOG_INFO, "SMC decoder just went out of bounds (row ptr = %d, height = %d)\n",
140 row_ptr, image_size);
144 opcode = s->buf[stream_ptr++];
145 switch (opcode & 0xF0) {
149 n_blocks = GET_BLOCK_COUNT();
155 /* repeat last block n times */
158 n_blocks = GET_BLOCK_COUNT();
161 if ((row_ptr == 0) && (pixel_ptr == 0)) {
162 av_log(s->avctx, AV_LOG_INFO, "encountered repeat block opcode (%02X) but no blocks rendered yet\n",
167 /* figure out where the previous block started */
170 (row_ptr - s->avctx->width * 4) + s->avctx->width - 4;
172 prev_block_ptr1 = row_ptr + pixel_ptr - 4;
175 block_ptr = row_ptr + pixel_ptr;
176 prev_block_ptr = prev_block_ptr1;
177 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
178 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
179 pixels[block_ptr++] = pixels[prev_block_ptr++];
181 block_ptr += row_inc;
182 prev_block_ptr += row_inc;
188 /* repeat previous pair of blocks n times */
191 n_blocks = GET_BLOCK_COUNT();
195 if ((row_ptr == 0) && (pixel_ptr < 2 * 4)) {
196 av_log(s->avctx, AV_LOG_INFO, "encountered repeat block opcode (%02X) but not enough blocks rendered yet\n",
201 /* figure out where the previous 2 blocks started */
203 prev_block_ptr1 = (row_ptr - s->avctx->width * 4) +
204 s->avctx->width - 4 * 2;
205 else if (pixel_ptr == 4)
206 prev_block_ptr1 = (row_ptr - s->avctx->width * 4) + row_inc;
208 prev_block_ptr1 = row_ptr + pixel_ptr - 4 * 2;
211 prev_block_ptr2 = (row_ptr - s->avctx->width * 4) + row_inc;
213 prev_block_ptr2 = row_ptr + pixel_ptr - 4;
217 block_ptr = row_ptr + pixel_ptr;
219 prev_block_ptr = prev_block_ptr2;
221 prev_block_ptr = prev_block_ptr1;
222 prev_block_flag = !prev_block_flag;
224 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
225 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
226 pixels[block_ptr++] = pixels[prev_block_ptr++];
228 block_ptr += row_inc;
229 prev_block_ptr += row_inc;
235 /* 1-color block encoding */
238 n_blocks = GET_BLOCK_COUNT();
239 pixel = s->buf[stream_ptr++];
242 block_ptr = row_ptr + pixel_ptr;
243 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
244 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
245 pixels[block_ptr++] = pixel;
247 block_ptr += row_inc;
253 /* 2-color block encoding */
256 n_blocks = (opcode & 0x0F) + 1;
258 /* figure out which color pair to use to paint the 2-color block */
259 if ((opcode & 0xF0) == 0x80) {
260 /* fetch the next 2 colors from bytestream and store in next
261 * available entry in the color pair table */
262 for (i = 0; i < CPAIR; i++) {
263 pixel = s->buf[stream_ptr++];
264 color_table_index = CPAIR * color_pair_index + i;
265 s->color_pairs[color_table_index] = pixel;
267 /* this is the base index to use for this block */
268 color_table_index = CPAIR * color_pair_index;
271 if (color_pair_index == COLORS_PER_TABLE)
272 color_pair_index = 0;
274 color_table_index = CPAIR * s->buf[stream_ptr++];
277 color_flags = AV_RB16(&s->buf[stream_ptr]);
280 block_ptr = row_ptr + pixel_ptr;
281 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
282 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
283 if (color_flags & flag_mask)
284 pixel = color_table_index + 1;
286 pixel = color_table_index;
288 pixels[block_ptr++] = s->color_pairs[pixel];
290 block_ptr += row_inc;
296 /* 4-color block encoding */
299 n_blocks = (opcode & 0x0F) + 1;
301 /* figure out which color quad to use to paint the 4-color block */
302 if ((opcode & 0xF0) == 0xA0) {
303 /* fetch the next 4 colors from bytestream and store in next
304 * available entry in the color quad table */
305 for (i = 0; i < CQUAD; i++) {
306 pixel = s->buf[stream_ptr++];
307 color_table_index = CQUAD * color_quad_index + i;
308 s->color_quads[color_table_index] = pixel;
310 /* this is the base index to use for this block */
311 color_table_index = CQUAD * color_quad_index;
314 if (color_quad_index == COLORS_PER_TABLE)
315 color_quad_index = 0;
317 color_table_index = CQUAD * s->buf[stream_ptr++];
320 color_flags = AV_RB32(&s->buf[stream_ptr]);
322 /* flag mask actually acts as a bit shift count here */
324 block_ptr = row_ptr + pixel_ptr;
325 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
326 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
327 pixel = color_table_index +
328 ((color_flags >> flag_mask) & 0x03);
330 pixels[block_ptr++] = s->color_quads[pixel];
332 block_ptr += row_inc;
338 /* 8-color block encoding */
341 n_blocks = (opcode & 0x0F) + 1;
343 /* figure out which color octet to use to paint the 8-color block */
344 if ((opcode & 0xF0) == 0xC0) {
345 /* fetch the next 8 colors from bytestream and store in next
346 * available entry in the color octet table */
347 for (i = 0; i < COCTET; i++) {
348 pixel = s->buf[stream_ptr++];
349 color_table_index = COCTET * color_octet_index + i;
350 s->color_octets[color_table_index] = pixel;
352 /* this is the base index to use for this block */
353 color_table_index = COCTET * color_octet_index;
356 if (color_octet_index == COLORS_PER_TABLE)
357 color_octet_index = 0;
359 color_table_index = COCTET * s->buf[stream_ptr++];
363 For this input of 6 hex bytes:
365 Mangle it to this output:
366 flags_a = xx012456, flags_b = xx89A37B
368 /* build the color flags */
369 color_flags_a = color_flags_b = 0;
371 (s->buf[stream_ptr + 0] << 16) |
372 ((s->buf[stream_ptr + 1] & 0xF0) << 8) |
373 ((s->buf[stream_ptr + 2] & 0xF0) << 4) |
374 ((s->buf[stream_ptr + 2] & 0x0F) << 4) |
375 ((s->buf[stream_ptr + 3] & 0xF0) >> 4);
377 (s->buf[stream_ptr + 4] << 16) |
378 ((s->buf[stream_ptr + 5] & 0xF0) << 8) |
379 ((s->buf[stream_ptr + 1] & 0x0F) << 8) |
380 ((s->buf[stream_ptr + 3] & 0x0F) << 4) |
381 (s->buf[stream_ptr + 5] & 0x0F);
384 color_flags = color_flags_a;
385 /* flag mask actually acts as a bit shift count here */
387 block_ptr = row_ptr + pixel_ptr;
388 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
389 /* reload flags at third row (iteration pixel_y == 2) */
391 color_flags = color_flags_b;
394 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
395 pixel = color_table_index +
396 ((color_flags >> flag_mask) & 0x07);
398 pixels[block_ptr++] = s->color_octets[pixel];
400 block_ptr += row_inc;
406 /* 16-color block encoding (every pixel is a different color) */
408 n_blocks = (opcode & 0x0F) + 1;
411 block_ptr = row_ptr + pixel_ptr;
412 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
413 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
414 pixels[block_ptr++] = s->buf[stream_ptr++];
416 block_ptr += row_inc;
423 av_log(s->avctx, AV_LOG_INFO, "0xF0 opcode seen in SMC chunk (contact the developers)\n");
429 static int smc_decode_init(AVCodecContext *avctx)
431 SmcContext *s = avctx->priv_data;
434 avctx->pix_fmt = PIX_FMT_PAL8;
436 s->frame.data[0] = NULL;
441 static int smc_decode_frame(AVCodecContext *avctx,
442 void *data, int *data_size,
443 const uint8_t *buf, int buf_size)
445 SmcContext *s = avctx->priv_data;
450 s->frame.reference = 1;
451 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
452 FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
453 if (avctx->reget_buffer(avctx, &s->frame)) {
454 av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
458 smc_decode_stream(s);
460 *data_size = sizeof(AVFrame);
461 *(AVFrame*)data = s->frame;
463 /* always report that the buffer was completely consumed */
467 static int smc_decode_end(AVCodecContext *avctx)
469 SmcContext *s = avctx->priv_data;
471 if (s->frame.data[0])
472 avctx->release_buffer(avctx, &s->frame);
477 AVCodec smc_decoder = {