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