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 GET_BLOCK_COUNT() \
65 (opcode & 0x10) ? (1 + s->buf[stream_ptr++]) : 1 + (opcode & 0x0F);
67 #define ADVANCE_BLOCK() \
70 if (pixel_ptr >= width) \
73 row_ptr += stride * 4; \
76 if (total_blocks < 0) \
78 printf("warning: block counter just went negative (this should not happen)\n"); \
83 static void smc_decode_stream(SmcContext *s)
85 int width = s->avctx->width;
86 int height = s->avctx->height;
87 int stride = s->frame.linesize[0];
93 unsigned int color_flags;
94 unsigned int color_flags_a;
95 unsigned int color_flags_b;
96 unsigned int flag_mask;
98 unsigned char *pixels = s->frame.data[0];
100 int image_size = height * s->frame.linesize[0];
103 int pixel_x, pixel_y;
104 int row_inc = stride - 4;
107 int prev_block_ptr1, prev_block_ptr2;
110 int color_table_index; /* indexes to color pair, quad, or octet tables */
113 int color_pair_index = 0;
114 int color_quad_index = 0;
115 int color_octet_index = 0;
117 /* make the palette available */
118 memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
119 if (s->avctx->palctrl->palette_changed) {
120 s->frame.palette_has_changed = 1;
121 s->avctx->palctrl->palette_changed = 0;
124 chunk_size = BE_32(&s->buf[stream_ptr]) & 0x00FFFFFF;
126 if (chunk_size != s->size)
127 printf("warning: MOV chunk size != encoded chunk size (%d != %d); using MOV chunk size\n",
128 chunk_size, s->size);
130 chunk_size = s->size;
131 total_blocks = (s->avctx->width * s->avctx->height) / (4 * 4);
133 /* traverse through the blocks */
134 while (total_blocks) {
136 /* make sure stream ptr hasn't gone out of bounds */
137 if (stream_ptr > chunk_size) {
138 printf("SMC decoder just went out of bounds (stream ptr = %d, chunk size = %d)\n",
139 stream_ptr, chunk_size);
142 /* make sure the row pointer hasn't gone wild */
143 if (row_ptr >= image_size) {
144 printf("SMC decoder just went out of bounds (row ptr = %d, height = %d)\n",
145 row_ptr, image_size);
149 opcode = s->buf[stream_ptr++];
150 switch (opcode & 0xF0) {
154 n_blocks = GET_BLOCK_COUNT();
160 /* repeat last block n times */
163 n_blocks = GET_BLOCK_COUNT();
166 if ((row_ptr == 0) && (pixel_ptr == 0)) {
167 printf("encountered repeat block opcode (%02X) but no blocks rendered yet\n",
172 /* figure out where the previous block started */
175 (row_ptr - s->avctx->width * 4) + s->avctx->width - 4;
177 prev_block_ptr1 = row_ptr + pixel_ptr - 4;
180 block_ptr = row_ptr + pixel_ptr;
181 prev_block_ptr = prev_block_ptr1;
182 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
183 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
184 pixels[block_ptr++] = pixels[prev_block_ptr++];
186 block_ptr += row_inc;
187 prev_block_ptr += row_inc;
193 /* repeat previous pair of blocks n times */
196 n_blocks = GET_BLOCK_COUNT();
200 if ((row_ptr == 0) && (pixel_ptr < 2 * 4)) {
201 printf("encountered repeat block opcode (%02X) but not enough blocks rendered yet\n",
206 /* figure out where the previous 2 blocks started */
208 prev_block_ptr1 = (row_ptr - s->avctx->width * 4) +
209 s->avctx->width - 4 * 2;
210 else if (pixel_ptr == 4)
211 prev_block_ptr1 = (row_ptr - s->avctx->width * 4) + row_inc;
213 prev_block_ptr1 = row_ptr + pixel_ptr - 4 * 2;
216 prev_block_ptr2 = (row_ptr - s->avctx->width * 4) + row_inc;
218 prev_block_ptr2 = row_ptr + pixel_ptr - 4;
222 block_ptr = row_ptr + pixel_ptr;
224 prev_block_ptr = prev_block_ptr2;
226 prev_block_ptr = prev_block_ptr1;
227 prev_block_flag = !prev_block_flag;
229 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
230 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
231 pixels[block_ptr++] = pixels[prev_block_ptr++];
233 block_ptr += row_inc;
234 prev_block_ptr += row_inc;
240 /* 1-color block encoding */
243 n_blocks = GET_BLOCK_COUNT();
244 pixel = s->buf[stream_ptr++];
247 block_ptr = row_ptr + pixel_ptr;
248 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
249 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
250 pixels[block_ptr++] = pixel;
252 block_ptr += row_inc;
258 /* 2-color block encoding */
261 n_blocks = (opcode & 0x0F) + 1;
263 /* figure out which color pair to use to paint the 2-color block */
264 if ((opcode & 0xF0) == 0x80) {
265 /* fetch the next 2 colors from bytestream and store in next
266 * available entry in the color pair table */
267 for (i = 0; i < CPAIR; i++) {
268 pixel = s->buf[stream_ptr++];
269 color_table_index = CPAIR * color_pair_index + i;
270 s->color_pairs[color_table_index] = pixel;
272 /* this is the base index to use for this block */
273 color_table_index = CPAIR * color_pair_index;
276 if (color_pair_index == COLORS_PER_TABLE)
277 color_pair_index = 0;
279 color_table_index = CPAIR * s->buf[stream_ptr++];
282 color_flags = BE_16(&s->buf[stream_ptr]);
285 block_ptr = row_ptr + pixel_ptr;
286 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
287 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
288 if (color_flags & flag_mask)
289 pixel = color_table_index + 1;
291 pixel = color_table_index;
293 pixels[block_ptr++] = s->color_pairs[pixel];
295 block_ptr += row_inc;
301 /* 4-color block encoding */
304 n_blocks = (opcode & 0x0F) + 1;
306 /* figure out which color quad to use to paint the 4-color block */
307 if ((opcode & 0xF0) == 0xA0) {
308 /* fetch the next 4 colors from bytestream and store in next
309 * available entry in the color quad table */
310 for (i = 0; i < CQUAD; i++) {
311 pixel = s->buf[stream_ptr++];
312 color_table_index = CQUAD * color_quad_index + i;
313 s->color_quads[color_table_index] = pixel;
315 /* this is the base index to use for this block */
316 color_table_index = CQUAD * color_quad_index;
319 if (color_quad_index == COLORS_PER_TABLE)
320 color_quad_index = 0;
322 color_table_index = CQUAD * s->buf[stream_ptr++];
325 color_flags = BE_32(&s->buf[stream_ptr]);
327 /* flag mask actually acts as a bit shift count here */
329 block_ptr = row_ptr + pixel_ptr;
330 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
331 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
332 pixel = color_table_index +
333 ((color_flags >> flag_mask) & 0x03);
335 pixels[block_ptr++] = s->color_quads[pixel];
337 block_ptr += row_inc;
343 /* 8-color block encoding */
346 n_blocks = (opcode & 0x0F) + 1;
348 /* figure out which color octet to use to paint the 8-color block */
349 if ((opcode & 0xF0) == 0xC0) {
350 /* fetch the next 8 colors from bytestream and store in next
351 * available entry in the color octet table */
352 for (i = 0; i < COCTET; i++) {
353 pixel = s->buf[stream_ptr++];
354 color_table_index = COCTET * color_octet_index + i;
355 s->color_octets[color_table_index] = pixel;
357 /* this is the base index to use for this block */
358 color_table_index = COCTET * color_octet_index;
361 if (color_octet_index == COLORS_PER_TABLE)
362 color_octet_index = 0;
364 color_table_index = COCTET * s->buf[stream_ptr++];
368 For this input of 6 hex bytes:
370 Mangle it to this output:
371 flags_a = xx012456, flags_b = xx89A37B
373 /* build the color flags */
374 color_flags_a = color_flags_b = 0;
376 (s->buf[stream_ptr + 0] << 16) |
377 ((s->buf[stream_ptr + 1] & 0xF0) << 8) |
378 ((s->buf[stream_ptr + 2] & 0xF0) << 4) |
379 ((s->buf[stream_ptr + 2] & 0x0F) << 4) |
380 ((s->buf[stream_ptr + 3] & 0xF0) >> 4);
382 (s->buf[stream_ptr + 4] << 16) |
383 ((s->buf[stream_ptr + 5] & 0xF0) << 8) |
384 ((s->buf[stream_ptr + 1] & 0x0F) << 8) |
385 ((s->buf[stream_ptr + 3] & 0x0F) << 4) |
386 (s->buf[stream_ptr + 5] & 0x0F);
389 color_flags = color_flags_a;
390 /* flag mask actually acts as a bit shift count here */
392 block_ptr = row_ptr + pixel_ptr;
393 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
394 /* reload flags at third row (iteration pixel_y == 2) */
396 color_flags = color_flags_b;
399 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
400 pixel = color_table_index +
401 ((color_flags >> flag_mask) & 0x07);
403 pixels[block_ptr++] = s->color_octets[pixel];
405 block_ptr += row_inc;
411 /* 16-color block encoding (every pixel is a different color) */
413 n_blocks = (opcode & 0x0F) + 1;
416 block_ptr = row_ptr + pixel_ptr;
417 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
418 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
419 pixels[block_ptr++] = s->buf[stream_ptr++];
421 block_ptr += row_inc;
428 printf("0xF0 opcode seen in SMC chunk (xine developers would like to know)\n");
434 static int smc_decode_init(AVCodecContext *avctx)
436 SmcContext *s = (SmcContext *)avctx->priv_data;
439 avctx->pix_fmt = PIX_FMT_PAL8;
440 avctx->has_b_frames = 0;
441 dsputil_init(&s->dsp, avctx);
443 s->frame.data[0] = NULL;
448 static int smc_decode_frame(AVCodecContext *avctx,
449 void *data, int *data_size,
450 uint8_t *buf, int buf_size)
452 SmcContext *s = (SmcContext *)avctx->priv_data;
454 /* no supplementary picture */
461 s->frame.reference = 1;
462 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
463 FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
464 if (avctx->reget_buffer(avctx, &s->frame)) {
465 printf ("reget_buffer() failed\n");
469 smc_decode_stream(s);
471 *data_size = sizeof(AVFrame);
472 *(AVFrame*)data = s->frame;
474 /* always report that the buffer was completely consumed */
478 static int smc_decode_end(AVCodecContext *avctx)
480 SmcContext *s = (SmcContext *)avctx->priv_data;
482 if (s->frame.data[0])
483 avctx->release_buffer(avctx, &s->frame);
488 AVCodec smc_decoder = {