Mercurial > libavcodec.hg
annotate smc.c @ 1857:00a6bfc81010 libavcodec
count > thread_count for execute()
| author | michael |
|---|---|
| date | Wed, 03 Mar 2004 19:29:00 +0000 |
| parents | a660ef952580 |
| children | 39ad6cd5d4a6 |
| rev | line source |
|---|---|
| 1610 | 1 /* |
| 2 * Quicktime Graphics (SMC) Video Decoder | |
| 3 * Copyright (C) 2003 the ffmpeg project | |
| 4 * | |
| 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. | |
| 9 * | |
| 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. | |
| 14 * | |
| 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 | |
| 18 * | |
| 19 */ | |
| 20 | |
| 21 /** | |
| 22 * @file smc.c | |
| 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/ | |
| 26 * | |
| 27 * The SMC decoder outputs PAL8 colorspace data. | |
| 28 */ | |
| 29 | |
| 30 #include <stdio.h> | |
| 31 #include <stdlib.h> | |
| 32 #include <string.h> | |
| 33 #include <unistd.h> | |
| 34 | |
| 35 #include "common.h" | |
| 36 #include "avcodec.h" | |
| 37 #include "dsputil.h" | |
| 38 | |
|
1823
a660ef952580
(f)printf() is disallowed in libavcodec, compilation will fail now if its used, except that codecs which where added after the printf->av_log change which did ignore av_log() and used prinf are now silent and wont print anything, they should be changed to use av_log, i could do that, but its better if the orginal developer decides which AV_LOG level each message should get
michael
parents:
1769
diff
changeset
|
39 #define printf(...) {} //(f)printf() usage is forbidden in libavcodec, use av_log |
|
a660ef952580
(f)printf() is disallowed in libavcodec, compilation will fail now if its used, except that codecs which where added after the printf->av_log change which did ignore av_log() and used prinf are now silent and wont print anything, they should be changed to use av_log, i could do that, but its better if the orginal developer decides which AV_LOG level each message should get
michael
parents:
1769
diff
changeset
|
40 #define fprintf(...) {} |
|
a660ef952580
(f)printf() is disallowed in libavcodec, compilation will fail now if its used, except that codecs which where added after the printf->av_log change which did ignore av_log() and used prinf are now silent and wont print anything, they should be changed to use av_log, i could do that, but its better if the orginal developer decides which AV_LOG level each message should get
michael
parents:
1769
diff
changeset
|
41 |
| 1610 | 42 #define CPAIR 2 |
| 43 #define CQUAD 4 | |
| 44 #define COCTET 8 | |
| 45 | |
| 46 #define COLORS_PER_TABLE 256 | |
| 47 | |
| 48 typedef struct SmcContext { | |
| 49 | |
| 50 AVCodecContext *avctx; | |
| 51 DSPContext dsp; | |
| 52 AVFrame frame; | |
| 53 | |
| 54 unsigned char *buf; | |
| 55 int size; | |
| 56 | |
| 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]; | |
| 61 | |
| 62 } SmcContext; | |
| 63 | |
| 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) | \ | |
| 68 ((uint8_t*)(x))[3]) | |
| 69 #define GET_BLOCK_COUNT() \ | |
| 70 (opcode & 0x10) ? (1 + s->buf[stream_ptr++]) : 1 + (opcode & 0x0F); | |
| 71 | |
| 72 #define ADVANCE_BLOCK() \ | |
| 73 { \ | |
| 74 pixel_ptr += 4; \ | |
| 75 if (pixel_ptr >= width) \ | |
| 76 { \ | |
| 77 pixel_ptr = 0; \ | |
| 78 row_ptr += stride * 4; \ | |
| 79 } \ | |
| 80 total_blocks--; \ | |
| 81 if (total_blocks < 0) \ | |
| 82 { \ | |
| 83 printf("warning: block counter just went negative (this should not happen)\n"); \ | |
| 84 return; \ | |
| 85 } \ | |
| 86 } | |
| 87 | |
| 88 static void smc_decode_stream(SmcContext *s) | |
| 89 { | |
| 90 int width = s->avctx->width; | |
| 91 int height = s->avctx->height; | |
| 92 int stride = s->frame.linesize[0]; | |
| 93 int i; | |
| 94 int stream_ptr = 0; | |
| 95 int chunk_size; | |
| 96 unsigned char opcode; | |
| 97 int n_blocks; | |
| 98 unsigned int color_flags; | |
| 99 unsigned int color_flags_a; | |
| 100 unsigned int color_flags_b; | |
| 101 unsigned int flag_mask; | |
| 102 | |
| 103 unsigned char *pixels = s->frame.data[0]; | |
| 104 | |
| 105 int image_size = height * s->frame.linesize[0]; | |
| 106 int row_ptr = 0; | |
| 107 int pixel_ptr = 0; | |
| 108 int pixel_x, pixel_y; | |
| 109 int row_inc = stride - 4; | |
| 110 int block_ptr; | |
| 111 int prev_block_ptr; | |
| 112 int prev_block_ptr1, prev_block_ptr2; | |
| 113 int prev_block_flag; | |
| 114 int total_blocks; | |
| 115 int color_table_index; /* indexes to color pair, quad, or octet tables */ | |
| 116 int pixel; | |
| 117 | |
| 118 int color_pair_index = 0; | |
| 119 int color_quad_index = 0; | |
| 120 int color_octet_index = 0; | |
| 121 | |
| 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; | |
| 127 } | |
| 128 | |
| 129 chunk_size = BE_32(&s->buf[stream_ptr]) & 0x00FFFFFF; | |
| 130 stream_ptr += 4; | |
| 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); | |
| 134 | |
| 135 chunk_size = s->size; | |
| 136 total_blocks = (s->avctx->width * s->avctx->height) / (4 * 4); | |
| 137 | |
| 138 /* traverse through the blocks */ | |
| 139 while (total_blocks) { | |
| 140 /* sanity checks */ | |
| 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); | |
| 145 return; | |
| 146 } | |
| 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); | |
| 151 return; | |
| 152 } | |
| 153 | |
| 154 opcode = s->buf[stream_ptr++]; | |
| 155 switch (opcode & 0xF0) { | |
| 156 /* skip n blocks */ | |
| 157 case 0x00: | |
| 158 case 0x10: | |
| 159 n_blocks = GET_BLOCK_COUNT(); | |
| 160 while (n_blocks--) { | |
| 161 ADVANCE_BLOCK(); | |
| 162 } | |
| 163 break; | |
| 164 | |
| 165 /* repeat last block n times */ | |
| 166 case 0x20: | |
| 167 case 0x30: | |
| 168 n_blocks = GET_BLOCK_COUNT(); | |
| 169 | |
| 170 /* sanity check */ | |
| 171 if ((row_ptr == 0) && (pixel_ptr == 0)) { | |
| 172 printf("encountered repeat block opcode (%02X) but no blocks rendered yet\n", | |
| 173 opcode & 0xF0); | |
| 174 break; | |
| 175 } | |
| 176 | |
| 177 /* figure out where the previous block started */ | |
| 178 if (pixel_ptr == 0) | |
| 179 prev_block_ptr1 = | |
| 180 (row_ptr - s->avctx->width * 4) + s->avctx->width - 4; | |
| 181 else | |
| 182 prev_block_ptr1 = row_ptr + pixel_ptr - 4; | |
| 183 | |
| 184 while (n_blocks--) { | |
| 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++]; | |
| 190 } | |
| 191 block_ptr += row_inc; | |
| 192 prev_block_ptr += row_inc; | |
| 193 } | |
| 194 ADVANCE_BLOCK(); | |
| 195 } | |
| 196 break; | |
| 197 | |
| 198 /* repeat previous pair of blocks n times */ | |
| 199 case 0x40: | |
| 200 case 0x50: | |
| 201 n_blocks = GET_BLOCK_COUNT(); | |
| 202 n_blocks *= 2; | |
| 203 | |
| 204 /* sanity check */ | |
| 205 if ((row_ptr == 0) && (pixel_ptr < 2 * 4)) { | |
| 206 printf("encountered repeat block opcode (%02X) but not enough blocks rendered yet\n", | |
| 207 opcode & 0xF0); | |
| 208 break; | |
| 209 } | |
| 210 | |
| 211 /* figure out where the previous 2 blocks started */ | |
| 212 if (pixel_ptr == 0) | |
| 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; | |
| 217 else | |
| 218 prev_block_ptr1 = row_ptr + pixel_ptr - 4 * 2; | |
| 219 | |
| 220 if (pixel_ptr == 0) | |
| 221 prev_block_ptr2 = (row_ptr - s->avctx->width * 4) + row_inc; | |
| 222 else | |
| 223 prev_block_ptr2 = row_ptr + pixel_ptr - 4; | |
| 224 | |
| 225 prev_block_flag = 0; | |
| 226 while (n_blocks--) { | |
| 227 block_ptr = row_ptr + pixel_ptr; | |
| 228 if (prev_block_flag) | |
| 229 prev_block_ptr = prev_block_ptr2; | |
| 230 else | |
| 231 prev_block_ptr = prev_block_ptr1; | |
| 232 prev_block_flag = !prev_block_flag; | |
| 233 | |
| 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++]; | |
| 237 } | |
| 238 block_ptr += row_inc; | |
| 239 prev_block_ptr += row_inc; | |
| 240 } | |
| 241 ADVANCE_BLOCK(); | |
| 242 } | |
| 243 break; | |
| 244 | |
| 245 /* 1-color block encoding */ | |
| 246 case 0x60: | |
| 247 case 0x70: | |
| 248 n_blocks = GET_BLOCK_COUNT(); | |
| 249 pixel = s->buf[stream_ptr++]; | |
| 250 | |
| 251 while (n_blocks--) { | |
| 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; | |
| 256 } | |
| 257 block_ptr += row_inc; | |
| 258 } | |
| 259 ADVANCE_BLOCK(); | |
| 260 } | |
| 261 break; | |
| 262 | |
| 263 /* 2-color block encoding */ | |
| 264 case 0x80: | |
| 265 case 0x90: | |
| 266 n_blocks = (opcode & 0x0F) + 1; | |
| 267 | |
| 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; | |
| 276 } | |
| 277 /* this is the base index to use for this block */ | |
| 278 color_table_index = CPAIR * color_pair_index; | |
| 279 color_pair_index++; | |
| 280 /* wraparound */ | |
| 281 if (color_pair_index == COLORS_PER_TABLE) | |
| 282 color_pair_index = 0; | |
| 283 } else | |
| 284 color_table_index = CPAIR * s->buf[stream_ptr++]; | |
| 285 | |
| 286 while (n_blocks--) { | |
| 287 color_flags = BE_16(&s->buf[stream_ptr]); | |
| 288 stream_ptr += 2; | |
| 289 flag_mask = 0x8000; | |
| 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; | |
| 295 else | |
| 296 pixel = color_table_index; | |
| 297 flag_mask >>= 1; | |
| 298 pixels[block_ptr++] = s->color_pairs[pixel]; | |
| 299 } | |
| 300 block_ptr += row_inc; | |
| 301 } | |
| 302 ADVANCE_BLOCK(); | |
| 303 } | |
| 304 break; | |
| 305 | |
| 306 /* 4-color block encoding */ | |
| 307 case 0xA0: | |
| 308 case 0xB0: | |
| 309 n_blocks = (opcode & 0x0F) + 1; | |
| 310 | |
| 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; | |
| 319 } | |
| 320 /* this is the base index to use for this block */ | |
| 321 color_table_index = CQUAD * color_quad_index; | |
| 322 color_quad_index++; | |
| 323 /* wraparound */ | |
| 324 if (color_quad_index == COLORS_PER_TABLE) | |
| 325 color_quad_index = 0; | |
| 326 } else | |
| 327 color_table_index = CQUAD * s->buf[stream_ptr++]; | |
| 328 | |
| 329 while (n_blocks--) { | |
| 330 color_flags = BE_32(&s->buf[stream_ptr]); | |
| 331 stream_ptr += 4; | |
| 332 /* flag mask actually acts as a bit shift count here */ | |
| 333 flag_mask = 30; | |
| 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); | |
| 339 flag_mask -= 2; | |
| 340 pixels[block_ptr++] = s->color_quads[pixel]; | |
| 341 } | |
| 342 block_ptr += row_inc; | |
| 343 } | |
| 344 ADVANCE_BLOCK(); | |
| 345 } | |
| 346 break; | |
| 347 | |
| 348 /* 8-color block encoding */ | |
| 349 case 0xC0: | |
| 350 case 0xD0: | |
| 351 n_blocks = (opcode & 0x0F) + 1; | |
| 352 | |
| 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; | |
| 361 } | |
| 362 /* this is the base index to use for this block */ | |
| 363 color_table_index = COCTET * color_octet_index; | |
| 364 color_octet_index++; | |
| 365 /* wraparound */ | |
| 366 if (color_octet_index == COLORS_PER_TABLE) | |
| 367 color_octet_index = 0; | |
| 368 } else | |
| 369 color_table_index = COCTET * s->buf[stream_ptr++]; | |
| 370 | |
| 371 while (n_blocks--) { | |
| 372 /* | |
| 373 For this input of 6 hex bytes: | |
| 374 01 23 45 67 89 AB | |
| 375 Mangle it to this output: | |
| 376 flags_a = xx012456, flags_b = xx89A37B | |
| 377 */ | |
| 378 /* build the color flags */ | |
| 379 color_flags_a = color_flags_b = 0; | |
| 380 color_flags_a = | |
| 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); | |
| 386 color_flags_b = | |
| 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); | |
| 392 stream_ptr += 6; | |
| 393 | |
| 394 color_flags = color_flags_a; | |
| 395 /* flag mask actually acts as a bit shift count here */ | |
| 396 flag_mask = 21; | |
| 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) */ | |
| 400 if (pixel_y == 2) { | |
| 401 color_flags = color_flags_b; | |
| 402 flag_mask = 21; | |
| 403 } | |
| 404 for (pixel_x = 0; pixel_x < 4; pixel_x++) { | |
| 405 pixel = color_table_index + | |
| 406 ((color_flags >> flag_mask) & 0x07); | |
| 407 flag_mask -= 3; | |
| 408 pixels[block_ptr++] = s->color_octets[pixel]; | |
| 409 } | |
| 410 block_ptr += row_inc; | |
| 411 } | |
| 412 ADVANCE_BLOCK(); | |
| 413 } | |
| 414 break; | |
| 415 | |
| 416 /* 16-color block encoding (every pixel is a different color) */ | |
| 417 case 0xE0: | |
| 418 n_blocks = (opcode & 0x0F) + 1; | |
| 419 | |
| 420 while (n_blocks--) { | |
| 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++]; | |
| 425 } | |
| 426 block_ptr += row_inc; | |
| 427 } | |
| 428 ADVANCE_BLOCK(); | |
| 429 } | |
| 430 break; | |
| 431 | |
| 432 case 0xF0: | |
| 433 printf("0xF0 opcode seen in SMC chunk (xine developers would like to know)\n"); | |
| 434 break; | |
| 435 } | |
| 436 } | |
| 437 } | |
| 438 | |
| 439 static int smc_decode_init(AVCodecContext *avctx) | |
| 440 { | |
| 441 SmcContext *s = (SmcContext *)avctx->priv_data; | |
| 442 | |
| 443 s->avctx = avctx; | |
| 444 avctx->pix_fmt = PIX_FMT_PAL8; | |
| 445 avctx->has_b_frames = 0; | |
| 446 dsputil_init(&s->dsp, avctx); | |
| 447 | |
| 1630 | 448 s->frame.data[0] = NULL; |
| 1610 | 449 |
| 450 return 0; | |
| 451 } | |
| 452 | |
| 453 static int smc_decode_frame(AVCodecContext *avctx, | |
| 454 void *data, int *data_size, | |
| 455 uint8_t *buf, int buf_size) | |
| 456 { | |
| 457 SmcContext *s = (SmcContext *)avctx->priv_data; | |
| 458 | |
| 1769 | 459 /* no supplementary picture */ |
| 460 if (buf_size == 0) | |
| 461 return 0; | |
| 1630 | 462 |
| 1610 | 463 s->buf = buf; |
| 464 s->size = buf_size; | |
| 465 | |
| 466 s->frame.reference = 1; | |
| 1769 | 467 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | |
| 468 FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE; | |
| 1630 | 469 if (avctx->reget_buffer(avctx, &s->frame)) { |
| 470 printf ("reget_buffer() failed\n"); | |
| 1610 | 471 return -1; |
| 472 } | |
| 473 | |
| 474 smc_decode_stream(s); | |
| 475 | |
| 476 *data_size = sizeof(AVFrame); | |
| 477 *(AVFrame*)data = s->frame; | |
| 478 | |
| 479 /* always report that the buffer was completely consumed */ | |
| 480 return buf_size; | |
| 481 } | |
| 482 | |
| 483 static int smc_decode_end(AVCodecContext *avctx) | |
| 484 { | |
| 485 SmcContext *s = (SmcContext *)avctx->priv_data; | |
| 486 | |
| 1630 | 487 if (s->frame.data[0]) |
| 488 avctx->release_buffer(avctx, &s->frame); | |
| 1610 | 489 |
| 490 return 0; | |
| 491 } | |
| 492 | |
| 493 AVCodec smc_decoder = { | |
| 494 "smc", | |
| 495 CODEC_TYPE_VIDEO, | |
| 496 CODEC_ID_SMC, | |
| 497 sizeof(SmcContext), | |
| 498 smc_decode_init, | |
| 499 NULL, | |
| 500 smc_decode_end, | |
| 501 smc_decode_frame, | |
| 502 CODEC_CAP_DR1, | |
| 503 }; |
