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