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

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 } BethsoftvidContext;
00038 
00039 static av_cold int bethsoftvid_decode_init(AVCodecContext *avctx)
00040 {
00041     BethsoftvidContext *vid = avctx->priv_data;
00042     vid->frame.reference = 1;
00043     vid->frame.buffer_hints = FF_BUFFER_HINTS_VALID |
00044         FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00045     avctx->pix_fmt = PIX_FMT_PAL8;
00046     return 0;
00047 }
00048 
00049 static void set_palette(AVFrame * frame, const uint8_t * palette_buffer)
00050 {
00051     uint32_t * palette = (uint32_t *)frame->data[1];
00052     int a;
00053     for(a = 0; a < 256; a++){
00054         palette[a] = AV_RB24(&palette_buffer[a * 3]) * 4;
00055     }
00056     frame->palette_has_changed = 1;
00057 }
00058 
00059 static int bethsoftvid_decode_frame(AVCodecContext *avctx,
00060                               void *data, int *data_size,
00061                               const uint8_t *buf, int buf_size)
00062 {
00063     BethsoftvidContext * vid = avctx->priv_data;
00064     char block_type;
00065     uint8_t * dst;
00066     uint8_t * frame_end;
00067     int remaining = avctx->width;          // number of bytes remaining on a line
00068     const int wrap_to_next_line = vid->frame.linesize[0] - avctx->width;
00069     int code;
00070     int yoffset;
00071 
00072     if (avctx->reget_buffer(avctx, &vid->frame)) {
00073         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00074         return -1;
00075     }
00076     dst = vid->frame.data[0];
00077     frame_end = vid->frame.data[0] + vid->frame.linesize[0] * avctx->height;
00078 
00079     switch(block_type = *buf++){
00080         case PALETTE_BLOCK:
00081             set_palette(&vid->frame, buf);
00082             return 0;
00083         case VIDEO_YOFF_P_FRAME:
00084             yoffset = bytestream_get_le16(&buf);
00085             if(yoffset >= avctx->height)
00086                 return -1;
00087             dst += vid->frame.linesize[0] * yoffset;
00088     }
00089 
00090     // main code
00091     while((code = *buf++)){
00092         int length = code & 0x7f;
00093 
00094         // copy any bytes starting at the current position, and ending at the frame width
00095         while(length > remaining){
00096             if(code < 0x80)
00097                 bytestream_get_buffer(&buf, dst, remaining);
00098             else if(block_type == VIDEO_I_FRAME)
00099                 memset(dst, buf[0], remaining);
00100             length -= remaining;      // decrement the number of bytes to be copied
00101             dst += remaining + wrap_to_next_line;    // skip over extra bytes at end of frame
00102             remaining = avctx->width;
00103             if(dst == frame_end)
00104                 goto end;
00105         }
00106 
00107         // copy any remaining bytes after / if line overflows
00108         if(code < 0x80)
00109             bytestream_get_buffer(&buf, dst, length);
00110         else if(block_type == VIDEO_I_FRAME)
00111             memset(dst, *buf++, length);
00112         remaining -= length;
00113         dst += length;
00114     }
00115     end:
00116 
00117     *data_size = sizeof(AVFrame);
00118     *(AVFrame*)data = vid->frame;
00119 
00120     return buf_size;
00121 }
00122 
00123 static av_cold int bethsoftvid_decode_end(AVCodecContext *avctx)
00124 {
00125     BethsoftvidContext * vid = avctx->priv_data;
00126     if(vid->frame.data[0])
00127         avctx->release_buffer(avctx, &vid->frame);
00128     return 0;
00129 }
00130 
00131 AVCodec bethsoftvid_decoder = {
00132     .name = "bethsoftvid",
00133     .type = CODEC_TYPE_VIDEO,
00134     .id = CODEC_ID_BETHSOFTVID,
00135     .priv_data_size = sizeof(BethsoftvidContext),
00136     .init = bethsoftvid_decode_init,
00137     .close = bethsoftvid_decode_end,
00138     .decode = bethsoftvid_decode_frame,
00139     .long_name = NULL_IF_CONFIG_SMALL("Bethesda VID video"),
00140 };

Generated on Tue Nov 4 2014 12:59:21 for ffmpeg by  doxygen 1.7.1