00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #define UNCHECKED_BITSTREAM_READER 1
00023
00024 #include "libavutil/intreadwrite.h"
00025 #include "libavutil/imgutils.h"
00026 #include "avcodec.h"
00027 #include "get_bits.h"
00028
00029 #define BIT_PLANAR 0x00
00030 #define CHUNKY 0x20
00031 #define BYTE_PLANAR 0x40
00032 #define BIT_LINE 0x80
00033 #define BYTE_LINE 0xC0
00034
00035 typedef struct {
00036 AVCodecContext *avctx;
00037 AVFrame frame;
00038 int bpp;
00039 int format;
00040 int padded_bits;
00041 const uint8_t *palette;
00042 int palette_size;
00043 const uint8_t *video;
00044 int video_size;
00045 uint8_t *new_video;
00046 int new_video_size;
00047 } CDXLVideoContext;
00048
00049 static av_cold int cdxl_decode_init(AVCodecContext *avctx)
00050 {
00051 CDXLVideoContext *c = avctx->priv_data;
00052
00053 avcodec_get_frame_defaults(&c->frame);
00054 c->new_video_size = 0;
00055 c->avctx = avctx;
00056
00057 return 0;
00058 }
00059
00060 static void import_palette(CDXLVideoContext *c, uint32_t *new_palette)
00061 {
00062 int i;
00063
00064 for (i = 0; i < c->palette_size / 2; i++) {
00065 unsigned rgb = AV_RB16(&c->palette[i * 2]);
00066 unsigned r = ((rgb >> 8) & 0xF) * 0x11;
00067 unsigned g = ((rgb >> 4) & 0xF) * 0x11;
00068 unsigned b = (rgb & 0xF) * 0x11;
00069 AV_WN32(&new_palette[i], (0xFFU << 24) | (r << 16) | (g << 8) | b);
00070 }
00071 }
00072
00073 static void bitplanar2chunky(CDXLVideoContext *c, int linesize, uint8_t *out)
00074 {
00075 GetBitContext gb;
00076 int x, y, plane;
00077
00078 init_get_bits(&gb, c->video, c->video_size * 8);
00079 for (plane = 0; plane < c->bpp; plane++) {
00080 for (y = 0; y < c->avctx->height; y++) {
00081 for (x = 0; x < c->avctx->width; x++)
00082 out[linesize * y + x] |= get_bits1(&gb) << plane;
00083 skip_bits(&gb, c->padded_bits);
00084 }
00085 }
00086 }
00087
00088 static void bitline2chunky(CDXLVideoContext *c, int linesize, uint8_t *out)
00089 {
00090 GetBitContext gb;
00091 int x, y, plane;
00092
00093 init_get_bits(&gb, c->video, c->video_size * 8);
00094 for (y = 0; y < c->avctx->height; y++) {
00095 for (plane = 0; plane < c->bpp; plane++) {
00096 for (x = 0; x < c->avctx->width; x++)
00097 out[linesize * y + x] |= get_bits1(&gb) << plane;
00098 skip_bits(&gb, c->padded_bits);
00099 }
00100 }
00101 }
00102
00103 static void import_format(CDXLVideoContext *c, int linesize, uint8_t *out)
00104 {
00105 memset(out, 0, linesize * c->avctx->height);
00106
00107 switch (c->format) {
00108 case BIT_PLANAR:
00109 bitplanar2chunky(c, linesize, out);
00110 break;
00111 case BIT_LINE:
00112 bitline2chunky(c, linesize, out);
00113 break;
00114 }
00115 }
00116
00117 static void cdxl_decode_rgb(CDXLVideoContext *c)
00118 {
00119 uint32_t *new_palette = (uint32_t *)c->frame.data[1];
00120
00121 import_palette(c, new_palette);
00122 import_format(c, c->frame.linesize[0], c->frame.data[0]);
00123 }
00124
00125 static void cdxl_decode_ham6(CDXLVideoContext *c)
00126 {
00127 AVCodecContext *avctx = c->avctx;
00128 uint32_t new_palette[16], r, g, b;
00129 uint8_t *ptr, *out, index, op;
00130 int x, y;
00131
00132 ptr = c->new_video;
00133 out = c->frame.data[0];
00134
00135 import_palette(c, new_palette);
00136 import_format(c, avctx->width, c->new_video);
00137
00138 for (y = 0; y < avctx->height; y++) {
00139 r = new_palette[0] & 0xFF0000;
00140 g = new_palette[0] & 0xFF00;
00141 b = new_palette[0] & 0xFF;
00142 for (x = 0; x < avctx->width; x++) {
00143 index = *ptr++;
00144 op = index >> 4;
00145 index &= 15;
00146 switch (op) {
00147 case 0:
00148 r = new_palette[index] & 0xFF0000;
00149 g = new_palette[index] & 0xFF00;
00150 b = new_palette[index] & 0xFF;
00151 break;
00152 case 1:
00153 b = index * 0x11;
00154 break;
00155 case 2:
00156 r = index * 0x11 << 16;
00157 break;
00158 case 3:
00159 g = index * 0x11 << 8;
00160 break;
00161 }
00162 AV_WL24(out + x * 3, r | g | b);
00163 }
00164 out += c->frame.linesize[0];
00165 }
00166 }
00167
00168 static void cdxl_decode_ham8(CDXLVideoContext *c)
00169 {
00170 AVCodecContext *avctx = c->avctx;
00171 uint32_t new_palette[64], r, g, b;
00172 uint8_t *ptr, *out, index, op;
00173 int x, y;
00174
00175 ptr = c->new_video;
00176 out = c->frame.data[0];
00177
00178 import_palette(c, new_palette);
00179 import_format(c, avctx->width, c->new_video);
00180
00181 for (y = 0; y < avctx->height; y++) {
00182 r = new_palette[0] & 0xFF0000;
00183 g = new_palette[0] & 0xFF00;
00184 b = new_palette[0] & 0xFF;
00185 for (x = 0; x < avctx->width; x++) {
00186 index = *ptr++;
00187 op = index >> 6;
00188 index &= 63;
00189 switch (op) {
00190 case 0:
00191 r = new_palette[index] & 0xFF0000;
00192 g = new_palette[index] & 0xFF00;
00193 b = new_palette[index] & 0xFF;
00194 break;
00195 case 1:
00196 b = (index << 2) | (b & 3);
00197 break;
00198 case 2:
00199 r = (index << 18) | (r & (3 << 16));
00200 break;
00201 case 3:
00202 g = (index << 10) | (g & (3 << 8));
00203 break;
00204 }
00205 AV_WL24(out + x * 3, r | g | b);
00206 }
00207 out += c->frame.linesize[0];
00208 }
00209 }
00210
00211 static int cdxl_decode_frame(AVCodecContext *avctx, void *data,
00212 int *data_size, AVPacket *pkt)
00213 {
00214 CDXLVideoContext *c = avctx->priv_data;
00215 AVFrame * const p = &c->frame;
00216 int ret, w, h, encoding, aligned_width, buf_size = pkt->size;
00217 const uint8_t *buf = pkt->data;
00218
00219 if (buf_size < 32)
00220 return AVERROR_INVALIDDATA;
00221 encoding = buf[1] & 7;
00222 c->format = buf[1] & 0xE0;
00223 w = AV_RB16(&buf[14]);
00224 h = AV_RB16(&buf[16]);
00225 c->bpp = buf[19];
00226 c->palette_size = AV_RB16(&buf[20]);
00227 c->palette = buf + 32;
00228 c->video = c->palette + c->palette_size;
00229 c->video_size = buf_size - c->palette_size - 32;
00230
00231 if (c->palette_size > 512)
00232 return AVERROR_INVALIDDATA;
00233 if (buf_size < c->palette_size + 32)
00234 return AVERROR_INVALIDDATA;
00235 if (c->bpp < 1)
00236 return AVERROR_INVALIDDATA;
00237 if (c->format != BIT_PLANAR && c->format != BIT_LINE) {
00238 av_log_ask_for_sample(avctx, "unsupported pixel format: 0x%0x\n", c->format);
00239 return AVERROR_PATCHWELCOME;
00240 }
00241
00242 if ((ret = av_image_check_size(w, h, 0, avctx)) < 0)
00243 return ret;
00244 if (w != avctx->width || h != avctx->height)
00245 avcodec_set_dimensions(avctx, w, h);
00246
00247 aligned_width = FFALIGN(c->avctx->width, 16);
00248 c->padded_bits = aligned_width - c->avctx->width;
00249 if (c->video_size < aligned_width * avctx->height * c->bpp / 8)
00250 return AVERROR_INVALIDDATA;
00251 if (!encoding && c->palette_size && c->bpp <= 8) {
00252 avctx->pix_fmt = PIX_FMT_PAL8;
00253 } else if (encoding == 1 && (c->bpp == 6 || c->bpp == 8)) {
00254 if (c->palette_size != (1 << (c->bpp - 1)))
00255 return AVERROR_INVALIDDATA;
00256 avctx->pix_fmt = PIX_FMT_BGR24;
00257 } else {
00258 av_log_ask_for_sample(avctx, "unsupported encoding %d and bpp %d\n",
00259 encoding, c->bpp);
00260 return AVERROR_PATCHWELCOME;
00261 }
00262
00263 if (p->data[0])
00264 avctx->release_buffer(avctx, p);
00265
00266 p->reference = 0;
00267 if ((ret = avctx->get_buffer(avctx, p)) < 0) {
00268 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00269 return ret;
00270 }
00271 p->pict_type = AV_PICTURE_TYPE_I;
00272
00273 if (encoding) {
00274 av_fast_padded_malloc(&c->new_video, &c->new_video_size,
00275 h * w + FF_INPUT_BUFFER_PADDING_SIZE);
00276 if (!c->new_video)
00277 return AVERROR(ENOMEM);
00278 if (c->bpp == 8)
00279 cdxl_decode_ham8(c);
00280 else
00281 cdxl_decode_ham6(c);
00282 } else {
00283 cdxl_decode_rgb(c);
00284 }
00285 *data_size = sizeof(AVFrame);
00286 *(AVFrame*)data = c->frame;
00287
00288 return buf_size;
00289 }
00290
00291 static av_cold int cdxl_decode_end(AVCodecContext *avctx)
00292 {
00293 CDXLVideoContext *c = avctx->priv_data;
00294
00295 av_free(c->new_video);
00296 if (c->frame.data[0])
00297 avctx->release_buffer(avctx, &c->frame);
00298
00299 return 0;
00300 }
00301
00302 AVCodec ff_cdxl_decoder = {
00303 .name = "cdxl",
00304 .type = AVMEDIA_TYPE_VIDEO,
00305 .id = AV_CODEC_ID_CDXL,
00306 .priv_data_size = sizeof(CDXLVideoContext),
00307 .init = cdxl_decode_init,
00308 .close = cdxl_decode_end,
00309 .decode = cdxl_decode_frame,
00310 .capabilities = CODEC_CAP_DR1,
00311 .long_name = NULL_IF_CONFIG_SMALL("Commodore CDXL video"),
00312 };