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

libavcodec/c93.c

Go to the documentation of this file.
00001 /*
00002  * Interplay C93 video decoder
00003  * Copyright (c) 2007 Anssi Hannula <anssi.hannula@gmail.com>
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "avcodec.h"
00023 #include "bytestream.h"
00024 
00025 typedef struct {
00026     AVFrame pictures[2];
00027     int currentpic;
00028 } C93DecoderContext;
00029 
00030 typedef enum {
00031     C93_8X8_FROM_PREV  = 0x02,
00032     C93_4X4_FROM_PREV  = 0x06,
00033     C93_4X4_FROM_CURR  = 0x07,
00034     C93_8X8_2COLOR     = 0x08,
00035     C93_4X4_2COLOR     = 0x0A,
00036     C93_4X4_4COLOR_GRP = 0x0B,
00037     C93_4X4_4COLOR     = 0x0D,
00038     C93_NOOP           = 0x0E,
00039     C93_8X8_INTRA      = 0x0F,
00040 } C93BlockType;
00041 
00042 #define WIDTH   320
00043 #define HEIGHT  192
00044 
00045 #define C93_HAS_PALETTE 0x01
00046 #define C93_FIRST_FRAME 0x02
00047 
00048 static av_cold int decode_init(AVCodecContext *avctx)
00049 {
00050     C93DecoderContext * const c93 = avctx->priv_data;
00051 
00052     avcodec_get_frame_defaults(&c93->pictures[0]);
00053     avcodec_get_frame_defaults(&c93->pictures[1]);
00054     avctx->pix_fmt = PIX_FMT_PAL8;
00055     return 0;
00056 }
00057 
00058 static av_cold int decode_end(AVCodecContext *avctx)
00059 {
00060     C93DecoderContext * const c93 = avctx->priv_data;
00061 
00062     if (c93->pictures[0].data[0])
00063         avctx->release_buffer(avctx, &c93->pictures[0]);
00064     if (c93->pictures[1].data[0])
00065         avctx->release_buffer(avctx, &c93->pictures[1]);
00066     return 0;
00067 }
00068 
00069 static inline int copy_block(AVCodecContext *avctx, uint8_t *to,
00070         uint8_t *from, int offset, int height, int stride)
00071 {
00072     int i;
00073     int width = height;
00074     int from_x = offset % WIDTH;
00075     int from_y = offset / WIDTH;
00076     int overflow = from_x + width - WIDTH;
00077 
00078     if (!from) {
00079         /* silently ignoring predictive blocks in first frame */
00080         return 0;
00081     }
00082 
00083     if (from_y + height > HEIGHT) {
00084         av_log(avctx, AV_LOG_ERROR, "invalid offset %d during C93 decoding\n",
00085                offset);
00086         return -1;
00087     }
00088 
00089     if (overflow > 0) {
00090         width -= overflow;
00091         for (i = 0; i < height; i++) {
00092             memcpy(&to[i*stride+width], &from[(from_y+i)*stride], overflow);
00093         }
00094     }
00095 
00096     for (i = 0; i < height; i++) {
00097         memcpy(&to[i*stride], &from[(from_y+i)*stride+from_x], width);
00098     }
00099 
00100     return 0;
00101 }
00102 
00103 static inline void draw_n_color(uint8_t *out, int stride, int width,
00104          int height, int bpp, uint8_t cols[4], uint8_t grps[4], uint32_t col)
00105 {
00106     int x, y;
00107     for (y = 0; y < height; y++) {
00108         if (grps)
00109             cols[0] = grps[3 * (y >> 1)];
00110         for (x = 0; x < width; x++) {
00111             if (grps)
00112                 cols[1]= grps[(x >> 1) + 1];
00113             out[x + y*stride] = cols[col & ((1 << bpp) - 1)];
00114             col >>= bpp;
00115         }
00116     }
00117 }
00118 
00119 static int decode_frame(AVCodecContext *avctx, void *data,
00120                             int *data_size, AVPacket *avpkt)
00121 {
00122     const uint8_t *buf = avpkt->data;
00123     int buf_size = avpkt->size;
00124     C93DecoderContext * const c93 = avctx->priv_data;
00125     AVFrame * const newpic = &c93->pictures[c93->currentpic];
00126     AVFrame * const oldpic = &c93->pictures[c93->currentpic^1];
00127     AVFrame *picture = data;
00128     uint8_t *out;
00129     int stride, i, x, y, bt = 0;
00130 
00131     c93->currentpic ^= 1;
00132 
00133     newpic->reference = 1;
00134     newpic->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
00135                          FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
00136     if (avctx->reget_buffer(avctx, newpic)) {
00137         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00138         return -1;
00139     }
00140 
00141     stride = newpic->linesize[0];
00142 
00143     if (buf[0] & C93_FIRST_FRAME) {
00144         newpic->pict_type = AV_PICTURE_TYPE_I;
00145         newpic->key_frame = 1;
00146     } else {
00147         newpic->pict_type = AV_PICTURE_TYPE_P;
00148         newpic->key_frame = 0;
00149     }
00150 
00151     if (*buf++ & C93_HAS_PALETTE) {
00152         uint32_t *palette = (uint32_t *) newpic->data[1];
00153         const uint8_t *palbuf = buf + buf_size - 768 - 1;
00154         for (i = 0; i < 256; i++) {
00155             palette[i] = bytestream_get_be24(&palbuf);
00156         }
00157     } else {
00158         if (oldpic->data[1])
00159             memcpy(newpic->data[1], oldpic->data[1], 256 * 4);
00160     }
00161 
00162     for (y = 0; y < HEIGHT; y += 8) {
00163         out = newpic->data[0] + y * stride;
00164         for (x = 0; x < WIDTH; x += 8) {
00165             uint8_t *copy_from = oldpic->data[0];
00166             unsigned int offset, j;
00167             uint8_t cols[4], grps[4];
00168             C93BlockType block_type;
00169 
00170             if (!bt)
00171                 bt = *buf++;
00172 
00173             block_type= bt & 0x0F;
00174             switch (block_type) {
00175             case C93_8X8_FROM_PREV:
00176                 offset = bytestream_get_le16(&buf);
00177                 if (copy_block(avctx, out, copy_from, offset, 8, stride))
00178                     return -1;
00179                 break;
00180 
00181             case C93_4X4_FROM_CURR:
00182                 copy_from = newpic->data[0];
00183             case C93_4X4_FROM_PREV:
00184                 for (j = 0; j < 8; j += 4) {
00185                     for (i = 0; i < 8; i += 4) {
00186                         offset = bytestream_get_le16(&buf);
00187                         if (copy_block(avctx, &out[j*stride+i],
00188                                            copy_from, offset, 4, stride))
00189                             return -1;
00190                     }
00191                 }
00192                 break;
00193 
00194             case C93_8X8_2COLOR:
00195                 bytestream_get_buffer(&buf, cols, 2);
00196                 for (i = 0; i < 8; i++) {
00197                     draw_n_color(out + i*stride, stride, 8, 1, 1, cols,
00198                                      NULL, *buf++);
00199                 }
00200 
00201                 break;
00202 
00203             case C93_4X4_2COLOR:
00204             case C93_4X4_4COLOR:
00205             case C93_4X4_4COLOR_GRP:
00206                 for (j = 0; j < 8; j += 4) {
00207                     for (i = 0; i < 8; i += 4) {
00208                         if (block_type == C93_4X4_2COLOR) {
00209                             bytestream_get_buffer(&buf, cols, 2);
00210                             draw_n_color(out + i + j*stride, stride, 4, 4,
00211                                     1, cols, NULL, bytestream_get_le16(&buf));
00212                         } else if (block_type == C93_4X4_4COLOR) {
00213                             bytestream_get_buffer(&buf, cols, 4);
00214                             draw_n_color(out + i + j*stride, stride, 4, 4,
00215                                     2, cols, NULL, bytestream_get_le32(&buf));
00216                         } else {
00217                             bytestream_get_buffer(&buf, grps, 4);
00218                             draw_n_color(out + i + j*stride, stride, 4, 4,
00219                                     1, cols, grps, bytestream_get_le16(&buf));
00220                         }
00221                     }
00222                 }
00223                 break;
00224 
00225             case C93_NOOP:
00226                 break;
00227 
00228             case C93_8X8_INTRA:
00229                 for (j = 0; j < 8; j++)
00230                     bytestream_get_buffer(&buf, out + j*stride, 8);
00231                 break;
00232 
00233             default:
00234                 av_log(avctx, AV_LOG_ERROR, "unexpected type %x at %dx%d\n",
00235                        block_type, x, y);
00236                 return -1;
00237             }
00238             bt >>= 4;
00239             out += 8;
00240         }
00241     }
00242 
00243     *picture = *newpic;
00244     *data_size = sizeof(AVFrame);
00245 
00246     return buf_size;
00247 }
00248 
00249 AVCodec ff_c93_decoder = {
00250     "c93",
00251     AVMEDIA_TYPE_VIDEO,
00252     CODEC_ID_C93,
00253     sizeof(C93DecoderContext),
00254     decode_init,
00255     NULL,
00256     decode_end,
00257     decode_frame,
00258     CODEC_CAP_DR1,
00259     .long_name = NULL_IF_CONFIG_SMALL("Interplay C93"),
00260 };

Generated on Wed Apr 11 2012 07:31:32 for FFmpeg by  doxygen 1.7.1