00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "avcodec.h"
00024 #include "bytestream.h"
00025
00026 #include "libavutil/imgutils.h"
00027 #include "libavutil/lzo.h"
00028
00029 typedef struct DfaContext {
00030 AVFrame pic;
00031
00032 uint32_t pal[256];
00033 uint8_t *frame_buf;
00034 } DfaContext;
00035
00036 static av_cold int dfa_decode_init(AVCodecContext *avctx)
00037 {
00038 DfaContext *s = avctx->priv_data;
00039 int ret;
00040
00041 avctx->pix_fmt = PIX_FMT_PAL8;
00042
00043 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
00044 return ret;
00045
00046 s->frame_buf = av_mallocz(avctx->width * avctx->height + AV_LZO_OUTPUT_PADDING);
00047 if (!s->frame_buf)
00048 return AVERROR(ENOMEM);
00049
00050 return 0;
00051 }
00052
00053 static int decode_copy(GetByteContext *gb, uint8_t *frame, int width, int height)
00054 {
00055 const int size = width * height;
00056
00057 if (bytestream2_get_buffer(gb, frame, size) != size)
00058 return AVERROR_INVALIDDATA;
00059 return 0;
00060 }
00061
00062 static int decode_tsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
00063 {
00064 const uint8_t *frame_start = frame;
00065 const uint8_t *frame_end = frame + width * height;
00066 int mask = 0x10000, bitbuf = 0;
00067 int v, count, segments;
00068 unsigned offset;
00069
00070 segments = bytestream2_get_le32(gb);
00071 offset = bytestream2_get_le32(gb);
00072 if (frame_end - frame <= offset)
00073 return AVERROR_INVALIDDATA;
00074 frame += offset;
00075 while (segments--) {
00076 if (bytestream2_get_bytes_left(gb) < 2)
00077 return AVERROR_INVALIDDATA;
00078 if (mask == 0x10000) {
00079 bitbuf = bytestream2_get_le16u(gb);
00080 mask = 1;
00081 }
00082 if (frame_end - frame < 2)
00083 return AVERROR_INVALIDDATA;
00084 if (bitbuf & mask) {
00085 v = bytestream2_get_le16(gb);
00086 offset = (v & 0x1FFF) << 1;
00087 count = ((v >> 13) + 2) << 1;
00088 if (frame - frame_start < offset || frame_end - frame < count)
00089 return AVERROR_INVALIDDATA;
00090 av_memcpy_backptr(frame, offset, count);
00091 frame += count;
00092 } else {
00093 *frame++ = bytestream2_get_byte(gb);
00094 *frame++ = bytestream2_get_byte(gb);
00095 }
00096 mask <<= 1;
00097 }
00098
00099 return 0;
00100 }
00101
00102 static int decode_dsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
00103 {
00104 const uint8_t *frame_start = frame;
00105 const uint8_t *frame_end = frame + width * height;
00106 int mask = 0x10000, bitbuf = 0;
00107 int v, offset, count, segments;
00108
00109 segments = bytestream2_get_le16(gb);
00110 while (segments--) {
00111 if (bytestream2_get_bytes_left(gb) < 2)
00112 return AVERROR_INVALIDDATA;
00113 if (mask == 0x10000) {
00114 bitbuf = bytestream2_get_le16u(gb);
00115 mask = 1;
00116 }
00117 if (frame_end - frame < 2)
00118 return AVERROR_INVALIDDATA;
00119 if (bitbuf & mask) {
00120 v = bytestream2_get_le16(gb);
00121 offset = (v & 0x1FFF) << 1;
00122 count = ((v >> 13) + 2) << 1;
00123 if (frame - frame_start < offset || frame_end - frame < count)
00124 return AVERROR_INVALIDDATA;
00125
00126 for (v = 0; v < count; v++)
00127 frame[v] = frame[v - offset];
00128 frame += count;
00129 } else if (bitbuf & (mask << 1)) {
00130 frame += bytestream2_get_le16(gb);
00131 } else {
00132 *frame++ = bytestream2_get_byte(gb);
00133 *frame++ = bytestream2_get_byte(gb);
00134 }
00135 mask <<= 2;
00136 }
00137
00138 return 0;
00139 }
00140
00141 static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
00142 {
00143 const uint8_t *frame_start = frame;
00144 const uint8_t *frame_end = frame + width * height;
00145 int mask = 0x10000, bitbuf = 0;
00146 int i, v, offset, count, segments;
00147
00148 segments = bytestream2_get_le16(gb);
00149 while (segments--) {
00150 if (bytestream2_get_bytes_left(gb) < 2)
00151 return AVERROR_INVALIDDATA;
00152 if (mask == 0x10000) {
00153 bitbuf = bytestream2_get_le16u(gb);
00154 mask = 1;
00155 }
00156
00157 if (bitbuf & mask) {
00158 v = bytestream2_get_le16(gb);
00159 offset = (v & 0x1FFF) << 2;
00160 count = ((v >> 13) + 2) << 1;
00161 if (frame - frame_start < offset || frame_end - frame < count*2 + width)
00162 return AVERROR_INVALIDDATA;
00163 for (i = 0; i < count; i++) {
00164 frame[0] = frame[1] =
00165 frame[width] = frame[width + 1] = frame[-offset];
00166
00167 frame += 2;
00168 }
00169 } else if (bitbuf & (mask << 1)) {
00170 v = bytestream2_get_le16(gb)*2;
00171 if (frame - frame_end < v)
00172 return AVERROR_INVALIDDATA;
00173 frame += v;
00174 } else {
00175 if (frame_end - frame < width + 3)
00176 return AVERROR_INVALIDDATA;
00177 frame[0] = frame[1] =
00178 frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
00179 frame += 2;
00180 frame[0] = frame[1] =
00181 frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
00182 frame += 2;
00183 }
00184 mask <<= 2;
00185 }
00186
00187 return 0;
00188 }
00189
00190 static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
00191 {
00192 uint8_t *line_ptr;
00193 int count, lines, segments;
00194
00195 count = bytestream2_get_le16(gb);
00196 if (count >= height)
00197 return AVERROR_INVALIDDATA;
00198 frame += width * count;
00199 lines = bytestream2_get_le16(gb);
00200 if (count + lines > height)
00201 return AVERROR_INVALIDDATA;
00202
00203 while (lines--) {
00204 if (bytestream2_get_bytes_left(gb) < 1)
00205 return AVERROR_INVALIDDATA;
00206 line_ptr = frame;
00207 frame += width;
00208 segments = bytestream2_get_byteu(gb);
00209 while (segments--) {
00210 if (frame - line_ptr <= bytestream2_peek_byte(gb))
00211 return AVERROR_INVALIDDATA;
00212 line_ptr += bytestream2_get_byte(gb);
00213 count = (int8_t)bytestream2_get_byte(gb);
00214 if (count >= 0) {
00215 if (frame - line_ptr < count)
00216 return AVERROR_INVALIDDATA;
00217 if (bytestream2_get_buffer(gb, line_ptr, count) != count)
00218 return AVERROR_INVALIDDATA;
00219 } else {
00220 count = -count;
00221 if (frame - line_ptr < count)
00222 return AVERROR_INVALIDDATA;
00223 memset(line_ptr, bytestream2_get_byte(gb), count);
00224 }
00225 line_ptr += count;
00226 }
00227 }
00228
00229 return 0;
00230 }
00231
00232 static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
00233 {
00234 const uint8_t *frame_end = frame + width * height;
00235 uint8_t *line_ptr;
00236 int count, i, v, lines, segments;
00237 int y = 0;
00238
00239 lines = bytestream2_get_le16(gb);
00240 if (lines > height)
00241 return AVERROR_INVALIDDATA;
00242
00243 while (lines--) {
00244 if (bytestream2_get_bytes_left(gb) < 2)
00245 return AVERROR_INVALIDDATA;
00246 segments = bytestream2_get_le16u(gb);
00247 while ((segments & 0xC000) == 0xC000) {
00248 unsigned skip_lines = -(int16_t)segments;
00249 unsigned delta = -((int16_t)segments * width);
00250 if (frame_end - frame <= delta || y + lines + skip_lines > height)
00251 return AVERROR_INVALIDDATA;
00252 frame += delta;
00253 y += skip_lines;
00254 segments = bytestream2_get_le16(gb);
00255 }
00256 if (segments & 0x8000) {
00257 frame[width - 1] = segments & 0xFF;
00258 segments = bytestream2_get_le16(gb);
00259 }
00260 line_ptr = frame;
00261 if (frame_end - frame < width)
00262 return AVERROR_INVALIDDATA;
00263 frame += width;
00264 y++;
00265 while (segments--) {
00266 if (frame - line_ptr <= bytestream2_peek_byte(gb))
00267 return AVERROR_INVALIDDATA;
00268 line_ptr += bytestream2_get_byte(gb);
00269 count = (int8_t)bytestream2_get_byte(gb);
00270 if (count >= 0) {
00271 if (frame - line_ptr < count * 2)
00272 return AVERROR_INVALIDDATA;
00273 if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
00274 return AVERROR_INVALIDDATA;
00275 line_ptr += count * 2;
00276 } else {
00277 count = -count;
00278 if (frame - line_ptr < count * 2)
00279 return AVERROR_INVALIDDATA;
00280 v = bytestream2_get_le16(gb);
00281 for (i = 0; i < count; i++)
00282 bytestream_put_le16(&line_ptr, v);
00283 }
00284 }
00285 }
00286
00287 return 0;
00288 }
00289
00290 static int decode_unk6(GetByteContext *gb, uint8_t *frame, int width, int height)
00291 {
00292 return AVERROR_PATCHWELCOME;
00293 }
00294
00295 static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
00296 {
00297 memset(frame, 0, width * height);
00298 return 0;
00299 }
00300
00301
00302 typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
00303
00304 static const chunk_decoder decoder[8] = {
00305 decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
00306 decode_unk6, decode_dsw1, decode_blck, decode_dds1,
00307 };
00308
00309 static const char* chunk_name[8] = {
00310 "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
00311 };
00312
00313 static int dfa_decode_frame(AVCodecContext *avctx,
00314 void *data, int *data_size,
00315 AVPacket *avpkt)
00316 {
00317 DfaContext *s = avctx->priv_data;
00318 GetByteContext gb;
00319 const uint8_t *buf = avpkt->data;
00320 uint32_t chunk_type, chunk_size;
00321 uint8_t *dst;
00322 int ret;
00323 int i, pal_elems;
00324
00325 if (s->pic.data[0])
00326 avctx->release_buffer(avctx, &s->pic);
00327
00328 if ((ret = avctx->get_buffer(avctx, &s->pic))) {
00329 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00330 return ret;
00331 }
00332
00333 bytestream2_init(&gb, avpkt->data, avpkt->size);
00334 while (bytestream2_get_bytes_left(&gb) > 0) {
00335 bytestream2_skip(&gb, 4);
00336 chunk_size = bytestream2_get_le32(&gb);
00337 chunk_type = bytestream2_get_le32(&gb);
00338 if (!chunk_type)
00339 break;
00340 if (chunk_type == 1) {
00341 pal_elems = FFMIN(chunk_size / 3, 256);
00342 for (i = 0; i < pal_elems; i++) {
00343 s->pal[i] = bytestream2_get_be24(&gb) << 2;
00344 s->pal[i] |= (s->pal[i] >> 6) & 0x333;
00345 }
00346 s->pic.palette_has_changed = 1;
00347 } else if (chunk_type <= 9) {
00348 if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
00349 av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
00350 chunk_name[chunk_type - 2]);
00351 return AVERROR_INVALIDDATA;
00352 }
00353 } else {
00354 av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
00355 chunk_type);
00356 }
00357 buf += chunk_size;
00358 }
00359
00360 buf = s->frame_buf;
00361 dst = s->pic.data[0];
00362 for (i = 0; i < avctx->height; i++) {
00363 memcpy(dst, buf, avctx->width);
00364 dst += s->pic.linesize[0];
00365 buf += avctx->width;
00366 }
00367 memcpy(s->pic.data[1], s->pal, sizeof(s->pal));
00368
00369 *data_size = sizeof(AVFrame);
00370 *(AVFrame*)data = s->pic;
00371
00372 return avpkt->size;
00373 }
00374
00375 static av_cold int dfa_decode_end(AVCodecContext *avctx)
00376 {
00377 DfaContext *s = avctx->priv_data;
00378
00379 if (s->pic.data[0])
00380 avctx->release_buffer(avctx, &s->pic);
00381
00382 av_freep(&s->frame_buf);
00383
00384 return 0;
00385 }
00386
00387 AVCodec ff_dfa_decoder = {
00388 .name = "dfa",
00389 .type = AVMEDIA_TYPE_VIDEO,
00390 .id = CODEC_ID_DFA,
00391 .priv_data_size = sizeof(DfaContext),
00392 .init = dfa_decode_init,
00393 .close = dfa_decode_end,
00394 .decode = dfa_decode_frame,
00395 .capabilities = CODEC_CAP_DR1,
00396 .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
00397 };