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"
44 #define COLORS_PER_TABLE 256
46 typedef struct SmcContext {
48 AVCodecContext *avctx;
53 /* SMC color tables */
54 unsigned char color_pairs[COLORS_PER_TABLE * CPAIR];
55 unsigned char color_quads[COLORS_PER_TABLE * CQUAD];
56 unsigned char color_octets[COLORS_PER_TABLE * COCTET];
61 #define GET_BLOCK_COUNT() \
62 (opcode & 0x10) ? (1 + bytestream2_get_byte(&s->gb)) : 1 + (opcode & 0x0F);
64 #define ADVANCE_BLOCK() \
67 if (pixel_ptr >= width) \
70 row_ptr += stride * 4; \
73 if (total_blocks < !!n_blocks) \
75 av_log(s->avctx, AV_LOG_INFO, "warning: block counter just went negative (this should not happen)\n"); \
80 static void smc_decode_stream(SmcContext *s)
82 int width = s->avctx->width;
83 int height = s->avctx->height;
84 int stride = s->frame->linesize[0];
87 int buf_size = bytestream2_size(&s->gb);
90 unsigned int color_flags;
91 unsigned int color_flags_a;
92 unsigned int color_flags_b;
93 unsigned int flag_mask;
95 unsigned char * const pixels = s->frame->data[0];
97 int image_size = height * s->frame->linesize[0];
100 int pixel_x, pixel_y;
101 int row_inc = stride - 4;
104 int prev_block_ptr1, prev_block_ptr2;
107 int color_table_index; /* indexes to color pair, quad, or octet tables */
110 int color_pair_index = 0;
111 int color_quad_index = 0;
112 int color_octet_index = 0;
114 /* make the palette available */
115 memcpy(s->frame->data[1], s->pal, AVPALETTE_SIZE);
117 bytestream2_skip(&s->gb, 1);
118 chunk_size = bytestream2_get_be24(&s->gb);
119 if (chunk_size != buf_size)
120 av_log(s->avctx, AV_LOG_INFO, "warning: MOV chunk size != encoded chunk size (%d != %d); using MOV chunk size\n",
121 chunk_size, buf_size);
123 chunk_size = buf_size;
124 total_blocks = ((s->avctx->width + 3) / 4) * ((s->avctx->height + 3) / 4);
126 /* traverse through the blocks */
127 while (total_blocks) {
129 /* make sure the row pointer hasn't gone wild */
130 if (row_ptr >= image_size) {
131 av_log(s->avctx, AV_LOG_INFO, "SMC decoder just went out of bounds (row ptr = %d, height = %d)\n",
132 row_ptr, image_size);
135 if (bytestream2_get_bytes_left(&s->gb) < 1) {
136 av_log(s->avctx, AV_LOG_ERROR, "input too small\n");
140 opcode = bytestream2_get_byte(&s->gb);
141 switch (opcode & 0xF0) {
145 n_blocks = GET_BLOCK_COUNT();
151 /* repeat last block n times */
154 n_blocks = GET_BLOCK_COUNT();
157 if ((row_ptr == 0) && (pixel_ptr == 0)) {
158 av_log(s->avctx, AV_LOG_INFO, "encountered repeat block opcode (%02X) but no blocks rendered yet\n",
163 /* figure out where the previous block started */
166 (row_ptr - s->avctx->width * 4) + s->avctx->width - 4;
168 prev_block_ptr1 = row_ptr + pixel_ptr - 4;
171 block_ptr = row_ptr + pixel_ptr;
172 prev_block_ptr = prev_block_ptr1;
173 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
174 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
175 pixels[block_ptr++] = pixels[prev_block_ptr++];
177 block_ptr += row_inc;
178 prev_block_ptr += row_inc;
184 /* repeat previous pair of blocks n times */
187 n_blocks = GET_BLOCK_COUNT();
191 if ((row_ptr == 0) && (pixel_ptr < 2 * 4)) {
192 av_log(s->avctx, AV_LOG_INFO, "encountered repeat block opcode (%02X) but not enough blocks rendered yet\n",
197 /* figure out where the previous 2 blocks started */
199 prev_block_ptr1 = (row_ptr - s->avctx->width * 4) +
200 s->avctx->width - 4 * 2;
201 else if (pixel_ptr == 4)
202 prev_block_ptr1 = (row_ptr - s->avctx->width * 4) + row_inc;
204 prev_block_ptr1 = row_ptr + pixel_ptr - 4 * 2;
207 prev_block_ptr2 = (row_ptr - s->avctx->width * 4) + row_inc;
209 prev_block_ptr2 = row_ptr + pixel_ptr - 4;
213 block_ptr = row_ptr + pixel_ptr;
215 prev_block_ptr = prev_block_ptr2;
217 prev_block_ptr = prev_block_ptr1;
218 prev_block_flag = !prev_block_flag;
220 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
221 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
222 pixels[block_ptr++] = pixels[prev_block_ptr++];
224 block_ptr += row_inc;
225 prev_block_ptr += row_inc;
231 /* 1-color block encoding */
234 n_blocks = GET_BLOCK_COUNT();
235 pixel = bytestream2_get_byte(&s->gb);
238 block_ptr = row_ptr + pixel_ptr;
239 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
240 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
241 pixels[block_ptr++] = pixel;
243 block_ptr += row_inc;
249 /* 2-color block encoding */
252 n_blocks = (opcode & 0x0F) + 1;
254 /* figure out which color pair to use to paint the 2-color block */
255 if ((opcode & 0xF0) == 0x80) {
256 /* fetch the next 2 colors from bytestream and store in next
257 * available entry in the color pair table */
258 for (i = 0; i < CPAIR; i++) {
259 pixel = bytestream2_get_byte(&s->gb);
260 color_table_index = CPAIR * color_pair_index + i;
261 s->color_pairs[color_table_index] = pixel;
263 /* this is the base index to use for this block */
264 color_table_index = CPAIR * color_pair_index;
267 if (color_pair_index == COLORS_PER_TABLE)
268 color_pair_index = 0;
270 color_table_index = CPAIR * bytestream2_get_byte(&s->gb);
273 color_flags = bytestream2_get_be16(&s->gb);
275 block_ptr = row_ptr + pixel_ptr;
276 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
277 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
278 if (color_flags & flag_mask)
279 pixel = color_table_index + 1;
281 pixel = color_table_index;
283 pixels[block_ptr++] = s->color_pairs[pixel];
285 block_ptr += row_inc;
291 /* 4-color block encoding */
294 n_blocks = (opcode & 0x0F) + 1;
296 /* figure out which color quad to use to paint the 4-color block */
297 if ((opcode & 0xF0) == 0xA0) {
298 /* fetch the next 4 colors from bytestream and store in next
299 * available entry in the color quad table */
300 for (i = 0; i < CQUAD; i++) {
301 pixel = bytestream2_get_byte(&s->gb);
302 color_table_index = CQUAD * color_quad_index + i;
303 s->color_quads[color_table_index] = pixel;
305 /* this is the base index to use for this block */
306 color_table_index = CQUAD * color_quad_index;
309 if (color_quad_index == COLORS_PER_TABLE)
310 color_quad_index = 0;
312 color_table_index = CQUAD * bytestream2_get_byte(&s->gb);
315 color_flags = bytestream2_get_be32(&s->gb);
316 /* flag mask actually acts as a bit shift count here */
318 block_ptr = row_ptr + pixel_ptr;
319 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
320 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
321 pixel = color_table_index +
322 ((color_flags >> flag_mask) & 0x03);
324 pixels[block_ptr++] = s->color_quads[pixel];
326 block_ptr += row_inc;
332 /* 8-color block encoding */
335 n_blocks = (opcode & 0x0F) + 1;
337 /* figure out which color octet to use to paint the 8-color block */
338 if ((opcode & 0xF0) == 0xC0) {
339 /* fetch the next 8 colors from bytestream and store in next
340 * available entry in the color octet table */
341 for (i = 0; i < COCTET; i++) {
342 pixel = bytestream2_get_byte(&s->gb);
343 color_table_index = COCTET * color_octet_index + i;
344 s->color_octets[color_table_index] = pixel;
346 /* this is the base index to use for this block */
347 color_table_index = COCTET * color_octet_index;
350 if (color_octet_index == COLORS_PER_TABLE)
351 color_octet_index = 0;
353 color_table_index = COCTET * bytestream2_get_byte(&s->gb);
357 For this input of 6 hex bytes:
359 Mangle it to this output:
360 flags_a = xx012456, flags_b = xx89A37B
362 /* build the color flags */
363 int val1 = bytestream2_get_be16(&s->gb);
364 int val2 = bytestream2_get_be16(&s->gb);
365 int val3 = bytestream2_get_be16(&s->gb);
366 color_flags_a = ((val1 & 0xFFF0) << 8) | (val2 >> 4);
367 color_flags_b = ((val3 & 0xFFF0) << 8) |
368 ((val1 & 0x0F) << 8) | ((val2 & 0x0F) << 4) | (val3 & 0x0F);
370 color_flags = color_flags_a;
371 /* flag mask actually acts as a bit shift count here */
373 block_ptr = row_ptr + pixel_ptr;
374 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
375 /* reload flags at third row (iteration pixel_y == 2) */
377 color_flags = color_flags_b;
380 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
381 pixel = color_table_index +
382 ((color_flags >> flag_mask) & 0x07);
384 pixels[block_ptr++] = s->color_octets[pixel];
386 block_ptr += row_inc;
392 /* 16-color block encoding (every pixel is a different color) */
394 n_blocks = (opcode & 0x0F) + 1;
397 block_ptr = row_ptr + pixel_ptr;
398 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
399 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
400 pixels[block_ptr++] = bytestream2_get_byte(&s->gb);
402 block_ptr += row_inc;
409 avpriv_request_sample(s->avctx, "0xF0 opcode");
417 static av_cold int smc_decode_init(AVCodecContext *avctx)
419 SmcContext *s = avctx->priv_data;
422 avctx->pix_fmt = AV_PIX_FMT_PAL8;
424 s->frame = av_frame_alloc();
426 return AVERROR(ENOMEM);
431 static int smc_decode_frame(AVCodecContext *avctx,
432 void *data, int *got_frame,
435 const uint8_t *buf = avpkt->data;
436 int buf_size = avpkt->size;
437 SmcContext *s = avctx->priv_data;
439 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &pal_size);
442 bytestream2_init(&s->gb, buf, buf_size);
444 if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
447 if (pal && pal_size == AVPALETTE_SIZE) {
448 s->frame->palette_has_changed = 1;
449 memcpy(s->pal, pal, AVPALETTE_SIZE);
451 av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", pal_size);
454 smc_decode_stream(s);
457 if ((ret = av_frame_ref(data, s->frame)) < 0)
460 /* always report that the buffer was completely consumed */
464 static av_cold int smc_decode_end(AVCodecContext *avctx)
466 SmcContext *s = avctx->priv_data;
468 av_frame_free(&s->frame);
473 AVCodec ff_smc_decoder = {
475 .long_name = NULL_IF_CONFIG_SMALL("QuickTime Graphics (SMC)"),
476 .type = AVMEDIA_TYPE_VIDEO,
477 .id = AV_CODEC_ID_SMC,
478 .priv_data_size = sizeof(SmcContext),
479 .init = smc_decode_init,
480 .close = smc_decode_end,
481 .decode = smc_decode_frame,
482 .capabilities = AV_CODEC_CAP_DR1,