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 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 0;
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;
00076 int wrap_to_next_line;
00077 int code, ret;
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 wrap_to_next_line = vid->frame.linesize[0] - avctx->width;
00085
00086 if (avpkt->side_data_elems > 0 &&
00087 avpkt->side_data[0].type == AV_PKT_DATA_PALETTE) {
00088 bytestream2_init(&vid->g, avpkt->side_data[0].data,
00089 avpkt->side_data[0].size);
00090 if ((ret = set_palette(vid)) < 0)
00091 return ret;
00092 }
00093
00094 bytestream2_init(&vid->g, avpkt->data, avpkt->size);
00095 dst = vid->frame.data[0];
00096 frame_end = vid->frame.data[0] + vid->frame.linesize[0] * avctx->height;
00097
00098 switch(block_type = bytestream2_get_byte(&vid->g)){
00099 case PALETTE_BLOCK: {
00100 *data_size = 0;
00101 if ((ret = set_palette(vid)) < 0) {
00102 av_log(avctx, AV_LOG_ERROR, "error reading palette\n");
00103 return ret;
00104 }
00105 return bytestream2_tell(&vid->g);
00106 }
00107 case VIDEO_YOFF_P_FRAME:
00108 yoffset = bytestream2_get_le16(&vid->g);
00109 if(yoffset >= avctx->height)
00110 return -1;
00111 dst += vid->frame.linesize[0] * yoffset;
00112 }
00113
00114
00115 while((code = bytestream2_get_byte(&vid->g))){
00116 int length = code & 0x7f;
00117
00118
00119 while(length > remaining){
00120 if(code < 0x80)
00121 bytestream2_get_buffer(&vid->g, dst, remaining);
00122 else if(block_type == VIDEO_I_FRAME)
00123 memset(dst, bytestream2_peek_byte(&vid->g), remaining);
00124 length -= remaining;
00125 dst += remaining + wrap_to_next_line;
00126 remaining = avctx->width;
00127 if(dst == frame_end)
00128 goto end;
00129 }
00130
00131
00132 if(code < 0x80)
00133 bytestream2_get_buffer(&vid->g, dst, length);
00134 else if(block_type == VIDEO_I_FRAME)
00135 memset(dst, bytestream2_get_byte(&vid->g), length);
00136 remaining -= length;
00137 dst += length;
00138 }
00139 end:
00140
00141 *data_size = sizeof(AVFrame);
00142 *(AVFrame*)data = vid->frame;
00143
00144 return avpkt->size;
00145 }
00146
00147 static av_cold int bethsoftvid_decode_end(AVCodecContext *avctx)
00148 {
00149 BethsoftvidContext * vid = avctx->priv_data;
00150 if(vid->frame.data[0])
00151 avctx->release_buffer(avctx, &vid->frame);
00152 return 0;
00153 }
00154
00155 AVCodec ff_bethsoftvid_decoder = {
00156 .name = "bethsoftvid",
00157 .type = AVMEDIA_TYPE_VIDEO,
00158 .id = AV_CODEC_ID_BETHSOFTVID,
00159 .priv_data_size = sizeof(BethsoftvidContext),
00160 .init = bethsoftvid_decode_init,
00161 .close = bethsoftvid_decode_end,
00162 .decode = bethsoftvid_decode_frame,
00163 .capabilities = CODEC_CAP_DR1,
00164 .long_name = NULL_IF_CONFIG_SMALL("Bethesda VID video"),
00165 };