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"
37 #include "bytestream.h"
43 #define COLORS_PER_TABLE 256
45 typedef struct SmcContext {
47 AVCodecContext *avctx;
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 + bytestream2_get_byte(&s->gb)) : 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];
86 int buf_size = bytestream2_size(&s->gb);
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 bytestream2_skip(&s->gb, 1);
117 chunk_size = bytestream2_get_be24(&s->gb);
118 if (chunk_size != buf_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, buf_size);
122 chunk_size = buf_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 the row pointer hasn't gone wild */
129 if (row_ptr >= image_size) {
130 av_log(s->avctx, AV_LOG_INFO, "SMC decoder just went out of bounds (row ptr = %d, height = %d)\n",
131 row_ptr, image_size);
135 opcode = bytestream2_get_byte(&s->gb);
136 switch (opcode & 0xF0) {
140 n_blocks = GET_BLOCK_COUNT();
146 /* repeat last block n times */
149 n_blocks = GET_BLOCK_COUNT();
152 if ((row_ptr == 0) && (pixel_ptr == 0)) {
153 av_log(s->avctx, AV_LOG_INFO, "encountered repeat block opcode (%02X) but no blocks rendered yet\n",
158 /* figure out where the previous block started */
161 (row_ptr - s->avctx->width * 4) + s->avctx->width - 4;
163 prev_block_ptr1 = row_ptr + pixel_ptr - 4;
166 block_ptr = row_ptr + pixel_ptr;
167 prev_block_ptr = prev_block_ptr1;
168 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
169 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
170 pixels[block_ptr++] = pixels[prev_block_ptr++];
172 block_ptr += row_inc;
173 prev_block_ptr += row_inc;
179 /* repeat previous pair of blocks n times */
182 n_blocks = GET_BLOCK_COUNT();
186 if ((row_ptr == 0) && (pixel_ptr < 2 * 4)) {
187 av_log(s->avctx, AV_LOG_INFO, "encountered repeat block opcode (%02X) but not enough blocks rendered yet\n",
192 /* figure out where the previous 2 blocks started */
194 prev_block_ptr1 = (row_ptr - s->avctx->width * 4) +
195 s->avctx->width - 4 * 2;
196 else if (pixel_ptr == 4)
197 prev_block_ptr1 = (row_ptr - s->avctx->width * 4) + row_inc;
199 prev_block_ptr1 = row_ptr + pixel_ptr - 4 * 2;
202 prev_block_ptr2 = (row_ptr - s->avctx->width * 4) + row_inc;
204 prev_block_ptr2 = row_ptr + pixel_ptr - 4;
208 block_ptr = row_ptr + pixel_ptr;
210 prev_block_ptr = prev_block_ptr2;
212 prev_block_ptr = prev_block_ptr1;
213 prev_block_flag = !prev_block_flag;
215 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
216 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
217 pixels[block_ptr++] = pixels[prev_block_ptr++];
219 block_ptr += row_inc;
220 prev_block_ptr += row_inc;
226 /* 1-color block encoding */
229 n_blocks = GET_BLOCK_COUNT();
230 pixel = bytestream2_get_byte(&s->gb);
233 block_ptr = row_ptr + pixel_ptr;
234 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
235 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
236 pixels[block_ptr++] = pixel;
238 block_ptr += row_inc;
244 /* 2-color block encoding */
247 n_blocks = (opcode & 0x0F) + 1;
249 /* figure out which color pair to use to paint the 2-color block */
250 if ((opcode & 0xF0) == 0x80) {
251 /* fetch the next 2 colors from bytestream and store in next
252 * available entry in the color pair table */
253 for (i = 0; i < CPAIR; i++) {
254 pixel = bytestream2_get_byte(&s->gb);
255 color_table_index = CPAIR * color_pair_index + i;
256 s->color_pairs[color_table_index] = pixel;
258 /* this is the base index to use for this block */
259 color_table_index = CPAIR * color_pair_index;
262 if (color_pair_index == COLORS_PER_TABLE)
263 color_pair_index = 0;
265 color_table_index = CPAIR * bytestream2_get_byte(&s->gb);
268 color_flags = bytestream2_get_be16(&s->gb);
270 block_ptr = row_ptr + pixel_ptr;
271 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
272 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
273 if (color_flags & flag_mask)
274 pixel = color_table_index + 1;
276 pixel = color_table_index;
278 pixels[block_ptr++] = s->color_pairs[pixel];
280 block_ptr += row_inc;
286 /* 4-color block encoding */
289 n_blocks = (opcode & 0x0F) + 1;
291 /* figure out which color quad to use to paint the 4-color block */
292 if ((opcode & 0xF0) == 0xA0) {
293 /* fetch the next 4 colors from bytestream and store in next
294 * available entry in the color quad table */
295 for (i = 0; i < CQUAD; i++) {
296 pixel = bytestream2_get_byte(&s->gb);
297 color_table_index = CQUAD * color_quad_index + i;
298 s->color_quads[color_table_index] = pixel;
300 /* this is the base index to use for this block */
301 color_table_index = CQUAD * color_quad_index;
304 if (color_quad_index == COLORS_PER_TABLE)
305 color_quad_index = 0;
307 color_table_index = CQUAD * bytestream2_get_byte(&s->gb);
310 color_flags = bytestream2_get_be32(&s->gb);
311 /* flag mask actually acts as a bit shift count here */
313 block_ptr = row_ptr + pixel_ptr;
314 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
315 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
316 pixel = color_table_index +
317 ((color_flags >> flag_mask) & 0x03);
319 pixels[block_ptr++] = s->color_quads[pixel];
321 block_ptr += row_inc;
327 /* 8-color block encoding */
330 n_blocks = (opcode & 0x0F) + 1;
332 /* figure out which color octet to use to paint the 8-color block */
333 if ((opcode & 0xF0) == 0xC0) {
334 /* fetch the next 8 colors from bytestream and store in next
335 * available entry in the color octet table */
336 for (i = 0; i < COCTET; i++) {
337 pixel = bytestream2_get_byte(&s->gb);
338 color_table_index = COCTET * color_octet_index + i;
339 s->color_octets[color_table_index] = pixel;
341 /* this is the base index to use for this block */
342 color_table_index = COCTET * color_octet_index;
345 if (color_octet_index == COLORS_PER_TABLE)
346 color_octet_index = 0;
348 color_table_index = COCTET * bytestream2_get_byte(&s->gb);
352 For this input of 6 hex bytes:
354 Mangle it to this output:
355 flags_a = xx012456, flags_b = xx89A37B
357 /* build the color flags */
358 int val1 = bytestream2_get_be16(&s->gb);
359 int val2 = bytestream2_get_be16(&s->gb);
360 int val3 = bytestream2_get_be16(&s->gb);
361 color_flags_a = ((val1 & 0xFFF0) << 8) | (val2 >> 4);
362 color_flags_b = ((val3 & 0xFFF0) << 8) |
363 ((val1 & 0x0F) << 8) | ((val2 & 0x0F) << 4) | (val3 & 0x0F);
365 color_flags = color_flags_a;
366 /* flag mask actually acts as a bit shift count here */
368 block_ptr = row_ptr + pixel_ptr;
369 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
370 /* reload flags at third row (iteration pixel_y == 2) */
372 color_flags = color_flags_b;
375 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
376 pixel = color_table_index +
377 ((color_flags >> flag_mask) & 0x07);
379 pixels[block_ptr++] = s->color_octets[pixel];
381 block_ptr += row_inc;
387 /* 16-color block encoding (every pixel is a different color) */
389 n_blocks = (opcode & 0x0F) + 1;
392 block_ptr = row_ptr + pixel_ptr;
393 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
394 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
395 pixels[block_ptr++] = bytestream2_get_byte(&s->gb);
397 block_ptr += row_inc;
404 av_log_missing_feature(s->avctx, "0xF0 opcode", 1);
412 static av_cold int smc_decode_init(AVCodecContext *avctx)
414 SmcContext *s = avctx->priv_data;
417 avctx->pix_fmt = PIX_FMT_PAL8;
419 avcodec_get_frame_defaults(&s->frame);
420 s->frame.data[0] = NULL;
425 static int smc_decode_frame(AVCodecContext *avctx,
426 void *data, int *data_size,
429 const uint8_t *buf = avpkt->data;
430 int buf_size = avpkt->size;
431 SmcContext *s = avctx->priv_data;
432 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
434 bytestream2_init(&s->gb, buf, buf_size);
436 s->frame.reference = 3;
437 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
438 FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
439 if (avctx->reget_buffer(avctx, &s->frame)) {
440 av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
445 s->frame.palette_has_changed = 1;
446 memcpy(s->pal, pal, AVPALETTE_SIZE);
449 smc_decode_stream(s);
451 *data_size = sizeof(AVFrame);
452 *(AVFrame*)data = s->frame;
454 /* always report that the buffer was completely consumed */
458 static av_cold int smc_decode_end(AVCodecContext *avctx)
460 SmcContext *s = avctx->priv_data;
462 if (s->frame.data[0])
463 avctx->release_buffer(avctx, &s->frame);
468 AVCodec ff_smc_decoder = {
470 .type = AVMEDIA_TYPE_VIDEO,
471 .id = AV_CODEC_ID_SMC,
472 .priv_data_size = sizeof(SmcContext),
473 .init = smc_decode_init,
474 .close = smc_decode_end,
475 .decode = smc_decode_frame,
476 .capabilities = CODEC_CAP_DR1,
477 .long_name = NULL_IF_CONFIG_SMALL("QuickTime Graphics (SMC)"),