00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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 GetByteContext gb;
00129 uint8_t *out;
00130 int stride, i, x, y, b, bt = 0;
00131
00132 c93->currentpic ^= 1;
00133
00134 newpic->reference = 3;
00135 newpic->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
00136 FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
00137 if (avctx->reget_buffer(avctx, newpic)) {
00138 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00139 return -1;
00140 }
00141
00142 stride = newpic->linesize[0];
00143
00144 bytestream2_init(&gb, buf, buf_size);
00145 b = bytestream2_get_byte(&gb);
00146 if (b & C93_FIRST_FRAME) {
00147 newpic->pict_type = AV_PICTURE_TYPE_I;
00148 newpic->key_frame = 1;
00149 } else {
00150 newpic->pict_type = AV_PICTURE_TYPE_P;
00151 newpic->key_frame = 0;
00152 }
00153
00154 for (y = 0; y < HEIGHT; y += 8) {
00155 out = newpic->data[0] + y * stride;
00156 for (x = 0; x < WIDTH; x += 8) {
00157 uint8_t *copy_from = oldpic->data[0];
00158 unsigned int offset, j;
00159 uint8_t cols[4], grps[4];
00160 C93BlockType block_type;
00161
00162 if (!bt)
00163 bt = bytestream2_get_byte(&gb);
00164
00165 block_type= bt & 0x0F;
00166 switch (block_type) {
00167 case C93_8X8_FROM_PREV:
00168 offset = bytestream2_get_le16(&gb);
00169 if (copy_block(avctx, out, copy_from, offset, 8, stride))
00170 return -1;
00171 break;
00172
00173 case C93_4X4_FROM_CURR:
00174 copy_from = newpic->data[0];
00175 case C93_4X4_FROM_PREV:
00176 for (j = 0; j < 8; j += 4) {
00177 for (i = 0; i < 8; i += 4) {
00178 offset = bytestream2_get_le16(&gb);
00179 if (copy_block(avctx, &out[j*stride+i],
00180 copy_from, offset, 4, stride))
00181 return -1;
00182 }
00183 }
00184 break;
00185
00186 case C93_8X8_2COLOR:
00187 bytestream2_get_buffer(&gb, cols, 2);
00188 for (i = 0; i < 8; i++) {
00189 draw_n_color(out + i*stride, stride, 8, 1, 1, cols,
00190 NULL, bytestream2_get_byte(&gb));
00191 }
00192
00193 break;
00194
00195 case C93_4X4_2COLOR:
00196 case C93_4X4_4COLOR:
00197 case C93_4X4_4COLOR_GRP:
00198 for (j = 0; j < 8; j += 4) {
00199 for (i = 0; i < 8; i += 4) {
00200 if (block_type == C93_4X4_2COLOR) {
00201 bytestream2_get_buffer(&gb, cols, 2);
00202 draw_n_color(out + i + j*stride, stride, 4, 4,
00203 1, cols, NULL, bytestream2_get_le16(&gb));
00204 } else if (block_type == C93_4X4_4COLOR) {
00205 bytestream2_get_buffer(&gb, cols, 4);
00206 draw_n_color(out + i + j*stride, stride, 4, 4,
00207 2, cols, NULL, bytestream2_get_le32(&gb));
00208 } else {
00209 bytestream2_get_buffer(&gb, grps, 4);
00210 draw_n_color(out + i + j*stride, stride, 4, 4,
00211 1, cols, grps, bytestream2_get_le16(&gb));
00212 }
00213 }
00214 }
00215 break;
00216
00217 case C93_NOOP:
00218 break;
00219
00220 case C93_8X8_INTRA:
00221 for (j = 0; j < 8; j++)
00222 bytestream2_get_buffer(&gb, out + j*stride, 8);
00223 break;
00224
00225 default:
00226 av_log(avctx, AV_LOG_ERROR, "unexpected type %x at %dx%d\n",
00227 block_type, x, y);
00228 return -1;
00229 }
00230 bt >>= 4;
00231 out += 8;
00232 }
00233 }
00234
00235 if (b & C93_HAS_PALETTE) {
00236 uint32_t *palette = (uint32_t *) newpic->data[1];
00237 for (i = 0; i < 256; i++) {
00238 palette[i] = 0xFFU << 24 | bytestream2_get_be24(&gb);
00239 }
00240 } else {
00241 if (oldpic->data[1])
00242 memcpy(newpic->data[1], oldpic->data[1], 256 * 4);
00243 }
00244
00245 *picture = *newpic;
00246 *data_size = sizeof(AVFrame);
00247
00248 return buf_size;
00249 }
00250
00251 AVCodec ff_c93_decoder = {
00252 .name = "c93",
00253 .type = AVMEDIA_TYPE_VIDEO,
00254 .id = CODEC_ID_C93,
00255 .priv_data_size = sizeof(C93DecoderContext),
00256 .init = decode_init,
00257 .close = decode_end,
00258 .decode = decode_frame,
00259 .capabilities = CODEC_CAP_DR1,
00260 .long_name = NULL_IF_CONFIG_SMALL("Interplay C93"),
00261 };