FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/cscd.c
Date: 2022-07-04 00:18:54
Exec Total Coverage
Lines: 59 82 72.0%
Branches: 17 33 51.5%

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 "codec_internal.h"
26 #include "internal.h"
27 #include "libavutil/common.h"
28
29 #if CONFIG_ZLIB
30 #include <zlib.h>
31 #endif
32 #include "libavutil/lzo.h"
33
34 typedef struct CamStudioContext {
35 AVFrame *pic;
36 int linelen, height, bpp;
37 unsigned int decomp_size;
38 unsigned char* decomp_buf;
39 } CamStudioContext;
40
41 8 static void copy_frame_default(AVFrame *f, const uint8_t *src,
42 int linelen, int height)
43 {
44 8 int i, src_stride = FFALIGN(linelen, 4);
45 8 uint8_t *dst = f->data[0];
46 8 dst += (height - 1) * f->linesize[0];
47
2/2
✓ Branch 0 taken 2400 times.
✓ Branch 1 taken 8 times.
2408 for (i = height; i; i--) {
48 2400 memcpy(dst, src, linelen);
49 2400 src += src_stride;
50 2400 dst -= f->linesize[0];
51 }
52 8 }
53
54 200 static void add_frame_default(AVFrame *f, const uint8_t *src,
55 int linelen, int height)
56 {
57 200 int i, j, src_stride = FFALIGN(linelen, 4);
58 200 uint8_t *dst = f->data[0];
59 200 dst += (height - 1) * f->linesize[0];
60
2/2
✓ Branch 0 taken 60000 times.
✓ Branch 1 taken 200 times.
60200 for (i = height; i; i--) {
61
2/2
✓ Branch 0 taken 72000000 times.
✓ Branch 1 taken 60000 times.
72060000 for (j = linelen; j; j--)
62 72000000 *dst++ += *src++;
63 60000 src += src_stride - linelen;
64 60000 dst -= f->linesize[0] + linelen;
65 }
66 200 }
67
68 208 static int decode_frame(AVCodecContext *avctx, AVFrame *rframe,
69 int *got_frame, AVPacket *avpkt)
70 {
71 208 const uint8_t *buf = avpkt->data;
72 208 int buf_size = avpkt->size;
73 208 CamStudioContext *c = avctx->priv_data;
74 int ret;
75
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 208 times.
208 if (buf_size < 2) {
77 av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
78 return AVERROR_INVALIDDATA;
79 }
80
81
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 208 times.
208 if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
82 return ret;
83
84 // decompress data
85
1/3
✓ Branch 0 taken 208 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
208 switch ((buf[0] >> 1) & 7) {
86 208 case 0: { // lzo compression
87 208 int outlen = c->decomp_size, inlen = buf_size - 2;
88
2/4
✓ Branch 1 taken 208 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 208 times.
208 if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen) || outlen) {
89 av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
90 return AVERROR_INVALIDDATA;
91 }
92 208 break;
93 }
94 case 1: { // zlib compression
95 #if CONFIG_ZLIB
96 unsigned long dlen = c->decomp_size;
97 if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK || dlen != c->decomp_size) {
98 av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
99 return AVERROR_INVALIDDATA;
100 }
101 break;
102 #else
103 av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
104 return AVERROR(ENOSYS);
105 #endif
106 }
107 default:
108 av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
109 return AVERROR_INVALIDDATA;
110 }
111
112 // flip upside down, add difference frame
113
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 200 times.
208 if (buf[0] & 1) { // keyframe
114 8 c->pic->pict_type = AV_PICTURE_TYPE_I;
115 8 c->pic->key_frame = 1;
116 8 copy_frame_default(c->pic, c->decomp_buf,
117 c->linelen, c->height);
118 } else {
119 200 c->pic->pict_type = AV_PICTURE_TYPE_P;
120 200 c->pic->key_frame = 0;
121 200 add_frame_default(c->pic, c->decomp_buf,
122 c->linelen, c->height);
123 }
124
125 208 *got_frame = 1;
126
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 208 times.
208 if ((ret = av_frame_ref(rframe, c->pic)) < 0)
127 return ret;
128
129 208 return buf_size;
130 }
131
132 2 static av_cold int decode_init(AVCodecContext *avctx)
133 {
134 2 CamStudioContext *c = avctx->priv_data;
135 int stride;
136
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) {
137 case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555LE; break;
138 case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
139 2 case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
140 default:
141 av_log(avctx, AV_LOG_ERROR,
142 "CamStudio codec error: invalid depth %i bpp\n",
143 avctx->bits_per_coded_sample);
144 return AVERROR_INVALIDDATA;
145 }
146 2 c->bpp = avctx->bits_per_coded_sample;
147 2 c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
148 2 c->height = avctx->height;
149 2 stride = FFALIGN(c->linelen, 4);
150 2 c->decomp_size = c->height * stride;
151 2 c->decomp_buf = av_malloc(c->decomp_size + AV_LZO_OUTPUT_PADDING);
152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!c->decomp_buf) {
153 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
154 return AVERROR(ENOMEM);
155 }
156 2 c->pic = av_frame_alloc();
157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!c->pic)
158 return AVERROR(ENOMEM);
159 2 return 0;
160 }
161
162 2 static av_cold int decode_end(AVCodecContext *avctx)
163 {
164 2 CamStudioContext *c = avctx->priv_data;
165 2 av_freep(&c->decomp_buf);
166 2 av_frame_free(&c->pic);
167 2 return 0;
168 }
169
170 const FFCodec ff_cscd_decoder = {
171 .p.name = "camstudio",
172 .p.long_name = NULL_IF_CONFIG_SMALL("CamStudio"),
173 .p.type = AVMEDIA_TYPE_VIDEO,
174 .p.id = AV_CODEC_ID_CSCD,
175 .priv_data_size = sizeof(CamStudioContext),
176 .init = decode_init,
177 .close = decode_end,
178 FF_CODEC_DECODE_CB(decode_frame),
179 .p.capabilities = AV_CODEC_CAP_DR1,
180 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
181 };
182