• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/smc.c

Go to the documentation of this file.
00001 /*
00002  * Quicktime Graphics (SMC) Video Decoder
00003  * Copyright (C) 2003 the ffmpeg project
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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     /* SMC color tables */
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 = (int) (s->gb.buffer_end - s->gb.buffer_start);
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;  /* indexes to color pair, quad, or octet tables */
00107     int pixel;
00108 
00109     int color_pair_index = 0;
00110     int color_quad_index = 0;
00111     int color_octet_index = 0;
00112 
00113     /* make the palette available */
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     /* traverse through the blocks */
00126     while (total_blocks) {
00127         /* sanity checks */
00128         /* make sure the row pointer hasn't gone wild */
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         /* skip n blocks */
00138         case 0x00:
00139         case 0x10:
00140             n_blocks = GET_BLOCK_COUNT();
00141             while (n_blocks--) {
00142                 ADVANCE_BLOCK();
00143             }
00144             break;
00145 
00146         /* repeat last block n times */
00147         case 0x20:
00148         case 0x30:
00149             n_blocks = GET_BLOCK_COUNT();
00150 
00151             /* sanity check */
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             /* figure out where the previous block started */
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         /* repeat previous pair of blocks n times */
00180         case 0x40:
00181         case 0x50:
00182             n_blocks = GET_BLOCK_COUNT();
00183             n_blocks *= 2;
00184 
00185             /* sanity check */
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             /* figure out where the previous 2 blocks started */
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         /* 1-color block encoding */
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         /* 2-color block encoding */
00245         case 0x80:
00246         case 0x90:
00247             n_blocks = (opcode & 0x0F) + 1;
00248 
00249             /* figure out which color pair to use to paint the 2-color block */
00250             if ((opcode & 0xF0) == 0x80) {
00251                 /* fetch the next 2 colors from bytestream and store in next
00252                  * available entry in the color pair table */
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                 /* this is the base index to use for this block */
00259                 color_table_index = CPAIR * color_pair_index;
00260                 color_pair_index++;
00261                 /* wraparound */
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         /* 4-color block encoding */
00287         case 0xA0:
00288         case 0xB0:
00289             n_blocks = (opcode & 0x0F) + 1;
00290 
00291             /* figure out which color quad to use to paint the 4-color block */
00292             if ((opcode & 0xF0) == 0xA0) {
00293                 /* fetch the next 4 colors from bytestream and store in next
00294                  * available entry in the color quad table */
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                 /* this is the base index to use for this block */
00301                 color_table_index = CQUAD * color_quad_index;
00302                 color_quad_index++;
00303                 /* wraparound */
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                 /* flag mask actually acts as a bit shift count here */
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         /* 8-color block encoding */
00328         case 0xC0:
00329         case 0xD0:
00330             n_blocks = (opcode & 0x0F) + 1;
00331 
00332             /* figure out which color octet to use to paint the 8-color block */
00333             if ((opcode & 0xF0) == 0xC0) {
00334                 /* fetch the next 8 colors from bytestream and store in next
00335                  * available entry in the color octet table */
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                 /* this is the base index to use for this block */
00342                 color_table_index = COCTET * color_octet_index;
00343                 color_octet_index++;
00344                 /* wraparound */
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                   For this input of 6 hex bytes:
00353                     01 23 45 67 89 AB
00354                   Mangle it to this output:
00355                     flags_a = xx012456, flags_b = xx89A37B
00356                 */
00357                 /* build the color flags */
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                 /* flag mask actually acts as a bit shift count here */
00367                 flag_mask = 21;
00368                 block_ptr = row_ptr + pixel_ptr;
00369                 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
00370                     /* reload flags at third row (iteration pixel_y == 2) */
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         /* 16-color block encoding (every pixel is a different color) */
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     s->frame.data[0] = NULL;
00420 
00421     return 0;
00422 }
00423 
00424 static int smc_decode_frame(AVCodecContext *avctx,
00425                              void *data, int *data_size,
00426                              AVPacket *avpkt)
00427 {
00428     const uint8_t *buf = avpkt->data;
00429     int buf_size = avpkt->size;
00430     SmcContext *s = avctx->priv_data;
00431     const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
00432 
00433     bytestream2_init(&s->gb, buf, buf_size);
00434 
00435     s->frame.reference = 1;
00436     s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
00437                             FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
00438     if (avctx->reget_buffer(avctx, &s->frame)) {
00439         av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00440         return -1;
00441     }
00442 
00443     if (pal) {
00444         s->frame.palette_has_changed = 1;
00445         memcpy(s->pal, pal, AVPALETTE_SIZE);
00446     }
00447 
00448     smc_decode_stream(s);
00449 
00450     *data_size = sizeof(AVFrame);
00451     *(AVFrame*)data = s->frame;
00452 
00453     /* always report that the buffer was completely consumed */
00454     return buf_size;
00455 }
00456 
00457 static av_cold int smc_decode_end(AVCodecContext *avctx)
00458 {
00459     SmcContext *s = avctx->priv_data;
00460 
00461     if (s->frame.data[0])
00462         avctx->release_buffer(avctx, &s->frame);
00463 
00464     return 0;
00465 }
00466 
00467 AVCodec ff_smc_decoder = {
00468     .name           = "smc",
00469     .type           = AVMEDIA_TYPE_VIDEO,
00470     .id             = CODEC_ID_SMC,
00471     .priv_data_size = sizeof(SmcContext),
00472     .init           = smc_decode_init,
00473     .close          = smc_decode_end,
00474     .decode         = smc_decode_frame,
00475     .capabilities   = CODEC_CAP_DR1,
00476     .long_name = NULL_IF_CONFIG_SMALL("QuickTime Graphics (SMC)"),
00477 };
Generated on Thu Jan 24 2013 17:08:53 for Libav by doxygen 1.7.1