libavcodec/dfa.c
Go to the documentation of this file.
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 < 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 (segments & 0x8000) {
00249             frame[width - 1] = segments & 0xFF;
00250             segments = bytestream_get_le16(&src);
00251         }
00252         line_ptr = frame;
00253         frame += width;
00254         while (segments--) {
00255             if (src_end - src < 2)
00256                 return -1;
00257             if (frame - line_ptr <= *src)
00258                 return -1;
00259             line_ptr += *src++;
00260             count = (int8_t)*src++;
00261             if (count >= 0) {
00262                 if (frame - line_ptr < count*2 || src_end - src < count*2)
00263                     return -1;
00264                 bytestream_get_buffer(&src, line_ptr, count*2);
00265                 line_ptr += count * 2;
00266             } else {
00267                 count = -count;
00268                 if (frame - line_ptr < count*2 || src_end - src < 2)
00269                     return -1;
00270                 v = bytestream_get_le16(&src);
00271                 for (i = 0; i < count; i++)
00272                     bytestream_put_le16(&line_ptr, v);
00273             }
00274         }
00275     }
00276 
00277     return 0;
00278 }
00279 
00280 static int decode_unk6(uint8_t *frame, int width, int height,
00281                        const uint8_t *src, const uint8_t *src_end)
00282 {
00283     return -1;
00284 }
00285 
00286 static int decode_blck(uint8_t *frame, int width, int height,
00287                        const uint8_t *src, const uint8_t *src_end)
00288 {
00289     memset(frame, 0, width * height);
00290     return 0;
00291 }
00292 
00293 
00294 typedef int (*chunk_decoder)(uint8_t *frame, int width, int height,
00295                              const uint8_t *src, const uint8_t *src_end);
00296 
00297 static const chunk_decoder decoder[8] = {
00298     decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
00299     decode_unk6, decode_dsw1, decode_blck, decode_dds1,
00300 };
00301 
00302 static const char* chunk_name[8] = {
00303     "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
00304 };
00305 
00306 static int dfa_decode_frame(AVCodecContext *avctx,
00307                             void *data, int *data_size,
00308                             AVPacket *avpkt)
00309 {
00310     DfaContext *s = avctx->priv_data;
00311     const uint8_t *buf = avpkt->data;
00312     const uint8_t *buf_end = avpkt->data + avpkt->size;
00313     const uint8_t *tmp_buf;
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     while (buf < buf_end) {
00328         chunk_size = AV_RL32(buf + 4);
00329         chunk_type = AV_RL32(buf + 8);
00330         buf += 12;
00331         if (buf_end - buf < chunk_size) {
00332             av_log(avctx, AV_LOG_ERROR, "Chunk size is too big (%d bytes)\n", chunk_size);
00333             return -1;
00334         }
00335         if (!chunk_type)
00336             break;
00337         if (chunk_type == 1) {
00338             pal_elems = FFMIN(chunk_size / 3, 256);
00339             tmp_buf = buf;
00340             for (i = 0; i < pal_elems; i++) {
00341                 s->pal[i] = bytestream_get_be24(&tmp_buf) << 2;
00342                 s->pal[i] |= (s->pal[i] >> 6) & 0x333;
00343             }
00344             s->pic.palette_has_changed = 1;
00345         } else if (chunk_type <= 9) {
00346             if (decoder[chunk_type - 2](s->frame_buf, avctx->width, avctx->height,
00347                                         buf, buf + chunk_size)) {
00348                 av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
00349                        chunk_name[chunk_type - 2]);
00350                 return -1;
00351             }
00352         } else {
00353             av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
00354                    chunk_type);
00355         }
00356         buf += chunk_size;
00357     }
00358 
00359     buf = s->frame_buf;
00360     dst = s->pic.data[0];
00361     for (i = 0; i < avctx->height; i++) {
00362         memcpy(dst, buf, avctx->width);
00363         dst += s->pic.linesize[0];
00364         buf += avctx->width;
00365     }
00366     memcpy(s->pic.data[1], s->pal, sizeof(s->pal));
00367 
00368     *data_size = sizeof(AVFrame);
00369     *(AVFrame*)data = s->pic;
00370 
00371     return avpkt->size;
00372 }
00373 
00374 static av_cold int dfa_decode_end(AVCodecContext *avctx)
00375 {
00376     DfaContext *s = avctx->priv_data;
00377 
00378     if (s->pic.data[0])
00379         avctx->release_buffer(avctx, &s->pic);
00380 
00381     av_freep(&s->frame_buf);
00382 
00383     return 0;
00384 }
00385 
00386 AVCodec ff_dfa_decoder = {
00387     .name           = "dfa",
00388     .type           = AVMEDIA_TYPE_VIDEO,
00389     .id             = CODEC_ID_DFA,
00390     .priv_data_size = sizeof(DfaContext),
00391     .init           = dfa_decode_init,
00392     .close          = dfa_decode_end,
00393     .decode         = dfa_decode_frame,
00394     .capabilities   = CODEC_CAP_DR1,
00395     .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
00396 };