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 #include "libavutil/avassert.h"
00025
00026 enum BMVFlags{
00027 BMV_NOP = 0,
00028 BMV_END,
00029 BMV_DELTA,
00030 BMV_INTRA,
00031
00032 BMV_SCROLL = 0x04,
00033 BMV_PALETTE = 0x08,
00034 BMV_COMMAND = 0x10,
00035 BMV_AUDIO = 0x20,
00036 BMV_EXT = 0x40,
00037 BMV_PRINT = 0x80
00038 };
00039
00040 #define SCREEN_WIDE 640
00041 #define SCREEN_HIGH 429
00042
00043 typedef struct BMVDecContext {
00044 AVCodecContext *avctx;
00045 AVFrame pic;
00046
00047 uint8_t *frame, frame_base[SCREEN_WIDE * (SCREEN_HIGH + 1)];
00048 uint32_t pal[256];
00049 const uint8_t *stream;
00050 } BMVDecContext;
00051
00052 #define NEXT_BYTE(v) v = forward ? v + 1 : v - 1;
00053
00054 static int decode_bmv_frame(const uint8_t *source, int src_len, uint8_t *frame, int frame_off)
00055 {
00056 unsigned val, saved_val = 0;
00057 int tmplen = src_len;
00058 const uint8_t *src, *source_end = source + src_len;
00059 uint8_t *frame_end = frame + SCREEN_WIDE * SCREEN_HIGH;
00060 uint8_t *dst, *dst_end;
00061 int len, mask;
00062 int forward = (frame_off <= -SCREEN_WIDE) || (frame_off >= 0);
00063 int read_two_nibbles, flag;
00064 int advance_mode;
00065 int mode = 0;
00066 int i;
00067
00068 if (src_len <= 0)
00069 return -1;
00070
00071 if (forward) {
00072 src = source;
00073 dst = frame;
00074 dst_end = frame_end;
00075 } else {
00076 src = source + src_len - 1;
00077 dst = frame_end - 1;
00078 dst_end = frame - 1;
00079 }
00080 for (;;) {
00081 int shift = 0;
00082 flag = 0;
00083
00084
00085
00086
00087
00088
00089
00090
00091 if (!mode || (tmplen == 4)) {
00092 if (src < source || src >= source_end)
00093 return -1;
00094 val = *src;
00095 read_two_nibbles = 1;
00096 } else {
00097 val = saved_val;
00098 read_two_nibbles = 0;
00099 }
00100 if (!(val & 0xC)) {
00101 for (;;) {
00102 if(shift>22)
00103 return -1;
00104 if (!read_two_nibbles) {
00105 if (src < source || src >= source_end)
00106 return -1;
00107 shift += 2;
00108 val |= *src << shift;
00109 if (*src & 0xC)
00110 break;
00111 }
00112
00113
00114 read_two_nibbles = 0;
00115 shift += 2;
00116 mask = (1 << shift) - 1;
00117 val = ((val >> 2) & ~mask) | (val & mask);
00118 NEXT_BYTE(src);
00119 if ((val & (0xC << shift))) {
00120 flag = 1;
00121 break;
00122 }
00123 }
00124 } else if (mode) {
00125 flag = tmplen != 4;
00126 }
00127 if (flag) {
00128 tmplen = 4;
00129 } else {
00130 saved_val = val >> (4 + shift);
00131 tmplen = 0;
00132 val &= (1 << (shift + 4)) - 1;
00133 NEXT_BYTE(src);
00134 }
00135 advance_mode = val & 1;
00136 len = (val >> 1) - 1;
00137 av_assert0(len>0);
00138 mode += 1 + advance_mode;
00139 if (mode >= 4)
00140 mode -= 3;
00141 if (FFABS(dst_end - dst) < len)
00142 return -1;
00143 switch (mode) {
00144 case 1:
00145 if (forward) {
00146 if (dst - frame + SCREEN_WIDE < frame_off ||
00147 dst - frame + SCREEN_WIDE + frame_off < 0 ||
00148 frame_end - dst < frame_off + len ||
00149 frame_end - dst < len)
00150 return -1;
00151 for (i = 0; i < len; i++)
00152 dst[i] = dst[frame_off + i];
00153 dst += len;
00154 } else {
00155 dst -= len;
00156 if (dst - frame + SCREEN_WIDE < frame_off ||
00157 dst - frame + SCREEN_WIDE + frame_off < 0 ||
00158 frame_end - dst < frame_off + len ||
00159 frame_end - dst < len)
00160 return -1;
00161 for (i = len - 1; i >= 0; i--)
00162 dst[i] = dst[frame_off + i];
00163 }
00164 break;
00165 case 2:
00166 if (forward) {
00167 if (source + src_len - src < len)
00168 return -1;
00169 memcpy(dst, src, len);
00170 dst += len;
00171 src += len;
00172 } else {
00173 if (src - source < len)
00174 return -1;
00175 dst -= len;
00176 src -= len;
00177 memcpy(dst, src, len);
00178 }
00179 break;
00180 case 3:
00181 val = forward ? dst[-1] : dst[1];
00182 if (forward) {
00183 memset(dst, val, len);
00184 dst += len;
00185 } else {
00186 dst -= len;
00187 memset(dst, val, len);
00188 }
00189 break;
00190 default:
00191 break;
00192 }
00193 if (dst == dst_end)
00194 return 0;
00195 }
00196 return 0;
00197 }
00198
00199 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
00200 {
00201 BMVDecContext * const c = avctx->priv_data;
00202 int type, scr_off;
00203 int i;
00204 uint8_t *srcptr, *outptr;
00205
00206 c->stream = pkt->data;
00207 type = bytestream_get_byte(&c->stream);
00208 if (type & BMV_AUDIO) {
00209 int blobs = bytestream_get_byte(&c->stream);
00210 if (pkt->size < blobs * 65 + 2) {
00211 av_log(avctx, AV_LOG_ERROR, "Audio data doesn't fit in frame\n");
00212 return AVERROR_INVALIDDATA;
00213 }
00214 c->stream += blobs * 65;
00215 }
00216 if (type & BMV_COMMAND) {
00217 int command_size = (type & BMV_PRINT) ? 8 : 10;
00218 if (c->stream - pkt->data + command_size > pkt->size) {
00219 av_log(avctx, AV_LOG_ERROR, "Command data doesn't fit in frame\n");
00220 return AVERROR_INVALIDDATA;
00221 }
00222 c->stream += command_size;
00223 }
00224 if (type & BMV_PALETTE) {
00225 if (c->stream - pkt->data > pkt->size - 768) {
00226 av_log(avctx, AV_LOG_ERROR, "Palette data doesn't fit in frame\n");
00227 return AVERROR_INVALIDDATA;
00228 }
00229 for (i = 0; i < 256; i++)
00230 c->pal[i] = 0xFF << 24 | bytestream_get_be24(&c->stream);
00231 }
00232 if (type & BMV_SCROLL) {
00233 if (c->stream - pkt->data > pkt->size - 2) {
00234 av_log(avctx, AV_LOG_ERROR, "Screen offset data doesn't fit in frame\n");
00235 return AVERROR_INVALIDDATA;
00236 }
00237 scr_off = (int16_t)bytestream_get_le16(&c->stream);
00238 } else if ((type & BMV_INTRA) == BMV_INTRA) {
00239 scr_off = -640;
00240 } else {
00241 scr_off = 0;
00242 }
00243
00244 if (decode_bmv_frame(c->stream, pkt->size - (c->stream - pkt->data), c->frame, scr_off)) {
00245 av_log(avctx, AV_LOG_ERROR, "Error decoding frame data\n");
00246 return AVERROR_INVALIDDATA;
00247 }
00248
00249 memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
00250 c->pic.palette_has_changed = type & BMV_PALETTE;
00251
00252 outptr = c->pic.data[0];
00253 srcptr = c->frame;
00254
00255 for (i = 0; i < avctx->height; i++) {
00256 memcpy(outptr, srcptr, avctx->width);
00257 srcptr += avctx->width;
00258 outptr += c->pic.linesize[0];
00259 }
00260
00261 *data_size = sizeof(AVFrame);
00262 *(AVFrame*)data = c->pic;
00263
00264
00265 return pkt->size;
00266 }
00267
00268 static av_cold int decode_init(AVCodecContext *avctx)
00269 {
00270 BMVDecContext * const c = avctx->priv_data;
00271
00272 c->avctx = avctx;
00273 avctx->pix_fmt = PIX_FMT_PAL8;
00274
00275 if (avctx->width != SCREEN_WIDE || avctx->height != SCREEN_HIGH) {
00276 av_log(avctx, AV_LOG_ERROR, "Invalid dimension %dx%d\n", avctx->width, avctx->height);
00277 return AVERROR_INVALIDDATA;
00278 }
00279
00280 c->pic.reference = 1;
00281 if (avctx->get_buffer(avctx, &c->pic) < 0) {
00282 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00283 return -1;
00284 }
00285
00286 c->frame = c->frame_base + 640;
00287
00288 return 0;
00289 }
00290
00291 static av_cold int decode_end(AVCodecContext *avctx)
00292 {
00293 BMVDecContext *c = avctx->priv_data;
00294
00295 if (c->pic.data[0])
00296 avctx->release_buffer(avctx, &c->pic);
00297
00298 return 0;
00299 }
00300
00301 typedef struct BMVAudioDecContext {
00302 AVFrame frame;
00303 } BMVAudioDecContext;
00304
00305 static const int bmv_aud_mults[16] = {
00306 16512, 8256, 4128, 2064, 1032, 516, 258, 192, 129, 88, 64, 56, 48, 40, 36, 32
00307 };
00308
00309 static av_cold int bmv_aud_decode_init(AVCodecContext *avctx)
00310 {
00311 BMVAudioDecContext *c = avctx->priv_data;
00312
00313 if (avctx->channels != 2) {
00314 av_log(avctx, AV_LOG_INFO, "invalid number of channels\n");
00315 return AVERROR(EINVAL);
00316 }
00317
00318 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00319
00320 avcodec_get_frame_defaults(&c->frame);
00321 avctx->coded_frame = &c->frame;
00322
00323 return 0;
00324 }
00325
00326 static int bmv_aud_decode_frame(AVCodecContext *avctx, void *data,
00327 int *got_frame_ptr, AVPacket *avpkt)
00328 {
00329 BMVAudioDecContext *c = avctx->priv_data;
00330 const uint8_t *buf = avpkt->data;
00331 int buf_size = avpkt->size;
00332 int blocks = 0, total_blocks, i;
00333 int ret;
00334 int16_t *output_samples;
00335 int scale[2];
00336
00337 total_blocks = *buf++;
00338 if (buf_size < total_blocks * 65 + 1) {
00339 av_log(avctx, AV_LOG_ERROR, "expected %d bytes, got %d\n",
00340 total_blocks * 65 + 1, buf_size);
00341 return AVERROR_INVALIDDATA;
00342 }
00343
00344
00345 c->frame.nb_samples = total_blocks * 32;
00346 if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
00347 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00348 return ret;
00349 }
00350 output_samples = (int16_t *)c->frame.data[0];
00351
00352 for (blocks = 0; blocks < total_blocks; blocks++) {
00353 uint8_t code = *buf++;
00354 code = (code >> 1) | (code << 7);
00355 scale[0] = bmv_aud_mults[code & 0xF];
00356 scale[1] = bmv_aud_mults[code >> 4];
00357 for (i = 0; i < 32; i++) {
00358 *output_samples++ = av_clip_int16((scale[0] * (int8_t)*buf++) >> 5);
00359 *output_samples++ = av_clip_int16((scale[1] * (int8_t)*buf++) >> 5);
00360 }
00361 }
00362
00363 *got_frame_ptr = 1;
00364 *(AVFrame *)data = c->frame;
00365
00366 return buf_size;
00367 }
00368
00369 AVCodec ff_bmv_video_decoder = {
00370 .name = "bmv_video",
00371 .type = AVMEDIA_TYPE_VIDEO,
00372 .id = AV_CODEC_ID_BMV_VIDEO,
00373 .priv_data_size = sizeof(BMVDecContext),
00374 .init = decode_init,
00375 .close = decode_end,
00376 .decode = decode_frame,
00377 .capabilities = CODEC_CAP_DR1,
00378 .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV video"),
00379 };
00380
00381 AVCodec ff_bmv_audio_decoder = {
00382 .name = "bmv_audio",
00383 .type = AVMEDIA_TYPE_AUDIO,
00384 .id = AV_CODEC_ID_BMV_AUDIO,
00385 .priv_data_size = sizeof(BMVAudioDecContext),
00386 .init = bmv_aud_decode_init,
00387 .decode = bmv_aud_decode_frame,
00388 .capabilities = CODEC_CAP_DR1,
00389 .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV audio"),
00390 };