libavcodec/bethsoftvideo.c
Go to the documentation of this file.
00001 /*
00002  * Bethesda VID video decoder
00003  * Copyright (C) 2007 Nicholas Tung
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00030 #include "libavutil/common.h"
00031 #include "dsputil.h"
00032 #include "bethsoftvideo.h"
00033 #include "bytestream.h"
00034 
00035 typedef struct BethsoftvidContext {
00036     AVFrame frame;
00037     GetByteContext g;
00038 } BethsoftvidContext;
00039 
00040 static av_cold int bethsoftvid_decode_init(AVCodecContext *avctx)
00041 {
00042     BethsoftvidContext *vid = avctx->priv_data;
00043     avcodec_get_frame_defaults(&vid->frame);
00044     vid->frame.reference = 3;
00045     vid->frame.buffer_hints = FF_BUFFER_HINTS_VALID |
00046         FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00047     avctx->pix_fmt = PIX_FMT_PAL8;
00048     return 0;
00049 }
00050 
00051 static int set_palette(BethsoftvidContext *ctx)
00052 {
00053     uint32_t *palette = (uint32_t *)ctx->frame.data[1];
00054     int a;
00055 
00056     if (bytestream2_get_bytes_left(&ctx->g) < 256*3)
00057         return AVERROR_INVALIDDATA;
00058 
00059     for(a = 0; a < 256; a++){
00060         palette[a] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->g) * 4;
00061         palette[a] |= palette[a] >> 6 & 0x30303;
00062     }
00063     ctx->frame.palette_has_changed = 1;
00064     return 256*3;
00065 }
00066 
00067 static int bethsoftvid_decode_frame(AVCodecContext *avctx,
00068                               void *data, int *data_size,
00069                               AVPacket *avpkt)
00070 {
00071     BethsoftvidContext * vid = avctx->priv_data;
00072     char block_type;
00073     uint8_t * dst;
00074     uint8_t * frame_end;
00075     int remaining = avctx->width;          // number of bytes remaining on a line
00076     const int wrap_to_next_line = vid->frame.linesize[0] - avctx->width;
00077     int code;
00078     int yoffset;
00079 
00080     if (avctx->reget_buffer(avctx, &vid->frame)) {
00081         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00082         return -1;
00083     }
00084 
00085     bytestream2_init(&vid->g, avpkt->data, avpkt->size);
00086     dst = vid->frame.data[0];
00087     frame_end = vid->frame.data[0] + vid->frame.linesize[0] * avctx->height;
00088 
00089     switch(block_type = bytestream2_get_byte(&vid->g)){
00090         case PALETTE_BLOCK: {
00091             return set_palette(vid);
00092         }
00093         case VIDEO_YOFF_P_FRAME:
00094             yoffset = bytestream2_get_le16(&vid->g);
00095             if(yoffset >= avctx->height)
00096                 return -1;
00097             dst += vid->frame.linesize[0] * yoffset;
00098     }
00099 
00100     // main code
00101     while((code = bytestream2_get_byte(&vid->g))){
00102         int length = code & 0x7f;
00103 
00104         // copy any bytes starting at the current position, and ending at the frame width
00105         while(length > remaining){
00106             if(code < 0x80)
00107                 bytestream2_get_buffer(&vid->g, dst, remaining);
00108             else if(block_type == VIDEO_I_FRAME)
00109                 memset(dst, bytestream2_peek_byte(&vid->g), remaining);
00110             length -= remaining;      // decrement the number of bytes to be copied
00111             dst += remaining + wrap_to_next_line;    // skip over extra bytes at end of frame
00112             remaining = avctx->width;
00113             if(dst == frame_end)
00114                 goto end;
00115         }
00116 
00117         // copy any remaining bytes after / if line overflows
00118         if(code < 0x80)
00119             bytestream2_get_buffer(&vid->g, dst, length);
00120         else if(block_type == VIDEO_I_FRAME)
00121             memset(dst, bytestream2_get_byte(&vid->g), length);
00122         remaining -= length;
00123         dst += length;
00124     }
00125     end:
00126 
00127     *data_size = sizeof(AVFrame);
00128     *(AVFrame*)data = vid->frame;
00129 
00130     return avpkt->size;
00131 }
00132 
00133 static av_cold int bethsoftvid_decode_end(AVCodecContext *avctx)
00134 {
00135     BethsoftvidContext * vid = avctx->priv_data;
00136     if(vid->frame.data[0])
00137         avctx->release_buffer(avctx, &vid->frame);
00138     return 0;
00139 }
00140 
00141 AVCodec ff_bethsoftvid_decoder = {
00142     .name = "bethsoftvid",
00143     .type = AVMEDIA_TYPE_VIDEO,
00144     .id = CODEC_ID_BETHSOFTVID,
00145     .priv_data_size = sizeof(BethsoftvidContext),
00146     .init = bethsoftvid_decode_init,
00147     .close = bethsoftvid_decode_end,
00148     .decode = bethsoftvid_decode_frame,
00149     .capabilities = CODEC_CAP_DR1,
00150     .long_name = NULL_IF_CONFIG_SMALL("Bethesda VID video"),
00151 };