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 *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);
136 opcode = bytestream2_get_byte(&s->gb);
137 switch (opcode & 0xF0) {
141 n_blocks = GET_BLOCK_COUNT();
147 /* repeat last block n times */
150 n_blocks = GET_BLOCK_COUNT();
153 if ((row_ptr == 0) && (pixel_ptr == 0)) {
154 av_log(s->avctx, AV_LOG_INFO, "encountered repeat block opcode (%02X) but no blocks rendered yet\n",
159 /* figure out where the previous block started */
162 (row_ptr - s->avctx->width * 4) + s->avctx->width - 4;
164 prev_block_ptr1 = row_ptr + pixel_ptr - 4;
167 block_ptr = row_ptr + pixel_ptr;
168 prev_block_ptr = prev_block_ptr1;
169 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
170 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
171 pixels[block_ptr++] = pixels[prev_block_ptr++];
173 block_ptr += row_inc;
174 prev_block_ptr += row_inc;
180 /* repeat previous pair of blocks n times */
183 n_blocks = GET_BLOCK_COUNT();
187 if ((row_ptr == 0) && (pixel_ptr < 2 * 4)) {
188 av_log(s->avctx, AV_LOG_INFO, "encountered repeat block opcode (%02X) but not enough blocks rendered yet\n",
193 /* figure out where the previous 2 blocks started */
195 prev_block_ptr1 = (row_ptr - s->avctx->width * 4) +
196 s->avctx->width - 4 * 2;
197 else if (pixel_ptr == 4)
198 prev_block_ptr1 = (row_ptr - s->avctx->width * 4) + row_inc;
200 prev_block_ptr1 = row_ptr + pixel_ptr - 4 * 2;
203 prev_block_ptr2 = (row_ptr - s->avctx->width * 4) + row_inc;
205 prev_block_ptr2 = row_ptr + pixel_ptr - 4;
209 block_ptr = row_ptr + pixel_ptr;
211 prev_block_ptr = prev_block_ptr2;
213 prev_block_ptr = prev_block_ptr1;
214 prev_block_flag = !prev_block_flag;
216 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
217 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
218 pixels[block_ptr++] = pixels[prev_block_ptr++];
220 block_ptr += row_inc;
221 prev_block_ptr += row_inc;
227 /* 1-color block encoding */
230 n_blocks = GET_BLOCK_COUNT();
231 pixel = bytestream2_get_byte(&s->gb);
234 block_ptr = row_ptr + pixel_ptr;
235 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
236 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
237 pixels[block_ptr++] = pixel;
239 block_ptr += row_inc;
245 /* 2-color block encoding */
248 n_blocks = (opcode & 0x0F) + 1;
250 /* figure out which color pair to use to paint the 2-color block */
251 if ((opcode & 0xF0) == 0x80) {
252 /* fetch the next 2 colors from bytestream and store in next
253 * available entry in the color pair table */
254 for (i = 0; i < CPAIR; i++) {
255 pixel = bytestream2_get_byte(&s->gb);
256 color_table_index = CPAIR * color_pair_index + i;
257 s->color_pairs[color_table_index] = pixel;
259 /* this is the base index to use for this block */
260 color_table_index = CPAIR * color_pair_index;
263 if (color_pair_index == COLORS_PER_TABLE)
264 color_pair_index = 0;
266 color_table_index = CPAIR * bytestream2_get_byte(&s->gb);
269 color_flags = bytestream2_get_be16(&s->gb);
271 block_ptr = row_ptr + pixel_ptr;
272 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
273 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
274 if (color_flags & flag_mask)
275 pixel = color_table_index + 1;
277 pixel = color_table_index;
279 pixels[block_ptr++] = s->color_pairs[pixel];
281 block_ptr += row_inc;
287 /* 4-color block encoding */
290 n_blocks = (opcode & 0x0F) + 1;
292 /* figure out which color quad to use to paint the 4-color block */
293 if ((opcode & 0xF0) == 0xA0) {
294 /* fetch the next 4 colors from bytestream and store in next
295 * available entry in the color quad table */
296 for (i = 0; i < CQUAD; i++) {
297 pixel = bytestream2_get_byte(&s->gb);
298 color_table_index = CQUAD * color_quad_index + i;
299 s->color_quads[color_table_index] = pixel;
301 /* this is the base index to use for this block */
302 color_table_index = CQUAD * color_quad_index;
305 if (color_quad_index == COLORS_PER_TABLE)
306 color_quad_index = 0;
308 color_table_index = CQUAD * bytestream2_get_byte(&s->gb);
311 color_flags = bytestream2_get_be32(&s->gb);
312 /* flag mask actually acts as a bit shift count here */
314 block_ptr = row_ptr + pixel_ptr;
315 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
316 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
317 pixel = color_table_index +
318 ((color_flags >> flag_mask) & 0x03);
320 pixels[block_ptr++] = s->color_quads[pixel];
322 block_ptr += row_inc;
328 /* 8-color block encoding */
331 n_blocks = (opcode & 0x0F) + 1;
333 /* figure out which color octet to use to paint the 8-color block */
334 if ((opcode & 0xF0) == 0xC0) {
335 /* fetch the next 8 colors from bytestream and store in next
336 * available entry in the color octet table */
337 for (i = 0; i < COCTET; i++) {
338 pixel = bytestream2_get_byte(&s->gb);
339 color_table_index = COCTET * color_octet_index + i;
340 s->color_octets[color_table_index] = pixel;
342 /* this is the base index to use for this block */
343 color_table_index = COCTET * color_octet_index;
346 if (color_octet_index == COLORS_PER_TABLE)
347 color_octet_index = 0;
349 color_table_index = COCTET * bytestream2_get_byte(&s->gb);
353 For this input of 6 hex bytes:
355 Mangle it to this output:
356 flags_a = xx012456, flags_b = xx89A37B
358 /* build the color flags */
359 int val1 = bytestream2_get_be16(&s->gb);
360 int val2 = bytestream2_get_be16(&s->gb);
361 int val3 = bytestream2_get_be16(&s->gb);
362 color_flags_a = ((val1 & 0xFFF0) << 8) | (val2 >> 4);
363 color_flags_b = ((val3 & 0xFFF0) << 8) |
364 ((val1 & 0x0F) << 8) | ((val2 & 0x0F) << 4) | (val3 & 0x0F);
366 color_flags = color_flags_a;
367 /* flag mask actually acts as a bit shift count here */
369 block_ptr = row_ptr + pixel_ptr;
370 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
371 /* reload flags at third row (iteration pixel_y == 2) */
373 color_flags = color_flags_b;
376 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
377 pixel = color_table_index +
378 ((color_flags >> flag_mask) & 0x07);
380 pixels[block_ptr++] = s->color_octets[pixel];
382 block_ptr += row_inc;
388 /* 16-color block encoding (every pixel is a different color) */
390 n_blocks = (opcode & 0x0F) + 1;
393 block_ptr = row_ptr + pixel_ptr;
394 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
395 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
396 pixels[block_ptr++] = bytestream2_get_byte(&s->gb);
398 block_ptr += row_inc;
405 avpriv_request_sample(s->avctx, "0xF0 opcode");
413 static av_cold int smc_decode_init(AVCodecContext *avctx)
415 SmcContext *s = avctx->priv_data;
418 avctx->pix_fmt = AV_PIX_FMT_PAL8;
420 s->frame = av_frame_alloc();
422 return AVERROR(ENOMEM);
427 static int smc_decode_frame(AVCodecContext *avctx,
428 void *data, int *got_frame,
431 const uint8_t *buf = avpkt->data;
432 int buf_size = avpkt->size;
433 SmcContext *s = avctx->priv_data;
434 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
437 bytestream2_init(&s->gb, buf, buf_size);
439 if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
443 s->frame->palette_has_changed = 1;
444 memcpy(s->pal, pal, AVPALETTE_SIZE);
447 smc_decode_stream(s);
450 if ((ret = av_frame_ref(data, s->frame)) < 0)
453 /* always report that the buffer was completely consumed */
457 static av_cold int smc_decode_end(AVCodecContext *avctx)
459 SmcContext *s = avctx->priv_data;
461 av_frame_free(&s->frame);
466 AVCodec ff_smc_decoder = {
468 .long_name = NULL_IF_CONFIG_SMALL("QuickTime Graphics (SMC)"),
469 .type = AVMEDIA_TYPE_VIDEO,
470 .id = AV_CODEC_ID_SMC,
471 .priv_data_size = sizeof(SmcContext),
472 .init = smc_decode_init,
473 .close = smc_decode_end,
474 .decode = smc_decode_frame,
475 .capabilities = CODEC_CAP_DR1,