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