annotate avs.c @ 3400:84de54d536bd libavcodec

4-MV mode final fixes (now it works for non-exotic modes)
author kostya
date Mon, 03 Jul 2006 17:21:18 +0000
parents 0ebbd476ba32
children c8c591fe26f8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3129
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
1 /*
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
2 * AVS video decoder.
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
3 * Copyright (c) 2006 Aurelien Jacobs <aurel@gnuage.org>
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
4 *
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
8 * version 2 of the License, or (at your option) any later version.
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
9 *
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
13 * Lesser General Public License for more details.
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
14 *
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
18 */
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
19
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
20 #include "avcodec.h"
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
21 #include "bitstream.h"
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
22
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
23
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
24 typedef struct {
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
25 AVFrame picture;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
26 } avs_context_t;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
27
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
28 typedef enum {
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
29 AVS_VIDEO = 0x01,
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
30 AVS_AUDIO = 0x02,
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
31 AVS_PALETTE = 0x03,
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
32 AVS_GAME_DATA = 0x04,
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
33 } avs_block_type_t;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
34
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
35 typedef enum {
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
36 AVS_I_FRAME = 0x00,
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
37 AVS_P_FRAME_3X3 = 0x01,
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
38 AVS_P_FRAME_2X2 = 0x02,
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
39 AVS_P_FRAME_2X3 = 0x03,
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
40 } avs_video_sub_type_t;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
41
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
42
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
43 static int
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
44 avs_decode_frame(AVCodecContext * avctx,
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
45 void *data, int *data_size, uint8_t * buf, int buf_size)
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
46 {
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
47 avs_context_t *const avs = avctx->priv_data;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
48 AVFrame *picture = data;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
49 AVFrame *const p = (AVFrame *) & avs->picture;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
50 uint8_t *table, *vect, *out;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
51 int i, j, x, y, stride, vect_w = 3, vect_h = 3;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
52 int sub_type;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
53 avs_block_type_t type;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
54 GetBitContext change_map;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
55
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
56 if (avctx->reget_buffer(avctx, p)) {
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
57 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
58 return -1;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
59 }
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
60 p->reference = 1;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
61 p->pict_type = FF_P_TYPE;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
62 p->key_frame = 0;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
63
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
64 out = avs->picture.data[0];
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
65 stride = avs->picture.linesize[0];
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
66
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
67 sub_type = buf[0];
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
68 type = buf[1];
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
69 buf += 4;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
70
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
71 if (type == AVS_PALETTE) {
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
72 int first, last;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
73 uint32_t *pal = (uint32_t *) avs->picture.data[1];
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
74
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
75 first = LE_16(buf);
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
76 last = first + LE_16(buf + 2);
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
77 buf += 4;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
78 for (i=first; i<last; i++, buf+=3)
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
79 pal[i] = (buf[0] << 18) | (buf[1] << 10) | (buf[2] << 2);
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
80
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
81 sub_type = buf[0];
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
82 type = buf[1];
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
83 buf += 4;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
84 }
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
85
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
86 if (type != AVS_VIDEO)
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
87 return -1;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
88
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
89 switch (sub_type) {
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
90 case AVS_I_FRAME:
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
91 p->pict_type = FF_I_TYPE;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
92 p->key_frame = 1;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
93 case AVS_P_FRAME_3X3:
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
94 vect_w = 3;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
95 vect_h = 3;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
96 break;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
97
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
98 case AVS_P_FRAME_2X2:
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
99 vect_w = 2;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
100 vect_h = 2;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
101 break;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
102
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
103 case AVS_P_FRAME_2X3:
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
104 vect_w = 2;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
105 vect_h = 3;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
106 break;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
107
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
108 default:
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
109 return -1;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
110 }
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
111
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
112 table = buf + (256 * vect_w * vect_h);
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
113 if (sub_type != AVS_I_FRAME) {
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
114 int map_size = ((318 / vect_w + 7) / 8) * (198 / vect_h);
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
115 init_get_bits(&change_map, table, map_size);
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
116 table += map_size;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
117 }
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
118
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
119 for (y=0; y<198; y+=vect_h) {
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
120 for (x=0; x<318; x+=vect_w) {
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
121 if (sub_type == AVS_I_FRAME || get_bits1(&change_map)) {
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
122 vect = &buf[*table++ * (vect_w * vect_h)];
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
123 for (j=0; j<vect_w; j++) {
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
124 out[(y + 0) * stride + x + j] = vect[(0 * vect_w) + j];
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
125 out[(y + 1) * stride + x + j] = vect[(1 * vect_w) + j];
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
126 if (vect_h == 3)
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
127 out[(y + 2) * stride + x + j] =
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
128 vect[(2 * vect_w) + j];
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
129 }
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
130 }
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
131 }
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
132 if (sub_type != AVS_I_FRAME)
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
133 align_get_bits(&change_map);
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
134 }
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
135
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
136 *picture = *(AVFrame *) & avs->picture;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
137 *data_size = sizeof(AVPicture);
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
138
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
139 return buf_size;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
140 }
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
141
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
142 static int avs_decode_init(AVCodecContext * avctx)
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
143 {
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
144 avctx->pix_fmt = PIX_FMT_PAL8;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
145 return 0;
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
146 }
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
147
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
148 AVCodec avs_decoder = {
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
149 "avs",
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
150 CODEC_TYPE_VIDEO,
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
151 CODEC_ID_AVS,
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
152 sizeof(avs_context_t),
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
153 avs_decode_init,
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
154 NULL,
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
155 NULL,
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
156 avs_decode_frame,
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
157 CODEC_CAP_DR1,
0ebbd476ba32 complete AVS playback system (from Creature Shock computer game),
melanson
parents:
diff changeset
158 };