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  import_palette(c, new_palette);
123  import_format(c, c->frame.linesize[0], c->frame.data[0]);
124 }
125 
127 {
128  AVCodecContext *avctx = c->avctx;
129  uint32_t new_palette[16], r, g, b;
130  uint8_t *ptr, *out, index, op;
131  int x, y;
132 
133  ptr = c->new_video;
134  out = c->frame.data[0];
135 
136  import_palette(c, new_palette);
137  import_format(c, avctx->width, c->new_video);
138 
139  for (y = 0; y < avctx->height; y++) {
140  r = new_palette[0] & 0xFF0000;
141  g = new_palette[0] & 0xFF00;
142  b = new_palette[0] & 0xFF;
143  for (x = 0; x < avctx->width; x++) {
144  index = *ptr++;
145  op = index >> 4;
146  index &= 15;
147  switch (op) {
148  case 0:
149  r = new_palette[index] & 0xFF0000;
150  g = new_palette[index] & 0xFF00;
151  b = new_palette[index] & 0xFF;
152  break;
153  case 1:
154  b = index * 0x11;
155  break;
156  case 2:
157  r = index * 0x11 << 16;
158  break;
159  case 3:
160  g = index * 0x11 << 8;
161  break;
162  }
163  AV_WL24(out + x * 3, r | g | b);
164  }
165  out += c->frame.linesize[0];
166  }
167 }
168 
170 {
171  AVCodecContext *avctx = c->avctx;
172  uint32_t new_palette[64], r, g, b;
173  uint8_t *ptr, *out, index, op;
174  int x, y;
175 
176  ptr = c->new_video;
177  out = c->frame.data[0];
178 
179  import_palette(c, new_palette);
180  import_format(c, avctx->width, c->new_video);
181 
182  for (y = 0; y < avctx->height; y++) {
183  r = new_palette[0] & 0xFF0000;
184  g = new_palette[0] & 0xFF00;
185  b = new_palette[0] & 0xFF;
186  for (x = 0; x < avctx->width; x++) {
187  index = *ptr++;
188  op = index >> 6;
189  index &= 63;
190  switch (op) {
191  case 0:
192  r = new_palette[index] & 0xFF0000;
193  g = new_palette[index] & 0xFF00;
194  b = new_palette[index] & 0xFF;
195  break;
196  case 1:
197  b = (index << 2) | (b & 3);
198  break;
199  case 2:
200  r = (index << 18) | (r & (3 << 16));
201  break;
202  case 3:
203  g = (index << 10) | (g & (3 << 8));
204  break;
205  }
206  AV_WL24(out + x * 3, r | g | b);
207  }
208  out += c->frame.linesize[0];
209  }
210 }
211 
212 static int cdxl_decode_frame(AVCodecContext *avctx, void *data,
213  int *got_frame, AVPacket *pkt)
214 {
215  CDXLVideoContext *c = avctx->priv_data;
216  AVFrame * const p = &c->frame;
217  int ret, w, h, encoding, aligned_width, buf_size = pkt->size;
218  const uint8_t *buf = pkt->data;
219 
220  if (buf_size < 32)
221  return AVERROR_INVALIDDATA;
222  encoding = buf[1] & 7;
223  c->format = buf[1] & 0xE0;
224  w = AV_RB16(&buf[14]);
225  h = AV_RB16(&buf[16]);
226  c->bpp = buf[19];
227  c->palette_size = AV_RB16(&buf[20]);
228  c->palette = buf + 32;
229  c->video = c->palette + c->palette_size;
230  c->video_size = buf_size - c->palette_size - 32;
231 
232  if (c->palette_size > 512)
233  return AVERROR_INVALIDDATA;
234  if (buf_size < c->palette_size + 32)
235  return AVERROR_INVALIDDATA;
236  if (c->bpp < 1)
237  return AVERROR_INVALIDDATA;
238  if (c->format != BIT_PLANAR && c->format != BIT_LINE) {
239  av_log_ask_for_sample(avctx, "unsupported pixel format: 0x%0x\n", c->format);
240  return AVERROR_PATCHWELCOME;
241  }
242 
243  if ((ret = av_image_check_size(w, h, 0, avctx)) < 0)
244  return ret;
245  if (w != avctx->width || h != avctx->height)
246  avcodec_set_dimensions(avctx, w, h);
247 
248  aligned_width = FFALIGN(c->avctx->width, 16);
249  c->padded_bits = aligned_width - c->avctx->width;
250  if (c->video_size < aligned_width * avctx->height * c->bpp / 8)
251  return AVERROR_INVALIDDATA;
252  if (!encoding && c->palette_size && c->bpp <= 8) {
253  avctx->pix_fmt = AV_PIX_FMT_PAL8;
254  } else if (encoding == 1 && (c->bpp == 6 || c->bpp == 8)) {
255  if (c->palette_size != (1 << (c->bpp - 1)))
256  return AVERROR_INVALIDDATA;
257  avctx->pix_fmt = AV_PIX_FMT_BGR24;
258  } else {
259  av_log_ask_for_sample(avctx, "unsupported encoding %d and bpp %d\n",
260  encoding, c->bpp);
261  return AVERROR_PATCHWELCOME;
262  }
263 
264  if (p->data[0])
265  avctx->release_buffer(avctx, p);
266 
267  p->reference = 0;
268  if ((ret = ff_get_buffer(avctx, p)) < 0) {
269  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
270  return ret;
271  }
273 
274  if (encoding) {
277  if (!c->new_video)
278  return AVERROR(ENOMEM);
279  if (c->bpp == 8)
280  cdxl_decode_ham8(c);
281  else
282  cdxl_decode_ham6(c);
283  } else {
284  cdxl_decode_rgb(c);
285  }
286  *got_frame = 1;
287  *(AVFrame*)data = c->frame;
288 
289  return buf_size;
290 }
291 
293 {
294  CDXLVideoContext *c = avctx->priv_data;
295 
296  av_free(c->new_video);
297  if (c->frame.data[0])
298  avctx->release_buffer(avctx, &c->frame);
299 
300  return 0;
301 }
302 
304  .name = "cdxl",
305  .type = AVMEDIA_TYPE_VIDEO,
306  .id = AV_CODEC_ID_CDXL,
307  .priv_data_size = sizeof(CDXLVideoContext),
311  .capabilities = CODEC_CAP_DR1,
312  .long_name = NULL_IF_CONFIG_SMALL("Commodore CDXL video"),
313 };