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 "bitstream.h"
00024
00025
00026 typedef struct {
00027 AVFrame picture;
00028 } avs_context_t;
00029
00030 typedef enum {
00031 AVS_VIDEO = 0x01,
00032 AVS_AUDIO = 0x02,
00033 AVS_PALETTE = 0x03,
00034 AVS_GAME_DATA = 0x04,
00035 } avs_block_type_t;
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 } avs_video_sub_type_t;
00043
00044
00045 static int
00046 avs_decode_frame(AVCodecContext * avctx,
00047 void *data, int *data_size, uint8_t * buf, int buf_size)
00048 {
00049 avs_context_t *const avs = avctx->priv_data;
00050 AVFrame *picture = data;
00051 AVFrame *const p = (AVFrame *) & avs->picture;
00052 uint8_t *table, *vect, *out;
00053 int i, j, x, y, stride, vect_w = 3, vect_h = 3;
00054 int sub_type;
00055 avs_block_type_t type;
00056 GetBitContext change_map;
00057
00058 if (avctx->reget_buffer(avctx, p)) {
00059 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00060 return -1;
00061 }
00062 p->reference = 1;
00063 p->pict_type = FF_P_TYPE;
00064 p->key_frame = 0;
00065
00066 out = avs->picture.data[0];
00067 stride = avs->picture.linesize[0];
00068
00069 sub_type = buf[0];
00070 type = buf[1];
00071 buf += 4;
00072
00073 if (type == AVS_PALETTE) {
00074 int first, last;
00075 uint32_t *pal = (uint32_t *) avs->picture.data[1];
00076
00077 first = AV_RL16(buf);
00078 last = first + AV_RL16(buf + 2);
00079 buf += 4;
00080 for (i=first; i<last; i++, buf+=3)
00081 pal[i] = (buf[0] << 18) | (buf[1] << 10) | (buf[2] << 2);
00082
00083 sub_type = buf[0];
00084 type = buf[1];
00085 buf += 4;
00086 }
00087
00088 if (type != AVS_VIDEO)
00089 return -1;
00090
00091 switch (sub_type) {
00092 case AVS_I_FRAME:
00093 p->pict_type = FF_I_TYPE;
00094 p->key_frame = 1;
00095 case AVS_P_FRAME_3X3:
00096 vect_w = 3;
00097 vect_h = 3;
00098 break;
00099
00100 case AVS_P_FRAME_2X2:
00101 vect_w = 2;
00102 vect_h = 2;
00103 break;
00104
00105 case AVS_P_FRAME_2X3:
00106 vect_w = 2;
00107 vect_h = 3;
00108 break;
00109
00110 default:
00111 return -1;
00112 }
00113
00114 table = buf + (256 * vect_w * vect_h);
00115 if (sub_type != AVS_I_FRAME) {
00116 int map_size = ((318 / vect_w + 7) / 8) * (198 / vect_h);
00117 init_get_bits(&change_map, table, map_size);
00118 table += map_size;
00119 }
00120
00121 for (y=0; y<198; y+=vect_h) {
00122 for (x=0; x<318; x+=vect_w) {
00123 if (sub_type == AVS_I_FRAME || get_bits1(&change_map)) {
00124 vect = &buf[*table++ * (vect_w * vect_h)];
00125 for (j=0; j<vect_w; j++) {
00126 out[(y + 0) * stride + x + j] = vect[(0 * vect_w) + j];
00127 out[(y + 1) * stride + x + j] = vect[(1 * vect_w) + j];
00128 if (vect_h == 3)
00129 out[(y + 2) * stride + x + j] =
00130 vect[(2 * vect_w) + j];
00131 }
00132 }
00133 }
00134 if (sub_type != AVS_I_FRAME)
00135 align_get_bits(&change_map);
00136 }
00137
00138 *picture = *(AVFrame *) & avs->picture;
00139 *data_size = sizeof(AVPicture);
00140
00141 return buf_size;
00142 }
00143
00144 static int avs_decode_init(AVCodecContext * avctx)
00145 {
00146 avctx->pix_fmt = PIX_FMT_PAL8;
00147 return 0;
00148 }
00149
00150 AVCodec avs_decoder = {
00151 "avs",
00152 CODEC_TYPE_VIDEO,
00153 CODEC_ID_AVS,
00154 sizeof(avs_context_t),
00155 avs_decode_init,
00156 NULL,
00157 NULL,
00158 avs_decode_frame,
00159 CODEC_CAP_DR1,
00160 };