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