00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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 avcodec_get_frame_defaults(&vid->frame);
00043 vid->frame.reference = 1;
00044 vid->frame.buffer_hints = FF_BUFFER_HINTS_VALID |
00045 FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00046 avctx->pix_fmt = PIX_FMT_PAL8;
00047 return 0;
00048 }
00049
00050 static void set_palette(AVFrame * frame, const uint8_t * palette_buffer)
00051 {
00052 uint32_t * palette = (uint32_t *)frame->data[1];
00053 int a;
00054 for(a = 0; a < 256; a++){
00055 palette[a] = AV_RB24(&palette_buffer[a * 3]) * 4;
00056 }
00057 frame->palette_has_changed = 1;
00058 }
00059
00060 static int bethsoftvid_decode_frame(AVCodecContext *avctx,
00061 void *data, int *data_size,
00062 AVPacket *avpkt)
00063 {
00064 const uint8_t *buf = avpkt->data;
00065 int buf_size = avpkt->size;
00066 BethsoftvidContext * vid = avctx->priv_data;
00067 char block_type;
00068 uint8_t * dst;
00069 uint8_t * frame_end;
00070 int remaining = avctx->width;
00071 const int wrap_to_next_line = vid->frame.linesize[0] - avctx->width;
00072 int code;
00073 int yoffset;
00074
00075 if (avctx->reget_buffer(avctx, &vid->frame)) {
00076 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00077 return -1;
00078 }
00079 dst = vid->frame.data[0];
00080 frame_end = vid->frame.data[0] + vid->frame.linesize[0] * avctx->height;
00081
00082 switch(block_type = *buf++){
00083 case PALETTE_BLOCK:
00084 set_palette(&vid->frame, buf);
00085 return 0;
00086 case VIDEO_YOFF_P_FRAME:
00087 yoffset = bytestream_get_le16(&buf);
00088 if(yoffset >= avctx->height)
00089 return -1;
00090 dst += vid->frame.linesize[0] * yoffset;
00091 }
00092
00093
00094 while((code = *buf++)){
00095 int length = code & 0x7f;
00096
00097
00098 while(length > remaining){
00099 if(code < 0x80)
00100 bytestream_get_buffer(&buf, dst, remaining);
00101 else if(block_type == VIDEO_I_FRAME)
00102 memset(dst, buf[0], remaining);
00103 length -= remaining;
00104 dst += remaining + wrap_to_next_line;
00105 remaining = avctx->width;
00106 if(dst == frame_end)
00107 goto end;
00108 }
00109
00110
00111 if(code < 0x80)
00112 bytestream_get_buffer(&buf, dst, length);
00113 else if(block_type == VIDEO_I_FRAME)
00114 memset(dst, *buf++, length);
00115 remaining -= length;
00116 dst += length;
00117 }
00118 end:
00119
00120 *data_size = sizeof(AVFrame);
00121 *(AVFrame*)data = vid->frame;
00122
00123 return buf_size;
00124 }
00125
00126 static av_cold int bethsoftvid_decode_end(AVCodecContext *avctx)
00127 {
00128 BethsoftvidContext * vid = avctx->priv_data;
00129 if(vid->frame.data[0])
00130 avctx->release_buffer(avctx, &vid->frame);
00131 return 0;
00132 }
00133
00134 AVCodec ff_bethsoftvid_decoder = {
00135 .name = "bethsoftvid",
00136 .type = AVMEDIA_TYPE_VIDEO,
00137 .id = CODEC_ID_BETHSOFTVID,
00138 .priv_data_size = sizeof(BethsoftvidContext),
00139 .init = bethsoftvid_decode_init,
00140 .close = bethsoftvid_decode_end,
00141 .decode = bethsoftvid_decode_frame,
00142 .capabilities = CODEC_CAP_DR1,
00143 .long_name = NULL_IF_CONFIG_SMALL("Bethesda VID video"),
00144 };