Imported Debian version 2.5.3~trusty1
[deb_ffmpeg.git] / ffmpeg / libavcodec / avs.c
CommitLineData
2ba45a60
DM
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#include "internal.h"
25
26
27typedef struct {
28 AVFrame *frame;
29} AvsContext;
30
31typedef enum {
32 AVS_VIDEO = 0x01,
33 AVS_AUDIO = 0x02,
34 AVS_PALETTE = 0x03,
35 AVS_GAME_DATA = 0x04,
36} AvsBlockType;
37
38typedef enum {
39 AVS_I_FRAME = 0x00,
40 AVS_P_FRAME_3X3 = 0x01,
41 AVS_P_FRAME_2X2 = 0x02,
42 AVS_P_FRAME_2X3 = 0x03,
43} AvsVideoSubType;
44
45
46static int
47avs_decode_frame(AVCodecContext * avctx,
48 void *data, int *got_frame, AVPacket *avpkt)
49{
50 const uint8_t *buf = avpkt->data;
51 const uint8_t *buf_end = avpkt->data + avpkt->size;
52 int buf_size = avpkt->size;
53 AvsContext *const avs = avctx->priv_data;
54 AVFrame *picture = data;
55 AVFrame *const p = avs->frame;
56 const uint8_t *table, *vect;
57 uint8_t *out;
58 int i, j, x, y, stride, ret, vect_w = 3, vect_h = 3;
59 AvsVideoSubType sub_type;
60 AvsBlockType type;
61 GetBitContext change_map = {0}; //init to silence warning
62
63 if ((ret = ff_reget_buffer(avctx, p)) < 0)
64 return ret;
65 p->pict_type = AV_PICTURE_TYPE_P;
66 p->key_frame = 0;
67
68 out = p->data[0];
69 stride = p->linesize[0];
70
71 if (buf_end - buf < 4)
72 return AVERROR_INVALIDDATA;
73 sub_type = buf[0];
74 type = buf[1];
75 buf += 4;
76
77 if (type == AVS_PALETTE) {
78 int first, last;
79 uint32_t *pal = (uint32_t *) p->data[1];
80
81 first = AV_RL16(buf);
82 last = first + AV_RL16(buf + 2);
83 if (first >= 256 || last > 256 || buf_end - buf < 4 + 4 + 3 * (last - first))
84 return AVERROR_INVALIDDATA;
85 buf += 4;
86 for (i=first; i<last; i++, buf+=3) {
87 pal[i] = (buf[0] << 18) | (buf[1] << 10) | (buf[2] << 2);
88 pal[i] |= 0xFFU << 24 | (pal[i] >> 6) & 0x30303;
89 }
90
91 sub_type = buf[0];
92 type = buf[1];
93 buf += 4;
94 }
95
96 if (type != AVS_VIDEO)
97 return AVERROR_INVALIDDATA;
98
99 switch (sub_type) {
100 case AVS_I_FRAME:
101 p->pict_type = AV_PICTURE_TYPE_I;
102 p->key_frame = 1;
103 case AVS_P_FRAME_3X3:
104 vect_w = 3;
105 vect_h = 3;
106 break;
107
108 case AVS_P_FRAME_2X2:
109 vect_w = 2;
110 vect_h = 2;
111 break;
112
113 case AVS_P_FRAME_2X3:
114 vect_w = 2;
115 vect_h = 3;
116 break;
117
118 default:
119 return AVERROR_INVALIDDATA;
120 }
121
122 if (buf_end - buf < 256 * vect_w * vect_h)
123 return AVERROR_INVALIDDATA;
124 table = buf + (256 * vect_w * vect_h);
125 if (sub_type != AVS_I_FRAME) {
126 int map_size = ((318 / vect_w + 7) / 8) * (198 / vect_h);
127 if (buf_end - table < map_size)
128 return AVERROR_INVALIDDATA;
129 init_get_bits(&change_map, table, map_size * 8);
130 table += map_size;
131 }
132
133 for (y=0; y<198; y+=vect_h) {
134 for (x=0; x<318; x+=vect_w) {
135 if (sub_type == AVS_I_FRAME || get_bits1(&change_map)) {
136 if (buf_end - table < 1)
137 return AVERROR_INVALIDDATA;
138 vect = &buf[*table++ * (vect_w * vect_h)];
139 for (j=0; j<vect_w; j++) {
140 out[(y + 0) * stride + x + j] = vect[(0 * vect_w) + j];
141 out[(y + 1) * stride + x + j] = vect[(1 * vect_w) + j];
142 if (vect_h == 3)
143 out[(y + 2) * stride + x + j] =
144 vect[(2 * vect_w) + j];
145 }
146 }
147 }
148 if (sub_type != AVS_I_FRAME)
149 align_get_bits(&change_map);
150 }
151
152 if ((ret = av_frame_ref(picture, p)) < 0)
153 return ret;
154 *got_frame = 1;
155
156 return buf_size;
157}
158
159static av_cold int avs_decode_init(AVCodecContext * avctx)
160{
161 AvsContext *s = avctx->priv_data;
162
163 s->frame = av_frame_alloc();
164 if (!s->frame)
165 return AVERROR(ENOMEM);
166
167 avctx->pix_fmt = AV_PIX_FMT_PAL8;
2ba45a60 168
f6fa7814 169 return ff_set_dimensions(avctx, 318, 198);
2ba45a60
DM
170}
171
172static av_cold int avs_decode_end(AVCodecContext *avctx)
173{
174 AvsContext *s = avctx->priv_data;
175 av_frame_free(&s->frame);
176 return 0;
177}
178
179
180AVCodec ff_avs_decoder = {
181 .name = "avs",
182 .long_name = NULL_IF_CONFIG_SMALL("AVS (Audio Video Standard) video"),
183 .type = AVMEDIA_TYPE_VIDEO,
184 .id = AV_CODEC_ID_AVS,
185 .priv_data_size = sizeof(AvsContext),
186 .init = avs_decode_init,
187 .decode = avs_decode_frame,
188 .close = avs_decode_end,
189 .capabilities = CODEC_CAP_DR1,
190};