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