blob: bb9df40122b50e65482115e8568ca595587dbebf [file] [log] [blame]
Paul B Maholdc4e5742012-02-14 17:36:201/*
2 * CDXL video decoder
3 * Copyright (c) 2011-2012 Paul B Mahol
4 *
5 * This file is part of Libav.
6 *
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.
11 *
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.
16 *
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
20 */
21
22#include "libavutil/intreadwrite.h"
23#include "libavutil/imgutils.h"
24#include "avcodec.h"
25#include "get_bits.h"
26
27typedef struct {
28 AVCodecContext *avctx;
29 AVFrame frame;
30 int bpp;
31 const uint8_t *palette;
32 int palette_size;
33 const uint8_t *video;
34 int video_size;
35 uint8_t *new_video;
36 int new_video_size;
37} CDXLVideoContext;
38
39static av_cold int cdxl_decode_init(AVCodecContext *avctx)
40{
41 CDXLVideoContext *c = avctx->priv_data;
42
43 avcodec_get_frame_defaults(&c->frame);
44 c->new_video_size = 0;
45 c->avctx = avctx;
46
47 return 0;
48}
49
50static void import_palette(CDXLVideoContext *c, uint32_t *new_palette)
51{
52 int i;
53
54 for (i = 0; i < c->palette_size / 2; i++) {
55 unsigned rgb = AV_RB16(&c->palette[i * 2]);
56 unsigned r = ((rgb >> 8) & 0xF) * 0x11;
57 unsigned g = ((rgb >> 4) & 0xF) * 0x11;
58 unsigned b = (rgb & 0xF) * 0x11;
59 AV_WN32(&new_palette[i], (r << 16) | (g << 8) | b);
60 }
61}
62
Paul B Maholb5c626f2012-02-17 17:42:3663static void bitplanar2chunky(CDXLVideoContext *c, int linesize, uint8_t *out)
Paul B Maholdc4e5742012-02-14 17:36:2064{
Paul B Maholb5c626f2012-02-17 17:42:3665 int skip = FFALIGN(c->avctx->width, 16) - c->avctx->width;
Paul B Maholdc4e5742012-02-14 17:36:2066 GetBitContext gb;
67 int x, y, plane;
68
69 init_get_bits(&gb, c->video, c->video_size * 8);
70 memset(out, 0, linesize * c->avctx->height);
Paul B Maholb5c626f2012-02-17 17:42:3671 for (plane = 0; plane < c->bpp; plane++) {
72 for (y = 0; y < c->avctx->height; y++) {
73 for (x = 0; x < c->avctx->width; x++)
Paul B Maholdc4e5742012-02-14 17:36:2074 out[linesize * y + x] |= get_bits1(&gb) << plane;
Paul B Maholb5c626f2012-02-17 17:42:3675 skip_bits(&gb, skip);
76 }
77 }
Paul B Maholdc4e5742012-02-14 17:36:2078}
79
80static void cdxl_decode_rgb(CDXLVideoContext *c)
81{
82 uint32_t *new_palette = (uint32_t *)c->frame.data[1];
Paul B Maholdc4e5742012-02-14 17:36:2083
84 import_palette(c, new_palette);
Paul B Maholb5c626f2012-02-17 17:42:3685 bitplanar2chunky(c, c->frame.linesize[0], c->frame.data[0]);
Paul B Maholdc4e5742012-02-14 17:36:2086}
87
88static void cdxl_decode_ham6(CDXLVideoContext *c)
89{
90 AVCodecContext *avctx = c->avctx;
91 uint32_t new_palette[16], r, g, b;
92 uint8_t *ptr, *out, index, op;
93 int x, y;
94
95 ptr = c->new_video;
96 out = c->frame.data[0];
97
98 import_palette(c, new_palette);
Paul B Maholb5c626f2012-02-17 17:42:3699 bitplanar2chunky(c, avctx->width, c->new_video);
Paul B Maholdc4e5742012-02-14 17:36:20100
101 for (y = 0; y < avctx->height; y++) {
102 r = new_palette[0] & 0xFF0000;
103 g = new_palette[0] & 0xFF00;
104 b = new_palette[0] & 0xFF;
105 for (x = 0; x < avctx->width; x++) {
106 index = *ptr++;
107 op = index >> 4;
108 index &= 15;
109 switch (op) {
110 case 0:
111 r = new_palette[index] & 0xFF0000;
112 g = new_palette[index] & 0xFF00;
113 b = new_palette[index] & 0xFF;
114 break;
115 case 1:
116 b = index * 0x11;
117 break;
118 case 2:
119 r = index * 0x11 << 16;
120 break;
121 case 3:
122 g = index * 0x11 << 8;
123 break;
124 }
125 AV_WN32(out + x * 3, r | g | b);
126 }
127 out += c->frame.linesize[0];
128 }
129}
130
131static void cdxl_decode_ham8(CDXLVideoContext *c)
132{
133 AVCodecContext *avctx = c->avctx;
134 uint32_t new_palette[64], r, g, b;
135 uint8_t *ptr, *out, index, op;
136 int x, y;
137
138 ptr = c->new_video;
139 out = c->frame.data[0];
140
141 import_palette(c, new_palette);
Paul B Maholb5c626f2012-02-17 17:42:36142 bitplanar2chunky(c, avctx->width, c->new_video);
Paul B Maholdc4e5742012-02-14 17:36:20143
144 for (y = 0; y < avctx->height; y++) {
145 r = new_palette[0] & 0xFF0000;
146 g = new_palette[0] & 0xFF00;
147 b = new_palette[0] & 0xFF;
148 for (x = 0; x < avctx->width; x++) {
149 index = *ptr++;
150 op = index >> 6;
151 index &= 63;
152 switch (op) {
153 case 0:
154 r = new_palette[index] & 0xFF0000;
155 g = new_palette[index] & 0xFF00;
156 b = new_palette[index] & 0xFF;
157 break;
158 case 1:
159 b = (index << 2) | (b & 3);
160 break;
161 case 2:
162 r = (index << 18) | (r & (3 << 16));
163 break;
164 case 3:
165 g = (index << 10) | (g & (3 << 8));
166 break;
167 }
168 AV_WN32(out + x * 3, r | g | b);
169 }
170 out += c->frame.linesize[0];
171 }
172}
173
174static int cdxl_decode_frame(AVCodecContext *avctx, void *data,
175 int *data_size, AVPacket *pkt)
176{
177 CDXLVideoContext *c = avctx->priv_data;
178 AVFrame * const p = &c->frame;
179 int ret, w, h, encoding, format, buf_size = pkt->size;
180 const uint8_t *buf = pkt->data;
181
182 if (buf_size < 32)
183 return AVERROR_INVALIDDATA;
184 encoding = buf[1] & 7;
185 format = buf[1] & 0xE0;
186 w = AV_RB16(&buf[14]);
187 h = AV_RB16(&buf[16]);
188 c->bpp = buf[19];
189 c->palette_size = AV_RB16(&buf[20]);
190 c->palette = buf + 32;
191 c->video = c->palette + c->palette_size;
192 c->video_size = buf_size - c->palette_size - 32;
193
194 if (c->palette_size > 512)
195 return AVERROR_INVALIDDATA;
196 if (buf_size < c->palette_size + 32)
197 return AVERROR_INVALIDDATA;
198 if (c->bpp < 1)
199 return AVERROR_INVALIDDATA;
200 if (c->bpp > 8) {
201 av_log_ask_for_sample(avctx, "unsupported pixel size: %d\n", c->bpp);
202 return AVERROR_PATCHWELCOME;
203 }
204 if (format) {
205 av_log_ask_for_sample(avctx, "unsupported pixel format: %d\n", format);
206 return AVERROR_PATCHWELCOME;
207 }
208
209 if ((ret = av_image_check_size(w, h, 0, avctx)) < 0)
210 return ret;
211 if (w != avctx->width || h != avctx->height)
212 avcodec_set_dimensions(avctx, w, h);
213
Paul B Maholb5c626f2012-02-17 17:42:36214 if (c->video_size < FFALIGN(avctx->width, 16) * avctx->height * c->bpp / 8)
215 return AVERROR_INVALIDDATA;
Paul B Maholdc4e5742012-02-14 17:36:20216 if (encoding == 0) {
Paul B Maholdc4e5742012-02-14 17:36:20217 avctx->pix_fmt = PIX_FMT_PAL8;
218 } else if (encoding == 1 && (c->bpp == 6 || c->bpp == 8)) {
219 if (c->palette_size != (1 << (c->bpp - 1)))
220 return AVERROR_INVALIDDATA;
Paul B Maholdc4e5742012-02-14 17:36:20221 avctx->pix_fmt = PIX_FMT_BGR24;
222 } else {
223 av_log_ask_for_sample(avctx, "unsupported encoding %d and bpp %d\n",
224 encoding, c->bpp);
225 return AVERROR_PATCHWELCOME;
226 }
227
228 if (p->data[0])
229 avctx->release_buffer(avctx, p);
230
231 p->reference = 0;
232 if ((ret = avctx->get_buffer(avctx, p)) < 0) {
233 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
234 return ret;
235 }
236 p->pict_type = AV_PICTURE_TYPE_I;
237
238 if (encoding) {
239 av_fast_padded_malloc(&c->new_video, &c->new_video_size,
240 h * w + FF_INPUT_BUFFER_PADDING_SIZE);
241 if (!c->new_video)
242 return AVERROR(ENOMEM);
243 if (c->bpp == 8)
244 cdxl_decode_ham8(c);
245 else
246 cdxl_decode_ham6(c);
247 } else {
248 cdxl_decode_rgb(c);
249 }
250 *data_size = sizeof(AVFrame);
251 *(AVFrame*)data = c->frame;
252
253 return buf_size;
254}
255
256static av_cold int cdxl_decode_end(AVCodecContext *avctx)
257{
258 CDXLVideoContext *c = avctx->priv_data;
259
260 av_free(c->new_video);
261 if (c->frame.data[0])
262 avctx->release_buffer(avctx, &c->frame);
263
264 return 0;
265}
266
267AVCodec ff_cdxl_decoder = {
268 .name = "cdxl",
269 .type = AVMEDIA_TYPE_VIDEO,
270 .id = CODEC_ID_CDXL,
271 .priv_data_size = sizeof(CDXLVideoContext),
272 .init = cdxl_decode_init,
273 .close = cdxl_decode_end,
274 .decode = cdxl_decode_frame,
275 .capabilities = CODEC_CAP_DR1,
276 .long_name = NULL_IF_CONFIG_SMALL("Commodore CDXL video"),
277};