Add function to compute ceil(log2(x)).
[FFMpeg-mirror/lagarith.git] / libavcodec / smc.c
blob0153f0c71ba5ecba8d26362529ec9116e7f38040
1 /*
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
22 /**
23 * @file libavcodec/smc.c
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.
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
35 #include "libavutil/intreadwrite.h"
36 #include "avcodec.h"
38 #define CPAIR 2
39 #define CQUAD 4
40 #define COCTET 8
42 #define COLORS_PER_TABLE 256
44 typedef struct SmcContext {
46 AVCodecContext *avctx;
47 AVFrame frame;
49 const unsigned char *buf;
50 int size;
52 /* SMC color tables */
53 unsigned char color_pairs[COLORS_PER_TABLE * CPAIR];
54 unsigned char color_quads[COLORS_PER_TABLE * CQUAD];
55 unsigned char color_octets[COLORS_PER_TABLE * COCTET];
57 } SmcContext;
59 #define GET_BLOCK_COUNT() \
60 (opcode & 0x10) ? (1 + s->buf[stream_ptr++]) : 1 + (opcode & 0x0F);
62 #define ADVANCE_BLOCK() \
63 { \
64 pixel_ptr += 4; \
65 if (pixel_ptr >= width) \
66 { \
67 pixel_ptr = 0; \
68 row_ptr += stride * 4; \
69 } \
70 total_blocks--; \
71 if (total_blocks < 0) \
72 { \
73 av_log(s->avctx, AV_LOG_INFO, "warning: block counter just went negative (this should not happen)\n"); \
74 return; \
75 } \
78 static void smc_decode_stream(SmcContext *s)
80 int width = s->avctx->width;
81 int height = s->avctx->height;
82 int stride = s->frame.linesize[0];
83 int i;
84 int stream_ptr = 0;
85 int chunk_size;
86 unsigned char opcode;
87 int n_blocks;
88 unsigned int color_flags;
89 unsigned int color_flags_a;
90 unsigned int color_flags_b;
91 unsigned int flag_mask;
93 unsigned char *pixels = s->frame.data[0];
95 int image_size = height * s->frame.linesize[0];
96 int row_ptr = 0;
97 int pixel_ptr = 0;
98 int pixel_x, pixel_y;
99 int row_inc = stride - 4;
100 int block_ptr;
101 int prev_block_ptr;
102 int prev_block_ptr1, prev_block_ptr2;
103 int prev_block_flag;
104 int total_blocks;
105 int color_table_index; /* indexes to color pair, quad, or octet tables */
106 int pixel;
108 int color_pair_index = 0;
109 int color_quad_index = 0;
110 int color_octet_index = 0;
112 /* make the palette available */
113 memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
114 if (s->avctx->palctrl->palette_changed) {
115 s->frame.palette_has_changed = 1;
116 s->avctx->palctrl->palette_changed = 0;
119 chunk_size = AV_RB32(&s->buf[stream_ptr]) & 0x00FFFFFF;
120 stream_ptr += 4;
121 if (chunk_size != s->size)
122 av_log(s->avctx, AV_LOG_INFO, "warning: MOV chunk size != encoded chunk size (%d != %d); using MOV chunk size\n",
123 chunk_size, s->size);
125 chunk_size = s->size;
126 total_blocks = ((s->avctx->width + 3) / 4) * ((s->avctx->height + 3) / 4);
128 /* traverse through the blocks */
129 while (total_blocks) {
130 /* sanity checks */
131 /* make sure stream ptr hasn't gone out of bounds */
132 if (stream_ptr > chunk_size) {
133 av_log(s->avctx, AV_LOG_INFO, "SMC decoder just went out of bounds (stream ptr = %d, chunk size = %d)\n",
134 stream_ptr, chunk_size);
135 return;
137 /* make sure the row pointer hasn't gone wild */
138 if (row_ptr >= image_size) {
139 av_log(s->avctx, AV_LOG_INFO, "SMC decoder just went out of bounds (row ptr = %d, height = %d)\n",
140 row_ptr, image_size);
141 return;
144 opcode = s->buf[stream_ptr++];
145 switch (opcode & 0xF0) {
146 /* skip n blocks */
147 case 0x00:
148 case 0x10:
149 n_blocks = GET_BLOCK_COUNT();
150 while (n_blocks--) {
151 ADVANCE_BLOCK();
153 break;
155 /* repeat last block n times */
156 case 0x20:
157 case 0x30:
158 n_blocks = GET_BLOCK_COUNT();
160 /* sanity check */
161 if ((row_ptr == 0) && (pixel_ptr == 0)) {
162 av_log(s->avctx, AV_LOG_INFO, "encountered repeat block opcode (%02X) but no blocks rendered yet\n",
163 opcode & 0xF0);
164 break;
167 /* figure out where the previous block started */
168 if (pixel_ptr == 0)
169 prev_block_ptr1 =
170 (row_ptr - s->avctx->width * 4) + s->avctx->width - 4;
171 else
172 prev_block_ptr1 = row_ptr + pixel_ptr - 4;
174 while (n_blocks--) {
175 block_ptr = row_ptr + pixel_ptr;
176 prev_block_ptr = prev_block_ptr1;
177 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
178 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
179 pixels[block_ptr++] = pixels[prev_block_ptr++];
181 block_ptr += row_inc;
182 prev_block_ptr += row_inc;
184 ADVANCE_BLOCK();
186 break;
188 /* repeat previous pair of blocks n times */
189 case 0x40:
190 case 0x50:
191 n_blocks = GET_BLOCK_COUNT();
192 n_blocks *= 2;
194 /* sanity check */
195 if ((row_ptr == 0) && (pixel_ptr < 2 * 4)) {
196 av_log(s->avctx, AV_LOG_INFO, "encountered repeat block opcode (%02X) but not enough blocks rendered yet\n",
197 opcode & 0xF0);
198 break;
201 /* figure out where the previous 2 blocks started */
202 if (pixel_ptr == 0)
203 prev_block_ptr1 = (row_ptr - s->avctx->width * 4) +
204 s->avctx->width - 4 * 2;
205 else if (pixel_ptr == 4)
206 prev_block_ptr1 = (row_ptr - s->avctx->width * 4) + row_inc;
207 else
208 prev_block_ptr1 = row_ptr + pixel_ptr - 4 * 2;
210 if (pixel_ptr == 0)
211 prev_block_ptr2 = (row_ptr - s->avctx->width * 4) + row_inc;
212 else
213 prev_block_ptr2 = row_ptr + pixel_ptr - 4;
215 prev_block_flag = 0;
216 while (n_blocks--) {
217 block_ptr = row_ptr + pixel_ptr;
218 if (prev_block_flag)
219 prev_block_ptr = prev_block_ptr2;
220 else
221 prev_block_ptr = prev_block_ptr1;
222 prev_block_flag = !prev_block_flag;
224 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
225 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
226 pixels[block_ptr++] = pixels[prev_block_ptr++];
228 block_ptr += row_inc;
229 prev_block_ptr += row_inc;
231 ADVANCE_BLOCK();
233 break;
235 /* 1-color block encoding */
236 case 0x60:
237 case 0x70:
238 n_blocks = GET_BLOCK_COUNT();
239 pixel = s->buf[stream_ptr++];
241 while (n_blocks--) {
242 block_ptr = row_ptr + pixel_ptr;
243 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
244 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
245 pixels[block_ptr++] = pixel;
247 block_ptr += row_inc;
249 ADVANCE_BLOCK();
251 break;
253 /* 2-color block encoding */
254 case 0x80:
255 case 0x90:
256 n_blocks = (opcode & 0x0F) + 1;
258 /* figure out which color pair to use to paint the 2-color block */
259 if ((opcode & 0xF0) == 0x80) {
260 /* fetch the next 2 colors from bytestream and store in next
261 * available entry in the color pair table */
262 for (i = 0; i < CPAIR; i++) {
263 pixel = s->buf[stream_ptr++];
264 color_table_index = CPAIR * color_pair_index + i;
265 s->color_pairs[color_table_index] = pixel;
267 /* this is the base index to use for this block */
268 color_table_index = CPAIR * color_pair_index;
269 color_pair_index++;
270 /* wraparound */
271 if (color_pair_index == COLORS_PER_TABLE)
272 color_pair_index = 0;
273 } else
274 color_table_index = CPAIR * s->buf[stream_ptr++];
276 while (n_blocks--) {
277 color_flags = AV_RB16(&s->buf[stream_ptr]);
278 stream_ptr += 2;
279 flag_mask = 0x8000;
280 block_ptr = row_ptr + pixel_ptr;
281 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
282 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
283 if (color_flags & flag_mask)
284 pixel = color_table_index + 1;
285 else
286 pixel = color_table_index;
287 flag_mask >>= 1;
288 pixels[block_ptr++] = s->color_pairs[pixel];
290 block_ptr += row_inc;
292 ADVANCE_BLOCK();
294 break;
296 /* 4-color block encoding */
297 case 0xA0:
298 case 0xB0:
299 n_blocks = (opcode & 0x0F) + 1;
301 /* figure out which color quad to use to paint the 4-color block */
302 if ((opcode & 0xF0) == 0xA0) {
303 /* fetch the next 4 colors from bytestream and store in next
304 * available entry in the color quad table */
305 for (i = 0; i < CQUAD; i++) {
306 pixel = s->buf[stream_ptr++];
307 color_table_index = CQUAD * color_quad_index + i;
308 s->color_quads[color_table_index] = pixel;
310 /* this is the base index to use for this block */
311 color_table_index = CQUAD * color_quad_index;
312 color_quad_index++;
313 /* wraparound */
314 if (color_quad_index == COLORS_PER_TABLE)
315 color_quad_index = 0;
316 } else
317 color_table_index = CQUAD * s->buf[stream_ptr++];
319 while (n_blocks--) {
320 color_flags = AV_RB32(&s->buf[stream_ptr]);
321 stream_ptr += 4;
322 /* flag mask actually acts as a bit shift count here */
323 flag_mask = 30;
324 block_ptr = row_ptr + pixel_ptr;
325 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
326 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
327 pixel = color_table_index +
328 ((color_flags >> flag_mask) & 0x03);
329 flag_mask -= 2;
330 pixels[block_ptr++] = s->color_quads[pixel];
332 block_ptr += row_inc;
334 ADVANCE_BLOCK();
336 break;
338 /* 8-color block encoding */
339 case 0xC0:
340 case 0xD0:
341 n_blocks = (opcode & 0x0F) + 1;
343 /* figure out which color octet to use to paint the 8-color block */
344 if ((opcode & 0xF0) == 0xC0) {
345 /* fetch the next 8 colors from bytestream and store in next
346 * available entry in the color octet table */
347 for (i = 0; i < COCTET; i++) {
348 pixel = s->buf[stream_ptr++];
349 color_table_index = COCTET * color_octet_index + i;
350 s->color_octets[color_table_index] = pixel;
352 /* this is the base index to use for this block */
353 color_table_index = COCTET * color_octet_index;
354 color_octet_index++;
355 /* wraparound */
356 if (color_octet_index == COLORS_PER_TABLE)
357 color_octet_index = 0;
358 } else
359 color_table_index = COCTET * s->buf[stream_ptr++];
361 while (n_blocks--) {
363 For this input of 6 hex bytes:
364 01 23 45 67 89 AB
365 Mangle it to this output:
366 flags_a = xx012456, flags_b = xx89A37B
368 /* build the color flags */
369 color_flags_a = color_flags_b = 0;
370 color_flags_a =
371 (s->buf[stream_ptr + 0] << 16) |
372 ((s->buf[stream_ptr + 1] & 0xF0) << 8) |
373 ((s->buf[stream_ptr + 2] & 0xF0) << 4) |
374 ((s->buf[stream_ptr + 2] & 0x0F) << 4) |
375 ((s->buf[stream_ptr + 3] & 0xF0) >> 4);
376 color_flags_b =
377 (s->buf[stream_ptr + 4] << 16) |
378 ((s->buf[stream_ptr + 5] & 0xF0) << 8) |
379 ((s->buf[stream_ptr + 1] & 0x0F) << 8) |
380 ((s->buf[stream_ptr + 3] & 0x0F) << 4) |
381 (s->buf[stream_ptr + 5] & 0x0F);
382 stream_ptr += 6;
384 color_flags = color_flags_a;
385 /* flag mask actually acts as a bit shift count here */
386 flag_mask = 21;
387 block_ptr = row_ptr + pixel_ptr;
388 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
389 /* reload flags at third row (iteration pixel_y == 2) */
390 if (pixel_y == 2) {
391 color_flags = color_flags_b;
392 flag_mask = 21;
394 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
395 pixel = color_table_index +
396 ((color_flags >> flag_mask) & 0x07);
397 flag_mask -= 3;
398 pixels[block_ptr++] = s->color_octets[pixel];
400 block_ptr += row_inc;
402 ADVANCE_BLOCK();
404 break;
406 /* 16-color block encoding (every pixel is a different color) */
407 case 0xE0:
408 n_blocks = (opcode & 0x0F) + 1;
410 while (n_blocks--) {
411 block_ptr = row_ptr + pixel_ptr;
412 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
413 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
414 pixels[block_ptr++] = s->buf[stream_ptr++];
416 block_ptr += row_inc;
418 ADVANCE_BLOCK();
420 break;
422 case 0xF0:
423 av_log(s->avctx, AV_LOG_INFO, "0xF0 opcode seen in SMC chunk (contact the developers)\n");
424 break;
429 static av_cold int smc_decode_init(AVCodecContext *avctx)
431 SmcContext *s = avctx->priv_data;
433 s->avctx = avctx;
434 avctx->pix_fmt = PIX_FMT_PAL8;
436 s->frame.data[0] = NULL;
438 return 0;
441 static int smc_decode_frame(AVCodecContext *avctx,
442 void *data, int *data_size,
443 AVPacket *avpkt)
445 const uint8_t *buf = avpkt->data;
446 int buf_size = avpkt->size;
447 SmcContext *s = avctx->priv_data;
449 s->buf = buf;
450 s->size = buf_size;
452 s->frame.reference = 1;
453 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
454 FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
455 if (avctx->reget_buffer(avctx, &s->frame)) {
456 av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
457 return -1;
460 smc_decode_stream(s);
462 *data_size = sizeof(AVFrame);
463 *(AVFrame*)data = s->frame;
465 /* always report that the buffer was completely consumed */
466 return buf_size;
469 static av_cold int smc_decode_end(AVCodecContext *avctx)
471 SmcContext *s = avctx->priv_data;
473 if (s->frame.data[0])
474 avctx->release_buffer(avctx, &s->frame);
476 return 0;
479 AVCodec smc_decoder = {
480 "smc",
481 CODEC_TYPE_VIDEO,
482 CODEC_ID_SMC,
483 sizeof(SmcContext),
484 smc_decode_init,
485 NULL,
486 smc_decode_end,
487 smc_decode_frame,
488 CODEC_CAP_DR1,
489 .long_name = NULL_IF_CONFIG_SMALL("QuickTime Graphics (SMC)"),