]> git.sesse.net Git - ffmpeg/blob - libavcodec/cdxl.c
CDXL demuxer and decoder
[ffmpeg] / libavcodec / cdxl.c
1 /*
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
27 typedef 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
39 static 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
50 static 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
63 static void bitplanar2chunky(CDXLVideoContext *c, int width,
64                              int linesize, uint8_t *out)
65 {
66     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);
71     for (plane = 0; plane < c->bpp; plane++)
72         for (y = 0; y < c->avctx->height; y++)
73             for (x = 0; x < width; x++)
74                 out[linesize * y + x] |= get_bits1(&gb) << plane;
75 }
76
77 static void cdxl_decode_rgb(CDXLVideoContext *c)
78 {
79     uint32_t *new_palette = (uint32_t *)c->frame.data[1];
80     int padded_width = FFALIGN(c->avctx->width, 16);
81
82     import_palette(c, new_palette);
83     bitplanar2chunky(c, padded_width, c->frame.linesize[0], c->frame.data[0]);
84 }
85
86 static void cdxl_decode_ham6(CDXLVideoContext *c)
87 {
88     AVCodecContext *avctx = c->avctx;
89     uint32_t new_palette[16], r, g, b;
90     uint8_t *ptr, *out, index, op;
91     int x, y;
92
93     ptr = c->new_video;
94     out = c->frame.data[0];
95
96     import_palette(c, new_palette);
97     bitplanar2chunky(c, avctx->width, avctx->width, c->new_video);
98
99     for (y = 0; y < avctx->height; y++) {
100         r = new_palette[0] & 0xFF0000;
101         g = new_palette[0] & 0xFF00;
102         b = new_palette[0] & 0xFF;
103         for (x = 0; x < avctx->width; x++) {
104             index  = *ptr++;
105             op     = index >> 4;
106             index &= 15;
107             switch (op) {
108             case 0:
109                 r = new_palette[index] & 0xFF0000;
110                 g = new_palette[index] & 0xFF00;
111                 b = new_palette[index] & 0xFF;
112                 break;
113             case 1:
114                 b = index * 0x11;
115                 break;
116             case 2:
117                 r = index * 0x11 << 16;
118                 break;
119             case 3:
120                 g = index * 0x11 << 8;
121                 break;
122             }
123             AV_WN32(out + x * 3, r | g | b);
124         }
125         out += c->frame.linesize[0];
126     }
127 }
128
129 static void cdxl_decode_ham8(CDXLVideoContext *c)
130 {
131     AVCodecContext *avctx = c->avctx;
132     uint32_t new_palette[64], r, g, b;
133     uint8_t *ptr, *out, index, op;
134     int x, y;
135
136     ptr = c->new_video;
137     out = c->frame.data[0];
138
139     import_palette(c, new_palette);
140     bitplanar2chunky(c, avctx->width, avctx->width, c->new_video);
141
142     for (y = 0; y < avctx->height; y++) {
143         r = new_palette[0] & 0xFF0000;
144         g = new_palette[0] & 0xFF00;
145         b = new_palette[0] & 0xFF;
146         for (x = 0; x < avctx->width; x++) {
147             index  = *ptr++;
148             op     = index >> 6;
149             index &= 63;
150             switch (op) {
151             case 0:
152                 r = new_palette[index] & 0xFF0000;
153                 g = new_palette[index] & 0xFF00;
154                 b = new_palette[index] & 0xFF;
155                 break;
156             case 1:
157                 b = (index <<  2) | (b & 3);
158                 break;
159             case 2:
160                 r = (index << 18) | (r & (3 << 16));
161                 break;
162             case 3:
163                 g = (index << 10) | (g & (3 << 8));
164                 break;
165             }
166             AV_WN32(out + x * 3, r | g | b);
167         }
168         out += c->frame.linesize[0];
169     }
170 }
171
172 static int cdxl_decode_frame(AVCodecContext *avctx, void *data,
173                              int *data_size, AVPacket *pkt)
174 {
175     CDXLVideoContext *c = avctx->priv_data;
176     AVFrame * const p = &c->frame;
177     int ret, w, h, encoding, format, buf_size = pkt->size;
178     const uint8_t *buf = pkt->data;
179
180     if (buf_size < 32)
181         return AVERROR_INVALIDDATA;
182     encoding        = buf[1] & 7;
183     format          = buf[1] & 0xE0;
184     w               = AV_RB16(&buf[14]);
185     h               = AV_RB16(&buf[16]);
186     c->bpp          = buf[19];
187     c->palette_size = AV_RB16(&buf[20]);
188     c->palette      = buf + 32;
189     c->video        = c->palette + c->palette_size;
190     c->video_size   = buf_size - c->palette_size - 32;
191
192     if (c->palette_size > 512)
193         return AVERROR_INVALIDDATA;
194     if (buf_size < c->palette_size + 32)
195         return AVERROR_INVALIDDATA;
196     if (c->bpp < 1)
197         return AVERROR_INVALIDDATA;
198     if (c->bpp > 8) {
199         av_log_ask_for_sample(avctx, "unsupported pixel size: %d\n", c->bpp);
200         return AVERROR_PATCHWELCOME;
201     }
202     if (format) {
203         av_log_ask_for_sample(avctx, "unsupported pixel format: %d\n", format);
204         return AVERROR_PATCHWELCOME;
205     }
206
207     if ((ret = av_image_check_size(w, h, 0, avctx)) < 0)
208         return ret;
209     if (w != avctx->width || h != avctx->height)
210         avcodec_set_dimensions(avctx, w, h);
211
212     if (encoding == 0) {
213         if (c->video_size < FFALIGN(avctx->width, 16) *
214                             avctx->height * c->bpp / 8)
215             return AVERROR_INVALIDDATA;
216         avctx->pix_fmt = PIX_FMT_PAL8;
217     } else if (encoding == 1 && (c->bpp == 6 || c->bpp == 8)) {
218         if (c->palette_size != (1 << (c->bpp - 1)))
219             return AVERROR_INVALIDDATA;
220         if (c->video_size < avctx->width * avctx->height * c->bpp / 8)
221             return AVERROR_INVALIDDATA;
222         avctx->pix_fmt = PIX_FMT_BGR24;
223     } else {
224         av_log_ask_for_sample(avctx, "unsupported encoding %d and bpp %d\n",
225                               encoding, c->bpp);
226         return AVERROR_PATCHWELCOME;
227     }
228
229     if (p->data[0])
230         avctx->release_buffer(avctx, p);
231
232     p->reference = 0;
233     if ((ret = avctx->get_buffer(avctx, p)) < 0) {
234         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
235         return ret;
236     }
237     p->pict_type = AV_PICTURE_TYPE_I;
238
239     if (encoding) {
240         av_fast_padded_malloc(&c->new_video, &c->new_video_size,
241                               h * w + FF_INPUT_BUFFER_PADDING_SIZE);
242         if (!c->new_video)
243             return AVERROR(ENOMEM);
244         if (c->bpp == 8)
245             cdxl_decode_ham8(c);
246         else
247             cdxl_decode_ham6(c);
248     } else {
249         cdxl_decode_rgb(c);
250     }
251     *data_size      = sizeof(AVFrame);
252     *(AVFrame*)data = c->frame;
253
254     return buf_size;
255 }
256
257 static av_cold int cdxl_decode_end(AVCodecContext *avctx)
258 {
259     CDXLVideoContext *c = avctx->priv_data;
260
261     av_free(c->new_video);
262     if (c->frame.data[0])
263         avctx->release_buffer(avctx, &c->frame);
264
265     return 0;
266 }
267
268 AVCodec ff_cdxl_decoder = {
269     .name           = "cdxl",
270     .type           = AVMEDIA_TYPE_VIDEO,
271     .id             = CODEC_ID_CDXL,
272     .priv_data_size = sizeof(CDXLVideoContext),
273     .init           = cdxl_decode_init,
274     .close          = cdxl_decode_end,
275     .decode         = cdxl_decode_frame,
276     .capabilities   = CODEC_CAP_DR1,
277     .long_name      = NULL_IF_CONFIG_SMALL("Commodore CDXL video"),
278 };