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