2 * Quicktime Graphics (SMC) Video Decoder
3 * Copyright (C) 2003 the ffmpeg project
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * QT SMC Video Decoder by Mike Melanson (melanson@pcisys.net)
24 * For more information about the SMC format, visit:
25 * http://www.pcisys.net/~melanson/codecs/
27 * The SMC decoder outputs PAL8 colorspace data.
43 #define COLORS_PER_TABLE 256
45 typedef struct SmcContext {
47 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];
61 #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
62 #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
63 (((uint8_t*)(x))[1] << 16) | \
64 (((uint8_t*)(x))[2] << 8) | \
66 #define GET_BLOCK_COUNT() \
67 (opcode & 0x10) ? (1 + s->buf[stream_ptr++]) : 1 + (opcode & 0x0F);
69 #define ADVANCE_BLOCK() \
72 if (pixel_ptr >= width) \
75 row_ptr += stride * 4; \
78 if (total_blocks < 0) \
80 printf("warning: block counter just went negative (this should not happen)\n"); \
85 static void smc_decode_stream(SmcContext *s)
87 int width = s->avctx->width;
88 int height = s->avctx->height;
89 int stride = s->frame.linesize[0];
95 unsigned int color_flags;
96 unsigned int color_flags_a;
97 unsigned int color_flags_b;
98 unsigned int flag_mask;
100 unsigned char *pixels = s->frame.data[0];
102 int image_size = height * s->frame.linesize[0];
105 int pixel_x, pixel_y;
106 int row_inc = stride - 4;
109 int prev_block_ptr1, prev_block_ptr2;
112 int color_table_index; /* indexes to color pair, quad, or octet tables */
115 int color_pair_index = 0;
116 int color_quad_index = 0;
117 int color_octet_index = 0;
119 /* make the palette available */
120 memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
121 if (s->avctx->palctrl->palette_changed) {
122 s->frame.palette_has_changed = 1;
123 s->avctx->palctrl->palette_changed = 0;
126 chunk_size = BE_32(&s->buf[stream_ptr]) & 0x00FFFFFF;
128 if (chunk_size != s->size)
129 printf("warning: MOV chunk size != encoded chunk size (%d != %d); using MOV chunk size\n",
130 chunk_size, s->size);
132 chunk_size = s->size;
133 total_blocks = (s->avctx->width * s->avctx->height) / (4 * 4);
135 /* traverse through the blocks */
136 while (total_blocks) {
138 /* make sure stream ptr hasn't gone out of bounds */
139 if (stream_ptr > chunk_size) {
140 printf("SMC decoder just went out of bounds (stream ptr = %d, chunk size = %d)\n",
141 stream_ptr, chunk_size);
144 /* make sure the row pointer hasn't gone wild */
145 if (row_ptr >= image_size) {
146 printf("SMC decoder just went out of bounds (row ptr = %d, height = %d)\n",
147 row_ptr, image_size);
151 opcode = s->buf[stream_ptr++];
152 switch (opcode & 0xF0) {
156 n_blocks = GET_BLOCK_COUNT();
162 /* repeat last block n times */
165 n_blocks = GET_BLOCK_COUNT();
168 if ((row_ptr == 0) && (pixel_ptr == 0)) {
169 printf("encountered repeat block opcode (%02X) but no blocks rendered yet\n",
174 /* figure out where the previous block started */
177 (row_ptr - s->avctx->width * 4) + s->avctx->width - 4;
179 prev_block_ptr1 = row_ptr + pixel_ptr - 4;
182 block_ptr = row_ptr + pixel_ptr;
183 prev_block_ptr = prev_block_ptr1;
184 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
185 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
186 pixels[block_ptr++] = pixels[prev_block_ptr++];
188 block_ptr += row_inc;
189 prev_block_ptr += row_inc;
195 /* repeat previous pair of blocks n times */
198 n_blocks = GET_BLOCK_COUNT();
202 if ((row_ptr == 0) && (pixel_ptr < 2 * 4)) {
203 printf("encountered repeat block opcode (%02X) but not enough blocks rendered yet\n",
208 /* figure out where the previous 2 blocks started */
210 prev_block_ptr1 = (row_ptr - s->avctx->width * 4) +
211 s->avctx->width - 4 * 2;
212 else if (pixel_ptr == 4)
213 prev_block_ptr1 = (row_ptr - s->avctx->width * 4) + row_inc;
215 prev_block_ptr1 = row_ptr + pixel_ptr - 4 * 2;
218 prev_block_ptr2 = (row_ptr - s->avctx->width * 4) + row_inc;
220 prev_block_ptr2 = row_ptr + pixel_ptr - 4;
224 block_ptr = row_ptr + pixel_ptr;
226 prev_block_ptr = prev_block_ptr2;
228 prev_block_ptr = prev_block_ptr1;
229 prev_block_flag = !prev_block_flag;
231 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
232 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
233 pixels[block_ptr++] = pixels[prev_block_ptr++];
235 block_ptr += row_inc;
236 prev_block_ptr += row_inc;
242 /* 1-color block encoding */
245 n_blocks = GET_BLOCK_COUNT();
246 pixel = s->buf[stream_ptr++];
249 block_ptr = row_ptr + pixel_ptr;
250 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
251 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
252 pixels[block_ptr++] = pixel;
254 block_ptr += row_inc;
260 /* 2-color block encoding */
263 n_blocks = (opcode & 0x0F) + 1;
265 /* figure out which color pair to use to paint the 2-color block */
266 if ((opcode & 0xF0) == 0x80) {
267 /* fetch the next 2 colors from bytestream and store in next
268 * available entry in the color pair table */
269 for (i = 0; i < CPAIR; i++) {
270 pixel = s->buf[stream_ptr++];
271 color_table_index = CPAIR * color_pair_index + i;
272 s->color_pairs[color_table_index] = pixel;
274 /* this is the base index to use for this block */
275 color_table_index = CPAIR * color_pair_index;
278 if (color_pair_index == COLORS_PER_TABLE)
279 color_pair_index = 0;
281 color_table_index = CPAIR * s->buf[stream_ptr++];
284 color_flags = BE_16(&s->buf[stream_ptr]);
287 block_ptr = row_ptr + pixel_ptr;
288 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
289 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
290 if (color_flags & flag_mask)
291 pixel = color_table_index + 1;
293 pixel = color_table_index;
295 pixels[block_ptr++] = s->color_pairs[pixel];
297 block_ptr += row_inc;
303 /* 4-color block encoding */
306 n_blocks = (opcode & 0x0F) + 1;
308 /* figure out which color quad to use to paint the 4-color block */
309 if ((opcode & 0xF0) == 0xA0) {
310 /* fetch the next 4 colors from bytestream and store in next
311 * available entry in the color quad table */
312 for (i = 0; i < CQUAD; i++) {
313 pixel = s->buf[stream_ptr++];
314 color_table_index = CQUAD * color_quad_index + i;
315 s->color_quads[color_table_index] = pixel;
317 /* this is the base index to use for this block */
318 color_table_index = CQUAD * color_quad_index;
321 if (color_quad_index == COLORS_PER_TABLE)
322 color_quad_index = 0;
324 color_table_index = CQUAD * s->buf[stream_ptr++];
327 color_flags = BE_32(&s->buf[stream_ptr]);
329 /* flag mask actually acts as a bit shift count here */
331 block_ptr = row_ptr + pixel_ptr;
332 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
333 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
334 pixel = color_table_index +
335 ((color_flags >> flag_mask) & 0x03);
337 pixels[block_ptr++] = s->color_quads[pixel];
339 block_ptr += row_inc;
345 /* 8-color block encoding */
348 n_blocks = (opcode & 0x0F) + 1;
350 /* figure out which color octet to use to paint the 8-color block */
351 if ((opcode & 0xF0) == 0xC0) {
352 /* fetch the next 8 colors from bytestream and store in next
353 * available entry in the color octet table */
354 for (i = 0; i < COCTET; i++) {
355 pixel = s->buf[stream_ptr++];
356 color_table_index = COCTET * color_octet_index + i;
357 s->color_octets[color_table_index] = pixel;
359 /* this is the base index to use for this block */
360 color_table_index = COCTET * color_octet_index;
363 if (color_octet_index == COLORS_PER_TABLE)
364 color_octet_index = 0;
366 color_table_index = COCTET * s->buf[stream_ptr++];
370 For this input of 6 hex bytes:
372 Mangle it to this output:
373 flags_a = xx012456, flags_b = xx89A37B
375 /* build the color flags */
376 color_flags_a = color_flags_b = 0;
378 (s->buf[stream_ptr + 0] << 16) |
379 ((s->buf[stream_ptr + 1] & 0xF0) << 8) |
380 ((s->buf[stream_ptr + 2] & 0xF0) << 4) |
381 ((s->buf[stream_ptr + 2] & 0x0F) << 4) |
382 ((s->buf[stream_ptr + 3] & 0xF0) >> 4);
384 (s->buf[stream_ptr + 4] << 16) |
385 ((s->buf[stream_ptr + 5] & 0xF0) << 8) |
386 ((s->buf[stream_ptr + 1] & 0x0F) << 8) |
387 ((s->buf[stream_ptr + 3] & 0x0F) << 4) |
388 (s->buf[stream_ptr + 5] & 0x0F);
391 color_flags = color_flags_a;
392 /* flag mask actually acts as a bit shift count here */
394 block_ptr = row_ptr + pixel_ptr;
395 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
396 /* reload flags at third row (iteration pixel_y == 2) */
398 color_flags = color_flags_b;
401 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
402 pixel = color_table_index +
403 ((color_flags >> flag_mask) & 0x07);
405 pixels[block_ptr++] = s->color_octets[pixel];
407 block_ptr += row_inc;
413 /* 16-color block encoding (every pixel is a different color) */
415 n_blocks = (opcode & 0x0F) + 1;
418 block_ptr = row_ptr + pixel_ptr;
419 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
420 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
421 pixels[block_ptr++] = s->buf[stream_ptr++];
423 block_ptr += row_inc;
430 printf("0xF0 opcode seen in SMC chunk (xine developers would like to know)\n");
436 static int smc_decode_init(AVCodecContext *avctx)
438 SmcContext *s = (SmcContext *)avctx->priv_data;
441 avctx->pix_fmt = PIX_FMT_PAL8;
442 avctx->has_b_frames = 0;
443 dsputil_init(&s->dsp, avctx);
445 s->frame.data[0] = NULL;
450 static int smc_decode_frame(AVCodecContext *avctx,
451 void *data, int *data_size,
452 uint8_t *buf, int buf_size)
454 SmcContext *s = (SmcContext *)avctx->priv_data;
456 /* no supplementary picture */
463 s->frame.reference = 1;
464 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
465 FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
466 if (avctx->reget_buffer(avctx, &s->frame)) {
467 printf ("reget_buffer() failed\n");
471 smc_decode_stream(s);
473 *data_size = sizeof(AVFrame);
474 *(AVFrame*)data = s->frame;
476 /* always report that the buffer was completely consumed */
480 static int smc_decode_end(AVCodecContext *avctx)
482 SmcContext *s = (SmcContext *)avctx->priv_data;
484 if (s->frame.data[0])
485 avctx->release_buffer(avctx, &s->frame);
490 AVCodec smc_decoder = {