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