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"
45 #define COLORS_PER_TABLE 256
47 typedef struct SmcContext {
49 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];
62 #define GET_BLOCK_COUNT() \
63 (opcode & 0x10) ? (1 + bytestream2_get_byte(&s->gb)) : 1 + (opcode & 0x0F);
65 #define ADVANCE_BLOCK() \
68 if (pixel_ptr >= width) \
71 row_ptr += stride * 4; \
74 if (total_blocks < !!n_blocks) \
76 av_log(s->avctx, AV_LOG_INFO, "warning: block counter just went negative (this should not happen)\n"); \
81 static void smc_decode_stream(SmcContext *s)
83 int width = s->avctx->width;
84 int height = s->avctx->height;
85 int stride = s->frame->linesize[0];
88 int buf_size = bytestream2_size(&s->gb);
91 unsigned int color_flags;
92 unsigned int color_flags_a;
93 unsigned int color_flags_b;
94 unsigned int flag_mask;
96 unsigned char * const pixels = s->frame->data[0];
98 int image_size = height * s->frame->linesize[0];
101 int pixel_x, pixel_y;
102 int row_inc = stride - 4;
105 int prev_block_ptr1, prev_block_ptr2;
108 int color_table_index; /* indexes to color pair, quad, or octet tables */
111 int color_pair_index = 0;
112 int color_quad_index = 0;
113 int color_octet_index = 0;
115 /* make the palette available */
116 memcpy(s->frame->data[1], s->pal, AVPALETTE_SIZE);
118 bytestream2_skip(&s->gb, 1);
119 chunk_size = bytestream2_get_be24(&s->gb);
120 if (chunk_size != buf_size)
121 av_log(s->avctx, AV_LOG_INFO, "warning: MOV chunk size != encoded chunk size (%d != %d); using MOV chunk size\n",
122 chunk_size, buf_size);
124 chunk_size = buf_size;
125 total_blocks = ((s->avctx->width + 3) / 4) * ((s->avctx->height + 3) / 4);
127 /* traverse through the blocks */
128 while (total_blocks) {
130 /* make sure the row pointer hasn't gone wild */
131 if (row_ptr >= image_size) {
132 av_log(s->avctx, AV_LOG_INFO, "SMC decoder just went out of bounds (row ptr = %d, height = %d)\n",
133 row_ptr, image_size);
136 if (bytestream2_get_bytes_left(&s->gb) < 1) {
137 av_log(s->avctx, AV_LOG_ERROR, "input too small\n");
141 opcode = bytestream2_get_byte(&s->gb);
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 = bytestream2_get_byte(&s->gb);
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 = bytestream2_get_byte(&s->gb);
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 * bytestream2_get_byte(&s->gb);
274 color_flags = bytestream2_get_be16(&s->gb);
276 block_ptr = row_ptr + pixel_ptr;
277 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
278 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
279 if (color_flags & flag_mask)
280 pixel = color_table_index + 1;
282 pixel = color_table_index;
284 pixels[block_ptr++] = s->color_pairs[pixel];
286 block_ptr += row_inc;
292 /* 4-color block encoding */
295 n_blocks = (opcode & 0x0F) + 1;
297 /* figure out which color quad to use to paint the 4-color block */
298 if ((opcode & 0xF0) == 0xA0) {
299 /* fetch the next 4 colors from bytestream and store in next
300 * available entry in the color quad table */
301 for (i = 0; i < CQUAD; i++) {
302 pixel = bytestream2_get_byte(&s->gb);
303 color_table_index = CQUAD * color_quad_index + i;
304 s->color_quads[color_table_index] = pixel;
306 /* this is the base index to use for this block */
307 color_table_index = CQUAD * color_quad_index;
310 if (color_quad_index == COLORS_PER_TABLE)
311 color_quad_index = 0;
313 color_table_index = CQUAD * bytestream2_get_byte(&s->gb);
316 color_flags = bytestream2_get_be32(&s->gb);
317 /* flag mask actually acts as a bit shift count here */
319 block_ptr = row_ptr + pixel_ptr;
320 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
321 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
322 pixel = color_table_index +
323 ((color_flags >> flag_mask) & 0x03);
325 pixels[block_ptr++] = s->color_quads[pixel];
327 block_ptr += row_inc;
333 /* 8-color block encoding */
336 n_blocks = (opcode & 0x0F) + 1;
338 /* figure out which color octet to use to paint the 8-color block */
339 if ((opcode & 0xF0) == 0xC0) {
340 /* fetch the next 8 colors from bytestream and store in next
341 * available entry in the color octet table */
342 for (i = 0; i < COCTET; i++) {
343 pixel = bytestream2_get_byte(&s->gb);
344 color_table_index = COCTET * color_octet_index + i;
345 s->color_octets[color_table_index] = pixel;
347 /* this is the base index to use for this block */
348 color_table_index = COCTET * color_octet_index;
351 if (color_octet_index == COLORS_PER_TABLE)
352 color_octet_index = 0;
354 color_table_index = COCTET * bytestream2_get_byte(&s->gb);
358 For this input of 6 hex bytes:
360 Mangle it to this output:
361 flags_a = xx012456, flags_b = xx89A37B
363 /* build the color flags */
364 int val1 = bytestream2_get_be16(&s->gb);
365 int val2 = bytestream2_get_be16(&s->gb);
366 int val3 = bytestream2_get_be16(&s->gb);
367 color_flags_a = ((val1 & 0xFFF0) << 8) | (val2 >> 4);
368 color_flags_b = ((val3 & 0xFFF0) << 8) |
369 ((val1 & 0x0F) << 8) | ((val2 & 0x0F) << 4) | (val3 & 0x0F);
371 color_flags = color_flags_a;
372 /* flag mask actually acts as a bit shift count here */
374 block_ptr = row_ptr + pixel_ptr;
375 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
376 /* reload flags at third row (iteration pixel_y == 2) */
378 color_flags = color_flags_b;
381 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
382 pixel = color_table_index +
383 ((color_flags >> flag_mask) & 0x07);
385 pixels[block_ptr++] = s->color_octets[pixel];
387 block_ptr += row_inc;
393 /* 16-color block encoding (every pixel is a different color) */
395 n_blocks = (opcode & 0x0F) + 1;
398 block_ptr = row_ptr + pixel_ptr;
399 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
400 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
401 pixels[block_ptr++] = bytestream2_get_byte(&s->gb);
403 block_ptr += row_inc;
410 avpriv_request_sample(s->avctx, "0xF0 opcode");
418 static av_cold int smc_decode_init(AVCodecContext *avctx)
420 SmcContext *s = avctx->priv_data;
423 avctx->pix_fmt = AV_PIX_FMT_PAL8;
425 s->frame = av_frame_alloc();
427 return AVERROR(ENOMEM);
432 static int smc_decode_frame(AVCodecContext *avctx,
433 void *data, int *got_frame,
436 const uint8_t *buf = avpkt->data;
437 int buf_size = avpkt->size;
438 SmcContext *s = avctx->priv_data;
440 int total_blocks = ((s->avctx->width + 3) / 4) * ((s->avctx->height + 3) / 4);
442 if (total_blocks / 1024 > avpkt->size)
443 return AVERROR_INVALIDDATA;
445 bytestream2_init(&s->gb, buf, buf_size);
447 if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
450 s->frame->palette_has_changed = ff_copy_palette(s->pal, avpkt, avctx);
452 smc_decode_stream(s);
455 if ((ret = av_frame_ref(data, s->frame)) < 0)
458 /* always report that the buffer was completely consumed */
462 static av_cold int smc_decode_end(AVCodecContext *avctx)
464 SmcContext *s = avctx->priv_data;
466 av_frame_free(&s->frame);
471 AVCodec ff_smc_decoder = {
473 .long_name = NULL_IF_CONFIG_SMALL("QuickTime Graphics (SMC)"),
474 .type = AVMEDIA_TYPE_VIDEO,
475 .id = AV_CODEC_ID_SMC,
476 .priv_data_size = sizeof(SmcContext),
477 .init = smc_decode_init,
478 .close = smc_decode_end,
479 .decode = smc_decode_frame,
480 .capabilities = AV_CODEC_CAP_DR1,
481 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,