3 * Copyright (c) 2006 Aurelien Jacobs <aurel@gnuage.org>
5 * This file is part of Libav.
7 * Libav 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.
12 * Libav 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
39 AVS_P_FRAME_3X3 = 0x01,
40 AVS_P_FRAME_2X2 = 0x02,
41 AVS_P_FRAME_2X3 = 0x03,
46 avs_decode_frame(AVCodecContext * avctx,
47 void *data, int *data_size, AVPacket *avpkt)
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 = &avs->picture;
55 const uint8_t *table, *vect;
57 int i, j, x, y, stride, vect_w = 3, vect_h = 3;
58 AvsVideoSubType sub_type;
60 GetBitContext change_map;
62 if (avctx->reget_buffer(avctx, p)) {
63 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
67 p->pict_type = AV_PICTURE_TYPE_P;
70 out = avs->picture.data[0];
71 stride = avs->picture.linesize[0];
73 if (buf_end - buf < 4)
74 return AVERROR_INVALIDDATA;
79 if (type == AVS_PALETTE) {
81 uint32_t *pal = (uint32_t *) avs->picture.data[1];
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;
88 for (i=first; i<last; i++, buf+=3)
89 pal[i] = (buf[0] << 18) | (buf[1] << 10) | (buf[2] << 2);
96 if (type != AVS_VIDEO)
101 p->pict_type = AV_PICTURE_TYPE_I;
103 case AVS_P_FRAME_3X3:
108 case AVS_P_FRAME_2X2:
113 case AVS_P_FRAME_2X3:
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);
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];
143 out[(y + 2) * stride + x + j] =
144 vect[(2 * vect_w) + j];
148 if (sub_type != AVS_I_FRAME)
149 align_get_bits(&change_map);
152 *picture = avs->picture;
153 *data_size = sizeof(AVPicture);
158 static av_cold int avs_decode_init(AVCodecContext * avctx)
160 avctx->pix_fmt = PIX_FMT_PAL8;
164 static av_cold int avs_decode_end(AVCodecContext *avctx)
166 AvsContext *s = avctx->priv_data;
167 if (s->picture.data[0])
168 avctx->release_buffer(avctx, &s->picture);
173 AVCodec ff_avs_decoder = {
175 .type = AVMEDIA_TYPE_VIDEO,
177 .priv_data_size = sizeof(AvsContext),
178 .init = avs_decode_init,
179 .decode = avs_decode_frame,
180 .close = avs_decode_end,
181 .capabilities = CODEC_CAP_DR1,
182 .long_name = NULL_IF_CONFIG_SMALL("AVS (Audio Video Standard) video"),