00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avcodec.h"
00023 #include "get_bits.h"
00024
00025
00026 typedef struct {
00027 AVFrame picture;
00028 } AvsContext;
00029
00030 typedef enum {
00031 AVS_VIDEO = 0x01,
00032 AVS_AUDIO = 0x02,
00033 AVS_PALETTE = 0x03,
00034 AVS_GAME_DATA = 0x04,
00035 } AvsBlockType;
00036
00037 typedef enum {
00038 AVS_I_FRAME = 0x00,
00039 AVS_P_FRAME_3X3 = 0x01,
00040 AVS_P_FRAME_2X2 = 0x02,
00041 AVS_P_FRAME_2X3 = 0x03,
00042 } AvsVideoSubType;
00043
00044
00045 static int
00046 avs_decode_frame(AVCodecContext * avctx,
00047 void *data, int *data_size, AVPacket *avpkt)
00048 {
00049 const uint8_t *buf = avpkt->data;
00050 const uint8_t *buf_end = avpkt->data + avpkt->size;
00051 int buf_size = avpkt->size;
00052 AvsContext *const avs = avctx->priv_data;
00053 AVFrame *picture = data;
00054 AVFrame *const p = (AVFrame *) & avs->picture;
00055 const uint8_t *table, *vect;
00056 uint8_t *out;
00057 int i, j, x, y, stride, vect_w = 3, vect_h = 3;
00058 AvsVideoSubType sub_type;
00059 AvsBlockType type;
00060 GetBitContext change_map;
00061
00062 if (avctx->reget_buffer(avctx, p)) {
00063 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00064 return -1;
00065 }
00066 p->reference = 3;
00067 p->pict_type = AV_PICTURE_TYPE_P;
00068 p->key_frame = 0;
00069
00070 out = avs->picture.data[0];
00071 stride = avs->picture.linesize[0];
00072
00073 if (buf_end - buf < 4)
00074 return AVERROR_INVALIDDATA;
00075 sub_type = buf[0];
00076 type = buf[1];
00077 buf += 4;
00078
00079 if (type == AVS_PALETTE) {
00080 int first, last;
00081 uint32_t *pal = (uint32_t *) avs->picture.data[1];
00082
00083 first = AV_RL16(buf);
00084 last = first + AV_RL16(buf + 2);
00085 if (first >= 256 || last > 256 || buf_end - buf < 4 + 4 + 3 * (last - first))
00086 return AVERROR_INVALIDDATA;
00087 buf += 4;
00088 for (i=first; i<last; i++, buf+=3) {
00089 pal[i] = (buf[0] << 18) | (buf[1] << 10) | (buf[2] << 2);
00090 pal[i] |= 0xFF << 24 | (pal[i] >> 6) & 0x30303;
00091 }
00092
00093 sub_type = buf[0];
00094 type = buf[1];
00095 buf += 4;
00096 }
00097
00098 if (type != AVS_VIDEO)
00099 return -1;
00100
00101 switch (sub_type) {
00102 case AVS_I_FRAME:
00103 p->pict_type = AV_PICTURE_TYPE_I;
00104 p->key_frame = 1;
00105 case AVS_P_FRAME_3X3:
00106 vect_w = 3;
00107 vect_h = 3;
00108 break;
00109
00110 case AVS_P_FRAME_2X2:
00111 vect_w = 2;
00112 vect_h = 2;
00113 break;
00114
00115 case AVS_P_FRAME_2X3:
00116 vect_w = 2;
00117 vect_h = 3;
00118 break;
00119
00120 default:
00121 return -1;
00122 }
00123
00124 if (buf_end - buf < 256 * vect_w * vect_h)
00125 return AVERROR_INVALIDDATA;
00126 table = buf + (256 * vect_w * vect_h);
00127 if (sub_type != AVS_I_FRAME) {
00128 int map_size = ((318 / vect_w + 7) / 8) * (198 / vect_h);
00129 if (buf_end - table < map_size)
00130 return AVERROR_INVALIDDATA;
00131 init_get_bits(&change_map, table, map_size * 8);
00132 table += map_size;
00133 }
00134
00135 for (y=0; y<198; y+=vect_h) {
00136 for (x=0; x<318; x+=vect_w) {
00137 if (sub_type == AVS_I_FRAME || get_bits1(&change_map)) {
00138 if (buf_end - table < 1)
00139 return AVERROR_INVALIDDATA;
00140 vect = &buf[*table++ * (vect_w * vect_h)];
00141 for (j=0; j<vect_w; j++) {
00142 out[(y + 0) * stride + x + j] = vect[(0 * vect_w) + j];
00143 out[(y + 1) * stride + x + j] = vect[(1 * vect_w) + j];
00144 if (vect_h == 3)
00145 out[(y + 2) * stride + x + j] =
00146 vect[(2 * vect_w) + j];
00147 }
00148 }
00149 }
00150 if (sub_type != AVS_I_FRAME)
00151 align_get_bits(&change_map);
00152 }
00153
00154 *picture = *(AVFrame *) & avs->picture;
00155 *data_size = sizeof(AVPicture);
00156
00157 return buf_size;
00158 }
00159
00160 static av_cold int avs_decode_init(AVCodecContext * avctx)
00161 {
00162 AvsContext *const avs = avctx->priv_data;
00163 avctx->pix_fmt = PIX_FMT_PAL8;
00164 avcodec_get_frame_defaults(&avs->picture);
00165 return 0;
00166 }
00167
00168 AVCodec ff_avs_decoder = {
00169 .name = "avs",
00170 .type = AVMEDIA_TYPE_VIDEO,
00171 .id = CODEC_ID_AVS,
00172 .priv_data_size = sizeof(AvsContext),
00173 .init = avs_decode_init,
00174 .decode = avs_decode_frame,
00175 .capabilities = CODEC_CAP_DR1,
00176 .long_name = NULL_IF_CONFIG_SMALL("AVS (Audio Video Standard) video"),
00177 };