Libav 0.7.1
|
00001 /* 00002 * Chronomaster DFA Video Decoder 00003 * Copyright (c) 2011 Konstantin Shishkov 00004 * based on work by Vladimir "VAG" Gneushev 00005 * 00006 * This file is part of Libav. 00007 * 00008 * Libav is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * Libav is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with Libav; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 #include "avcodec.h" 00024 #include "libavutil/intreadwrite.h" 00025 #include "bytestream.h" 00026 #include "libavutil/lzo.h" // for av_memcpy_backptr 00027 00028 typedef struct DfaContext { 00029 AVFrame pic; 00030 00031 uint32_t pal[256]; 00032 uint8_t *frame_buf; 00033 } DfaContext; 00034 00035 static av_cold int dfa_decode_init(AVCodecContext *avctx) 00036 { 00037 DfaContext *s = avctx->priv_data; 00038 00039 avctx->pix_fmt = PIX_FMT_PAL8; 00040 00041 s->frame_buf = av_mallocz(avctx->width * avctx->height + AV_LZO_OUTPUT_PADDING); 00042 if (!s->frame_buf) 00043 return AVERROR(ENOMEM); 00044 00045 return 0; 00046 } 00047 00048 static int decode_copy(uint8_t *frame, int width, int height, 00049 const uint8_t *src, const uint8_t *src_end) 00050 { 00051 const int size = width * height; 00052 00053 if (src_end - src < size) 00054 return -1; 00055 bytestream_get_buffer(&src, frame, size); 00056 return 0; 00057 } 00058 00059 static int decode_tsw1(uint8_t *frame, int width, int height, 00060 const uint8_t *src, const uint8_t *src_end) 00061 { 00062 const uint8_t *frame_start = frame; 00063 const uint8_t *frame_end = frame + width * height; 00064 int mask = 0x10000, bitbuf = 0; 00065 int v, count, segments; 00066 unsigned offset; 00067 00068 segments = bytestream_get_le32(&src); 00069 offset = bytestream_get_le32(&src); 00070 if (frame_end - frame <= offset) 00071 return -1; 00072 frame += offset; 00073 while (segments--) { 00074 if (mask == 0x10000) { 00075 if (src >= src_end) 00076 return -1; 00077 bitbuf = bytestream_get_le16(&src); 00078 mask = 1; 00079 } 00080 if (src_end - src < 2 || frame_end - frame < 2) 00081 return -1; 00082 if (bitbuf & mask) { 00083 v = bytestream_get_le16(&src); 00084 offset = (v & 0x1FFF) << 1; 00085 count = ((v >> 13) + 2) << 1; 00086 if (frame - frame_start < offset || frame_end - frame < count) 00087 return -1; 00088 av_memcpy_backptr(frame, offset, count); 00089 frame += count; 00090 } else { 00091 *frame++ = *src++; 00092 *frame++ = *src++; 00093 } 00094 mask <<= 1; 00095 } 00096 00097 return 0; 00098 } 00099 00100 static int decode_dsw1(uint8_t *frame, int width, int height, 00101 const uint8_t *src, const uint8_t *src_end) 00102 { 00103 const uint8_t *frame_start = frame; 00104 const uint8_t *frame_end = frame + width * height; 00105 int mask = 0x10000, bitbuf = 0; 00106 int v, offset, count, segments; 00107 00108 segments = bytestream_get_le16(&src); 00109 while (segments--) { 00110 if (mask == 0x10000) { 00111 if (src >= src_end) 00112 return -1; 00113 bitbuf = bytestream_get_le16(&src); 00114 mask = 1; 00115 } 00116 if (src_end - src < 2 || frame_end - frame < 2) 00117 return -1; 00118 if (bitbuf & mask) { 00119 v = bytestream_get_le16(&src); 00120 offset = (v & 0x1FFF) << 1; 00121 count = ((v >> 13) + 2) << 1; 00122 if (frame - frame_start < offset || frame_end - frame < count) 00123 return -1; 00124 // can't use av_memcpy_backptr() since it can overwrite following pixels 00125 for (v = 0; v < count; v++) 00126 frame[v] = frame[v - offset]; 00127 frame += count; 00128 } else if (bitbuf & (mask << 1)) { 00129 frame += bytestream_get_le16(&src); 00130 } else { 00131 *frame++ = *src++; 00132 *frame++ = *src++; 00133 } 00134 mask <<= 2; 00135 } 00136 00137 return 0; 00138 } 00139 00140 static int decode_dds1(uint8_t *frame, int width, int height, 00141 const uint8_t *src, const uint8_t *src_end) 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 = bytestream_get_le16(&src); 00149 while (segments--) { 00150 if (mask == 0x10000) { 00151 if (src >= src_end) 00152 return -1; 00153 bitbuf = bytestream_get_le16(&src); 00154 mask = 1; 00155 } 00156 if (src_end - src < 2 || frame_end - frame < width + 2) 00157 return -1; 00158 if (bitbuf & mask) { 00159 v = bytestream_get_le16(&src); 00160 offset = (v & 0x1FFF) << 2; 00161 count = ((v >> 13) + 2) << 1; 00162 if (frame - frame_start < offset || frame_end - frame < count*2 + width) 00163 return -1; 00164 for (i = 0; i < count; i++) { 00165 frame[0] = frame[1] = 00166 frame[width] = frame[width + 1] = frame[-offset]; 00167 00168 frame += 2; 00169 } 00170 } else if (bitbuf & (mask << 1)) { 00171 frame += bytestream_get_le16(&src) * 2; 00172 } else { 00173 frame[0] = frame[1] = 00174 frame[width] = frame[width + 1] = *src++; 00175 frame += 2; 00176 frame[0] = frame[1] = 00177 frame[width] = frame[width + 1] = *src++; 00178 frame += 2; 00179 } 00180 mask <<= 2; 00181 } 00182 00183 return 0; 00184 } 00185 00186 static int decode_bdlt(uint8_t *frame, int width, int height, 00187 const uint8_t *src, const uint8_t *src_end) 00188 { 00189 uint8_t *line_ptr; 00190 int count, lines, segments; 00191 00192 count = bytestream_get_le16(&src); 00193 if (count >= height) 00194 return -1; 00195 frame += width * count; 00196 lines = bytestream_get_le16(&src); 00197 if (count + lines > height || src >= src_end) 00198 return -1; 00199 00200 while (lines--) { 00201 line_ptr = frame; 00202 frame += width; 00203 segments = *src++; 00204 while (segments--) { 00205 if (src_end - src < 3) 00206 return -1; 00207 if (frame - line_ptr <= *src) 00208 return -1; 00209 line_ptr += *src++; 00210 count = (int8_t)*src++; 00211 if (count >= 0) { 00212 if (frame - line_ptr < count || src_end - src < count) 00213 return -1; 00214 bytestream_get_buffer(&src, line_ptr, count); 00215 } else { 00216 count = -count; 00217 if (frame - line_ptr < count || src >= src_end) 00218 return -1; 00219 memset(line_ptr, *src++, count); 00220 } 00221 line_ptr += count; 00222 } 00223 } 00224 00225 return 0; 00226 } 00227 00228 static int decode_wdlt(uint8_t *frame, int width, int height, 00229 const uint8_t *src, const uint8_t *src_end) 00230 { 00231 const uint8_t *frame_end = frame + width * height; 00232 uint8_t *line_ptr; 00233 int count, i, v, lines, segments; 00234 00235 lines = bytestream_get_le16(&src); 00236 if (lines > height || src >= src_end) 00237 return -1; 00238 00239 while (lines--) { 00240 segments = bytestream_get_le16(&src); 00241 while ((segments & 0xC000) == 0xC000) { 00242 unsigned delta = -((int16_t)segments * width); 00243 if (frame_end - frame <= delta) 00244 return -1; 00245 frame += delta; 00246 segments = bytestream_get_le16(&src); 00247 } 00248 if (frame_end <= frame) 00249 return -1; 00250 if (segments & 0x8000) { 00251 frame[width - 1] = segments & 0xFF; 00252 segments = bytestream_get_le16(&src); 00253 } 00254 line_ptr = frame; 00255 frame += width; 00256 while (segments--) { 00257 if (src_end - src < 2) 00258 return -1; 00259 if (frame - line_ptr <= *src) 00260 return -1; 00261 line_ptr += *src++; 00262 count = (int8_t)*src++; 00263 if (count >= 0) { 00264 if (frame - line_ptr < count*2 || src_end - src < count*2) 00265 return -1; 00266 bytestream_get_buffer(&src, line_ptr, count*2); 00267 line_ptr += count * 2; 00268 } else { 00269 count = -count; 00270 if (frame - line_ptr < count*2 || src_end - src < 2) 00271 return -1; 00272 v = bytestream_get_le16(&src); 00273 for (i = 0; i < count; i++) 00274 bytestream_put_le16(&line_ptr, v); 00275 } 00276 } 00277 } 00278 00279 return 0; 00280 } 00281 00282 static int decode_unk6(uint8_t *frame, int width, int height, 00283 const uint8_t *src, const uint8_t *src_end) 00284 { 00285 return -1; 00286 } 00287 00288 static int decode_blck(uint8_t *frame, int width, int height, 00289 const uint8_t *src, const uint8_t *src_end) 00290 { 00291 memset(frame, 0, width * height); 00292 return 0; 00293 } 00294 00295 00296 typedef int (*chunk_decoder)(uint8_t *frame, int width, int height, 00297 const uint8_t *src, const uint8_t *src_end); 00298 00299 static const chunk_decoder decoder[8] = { 00300 decode_copy, decode_tsw1, decode_bdlt, decode_wdlt, 00301 decode_unk6, decode_dsw1, decode_blck, decode_dds1, 00302 }; 00303 00304 static const char* chunk_name[8] = { 00305 "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1" 00306 }; 00307 00308 static int dfa_decode_frame(AVCodecContext *avctx, 00309 void *data, int *data_size, 00310 AVPacket *avpkt) 00311 { 00312 DfaContext *s = avctx->priv_data; 00313 const uint8_t *buf = avpkt->data; 00314 const uint8_t *buf_end = avpkt->data + avpkt->size; 00315 const uint8_t *tmp_buf; 00316 uint32_t chunk_type, chunk_size; 00317 uint8_t *dst; 00318 int ret; 00319 int i, pal_elems; 00320 00321 if (s->pic.data[0]) 00322 avctx->release_buffer(avctx, &s->pic); 00323 00324 if ((ret = avctx->get_buffer(avctx, &s->pic))) { 00325 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); 00326 return ret; 00327 } 00328 00329 while (buf < buf_end) { 00330 chunk_size = AV_RL32(buf + 4); 00331 chunk_type = AV_RL32(buf + 8); 00332 buf += 12; 00333 if (buf_end - buf < chunk_size) { 00334 av_log(avctx, AV_LOG_ERROR, "Chunk size is too big (%d bytes)\n", chunk_size); 00335 return -1; 00336 } 00337 if (!chunk_type) 00338 break; 00339 if (chunk_type == 1) { 00340 pal_elems = FFMIN(chunk_size / 3, 256); 00341 tmp_buf = buf; 00342 for (i = 0; i < pal_elems; i++) { 00343 s->pal[i] = bytestream_get_be24(&tmp_buf) << 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](s->frame_buf, avctx->width, avctx->height, 00349 buf, buf + chunk_size)) { 00350 av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n", 00351 chunk_name[chunk_type - 2]); 00352 return -1; 00353 } 00354 } else { 00355 av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n", 00356 chunk_type); 00357 } 00358 buf += chunk_size; 00359 } 00360 00361 buf = s->frame_buf; 00362 dst = s->pic.data[0]; 00363 for (i = 0; i < avctx->height; i++) { 00364 memcpy(dst, buf, avctx->width); 00365 dst += s->pic.linesize[0]; 00366 buf += avctx->width; 00367 } 00368 memcpy(s->pic.data[1], s->pal, sizeof(s->pal)); 00369 00370 *data_size = sizeof(AVFrame); 00371 *(AVFrame*)data = s->pic; 00372 00373 return avpkt->size; 00374 } 00375 00376 static av_cold int dfa_decode_end(AVCodecContext *avctx) 00377 { 00378 DfaContext *s = avctx->priv_data; 00379 00380 if (s->pic.data[0]) 00381 avctx->release_buffer(avctx, &s->pic); 00382 00383 av_freep(&s->frame_buf); 00384 00385 return 0; 00386 } 00387 00388 AVCodec ff_dfa_decoder = { 00389 "dfa", 00390 AVMEDIA_TYPE_VIDEO, 00391 CODEC_ID_DFA, 00392 sizeof(DfaContext), 00393 dfa_decode_init, 00394 NULL, 00395 dfa_decode_end, 00396 dfa_decode_frame, 00397 CODEC_CAP_DR1, 00398 .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"), 00399 };