FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cdxl.c
Go to the documentation of this file.
1 /*
2  * CDXL video decoder
3  * Copyright (c) 2011-2012 Paul B Mahol
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 #define UNCHECKED_BITSTREAM_READER 1
23 
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/imgutils.h"
26 #include "avcodec.h"
27 #include "get_bits.h"
28 #include "internal.h"
29 
30 #define BIT_PLANAR 0x00
31 #define CHUNKY 0x20
32 #define BYTE_PLANAR 0x40
33 #define BIT_LINE 0x80
34 #define BYTE_LINE 0xC0
35 
36 typedef struct {
39  int bpp;
40  int format;
42  const uint8_t *palette;
44  const uint8_t *video;
49 
51 {
52  CDXLVideoContext *c = avctx->priv_data;
53 
55  c->new_video_size = 0;
56  c->avctx = avctx;
57 
58  return 0;
59 }
60 
61 static void import_palette(CDXLVideoContext *c, uint32_t *new_palette)
62 {
63  int i;
64 
65  for (i = 0; i < c->palette_size / 2; i++) {
66  unsigned rgb = AV_RB16(&c->palette[i * 2]);
67  unsigned r = ((rgb >> 8) & 0xF) * 0x11;
68  unsigned g = ((rgb >> 4) & 0xF) * 0x11;
69  unsigned b = (rgb & 0xF) * 0x11;
70  AV_WN32(&new_palette[i], (0xFFU << 24) | (r << 16) | (g << 8) | b);
71  }
72 }
73 
74 static void bitplanar2chunky(CDXLVideoContext *c, int linesize, uint8_t *out)
75 {
76  GetBitContext gb;
77  int x, y, plane;
78 
79  init_get_bits(&gb, c->video, c->video_size * 8);
80  for (plane = 0; plane < c->bpp; plane++) {
81  for (y = 0; y < c->avctx->height; y++) {
82  for (x = 0; x < c->avctx->width; x++)
83  out[linesize * y + x] |= get_bits1(&gb) << plane;
84  skip_bits(&gb, c->padded_bits);
85  }
86  }
87 }
88 
89 static void bitline2chunky(CDXLVideoContext *c, int linesize, uint8_t *out)
90 {
91  GetBitContext gb;
92  int x, y, plane;
93 
94  init_get_bits(&gb, c->video, c->video_size * 8);
95  for (y = 0; y < c->avctx->height; y++) {
96  for (plane = 0; plane < c->bpp; plane++) {
97  for (x = 0; x < c->avctx->width; x++)
98  out[linesize * y + x] |= get_bits1(&gb) << plane;
99  skip_bits(&gb, c->padded_bits);
100  }
101  }
102 }
103 
104 static void import_format(CDXLVideoContext *c, int linesize, uint8_t *out)
105 {
106  memset(out, 0, linesize * c->avctx->height);
107 
108  switch (c->format) {
109  case BIT_PLANAR:
110  bitplanar2chunky(c, linesize, out);
111  break;
112  case BIT_LINE:
113  bitline2chunky(c, linesize, out);
114  break;
115  }
116 }
117 
119 {
120  uint32_t *new_palette = (uint32_t *)c->frame.data[1];
121 
122  memset(c->frame.data[1], 0, AVPALETTE_SIZE);
123  import_palette(c, new_palette);
124  import_format(c, c->frame.linesize[0], c->frame.data[0]);
125 }
126 
128 {
129  AVCodecContext *avctx = c->avctx;
130  uint32_t new_palette[16], r, g, b;
131  uint8_t *ptr, *out, index, op;
132  int x, y;
133 
134  ptr = c->new_video;
135  out = c->frame.data[0];
136 
137  import_palette(c, new_palette);
138  import_format(c, avctx->width, c->new_video);
139 
140  for (y = 0; y < avctx->height; y++) {
141  r = new_palette[0] & 0xFF0000;
142  g = new_palette[0] & 0xFF00;
143  b = new_palette[0] & 0xFF;
144  for (x = 0; x < avctx->width; x++) {
145  index = *ptr++;
146  op = index >> 4;
147  index &= 15;
148  switch (op) {
149  case 0:
150  r = new_palette[index] & 0xFF0000;
151  g = new_palette[index] & 0xFF00;
152  b = new_palette[index] & 0xFF;
153  break;
154  case 1:
155  b = index * 0x11;
156  break;
157  case 2:
158  r = index * 0x11 << 16;
159  break;
160  case 3:
161  g = index * 0x11 << 8;
162  break;
163  }
164  AV_WL24(out + x * 3, r | g | b);
165  }
166  out += c->frame.linesize[0];
167  }
168 }
169 
171 {
172  AVCodecContext *avctx = c->avctx;
173  uint32_t new_palette[64], r, g, b;
174  uint8_t *ptr, *out, index, op;
175  int x, y;
176 
177  ptr = c->new_video;
178  out = c->frame.data[0];
179 
180  import_palette(c, new_palette);
181  import_format(c, avctx->width, c->new_video);
182 
183  for (y = 0; y < avctx->height; y++) {
184  r = new_palette[0] & 0xFF0000;
185  g = new_palette[0] & 0xFF00;
186  b = new_palette[0] & 0xFF;
187  for (x = 0; x < avctx->width; x++) {
188  index = *ptr++;
189  op = index >> 6;
190  index &= 63;
191  switch (op) {
192  case 0:
193  r = new_palette[index] & 0xFF0000;
194  g = new_palette[index] & 0xFF00;
195  b = new_palette[index] & 0xFF;
196  break;
197  case 1:
198  b = (index << 2) | (b & 3);
199  break;
200  case 2:
201  r = (index << 18) | (r & (3 << 16));
202  break;
203  case 3:
204  g = (index << 10) | (g & (3 << 8));
205  break;
206  }
207  AV_WL24(out + x * 3, r | g | b);
208  }
209  out += c->frame.linesize[0];
210  }
211 }
212 
213 static int cdxl_decode_frame(AVCodecContext *avctx, void *data,
214  int *got_frame, AVPacket *pkt)
215 {
216  CDXLVideoContext *c = avctx->priv_data;
217  AVFrame * const p = &c->frame;
218  int ret, w, h, encoding, aligned_width, buf_size = pkt->size;
219  const uint8_t *buf = pkt->data;
220 
221  if (buf_size < 32)
222  return AVERROR_INVALIDDATA;
223  encoding = buf[1] & 7;
224  c->format = buf[1] & 0xE0;
225  w = AV_RB16(&buf[14]);
226  h = AV_RB16(&buf[16]);
227  c->bpp = buf[19];
228  c->palette_size = AV_RB16(&buf[20]);
229  c->palette = buf + 32;
230  c->video = c->palette + c->palette_size;
231  c->video_size = buf_size - c->palette_size - 32;
232 
233  if (c->palette_size > 512)
234  return AVERROR_INVALIDDATA;
235  if (buf_size < c->palette_size + 32)
236  return AVERROR_INVALIDDATA;
237  if (c->bpp < 1)
238  return AVERROR_INVALIDDATA;
239  if (c->format != BIT_PLANAR && c->format != BIT_LINE) {
240  av_log_ask_for_sample(avctx, "unsupported pixel format: 0x%0x\n", c->format);
241  return AVERROR_PATCHWELCOME;
242  }
243 
244  if ((ret = av_image_check_size(w, h, 0, avctx)) < 0)
245  return ret;
246  if (w != avctx->width || h != avctx->height)
247  avcodec_set_dimensions(avctx, w, h);
248 
249  aligned_width = FFALIGN(c->avctx->width, 16);
250  c->padded_bits = aligned_width - c->avctx->width;
251  if (c->video_size < aligned_width * avctx->height * c->bpp / 8)
252  return AVERROR_INVALIDDATA;
253  if (!encoding && c->palette_size && c->bpp <= 8) {
254  avctx->pix_fmt = AV_PIX_FMT_PAL8;
255  } else if (encoding == 1 && (c->bpp == 6 || c->bpp == 8)) {
256  if (c->palette_size != (1 << (c->bpp - 1)))
257  return AVERROR_INVALIDDATA;
258  avctx->pix_fmt = AV_PIX_FMT_BGR24;
259  } else {
260  av_log_ask_for_sample(avctx, "unsupported encoding %d and bpp %d\n",
261  encoding, c->bpp);
262  return AVERROR_PATCHWELCOME;
263  }
264 
265  if (p->data[0])
266  avctx->release_buffer(avctx, p);
267 
268  p->reference = 0;
269  if ((ret = ff_get_buffer(avctx, p)) < 0) {
270  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
271  return ret;
272  }
274 
275  if (encoding) {
278  if (!c->new_video)
279  return AVERROR(ENOMEM);
280  if (c->bpp == 8)
281  cdxl_decode_ham8(c);
282  else
283  cdxl_decode_ham6(c);
284  } else {
285  cdxl_decode_rgb(c);
286  }
287  *got_frame = 1;
288  *(AVFrame*)data = c->frame;
289 
290  return buf_size;
291 }
292 
294 {
295  CDXLVideoContext *c = avctx->priv_data;
296 
297  av_free(c->new_video);
298  if (c->frame.data[0])
299  avctx->release_buffer(avctx, &c->frame);
300 
301  return 0;
302 }
303 
305  .name = "cdxl",
306  .type = AVMEDIA_TYPE_VIDEO,
307  .id = AV_CODEC_ID_CDXL,
308  .priv_data_size = sizeof(CDXLVideoContext),
312  .capabilities = CODEC_CAP_DR1,
313  .long_name = NULL_IF_CONFIG_SMALL("Commodore CDXL video"),
314 };