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.
35 #include "libavutil/intreadwrite.h"
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];
60 #define GET_BLOCK_COUNT() \
61 (opcode & 0x10) ? (1 + s->buf[stream_ptr++]) : 1 + (opcode & 0x0F);
63 #define ADVANCE_BLOCK() \
66 if (pixel_ptr >= width) \
69 row_ptr += stride * 4; \
72 if (total_blocks < 0) \
74 av_log(s->avctx, AV_LOG_INFO, "warning: block counter just went negative (this should not happen)\n"); \
79 static void smc_decode_stream(SmcContext *s)
81 int width = s->avctx->width;
82 int height = s->avctx->height;
83 int stride = s->frame.linesize[0];
89 unsigned int color_flags;
90 unsigned int color_flags_a;
91 unsigned int color_flags_b;
92 unsigned int flag_mask;
94 unsigned char *pixels = s->frame.data[0];
96 int image_size = height * s->frame.linesize[0];
100 int row_inc = stride - 4;
103 int prev_block_ptr1, prev_block_ptr2;
106 int color_table_index; /* indexes to color pair, quad, or octet tables */
109 int color_pair_index = 0;
110 int color_quad_index = 0;
111 int color_octet_index = 0;
113 /* make the palette available */
114 memcpy(s->frame.data[1], s->pal, AVPALETTE_SIZE);
116 chunk_size = AV_RB32(&s->buf[stream_ptr]) & 0x00FFFFFF;
118 if (chunk_size != s->size)
119 av_log(s->avctx, AV_LOG_INFO, "warning: MOV chunk size != encoded chunk size (%d != %d); using MOV chunk size\n",
120 chunk_size, s->size);
122 chunk_size = s->size;
123 total_blocks = ((s->avctx->width + 3) / 4) * ((s->avctx->height + 3) / 4);
125 /* traverse through the blocks */
126 while (total_blocks) {
128 /* make sure stream ptr hasn't gone out of bounds */
129 if (stream_ptr > chunk_size) {
130 av_log(s->avctx, AV_LOG_INFO, "SMC decoder just went out of bounds (stream ptr = %d, chunk size = %d)\n",
131 stream_ptr, chunk_size);
134 /* make sure the row pointer hasn't gone wild */
135 if (row_ptr >= image_size) {
136 av_log(s->avctx, AV_LOG_INFO, "SMC decoder just went out of bounds (row ptr = %d, height = %d)\n",
137 row_ptr, image_size);
141 opcode = s->buf[stream_ptr++];
142 switch (opcode & 0xF0) {
146 n_blocks = GET_BLOCK_COUNT();
152 /* repeat last block n times */
155 n_blocks = GET_BLOCK_COUNT();
158 if ((row_ptr == 0) && (pixel_ptr == 0)) {
159 av_log(s->avctx, AV_LOG_INFO, "encountered repeat block opcode (%02X) but no blocks rendered yet\n",
164 /* figure out where the previous block started */
167 (row_ptr - s->avctx->width * 4) + s->avctx->width - 4;
169 prev_block_ptr1 = row_ptr + pixel_ptr - 4;
172 block_ptr = row_ptr + pixel_ptr;
173 prev_block_ptr = prev_block_ptr1;
174 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
175 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
176 pixels[block_ptr++] = pixels[prev_block_ptr++];
178 block_ptr += row_inc;
179 prev_block_ptr += row_inc;
185 /* repeat previous pair of blocks n times */
188 n_blocks = GET_BLOCK_COUNT();
192 if ((row_ptr == 0) && (pixel_ptr < 2 * 4)) {
193 av_log(s->avctx, AV_LOG_INFO, "encountered repeat block opcode (%02X) but not enough blocks rendered yet\n",
198 /* figure out where the previous 2 blocks started */
200 prev_block_ptr1 = (row_ptr - s->avctx->width * 4) +
201 s->avctx->width - 4 * 2;
202 else if (pixel_ptr == 4)
203 prev_block_ptr1 = (row_ptr - s->avctx->width * 4) + row_inc;
205 prev_block_ptr1 = row_ptr + pixel_ptr - 4 * 2;
208 prev_block_ptr2 = (row_ptr - s->avctx->width * 4) + row_inc;
210 prev_block_ptr2 = row_ptr + pixel_ptr - 4;
214 block_ptr = row_ptr + pixel_ptr;
216 prev_block_ptr = prev_block_ptr2;
218 prev_block_ptr = prev_block_ptr1;
219 prev_block_flag = !prev_block_flag;
221 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
222 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
223 pixels[block_ptr++] = pixels[prev_block_ptr++];
225 block_ptr += row_inc;
226 prev_block_ptr += row_inc;
232 /* 1-color block encoding */
235 n_blocks = GET_BLOCK_COUNT();
236 pixel = s->buf[stream_ptr++];
239 block_ptr = row_ptr + pixel_ptr;
240 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
241 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
242 pixels[block_ptr++] = pixel;
244 block_ptr += row_inc;
250 /* 2-color block encoding */
253 n_blocks = (opcode & 0x0F) + 1;
255 /* figure out which color pair to use to paint the 2-color block */
256 if ((opcode & 0xF0) == 0x80) {
257 /* fetch the next 2 colors from bytestream and store in next
258 * available entry in the color pair table */
259 for (i = 0; i < CPAIR; i++) {
260 pixel = s->buf[stream_ptr++];
261 color_table_index = CPAIR * color_pair_index + i;
262 s->color_pairs[color_table_index] = pixel;
264 /* this is the base index to use for this block */
265 color_table_index = CPAIR * color_pair_index;
268 if (color_pair_index == COLORS_PER_TABLE)
269 color_pair_index = 0;
271 color_table_index = CPAIR * s->buf[stream_ptr++];
274 color_flags = AV_RB16(&s->buf[stream_ptr]);
277 block_ptr = row_ptr + pixel_ptr;
278 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
279 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
280 if (color_flags & flag_mask)
281 pixel = color_table_index + 1;
283 pixel = color_table_index;
285 pixels[block_ptr++] = s->color_pairs[pixel];
287 block_ptr += row_inc;
293 /* 4-color block encoding */
296 n_blocks = (opcode & 0x0F) + 1;
298 /* figure out which color quad to use to paint the 4-color block */
299 if ((opcode & 0xF0) == 0xA0) {
300 /* fetch the next 4 colors from bytestream and store in next
301 * available entry in the color quad table */
302 for (i = 0; i < CQUAD; i++) {
303 pixel = s->buf[stream_ptr++];
304 color_table_index = CQUAD * color_quad_index + i;
305 s->color_quads[color_table_index] = pixel;
307 /* this is the base index to use for this block */
308 color_table_index = CQUAD * color_quad_index;
311 if (color_quad_index == COLORS_PER_TABLE)
312 color_quad_index = 0;
314 color_table_index = CQUAD * s->buf[stream_ptr++];
317 color_flags = AV_RB32(&s->buf[stream_ptr]);
319 /* flag mask actually acts as a bit shift count here */
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;
335 /* 8-color block encoding */
338 n_blocks = (opcode & 0x0F) + 1;
340 /* figure out which color octet to use to paint the 8-color block */
341 if ((opcode & 0xF0) == 0xC0) {
342 /* fetch the next 8 colors from bytestream and store in next
343 * available entry in the color octet table */
344 for (i = 0; i < COCTET; i++) {
345 pixel = s->buf[stream_ptr++];
346 color_table_index = COCTET * color_octet_index + i;
347 s->color_octets[color_table_index] = pixel;
349 /* this is the base index to use for this block */
350 color_table_index = COCTET * color_octet_index;
353 if (color_octet_index == COLORS_PER_TABLE)
354 color_octet_index = 0;
356 color_table_index = COCTET * s->buf[stream_ptr++];
360 For this input of 6 hex bytes:
362 Mangle it to this output:
363 flags_a = xx012456, flags_b = xx89A37B
365 /* build the color flags */
367 ((AV_RB16(s->buf + stream_ptr ) & 0xFFF0) << 8) |
368 (AV_RB16(s->buf + stream_ptr + 2) >> 4);
370 ((AV_RB16(s->buf + stream_ptr + 4) & 0xFFF0) << 8) |
371 ((s->buf[stream_ptr + 1] & 0x0F) << 8) |
372 ((s->buf[stream_ptr + 3] & 0x0F) << 4) |
373 (s->buf[stream_ptr + 5] & 0x0F);
376 color_flags = color_flags_a;
377 /* flag mask actually acts as a bit shift count here */
379 block_ptr = row_ptr + pixel_ptr;
380 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
381 /* reload flags at third row (iteration pixel_y == 2) */
383 color_flags = color_flags_b;
386 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
387 pixel = color_table_index +
388 ((color_flags >> flag_mask) & 0x07);
390 pixels[block_ptr++] = s->color_octets[pixel];
392 block_ptr += row_inc;
398 /* 16-color block encoding (every pixel is a different color) */
400 n_blocks = (opcode & 0x0F) + 1;
403 block_ptr = row_ptr + pixel_ptr;
404 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
405 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
406 pixels[block_ptr++] = s->buf[stream_ptr++];
408 block_ptr += row_inc;
415 av_log(s->avctx, AV_LOG_INFO, "0xF0 opcode seen in SMC chunk (contact the developers)\n");
421 static av_cold int smc_decode_init(AVCodecContext *avctx)
423 SmcContext *s = avctx->priv_data;
426 avctx->pix_fmt = PIX_FMT_PAL8;
428 avcodec_get_frame_defaults(&s->frame);
429 s->frame.data[0] = NULL;
434 static int smc_decode_frame(AVCodecContext *avctx,
435 void *data, int *data_size,
438 const uint8_t *buf = avpkt->data;
439 int buf_size = avpkt->size;
440 SmcContext *s = avctx->priv_data;
441 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
446 s->frame.reference = 1;
447 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
448 FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
449 if (avctx->reget_buffer(avctx, &s->frame)) {
450 av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
455 s->frame.palette_has_changed = 1;
456 memcpy(s->pal, pal, AVPALETTE_SIZE);
459 smc_decode_stream(s);
461 *data_size = sizeof(AVFrame);
462 *(AVFrame*)data = s->frame;
464 /* always report that the buffer was completely consumed */
468 static av_cold int smc_decode_end(AVCodecContext *avctx)
470 SmcContext *s = avctx->priv_data;
472 if (s->frame.data[0])
473 avctx->release_buffer(avctx, &s->frame);
478 AVCodec ff_smc_decoder = {
480 .type = AVMEDIA_TYPE_VIDEO,
482 .priv_data_size = sizeof(SmcContext),
483 .init = smc_decode_init,
484 .close = smc_decode_end,
485 .decode = smc_decode_frame,
486 .capabilities = CODEC_CAP_DR1,
487 .long_name = NULL_IF_CONFIG_SMALL("QuickTime Graphics (SMC)"),