FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/tscc.c
Date: 2021-09-23 20:34:37
Exec Total Coverage
Lines: 55 73 75.3%
Branches: 20 37 54.1%

Line Branch Exec Source
1 /*
2 * TechSmith Camtasia decoder
3 * Copyright (c) 2004 Konstantin Shishkov
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 /**
23 * @file
24 * TechSmith Camtasia decoder
25 *
26 * Fourcc: TSCC
27 *
28 * Codec is very simple:
29 * it codes picture (picture difference, really)
30 * with algorithm almost identical to Windows RLE8,
31 * only without padding and with greater pixel sizes,
32 * then this coded picture is packed with ZLib
33 *
34 * Supports: BGR8,BGR555,BGR24 - only BGR8 and BGR555 tested
35 */
36
37 #include <stdio.h>
38 #include <stdlib.h>
39
40 #include "avcodec.h"
41 #include "decode.h"
42 #include "internal.h"
43 #include "msrledec.h"
44
45 #include <zlib.h>
46
47 typedef struct TsccContext {
48
49 AVCodecContext *avctx;
50 AVFrame *frame;
51
52 // Bits per pixel
53 int bpp;
54 // Decompressed data size
55 unsigned int decomp_size;
56 // Decompression buffer
57 unsigned char* decomp_buf;
58 GetByteContext gb;
59 int height;
60 int zlib_init_ok;
61 z_stream zstream;
62
63 uint32_t pal[256];
64 } CamtasiaContext;
65
66 392 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
67 AVPacket *avpkt)
68 {
69 392 const uint8_t *buf = avpkt->data;
70 392 int buf_size = avpkt->size;
71 392 CamtasiaContext * const c = avctx->priv_data;
72 392 AVFrame *frame = c->frame;
73 int ret;
74 392 int palette_has_changed = 0;
75
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 392 times.
392 if (c->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
77 palette_has_changed = ff_copy_palette(c->pal, avpkt, avctx);
78 }
79
80 392 ret = inflateReset(&c->zstream);
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 392 times.
392 if (ret != Z_OK) {
82 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
83 return AVERROR_UNKNOWN;
84 }
85 392 c->zstream.next_in = buf;
86 392 c->zstream.avail_in = buf_size;
87 392 c->zstream.next_out = c->decomp_buf;
88 392 c->zstream.avail_out = c->decomp_size;
89 392 ret = inflate(&c->zstream, Z_FINISH);
90 // Z_DATA_ERROR means empty picture
91
3/4
✓ Branch 0 taken 268 times.
✓ Branch 1 taken 124 times.
✓ Branch 2 taken 268 times.
✗ Branch 3 not taken.
392 if (ret == Z_DATA_ERROR && !palette_has_changed) {
92 268 return buf_size;
93 }
94
95
4/6
✓ Branch 0 taken 124 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 123 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
124 if ((ret != Z_OK) && (ret != Z_STREAM_END) && (ret != Z_DATA_ERROR)) {
96 1 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret);
97 1 return AVERROR_UNKNOWN;
98 }
99
100
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 123 times.
123 if ((ret = ff_reget_buffer(avctx, frame, 0)) < 0)
101 return ret;
102
103
1/2
✓ Branch 0 taken 123 times.
✗ Branch 1 not taken.
123 if (ret != Z_DATA_ERROR) {
104 123 bytestream2_init(&c->gb, c->decomp_buf,
105 123 c->decomp_size - c->zstream.avail_out);
106 123 ff_msrle_decode(avctx, frame, c->bpp, &c->gb);
107 }
108
109 /* make the palette available on the way out */
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 123 times.
123 if (c->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
111 frame->palette_has_changed = palette_has_changed;
112 memcpy(frame->data[1], c->pal, AVPALETTE_SIZE);
113 }
114
115
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 123 times.
123 if ((ret = av_frame_ref(data, frame)) < 0)
116 return ret;
117 123 *got_frame = 1;
118
119 /* always report that the buffer was completely consumed */
120 123 return buf_size;
121 }
122
123 4 static av_cold int decode_init(AVCodecContext *avctx)
124 {
125 4 CamtasiaContext * const c = avctx->priv_data;
126 int zret; // Zlib return code
127
128 4 c->avctx = avctx;
129
130 4 c->height = avctx->height;
131
132
2/5
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
4 switch(avctx->bits_per_coded_sample){
133 case 8: avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
134 2 case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
135 case 24:
136 avctx->pix_fmt = AV_PIX_FMT_BGR24;
137 break;
138 2 case 32: avctx->pix_fmt = AV_PIX_FMT_0RGB32; break;
139 default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_coded_sample);
140 return AVERROR_PATCHWELCOME;
141 }
142 4 c->bpp = avctx->bits_per_coded_sample;
143 // buffer size for RLE 'best' case when 2-byte code precedes each pixel and there may be padding after it too
144 4 c->decomp_size = (((avctx->width * c->bpp + 7) >> 3) + 3 * avctx->width + 2) * avctx->height + 2;
145
146 /* Allocate decompression buffer */
147
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (c->decomp_size) {
148
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (!(c->decomp_buf = av_malloc(c->decomp_size))) {
149 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
150 return AVERROR(ENOMEM);
151 }
152 }
153
154 4 c->zstream.zalloc = Z_NULL;
155 4 c->zstream.zfree = Z_NULL;
156 4 c->zstream.opaque = Z_NULL;
157 4 zret = inflateInit(&c->zstream);
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (zret != Z_OK) {
159 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
160 return AVERROR_UNKNOWN;
161 }
162 4 c->zlib_init_ok = 1;
163
164 4 c->frame = av_frame_alloc();
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!c->frame)
166 return AVERROR(ENOMEM);
167
168 4 return 0;
169 }
170
171 4 static av_cold int decode_end(AVCodecContext *avctx)
172 {
173 4 CamtasiaContext * const c = avctx->priv_data;
174
175 4 av_freep(&c->decomp_buf);
176 4 av_frame_free(&c->frame);
177
178
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (c->zlib_init_ok)
179 4 inflateEnd(&c->zstream);
180
181 4 return 0;
182 }
183
184 const AVCodec ff_tscc_decoder = {
185 .name = "camtasia",
186 .long_name = NULL_IF_CONFIG_SMALL("TechSmith Screen Capture Codec"),
187 .type = AVMEDIA_TYPE_VIDEO,
188 .id = AV_CODEC_ID_TSCC,
189 .priv_data_size = sizeof(CamtasiaContext),
190 .init = decode_init,
191 .close = decode_end,
192 .decode = decode_frame,
193 .capabilities = AV_CODEC_CAP_DR1,
194 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
195 };
196