3 * Copyright (c) 2011-2012 Paul B Mahol
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
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/imgutils.h"
28 AVCodecContext *avctx;
31 const uint8_t *palette;
39 static av_cold int cdxl_decode_init(AVCodecContext *avctx)
41 CDXLVideoContext *c = avctx->priv_data;
43 avcodec_get_frame_defaults(&c->frame);
44 c->new_video_size = 0;
50 static void import_palette(CDXLVideoContext *c, uint32_t *new_palette)
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);
63 static void bitplanar2chunky(CDXLVideoContext *c, int linesize, uint8_t *out)
65 int skip = FFALIGN(c->avctx->width, 16) - c->avctx->width;
69 init_get_bits(&gb, c->video, c->video_size * 8);
70 memset(out, 0, linesize * c->avctx->height);
71 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++)
74 out[linesize * y + x] |= get_bits1(&gb) << plane;
80 static void cdxl_decode_rgb(CDXLVideoContext *c)
82 uint32_t *new_palette = (uint32_t *)c->frame.data[1];
84 import_palette(c, new_palette);
85 bitplanar2chunky(c, c->frame.linesize[0], c->frame.data[0]);
88 static void cdxl_decode_ham6(CDXLVideoContext *c)
90 AVCodecContext *avctx = c->avctx;
91 uint32_t new_palette[16], r, g, b;
92 uint8_t *ptr, *out, index, op;
96 out = c->frame.data[0];
98 import_palette(c, new_palette);
99 bitplanar2chunky(c, avctx->width, c->new_video);
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++) {
111 r = new_palette[index] & 0xFF0000;
112 g = new_palette[index] & 0xFF00;
113 b = new_palette[index] & 0xFF;
119 r = index * 0x11 << 16;
122 g = index * 0x11 << 8;
125 AV_WN32(out + x * 3, r | g | b);
127 out += c->frame.linesize[0];
131 static void cdxl_decode_ham8(CDXLVideoContext *c)
133 AVCodecContext *avctx = c->avctx;
134 uint32_t new_palette[64], r, g, b;
135 uint8_t *ptr, *out, index, op;
139 out = c->frame.data[0];
141 import_palette(c, new_palette);
142 bitplanar2chunky(c, avctx->width, c->new_video);
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++) {
154 r = new_palette[index] & 0xFF0000;
155 g = new_palette[index] & 0xFF00;
156 b = new_palette[index] & 0xFF;
159 b = (index << 2) | (b & 3);
162 r = (index << 18) | (r & (3 << 16));
165 g = (index << 10) | (g & (3 << 8));
168 AV_WN32(out + x * 3, r | g | b);
170 out += c->frame.linesize[0];
174 static int cdxl_decode_frame(AVCodecContext *avctx, void *data,
175 int *data_size, AVPacket *pkt)
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;
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]);
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;
194 if (c->palette_size > 512)
195 return AVERROR_INVALIDDATA;
196 if (buf_size < c->palette_size + 32)
197 return AVERROR_INVALIDDATA;
199 return AVERROR_INVALIDDATA;
201 av_log_ask_for_sample(avctx, "unsupported pixel size: %d\n", c->bpp);
202 return AVERROR_PATCHWELCOME;
205 av_log_ask_for_sample(avctx, "unsupported pixel format: %d\n", format);
206 return AVERROR_PATCHWELCOME;
209 if ((ret = av_image_check_size(w, h, 0, avctx)) < 0)
211 if (w != avctx->width || h != avctx->height)
212 avcodec_set_dimensions(avctx, w, h);
214 if (c->video_size < FFALIGN(avctx->width, 16) * avctx->height * c->bpp / 8)
215 return AVERROR_INVALIDDATA;
217 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;
221 avctx->pix_fmt = PIX_FMT_BGR24;
223 av_log_ask_for_sample(avctx, "unsupported encoding %d and bpp %d\n",
225 return AVERROR_PATCHWELCOME;
229 avctx->release_buffer(avctx, p);
232 if ((ret = avctx->get_buffer(avctx, p)) < 0) {
233 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
236 p->pict_type = AV_PICTURE_TYPE_I;
239 av_fast_padded_malloc(&c->new_video, &c->new_video_size,
240 h * w + FF_INPUT_BUFFER_PADDING_SIZE);
242 return AVERROR(ENOMEM);
250 *data_size = sizeof(AVFrame);
251 *(AVFrame*)data = c->frame;
256 static av_cold int cdxl_decode_end(AVCodecContext *avctx)
258 CDXLVideoContext *c = avctx->priv_data;
260 av_free(c->new_video);
261 if (c->frame.data[0])
262 avctx->release_buffer(avctx, &c->frame);
267 AVCodec ff_cdxl_decoder = {
269 .type = AVMEDIA_TYPE_VIDEO,
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"),