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.
39 #define printf(...) {} //(f)printf() usage is forbidden in libavcodec, use av_log
40 #define fprintf(...) {}
46 #define COLORS_PER_TABLE 256
48 typedef struct SmcContext {
50 AVCodecContext *avctx;
57 /* SMC color tables */
58 unsigned char color_pairs[COLORS_PER_TABLE * CPAIR];
59 unsigned char color_quads[COLORS_PER_TABLE * CQUAD];
60 unsigned char color_octets[COLORS_PER_TABLE * COCTET];
64 #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
65 #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
66 (((uint8_t*)(x))[1] << 16) | \
67 (((uint8_t*)(x))[2] << 8) | \
69 #define GET_BLOCK_COUNT() \
70 (opcode & 0x10) ? (1 + s->buf[stream_ptr++]) : 1 + (opcode & 0x0F);
72 #define ADVANCE_BLOCK() \
75 if (pixel_ptr >= width) \
78 row_ptr += stride * 4; \
81 if (total_blocks < 0) \
83 printf("warning: block counter just went negative (this should not happen)\n"); \
88 static void smc_decode_stream(SmcContext *s)
90 int width = s->avctx->width;
91 int height = s->avctx->height;
92 int stride = s->frame.linesize[0];
98 unsigned int color_flags;
99 unsigned int color_flags_a;
100 unsigned int color_flags_b;
101 unsigned int flag_mask;
103 unsigned char *pixels = s->frame.data[0];
105 int image_size = height * s->frame.linesize[0];
108 int pixel_x, pixel_y;
109 int row_inc = stride - 4;
112 int prev_block_ptr1, prev_block_ptr2;
115 int color_table_index; /* indexes to color pair, quad, or octet tables */
118 int color_pair_index = 0;
119 int color_quad_index = 0;
120 int color_octet_index = 0;
122 /* make the palette available */
123 memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
124 if (s->avctx->palctrl->palette_changed) {
125 s->frame.palette_has_changed = 1;
126 s->avctx->palctrl->palette_changed = 0;
129 chunk_size = BE_32(&s->buf[stream_ptr]) & 0x00FFFFFF;
131 if (chunk_size != s->size)
132 printf("warning: MOV chunk size != encoded chunk size (%d != %d); using MOV chunk size\n",
133 chunk_size, s->size);
135 chunk_size = s->size;
136 total_blocks = (s->avctx->width * s->avctx->height) / (4 * 4);
138 /* traverse through the blocks */
139 while (total_blocks) {
141 /* make sure stream ptr hasn't gone out of bounds */
142 if (stream_ptr > chunk_size) {
143 printf("SMC decoder just went out of bounds (stream ptr = %d, chunk size = %d)\n",
144 stream_ptr, chunk_size);
147 /* make sure the row pointer hasn't gone wild */
148 if (row_ptr >= image_size) {
149 printf("SMC decoder just went out of bounds (row ptr = %d, height = %d)\n",
150 row_ptr, image_size);
154 opcode = s->buf[stream_ptr++];
155 switch (opcode & 0xF0) {
159 n_blocks = GET_BLOCK_COUNT();
165 /* repeat last block n times */
168 n_blocks = GET_BLOCK_COUNT();
171 if ((row_ptr == 0) && (pixel_ptr == 0)) {
172 printf("encountered repeat block opcode (%02X) but no blocks rendered yet\n",
177 /* figure out where the previous block started */
180 (row_ptr - s->avctx->width * 4) + s->avctx->width - 4;
182 prev_block_ptr1 = row_ptr + pixel_ptr - 4;
185 block_ptr = row_ptr + pixel_ptr;
186 prev_block_ptr = prev_block_ptr1;
187 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
188 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
189 pixels[block_ptr++] = pixels[prev_block_ptr++];
191 block_ptr += row_inc;
192 prev_block_ptr += row_inc;
198 /* repeat previous pair of blocks n times */
201 n_blocks = GET_BLOCK_COUNT();
205 if ((row_ptr == 0) && (pixel_ptr < 2 * 4)) {
206 printf("encountered repeat block opcode (%02X) but not enough blocks rendered yet\n",
211 /* figure out where the previous 2 blocks started */
213 prev_block_ptr1 = (row_ptr - s->avctx->width * 4) +
214 s->avctx->width - 4 * 2;
215 else if (pixel_ptr == 4)
216 prev_block_ptr1 = (row_ptr - s->avctx->width * 4) + row_inc;
218 prev_block_ptr1 = row_ptr + pixel_ptr - 4 * 2;
221 prev_block_ptr2 = (row_ptr - s->avctx->width * 4) + row_inc;
223 prev_block_ptr2 = row_ptr + pixel_ptr - 4;
227 block_ptr = row_ptr + pixel_ptr;
229 prev_block_ptr = prev_block_ptr2;
231 prev_block_ptr = prev_block_ptr1;
232 prev_block_flag = !prev_block_flag;
234 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
235 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
236 pixels[block_ptr++] = pixels[prev_block_ptr++];
238 block_ptr += row_inc;
239 prev_block_ptr += row_inc;
245 /* 1-color block encoding */
248 n_blocks = GET_BLOCK_COUNT();
249 pixel = s->buf[stream_ptr++];
252 block_ptr = row_ptr + pixel_ptr;
253 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
254 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
255 pixels[block_ptr++] = pixel;
257 block_ptr += row_inc;
263 /* 2-color block encoding */
266 n_blocks = (opcode & 0x0F) + 1;
268 /* figure out which color pair to use to paint the 2-color block */
269 if ((opcode & 0xF0) == 0x80) {
270 /* fetch the next 2 colors from bytestream and store in next
271 * available entry in the color pair table */
272 for (i = 0; i < CPAIR; i++) {
273 pixel = s->buf[stream_ptr++];
274 color_table_index = CPAIR * color_pair_index + i;
275 s->color_pairs[color_table_index] = pixel;
277 /* this is the base index to use for this block */
278 color_table_index = CPAIR * color_pair_index;
281 if (color_pair_index == COLORS_PER_TABLE)
282 color_pair_index = 0;
284 color_table_index = CPAIR * s->buf[stream_ptr++];
287 color_flags = BE_16(&s->buf[stream_ptr]);
290 block_ptr = row_ptr + pixel_ptr;
291 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
292 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
293 if (color_flags & flag_mask)
294 pixel = color_table_index + 1;
296 pixel = color_table_index;
298 pixels[block_ptr++] = s->color_pairs[pixel];
300 block_ptr += row_inc;
306 /* 4-color block encoding */
309 n_blocks = (opcode & 0x0F) + 1;
311 /* figure out which color quad to use to paint the 4-color block */
312 if ((opcode & 0xF0) == 0xA0) {
313 /* fetch the next 4 colors from bytestream and store in next
314 * available entry in the color quad table */
315 for (i = 0; i < CQUAD; i++) {
316 pixel = s->buf[stream_ptr++];
317 color_table_index = CQUAD * color_quad_index + i;
318 s->color_quads[color_table_index] = pixel;
320 /* this is the base index to use for this block */
321 color_table_index = CQUAD * color_quad_index;
324 if (color_quad_index == COLORS_PER_TABLE)
325 color_quad_index = 0;
327 color_table_index = CQUAD * s->buf[stream_ptr++];
330 color_flags = BE_32(&s->buf[stream_ptr]);
332 /* flag mask actually acts as a bit shift count here */
334 block_ptr = row_ptr + pixel_ptr;
335 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
336 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
337 pixel = color_table_index +
338 ((color_flags >> flag_mask) & 0x03);
340 pixels[block_ptr++] = s->color_quads[pixel];
342 block_ptr += row_inc;
348 /* 8-color block encoding */
351 n_blocks = (opcode & 0x0F) + 1;
353 /* figure out which color octet to use to paint the 8-color block */
354 if ((opcode & 0xF0) == 0xC0) {
355 /* fetch the next 8 colors from bytestream and store in next
356 * available entry in the color octet table */
357 for (i = 0; i < COCTET; i++) {
358 pixel = s->buf[stream_ptr++];
359 color_table_index = COCTET * color_octet_index + i;
360 s->color_octets[color_table_index] = pixel;
362 /* this is the base index to use for this block */
363 color_table_index = COCTET * color_octet_index;
366 if (color_octet_index == COLORS_PER_TABLE)
367 color_octet_index = 0;
369 color_table_index = COCTET * s->buf[stream_ptr++];
373 For this input of 6 hex bytes:
375 Mangle it to this output:
376 flags_a = xx012456, flags_b = xx89A37B
378 /* build the color flags */
379 color_flags_a = color_flags_b = 0;
381 (s->buf[stream_ptr + 0] << 16) |
382 ((s->buf[stream_ptr + 1] & 0xF0) << 8) |
383 ((s->buf[stream_ptr + 2] & 0xF0) << 4) |
384 ((s->buf[stream_ptr + 2] & 0x0F) << 4) |
385 ((s->buf[stream_ptr + 3] & 0xF0) >> 4);
387 (s->buf[stream_ptr + 4] << 16) |
388 ((s->buf[stream_ptr + 5] & 0xF0) << 8) |
389 ((s->buf[stream_ptr + 1] & 0x0F) << 8) |
390 ((s->buf[stream_ptr + 3] & 0x0F) << 4) |
391 (s->buf[stream_ptr + 5] & 0x0F);
394 color_flags = color_flags_a;
395 /* flag mask actually acts as a bit shift count here */
397 block_ptr = row_ptr + pixel_ptr;
398 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
399 /* reload flags at third row (iteration pixel_y == 2) */
401 color_flags = color_flags_b;
404 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
405 pixel = color_table_index +
406 ((color_flags >> flag_mask) & 0x07);
408 pixels[block_ptr++] = s->color_octets[pixel];
410 block_ptr += row_inc;
416 /* 16-color block encoding (every pixel is a different color) */
418 n_blocks = (opcode & 0x0F) + 1;
421 block_ptr = row_ptr + pixel_ptr;
422 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
423 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
424 pixels[block_ptr++] = s->buf[stream_ptr++];
426 block_ptr += row_inc;
433 printf("0xF0 opcode seen in SMC chunk (xine developers would like to know)\n");
439 static int smc_decode_init(AVCodecContext *avctx)
441 SmcContext *s = (SmcContext *)avctx->priv_data;
444 avctx->pix_fmt = PIX_FMT_PAL8;
445 avctx->has_b_frames = 0;
446 dsputil_init(&s->dsp, avctx);
448 s->frame.data[0] = NULL;
453 static int smc_decode_frame(AVCodecContext *avctx,
454 void *data, int *data_size,
455 uint8_t *buf, int buf_size)
457 SmcContext *s = (SmcContext *)avctx->priv_data;
459 /* no supplementary picture */
466 s->frame.reference = 1;
467 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
468 FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
469 if (avctx->reget_buffer(avctx, &s->frame)) {
470 printf ("reget_buffer() failed\n");
474 smc_decode_stream(s);
476 *data_size = sizeof(AVFrame);
477 *(AVFrame*)data = s->frame;
479 /* always report that the buffer was completely consumed */
483 static int smc_decode_end(AVCodecContext *avctx)
485 SmcContext *s = (SmcContext *)avctx->priv_data;
487 if (s->frame.data[0])
488 avctx->release_buffer(avctx, &s->frame);
493 AVCodec smc_decoder = {