• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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

Generated on Fri Feb 22 2013 07:24:26 for FFmpeg by  doxygen 1.7.1