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

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 #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         /* 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(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                 // two upper bits of the nibble is zero,
00113                 // so shift top nibble value down into their place
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                         frame_end - dst < frame_off + len)
00148                     return -1;
00149                 for (i = 0; i < len; i++)
00150                     dst[i] = dst[frame_off + i];
00151                 dst += len;
00152             } else {
00153                 dst -= len;
00154                 if (dst - frame + SCREEN_WIDE < -frame_off ||
00155                         frame_end - dst < frame_off + len)
00156                     return -1;
00157                 for (i = len - 1; i >= 0; i--)
00158                     dst[i] = dst[frame_off + i];
00159             }
00160             break;
00161         case 2:
00162             if (forward) {
00163                 if (source + src_len - src < len)
00164                     return -1;
00165                 memcpy(dst, src, len);
00166                 dst += len;
00167                 src += len;
00168             } else {
00169                 if (src - source < len)
00170                     return -1;
00171                 dst -= len;
00172                 src -= len;
00173                 memcpy(dst, src, len);
00174             }
00175             break;
00176         case 3:
00177             val = forward ? dst[-1] : dst[1];
00178             if (forward) {
00179                 memset(dst, val, len);
00180                 dst += len;
00181             } else {
00182                 dst -= len;
00183                 memset(dst, val, len);
00184             }
00185             break;
00186         default:
00187             break;
00188         }
00189         if (dst == dst_end)
00190             return 0;
00191     }
00192     return 0;
00193 }
00194 
00195 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
00196 {
00197     BMVDecContext * const c = avctx->priv_data;
00198     int type, scr_off;
00199     int i;
00200     uint8_t *srcptr, *outptr;
00201 
00202     c->stream = pkt->data;
00203     type = bytestream_get_byte(&c->stream);
00204     if (type & BMV_AUDIO) {
00205         int blobs = bytestream_get_byte(&c->stream);
00206         if (pkt->size < blobs * 65 + 2) {
00207             av_log(avctx, AV_LOG_ERROR, "Audio data doesn't fit in frame\n");
00208             return AVERROR_INVALIDDATA;
00209         }
00210         c->stream += blobs * 65;
00211     }
00212     if (type & BMV_COMMAND) {
00213         int command_size = (type & BMV_PRINT) ? 8 : 10;
00214         if (c->stream - pkt->data + command_size > pkt->size) {
00215             av_log(avctx, AV_LOG_ERROR, "Command data doesn't fit in frame\n");
00216             return AVERROR_INVALIDDATA;
00217         }
00218         c->stream += command_size;
00219     }
00220     if (type & BMV_PALETTE) {
00221         if (c->stream - pkt->data > pkt->size - 768) {
00222             av_log(avctx, AV_LOG_ERROR, "Palette data doesn't fit in frame\n");
00223             return AVERROR_INVALIDDATA;
00224         }
00225         for (i = 0; i < 256; i++)
00226             c->pal[i] = 0xFF << 24 | bytestream_get_be24(&c->stream);
00227     }
00228     if (type & BMV_SCROLL) {
00229         if (c->stream - pkt->data > pkt->size - 2) {
00230             av_log(avctx, AV_LOG_ERROR, "Screen offset data doesn't fit in frame\n");
00231             return AVERROR_INVALIDDATA;
00232         }
00233         scr_off = (int16_t)bytestream_get_le16(&c->stream);
00234     } else if ((type & BMV_INTRA) == BMV_INTRA) {
00235         scr_off = -640;
00236     } else {
00237         scr_off = 0;
00238     }
00239 
00240     if (decode_bmv_frame(c->stream, pkt->size - (c->stream - pkt->data), c->frame, scr_off)) {
00241         av_log(avctx, AV_LOG_ERROR, "Error decoding frame data\n");
00242         return AVERROR_INVALIDDATA;
00243     }
00244 
00245     memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
00246     c->pic.palette_has_changed = type & BMV_PALETTE;
00247 
00248     outptr = c->pic.data[0];
00249     srcptr = c->frame;
00250 
00251     for (i = 0; i < avctx->height; i++) {
00252         memcpy(outptr, srcptr, avctx->width);
00253         srcptr += avctx->width;
00254         outptr += c->pic.linesize[0];
00255     }
00256 
00257     *data_size = sizeof(AVFrame);
00258     *(AVFrame*)data = c->pic;
00259 
00260     /* always report that the buffer was completely consumed */
00261     return pkt->size;
00262 }
00263 
00264 static av_cold int decode_init(AVCodecContext *avctx)
00265 {
00266     BMVDecContext * const c = avctx->priv_data;
00267 
00268     c->avctx = avctx;
00269     avctx->pix_fmt = PIX_FMT_PAL8;
00270 
00271     if (avctx->width != SCREEN_WIDE || avctx->height != SCREEN_HIGH) {
00272         av_log(avctx, AV_LOG_ERROR, "Invalid dimension %dx%d\n", avctx->width, avctx->height);
00273         return AVERROR_INVALIDDATA;
00274     }
00275 
00276     c->pic.reference = 1;
00277     if (avctx->get_buffer(avctx, &c->pic) < 0) {
00278         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00279         return -1;
00280     }
00281 
00282     c->frame = c->frame_base + 640;
00283 
00284     return 0;
00285 }
00286 
00287 static av_cold int decode_end(AVCodecContext *avctx)
00288 {
00289     BMVDecContext *c = avctx->priv_data;
00290 
00291     if (c->pic.data[0])
00292         avctx->release_buffer(avctx, &c->pic);
00293 
00294     return 0;
00295 }
00296 
00297 typedef struct BMVAudioDecContext {
00298     AVFrame frame;
00299 } BMVAudioDecContext;
00300 
00301 static const int bmv_aud_mults[16] = {
00302     16512, 8256, 4128, 2064, 1032, 516, 258, 192, 129, 88, 64, 56, 48, 40, 36, 32
00303 };
00304 
00305 static av_cold int bmv_aud_decode_init(AVCodecContext *avctx)
00306 {
00307     BMVAudioDecContext *c = avctx->priv_data;
00308 
00309     if (avctx->channels != 2) {
00310         av_log(avctx, AV_LOG_INFO, "invalid number of channels\n");
00311         return AVERROR(EINVAL);
00312     }
00313 
00314     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00315 
00316     avcodec_get_frame_defaults(&c->frame);
00317     avctx->coded_frame = &c->frame;
00318 
00319     return 0;
00320 }
00321 
00322 static int bmv_aud_decode_frame(AVCodecContext *avctx, void *data,
00323                                 int *got_frame_ptr, AVPacket *avpkt)
00324 {
00325     BMVAudioDecContext *c = avctx->priv_data;
00326     const uint8_t *buf = avpkt->data;
00327     int buf_size = avpkt->size;
00328     int blocks = 0, total_blocks, i;
00329     int ret;
00330     int16_t *output_samples;
00331     int scale[2];
00332 
00333     total_blocks = *buf++;
00334     if (buf_size < total_blocks * 65 + 1) {
00335         av_log(avctx, AV_LOG_ERROR, "expected %d bytes, got %d\n",
00336                total_blocks * 65 + 1, buf_size);
00337         return AVERROR_INVALIDDATA;
00338     }
00339 
00340     /* get output buffer */
00341     c->frame.nb_samples = total_blocks * 32;
00342     if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
00343         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00344         return ret;
00345     }
00346     output_samples = (int16_t *)c->frame.data[0];
00347 
00348     for (blocks = 0; blocks < total_blocks; blocks++) {
00349         uint8_t code = *buf++;
00350         code = (code >> 1) | (code << 7);
00351         scale[0] = bmv_aud_mults[code & 0xF];
00352         scale[1] = bmv_aud_mults[code >> 4];
00353         for (i = 0; i < 32; i++) {
00354             *output_samples++ = av_clip_int16((scale[0] * (int8_t)*buf++) >> 5);
00355             *output_samples++ = av_clip_int16((scale[1] * (int8_t)*buf++) >> 5);
00356         }
00357     }
00358 
00359     *got_frame_ptr   = 1;
00360     *(AVFrame *)data = c->frame;
00361 
00362     return buf_size;
00363 }
00364 
00365 AVCodec ff_bmv_video_decoder = {
00366     .name           = "bmv_video",
00367     .type           = AVMEDIA_TYPE_VIDEO,
00368     .id             = CODEC_ID_BMV_VIDEO,
00369     .priv_data_size = sizeof(BMVDecContext),
00370     .init           = decode_init,
00371     .close          = decode_end,
00372     .decode         = decode_frame,
00373     .long_name      = NULL_IF_CONFIG_SMALL("Discworld II BMV video"),
00374 };
00375 
00376 AVCodec ff_bmv_audio_decoder = {
00377     .name           = "bmv_audio",
00378     .type           = AVMEDIA_TYPE_AUDIO,
00379     .id             = CODEC_ID_BMV_AUDIO,
00380     .priv_data_size = sizeof(BMVAudioDecContext),
00381     .init           = bmv_aud_decode_init,
00382     .decode         = bmv_aud_decode_frame,
00383     .capabilities   = CODEC_CAP_DR1,
00384     .long_name      = NULL_IF_CONFIG_SMALL("Discworld II BMV audio"),
00385 };
Generated on Fri Feb 1 2013 14:34:31 for FFmpeg by doxygen 1.7.1