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