GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/cscd.c Lines: 59 82 72.0 %
Date: 2019-11-22 03:34:36 Branches: 17 31 54.8 %

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
#include <stdio.h>
22
#include <stdlib.h>
23
24
#include "avcodec.h"
25
#include "internal.h"
26
#include "libavutil/common.h"
27
28
#if CONFIG_ZLIB
29
#include <zlib.h>
30
#endif
31
#include "libavutil/lzo.h"
32
33
typedef struct CamStudioContext {
34
    AVFrame *pic;
35
    int linelen, height, bpp;
36
    unsigned int decomp_size;
37
    unsigned char* decomp_buf;
38
} CamStudioContext;
39
40
8
static void copy_frame_default(AVFrame *f, const uint8_t *src,
41
                               int linelen, int height)
42
{
43
8
    int i, src_stride = FFALIGN(linelen, 4);
44
8
    uint8_t *dst = f->data[0];
45
8
    dst += (height - 1) * f->linesize[0];
46
2408
    for (i = height; i; i--) {
47
2400
        memcpy(dst, src, linelen);
48
2400
        src += src_stride;
49
2400
        dst -= f->linesize[0];
50
    }
51
8
}
52
53
200
static void add_frame_default(AVFrame *f, const uint8_t *src,
54
                              int linelen, int height)
55
{
56
200
    int i, j, src_stride = FFALIGN(linelen, 4);
57
200
    uint8_t *dst = f->data[0];
58
200
    dst += (height - 1) * f->linesize[0];
59
60200
    for (i = height; i; i--) {
60
72060000
        for (j = linelen; j; j--)
61
72000000
            *dst++ += *src++;
62
60000
        src += src_stride - linelen;
63
60000
        dst -= f->linesize[0] + linelen;
64
    }
65
200
}
66
67
208
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
68
                        AVPacket *avpkt)
69
{
70
208
    const uint8_t *buf = avpkt->data;
71
208
    int buf_size = avpkt->size;
72
208
    CamStudioContext *c = avctx->priv_data;
73
    int ret;
74
75
208
    if (buf_size < 2) {
76
        av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
77
        return AVERROR_INVALIDDATA;
78
    }
79
80
208
    if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
81
        return ret;
82
83
    // decompress data
84
208
    switch ((buf[0] >> 1) & 7) {
85
208
    case 0: { // lzo compression
86
208
        int outlen = c->decomp_size, inlen = buf_size - 2;
87

208
        if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen) || outlen) {
88
            av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
89
            return AVERROR_INVALIDDATA;
90
        }
91
208
        break;
92
    }
93
    case 1: { // zlib compression
94
#if CONFIG_ZLIB
95
        unsigned long dlen = c->decomp_size;
96
        if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK) {
97
            av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
98
            return AVERROR_INVALIDDATA;
99
        }
100
        break;
101
#else
102
        av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
103
        return AVERROR(ENOSYS);
104
#endif
105
    }
106
    default:
107
        av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
108
        return AVERROR_INVALIDDATA;
109
    }
110
111
    // flip upside down, add difference frame
112
208
    if (buf[0] & 1) { // keyframe
113
8
        c->pic->pict_type = AV_PICTURE_TYPE_I;
114
8
        c->pic->key_frame = 1;
115
8
              copy_frame_default(c->pic, c->decomp_buf,
116
                                 c->linelen, c->height);
117
    } else {
118
200
        c->pic->pict_type = AV_PICTURE_TYPE_P;
119
200
        c->pic->key_frame = 0;
120
200
              add_frame_default(c->pic, c->decomp_buf,
121
                                c->linelen, c->height);
122
    }
123
124
208
    *got_frame = 1;
125
208
    if ((ret = av_frame_ref(data, c->pic)) < 0)
126
        return ret;
127
128
208
    return buf_size;
129
}
130
131
2
static av_cold int decode_init(AVCodecContext *avctx)
132
{
133
2
    CamStudioContext *c = avctx->priv_data;
134
    int stride;
135

2
    switch (avctx->bits_per_coded_sample) {
136
    case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555LE; break;
137
    case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
138
2
    case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
139
    default:
140
        av_log(avctx, AV_LOG_ERROR,
141
               "CamStudio codec error: invalid depth %i bpp\n",
142
               avctx->bits_per_coded_sample);
143
        return AVERROR_INVALIDDATA;
144
    }
145
2
    c->bpp = avctx->bits_per_coded_sample;
146
2
    c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
147
2
    c->height = avctx->height;
148
2
    stride = FFALIGN(c->linelen, 4);
149
2
    c->decomp_size = c->height * stride;
150
2
    c->decomp_buf = av_malloc(c->decomp_size + AV_LZO_OUTPUT_PADDING);
151
2
    if (!c->decomp_buf) {
152
        av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
153
        return AVERROR(ENOMEM);
154
    }
155
2
    c->pic = av_frame_alloc();
156
2
    if (!c->pic)
157
        return AVERROR(ENOMEM);
158
2
    return 0;
159
}
160
161
2
static av_cold int decode_end(AVCodecContext *avctx)
162
{
163
2
    CamStudioContext *c = avctx->priv_data;
164
2
    av_freep(&c->decomp_buf);
165
2
    av_frame_free(&c->pic);
166
2
    return 0;
167
}
168
169
AVCodec ff_cscd_decoder = {
170
    .name           = "camstudio",
171
    .long_name      = NULL_IF_CONFIG_SMALL("CamStudio"),
172
    .type           = AVMEDIA_TYPE_VIDEO,
173
    .id             = AV_CODEC_ID_CSCD,
174
    .priv_data_size = sizeof(CamStudioContext),
175
    .init           = decode_init,
176
    .close          = decode_end,
177
    .decode         = decode_frame,
178
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
179
    .capabilities   = AV_CODEC_CAP_DR1,
180
};