00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034
00035 #include "libavutil/intreadwrite.h"
00036 #include "avcodec.h"
00037 #include "bytestream.h"
00038
00039 #define CPAIR 2
00040 #define CQUAD 4
00041 #define COCTET 8
00042
00043 #define COLORS_PER_TABLE 256
00044
00045 typedef struct SmcContext {
00046
00047 AVCodecContext *avctx;
00048 AVFrame frame;
00049
00050 GetByteContext gb;
00051
00052
00053 unsigned char color_pairs[COLORS_PER_TABLE * CPAIR];
00054 unsigned char color_quads[COLORS_PER_TABLE * CQUAD];
00055 unsigned char color_octets[COLORS_PER_TABLE * COCTET];
00056
00057 uint32_t pal[256];
00058 } SmcContext;
00059
00060 #define GET_BLOCK_COUNT() \
00061 (opcode & 0x10) ? (1 + bytestream2_get_byte(&s->gb)) : 1 + (opcode & 0x0F);
00062
00063 #define ADVANCE_BLOCK() \
00064 { \
00065 pixel_ptr += 4; \
00066 if (pixel_ptr >= width) \
00067 { \
00068 pixel_ptr = 0; \
00069 row_ptr += stride * 4; \
00070 } \
00071 total_blocks--; \
00072 if (total_blocks < 0) \
00073 { \
00074 av_log(s->avctx, AV_LOG_INFO, "warning: block counter just went negative (this should not happen)\n"); \
00075 return; \
00076 } \
00077 }
00078
00079 static void smc_decode_stream(SmcContext *s)
00080 {
00081 int width = s->avctx->width;
00082 int height = s->avctx->height;
00083 int stride = s->frame.linesize[0];
00084 int i;
00085 int chunk_size;
00086 int buf_size = bytestream2_size(&s->gb);
00087 unsigned char opcode;
00088 int n_blocks;
00089 unsigned int color_flags;
00090 unsigned int color_flags_a;
00091 unsigned int color_flags_b;
00092 unsigned int flag_mask;
00093
00094 unsigned char *pixels = s->frame.data[0];
00095
00096 int image_size = height * s->frame.linesize[0];
00097 int row_ptr = 0;
00098 int pixel_ptr = 0;
00099 int pixel_x, pixel_y;
00100 int row_inc = stride - 4;
00101 int block_ptr;
00102 int prev_block_ptr;
00103 int prev_block_ptr1, prev_block_ptr2;
00104 int prev_block_flag;
00105 int total_blocks;
00106 int color_table_index;
00107 int pixel;
00108
00109 int color_pair_index = 0;
00110 int color_quad_index = 0;
00111 int color_octet_index = 0;
00112
00113
00114 memcpy(s->frame.data[1], s->pal, AVPALETTE_SIZE);
00115
00116 bytestream2_skip(&s->gb, 1);
00117 chunk_size = bytestream2_get_be24(&s->gb);
00118 if (chunk_size != buf_size)
00119 av_log(s->avctx, AV_LOG_INFO, "warning: MOV chunk size != encoded chunk size (%d != %d); using MOV chunk size\n",
00120 chunk_size, buf_size);
00121
00122 chunk_size = buf_size;
00123 total_blocks = ((s->avctx->width + 3) / 4) * ((s->avctx->height + 3) / 4);
00124
00125
00126 while (total_blocks) {
00127
00128
00129 if (row_ptr >= image_size) {
00130 av_log(s->avctx, AV_LOG_INFO, "SMC decoder just went out of bounds (row ptr = %d, height = %d)\n",
00131 row_ptr, image_size);
00132 return;
00133 }
00134
00135 opcode = bytestream2_get_byte(&s->gb);
00136 switch (opcode & 0xF0) {
00137
00138 case 0x00:
00139 case 0x10:
00140 n_blocks = GET_BLOCK_COUNT();
00141 while (n_blocks--) {
00142 ADVANCE_BLOCK();
00143 }
00144 break;
00145
00146
00147 case 0x20:
00148 case 0x30:
00149 n_blocks = GET_BLOCK_COUNT();
00150
00151
00152 if ((row_ptr == 0) && (pixel_ptr == 0)) {
00153 av_log(s->avctx, AV_LOG_INFO, "encountered repeat block opcode (%02X) but no blocks rendered yet\n",
00154 opcode & 0xF0);
00155 return;
00156 }
00157
00158
00159 if (pixel_ptr == 0)
00160 prev_block_ptr1 =
00161 (row_ptr - s->avctx->width * 4) + s->avctx->width - 4;
00162 else
00163 prev_block_ptr1 = row_ptr + pixel_ptr - 4;
00164
00165 while (n_blocks--) {
00166 block_ptr = row_ptr + pixel_ptr;
00167 prev_block_ptr = prev_block_ptr1;
00168 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
00169 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
00170 pixels[block_ptr++] = pixels[prev_block_ptr++];
00171 }
00172 block_ptr += row_inc;
00173 prev_block_ptr += row_inc;
00174 }
00175 ADVANCE_BLOCK();
00176 }
00177 break;
00178
00179
00180 case 0x40:
00181 case 0x50:
00182 n_blocks = GET_BLOCK_COUNT();
00183 n_blocks *= 2;
00184
00185
00186 if ((row_ptr == 0) && (pixel_ptr < 2 * 4)) {
00187 av_log(s->avctx, AV_LOG_INFO, "encountered repeat block opcode (%02X) but not enough blocks rendered yet\n",
00188 opcode & 0xF0);
00189 return;
00190 }
00191
00192
00193 if (pixel_ptr == 0)
00194 prev_block_ptr1 = (row_ptr - s->avctx->width * 4) +
00195 s->avctx->width - 4 * 2;
00196 else if (pixel_ptr == 4)
00197 prev_block_ptr1 = (row_ptr - s->avctx->width * 4) + row_inc;
00198 else
00199 prev_block_ptr1 = row_ptr + pixel_ptr - 4 * 2;
00200
00201 if (pixel_ptr == 0)
00202 prev_block_ptr2 = (row_ptr - s->avctx->width * 4) + row_inc;
00203 else
00204 prev_block_ptr2 = row_ptr + pixel_ptr - 4;
00205
00206 prev_block_flag = 0;
00207 while (n_blocks--) {
00208 block_ptr = row_ptr + pixel_ptr;
00209 if (prev_block_flag)
00210 prev_block_ptr = prev_block_ptr2;
00211 else
00212 prev_block_ptr = prev_block_ptr1;
00213 prev_block_flag = !prev_block_flag;
00214
00215 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
00216 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
00217 pixels[block_ptr++] = pixels[prev_block_ptr++];
00218 }
00219 block_ptr += row_inc;
00220 prev_block_ptr += row_inc;
00221 }
00222 ADVANCE_BLOCK();
00223 }
00224 break;
00225
00226
00227 case 0x60:
00228 case 0x70:
00229 n_blocks = GET_BLOCK_COUNT();
00230 pixel = bytestream2_get_byte(&s->gb);
00231
00232 while (n_blocks--) {
00233 block_ptr = row_ptr + pixel_ptr;
00234 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
00235 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
00236 pixels[block_ptr++] = pixel;
00237 }
00238 block_ptr += row_inc;
00239 }
00240 ADVANCE_BLOCK();
00241 }
00242 break;
00243
00244
00245 case 0x80:
00246 case 0x90:
00247 n_blocks = (opcode & 0x0F) + 1;
00248
00249
00250 if ((opcode & 0xF0) == 0x80) {
00251
00252
00253 for (i = 0; i < CPAIR; i++) {
00254 pixel = bytestream2_get_byte(&s->gb);
00255 color_table_index = CPAIR * color_pair_index + i;
00256 s->color_pairs[color_table_index] = pixel;
00257 }
00258
00259 color_table_index = CPAIR * color_pair_index;
00260 color_pair_index++;
00261
00262 if (color_pair_index == COLORS_PER_TABLE)
00263 color_pair_index = 0;
00264 } else
00265 color_table_index = CPAIR * bytestream2_get_byte(&s->gb);
00266
00267 while (n_blocks--) {
00268 color_flags = bytestream2_get_be16(&s->gb);
00269 flag_mask = 0x8000;
00270 block_ptr = row_ptr + pixel_ptr;
00271 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
00272 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
00273 if (color_flags & flag_mask)
00274 pixel = color_table_index + 1;
00275 else
00276 pixel = color_table_index;
00277 flag_mask >>= 1;
00278 pixels[block_ptr++] = s->color_pairs[pixel];
00279 }
00280 block_ptr += row_inc;
00281 }
00282 ADVANCE_BLOCK();
00283 }
00284 break;
00285
00286
00287 case 0xA0:
00288 case 0xB0:
00289 n_blocks = (opcode & 0x0F) + 1;
00290
00291
00292 if ((opcode & 0xF0) == 0xA0) {
00293
00294
00295 for (i = 0; i < CQUAD; i++) {
00296 pixel = bytestream2_get_byte(&s->gb);
00297 color_table_index = CQUAD * color_quad_index + i;
00298 s->color_quads[color_table_index] = pixel;
00299 }
00300
00301 color_table_index = CQUAD * color_quad_index;
00302 color_quad_index++;
00303
00304 if (color_quad_index == COLORS_PER_TABLE)
00305 color_quad_index = 0;
00306 } else
00307 color_table_index = CQUAD * bytestream2_get_byte(&s->gb);
00308
00309 while (n_blocks--) {
00310 color_flags = bytestream2_get_be32(&s->gb);
00311
00312 flag_mask = 30;
00313 block_ptr = row_ptr + pixel_ptr;
00314 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
00315 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
00316 pixel = color_table_index +
00317 ((color_flags >> flag_mask) & 0x03);
00318 flag_mask -= 2;
00319 pixels[block_ptr++] = s->color_quads[pixel];
00320 }
00321 block_ptr += row_inc;
00322 }
00323 ADVANCE_BLOCK();
00324 }
00325 break;
00326
00327
00328 case 0xC0:
00329 case 0xD0:
00330 n_blocks = (opcode & 0x0F) + 1;
00331
00332
00333 if ((opcode & 0xF0) == 0xC0) {
00334
00335
00336 for (i = 0; i < COCTET; i++) {
00337 pixel = bytestream2_get_byte(&s->gb);
00338 color_table_index = COCTET * color_octet_index + i;
00339 s->color_octets[color_table_index] = pixel;
00340 }
00341
00342 color_table_index = COCTET * color_octet_index;
00343 color_octet_index++;
00344
00345 if (color_octet_index == COLORS_PER_TABLE)
00346 color_octet_index = 0;
00347 } else
00348 color_table_index = COCTET * bytestream2_get_byte(&s->gb);
00349
00350 while (n_blocks--) {
00351
00352
00353
00354
00355
00356
00357
00358 int val1 = bytestream2_get_be16(&s->gb);
00359 int val2 = bytestream2_get_be16(&s->gb);
00360 int val3 = bytestream2_get_be16(&s->gb);
00361 color_flags_a = ((val1 & 0xFFF0) << 8) | (val2 >> 4);
00362 color_flags_b = ((val3 & 0xFFF0) << 8) |
00363 ((val1 & 0x0F) << 8) | ((val2 & 0x0F) << 4) | (val3 & 0x0F);
00364
00365 color_flags = color_flags_a;
00366
00367 flag_mask = 21;
00368 block_ptr = row_ptr + pixel_ptr;
00369 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
00370
00371 if (pixel_y == 2) {
00372 color_flags = color_flags_b;
00373 flag_mask = 21;
00374 }
00375 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
00376 pixel = color_table_index +
00377 ((color_flags >> flag_mask) & 0x07);
00378 flag_mask -= 3;
00379 pixels[block_ptr++] = s->color_octets[pixel];
00380 }
00381 block_ptr += row_inc;
00382 }
00383 ADVANCE_BLOCK();
00384 }
00385 break;
00386
00387
00388 case 0xE0:
00389 n_blocks = (opcode & 0x0F) + 1;
00390
00391 while (n_blocks--) {
00392 block_ptr = row_ptr + pixel_ptr;
00393 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
00394 for (pixel_x = 0; pixel_x < 4; pixel_x++) {
00395 pixels[block_ptr++] = bytestream2_get_byte(&s->gb);
00396 }
00397 block_ptr += row_inc;
00398 }
00399 ADVANCE_BLOCK();
00400 }
00401 break;
00402
00403 case 0xF0:
00404 av_log_missing_feature(s->avctx, "0xF0 opcode", 1);
00405 break;
00406 }
00407 }
00408
00409 return;
00410 }
00411
00412 static av_cold int smc_decode_init(AVCodecContext *avctx)
00413 {
00414 SmcContext *s = avctx->priv_data;
00415
00416 s->avctx = avctx;
00417 avctx->pix_fmt = PIX_FMT_PAL8;
00418
00419 avcodec_get_frame_defaults(&s->frame);
00420 s->frame.data[0] = NULL;
00421
00422 return 0;
00423 }
00424
00425 static int smc_decode_frame(AVCodecContext *avctx,
00426 void *data, int *data_size,
00427 AVPacket *avpkt)
00428 {
00429 const uint8_t *buf = avpkt->data;
00430 int buf_size = avpkt->size;
00431 SmcContext *s = avctx->priv_data;
00432 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
00433
00434 bytestream2_init(&s->gb, buf, buf_size);
00435
00436 s->frame.reference = 3;
00437 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
00438 FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
00439 if (avctx->reget_buffer(avctx, &s->frame)) {
00440 av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00441 return -1;
00442 }
00443
00444 if (pal) {
00445 s->frame.palette_has_changed = 1;
00446 memcpy(s->pal, pal, AVPALETTE_SIZE);
00447 }
00448
00449 smc_decode_stream(s);
00450
00451 *data_size = sizeof(AVFrame);
00452 *(AVFrame*)data = s->frame;
00453
00454
00455 return buf_size;
00456 }
00457
00458 static av_cold int smc_decode_end(AVCodecContext *avctx)
00459 {
00460 SmcContext *s = avctx->priv_data;
00461
00462 if (s->frame.data[0])
00463 avctx->release_buffer(avctx, &s->frame);
00464
00465 return 0;
00466 }
00467
00468 AVCodec ff_smc_decoder = {
00469 .name = "smc",
00470 .type = AVMEDIA_TYPE_VIDEO,
00471 .id = CODEC_ID_SMC,
00472 .priv_data_size = sizeof(SmcContext),
00473 .init = smc_decode_init,
00474 .close = smc_decode_end,
00475 .decode = smc_decode_frame,
00476 .capabilities = CODEC_CAP_DR1,
00477 .long_name = NULL_IF_CONFIG_SMALL("QuickTime Graphics (SMC)"),
00478 };