| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * CamStudio decoder | ||
| 3 | * Copyright (c) 2006 Reimar Doeffinger | ||
| 4 | * | ||
| 5 | * This file is part of FFmpeg. | ||
| 6 | * | ||
| 7 | * FFmpeg is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with FFmpeg; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include "avcodec.h" | ||
| 23 | #include "codec_internal.h" | ||
| 24 | #include "decode.h" | ||
| 25 | #include "libavutil/mem.h" | ||
| 26 | |||
| 27 | #if CONFIG_ZLIB | ||
| 28 | #include <zlib.h> | ||
| 29 | #endif | ||
| 30 | #include "libavutil/lzo.h" | ||
| 31 | |||
| 32 | typedef struct CamStudioContext { | ||
| 33 | AVFrame *pic; | ||
| 34 | int linelen, height, bpp; | ||
| 35 | unsigned int decomp_size; | ||
| 36 | unsigned char* decomp_buf; | ||
| 37 | } CamStudioContext; | ||
| 38 | |||
| 39 | 8 | static void copy_frame_default(AVFrame *f, const uint8_t *src, | |
| 40 | int linelen, int height) | ||
| 41 | { | ||
| 42 | 8 | int i, src_stride = FFALIGN(linelen, 4); | |
| 43 | 8 | uint8_t *dst = f->data[0]; | |
| 44 | 8 | dst += (height - 1) * f->linesize[0]; | |
| 45 |
2/2✓ Branch 0 taken 2400 times.
✓ Branch 1 taken 8 times.
|
2408 | for (i = height; i; i--) { |
| 46 | 2400 | memcpy(dst, src, linelen); | |
| 47 | 2400 | src += src_stride; | |
| 48 | 2400 | dst -= f->linesize[0]; | |
| 49 | } | ||
| 50 | 8 | } | |
| 51 | |||
| 52 | 200 | static void add_frame_default(AVFrame *f, const uint8_t *src, | |
| 53 | int linelen, int height) | ||
| 54 | { | ||
| 55 | 200 | int i, j, src_stride = FFALIGN(linelen, 4); | |
| 56 | 200 | uint8_t *dst = f->data[0]; | |
| 57 | 200 | dst += (height - 1) * f->linesize[0]; | |
| 58 |
2/2✓ Branch 0 taken 60000 times.
✓ Branch 1 taken 200 times.
|
60200 | for (i = height; i; i--) { |
| 59 |
2/2✓ Branch 0 taken 72000000 times.
✓ Branch 1 taken 60000 times.
|
72060000 | for (j = linelen; j; j--) |
| 60 | 72000000 | *dst++ += *src++; | |
| 61 | 60000 | src += src_stride - linelen; | |
| 62 | 60000 | dst -= f->linesize[0] + linelen; | |
| 63 | } | ||
| 64 | 200 | } | |
| 65 | |||
| 66 | 208 | static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, | |
| 67 | int *got_frame, AVPacket *avpkt) | ||
| 68 | { | ||
| 69 | 208 | const uint8_t *buf = avpkt->data; | |
| 70 | 208 | int buf_size = avpkt->size; | |
| 71 | 208 | CamStudioContext *c = avctx->priv_data; | |
| 72 | int ret; | ||
| 73 | 208 | int bpp = avctx->bits_per_coded_sample / 8; | |
| 74 | 208 | int bugdelta = FFALIGN(avctx->width * bpp, 4) * avctx->height | |
| 75 | 208 | - (avctx->width & ~3) * bpp * avctx->height; | |
| 76 | |||
| 77 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 208 times.
|
208 | if (buf_size < 2) { |
| 78 | ✗ | av_log(avctx, AV_LOG_ERROR, "coded frame too small\n"); | |
| 79 | ✗ | return AVERROR_INVALIDDATA; | |
| 80 | } | ||
| 81 | |||
| 82 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 208 times.
|
208 | if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0) |
| 83 | ✗ | return ret; | |
| 84 | |||
| 85 | // decompress data | ||
| 86 |
1/3✓ Branch 0 taken 208 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
208 | switch ((buf[0] >> 1) & 7) { |
| 87 | 208 | case 0: { // lzo compression | |
| 88 | 208 | int outlen = c->decomp_size, inlen = buf_size - 2; | |
| 89 |
2/6✓ Branch 1 taken 208 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 208 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
208 | if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen) || (outlen && outlen != bugdelta)) { |
| 90 | ✗ | av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n"); | |
| 91 | ✗ | return AVERROR_INVALIDDATA; | |
| 92 | } | ||
| 93 | 208 | break; | |
| 94 | } | ||
| 95 | ✗ | case 1: { // zlib compression | |
| 96 | #if CONFIG_ZLIB | ||
| 97 | ✗ | unsigned long dlen = c->decomp_size; | |
| 98 | ✗ | if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK || (dlen != c->decomp_size && dlen != c->decomp_size - bugdelta)) { | |
| 99 | ✗ | av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n"); | |
| 100 | ✗ | return AVERROR_INVALIDDATA; | |
| 101 | } | ||
| 102 | ✗ | break; | |
| 103 | #else | ||
| 104 | av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n"); | ||
| 105 | return AVERROR(ENOSYS); | ||
| 106 | #endif | ||
| 107 | } | ||
| 108 | ✗ | default: | |
| 109 | ✗ | av_log(avctx, AV_LOG_ERROR, "unknown compression\n"); | |
| 110 | ✗ | return AVERROR_INVALIDDATA; | |
| 111 | } | ||
| 112 | |||
| 113 | // flip upside down, add difference frame | ||
| 114 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 200 times.
|
208 | if (buf[0] & 1) { // keyframe |
| 115 | 8 | c->pic->pict_type = AV_PICTURE_TYPE_I; | |
| 116 | 8 | c->pic->flags |= AV_FRAME_FLAG_KEY; | |
| 117 | 8 | copy_frame_default(c->pic, c->decomp_buf, | |
| 118 | c->linelen, c->height); | ||
| 119 | } else { | ||
| 120 | 200 | c->pic->pict_type = AV_PICTURE_TYPE_P; | |
| 121 | 200 | c->pic->flags &= ~AV_FRAME_FLAG_KEY; | |
| 122 | 200 | add_frame_default(c->pic, c->decomp_buf, | |
| 123 | c->linelen, c->height); | ||
| 124 | } | ||
| 125 | |||
| 126 | 208 | *got_frame = 1; | |
| 127 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 208 times.
|
208 | if ((ret = av_frame_ref(rframe, c->pic)) < 0) |
| 128 | ✗ | return ret; | |
| 129 | |||
| 130 | 208 | return buf_size; | |
| 131 | } | ||
| 132 | |||
| 133 | 2 | static av_cold int decode_init(AVCodecContext *avctx) | |
| 134 | { | ||
| 135 | 2 | CamStudioContext *c = avctx->priv_data; | |
| 136 | int stride; | ||
| 137 |
1/4✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | switch (avctx->bits_per_coded_sample) { |
| 138 | ✗ | case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555LE; break; | |
| 139 | ✗ | case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break; | |
| 140 | 2 | case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break; | |
| 141 | ✗ | default: | |
| 142 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 143 | "CamStudio codec error: invalid depth %i bpp\n", | ||
| 144 | avctx->bits_per_coded_sample); | ||
| 145 | ✗ | return AVERROR_INVALIDDATA; | |
| 146 | } | ||
| 147 | 2 | c->bpp = avctx->bits_per_coded_sample; | |
| 148 | 2 | c->linelen = avctx->width * avctx->bits_per_coded_sample / 8; | |
| 149 | 2 | c->height = avctx->height; | |
| 150 | 2 | stride = FFALIGN(c->linelen, 4); | |
| 151 | 2 | c->decomp_size = c->height * stride; | |
| 152 | 2 | c->decomp_buf = av_malloc(c->decomp_size + AV_LZO_OUTPUT_PADDING); | |
| 153 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!c->decomp_buf) { |
| 154 | ✗ | av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n"); | |
| 155 | ✗ | return AVERROR(ENOMEM); | |
| 156 | } | ||
| 157 | 2 | c->pic = av_frame_alloc(); | |
| 158 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!c->pic) |
| 159 | ✗ | return AVERROR(ENOMEM); | |
| 160 | 2 | return 0; | |
| 161 | } | ||
| 162 | |||
| 163 | 2 | static av_cold int decode_end(AVCodecContext *avctx) | |
| 164 | { | ||
| 165 | 2 | CamStudioContext *c = avctx->priv_data; | |
| 166 | 2 | av_freep(&c->decomp_buf); | |
| 167 | 2 | av_frame_free(&c->pic); | |
| 168 | 2 | return 0; | |
| 169 | } | ||
| 170 | |||
| 171 | const FFCodec ff_cscd_decoder = { | ||
| 172 | .p.name = "camstudio", | ||
| 173 | CODEC_LONG_NAME("CamStudio"), | ||
| 174 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 175 | .p.id = AV_CODEC_ID_CSCD, | ||
| 176 | .priv_data_size = sizeof(CamStudioContext), | ||
| 177 | .init = decode_init, | ||
| 178 | .close = decode_end, | ||
| 179 | FF_CODEC_DECODE_CB(decode_frame), | ||
| 180 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
| 181 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
| 182 | }; | ||
| 183 |