yading@10
|
1 /*
|
yading@10
|
2 * TechSmith Camtasia decoder
|
yading@10
|
3 * Copyright (c) 2004 Konstantin Shishkov
|
yading@10
|
4 *
|
yading@10
|
5 * This file is part of FFmpeg.
|
yading@10
|
6 *
|
yading@10
|
7 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
8 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
9 * License as published by the Free Software Foundation; either
|
yading@10
|
10 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
11 *
|
yading@10
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
15 * Lesser General Public License for more details.
|
yading@10
|
16 *
|
yading@10
|
17 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
18 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
20 */
|
yading@10
|
21
|
yading@10
|
22 /**
|
yading@10
|
23 * @file
|
yading@10
|
24 * TechSmith Camtasia decoder
|
yading@10
|
25 *
|
yading@10
|
26 * Fourcc: TSCC
|
yading@10
|
27 *
|
yading@10
|
28 * Codec is very simple:
|
yading@10
|
29 * it codes picture (picture difference, really)
|
yading@10
|
30 * with algorithm almost identical to Windows RLE8,
|
yading@10
|
31 * only without padding and with greater pixel sizes,
|
yading@10
|
32 * then this coded picture is packed with ZLib
|
yading@10
|
33 *
|
yading@10
|
34 * Supports: BGR8,BGR555,BGR24 - only BGR8 and BGR555 tested
|
yading@10
|
35 *
|
yading@10
|
36 */
|
yading@10
|
37
|
yading@10
|
38 #include <stdio.h>
|
yading@10
|
39 #include <stdlib.h>
|
yading@10
|
40
|
yading@10
|
41 #include "avcodec.h"
|
yading@10
|
42 #include "internal.h"
|
yading@10
|
43 #include "msrledec.h"
|
yading@10
|
44
|
yading@10
|
45 #include <zlib.h>
|
yading@10
|
46
|
yading@10
|
47 typedef struct TsccContext {
|
yading@10
|
48
|
yading@10
|
49 AVCodecContext *avctx;
|
yading@10
|
50 AVFrame *frame;
|
yading@10
|
51
|
yading@10
|
52 // Bits per pixel
|
yading@10
|
53 int bpp;
|
yading@10
|
54 // Decompressed data size
|
yading@10
|
55 unsigned int decomp_size;
|
yading@10
|
56 // Decompression buffer
|
yading@10
|
57 unsigned char* decomp_buf;
|
yading@10
|
58 GetByteContext gb;
|
yading@10
|
59 int height;
|
yading@10
|
60 z_stream zstream;
|
yading@10
|
61
|
yading@10
|
62 uint32_t pal[256];
|
yading@10
|
63 } CamtasiaContext;
|
yading@10
|
64
|
yading@10
|
65 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
|
yading@10
|
66 AVPacket *avpkt)
|
yading@10
|
67 {
|
yading@10
|
68 const uint8_t *buf = avpkt->data;
|
yading@10
|
69 int buf_size = avpkt->size;
|
yading@10
|
70 CamtasiaContext * const c = avctx->priv_data;
|
yading@10
|
71 const unsigned char *encoded = buf;
|
yading@10
|
72 AVFrame *frame = c->frame;
|
yading@10
|
73 int zret; // Zlib return code
|
yading@10
|
74 int ret, len = buf_size;
|
yading@10
|
75
|
yading@10
|
76 if ((ret = ff_reget_buffer(avctx, frame)) < 0)
|
yading@10
|
77 return ret;
|
yading@10
|
78
|
yading@10
|
79 zret = inflateReset(&c->zstream);
|
yading@10
|
80 if (zret != Z_OK) {
|
yading@10
|
81 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
|
yading@10
|
82 return AVERROR_UNKNOWN;
|
yading@10
|
83 }
|
yading@10
|
84 c->zstream.next_in = (uint8_t*)encoded;
|
yading@10
|
85 c->zstream.avail_in = len;
|
yading@10
|
86 c->zstream.next_out = c->decomp_buf;
|
yading@10
|
87 c->zstream.avail_out = c->decomp_size;
|
yading@10
|
88 zret = inflate(&c->zstream, Z_FINISH);
|
yading@10
|
89 // Z_DATA_ERROR means empty picture
|
yading@10
|
90 if ((zret != Z_OK) && (zret != Z_STREAM_END) && (zret != Z_DATA_ERROR)) {
|
yading@10
|
91 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
|
yading@10
|
92 return AVERROR_UNKNOWN;
|
yading@10
|
93 }
|
yading@10
|
94
|
yading@10
|
95
|
yading@10
|
96 if (zret != Z_DATA_ERROR) {
|
yading@10
|
97 bytestream2_init(&c->gb, c->decomp_buf,
|
yading@10
|
98 c->decomp_size - c->zstream.avail_out);
|
yading@10
|
99 ff_msrle_decode(avctx, (AVPicture*)frame, c->bpp, &c->gb);
|
yading@10
|
100 }
|
yading@10
|
101
|
yading@10
|
102 /* make the palette available on the way out */
|
yading@10
|
103 if (c->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
|
yading@10
|
104 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
|
yading@10
|
105
|
yading@10
|
106 if (pal) {
|
yading@10
|
107 frame->palette_has_changed = 1;
|
yading@10
|
108 memcpy(c->pal, pal, AVPALETTE_SIZE);
|
yading@10
|
109 }
|
yading@10
|
110 memcpy(frame->data[1], c->pal, AVPALETTE_SIZE);
|
yading@10
|
111 }
|
yading@10
|
112
|
yading@10
|
113 if ((ret = av_frame_ref(data, frame)) < 0)
|
yading@10
|
114 return ret;
|
yading@10
|
115 *got_frame = 1;
|
yading@10
|
116
|
yading@10
|
117 /* always report that the buffer was completely consumed */
|
yading@10
|
118 return buf_size;
|
yading@10
|
119 }
|
yading@10
|
120
|
yading@10
|
121 static av_cold int decode_init(AVCodecContext *avctx)
|
yading@10
|
122 {
|
yading@10
|
123 CamtasiaContext * const c = avctx->priv_data;
|
yading@10
|
124 int zret; // Zlib return code
|
yading@10
|
125
|
yading@10
|
126 c->avctx = avctx;
|
yading@10
|
127
|
yading@10
|
128 c->height = avctx->height;
|
yading@10
|
129
|
yading@10
|
130 // Needed if zlib unused or init aborted before inflateInit
|
yading@10
|
131 memset(&c->zstream, 0, sizeof(z_stream));
|
yading@10
|
132 switch(avctx->bits_per_coded_sample){
|
yading@10
|
133 case 8: avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
|
yading@10
|
134 case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
|
yading@10
|
135 case 24:
|
yading@10
|
136 avctx->pix_fmt = AV_PIX_FMT_BGR24;
|
yading@10
|
137 break;
|
yading@10
|
138 case 32: avctx->pix_fmt = AV_PIX_FMT_RGB32; break;
|
yading@10
|
139 default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_coded_sample);
|
yading@10
|
140 return AVERROR_PATCHWELCOME;
|
yading@10
|
141 }
|
yading@10
|
142 c->bpp = avctx->bits_per_coded_sample;
|
yading@10
|
143 // buffer size for RLE 'best' case when 2-byte code precedes each pixel and there may be padding after it too
|
yading@10
|
144 c->decomp_size = (((avctx->width * c->bpp + 7) >> 3) + 3 * avctx->width + 2) * avctx->height + 2;
|
yading@10
|
145
|
yading@10
|
146 /* Allocate decompression buffer */
|
yading@10
|
147 if (c->decomp_size) {
|
yading@10
|
148 if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
|
yading@10
|
149 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
|
yading@10
|
150 return AVERROR(ENOMEM);
|
yading@10
|
151 }
|
yading@10
|
152 }
|
yading@10
|
153
|
yading@10
|
154 c->zstream.zalloc = Z_NULL;
|
yading@10
|
155 c->zstream.zfree = Z_NULL;
|
yading@10
|
156 c->zstream.opaque = Z_NULL;
|
yading@10
|
157 zret = inflateInit(&c->zstream);
|
yading@10
|
158 if (zret != Z_OK) {
|
yading@10
|
159 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
|
yading@10
|
160 return AVERROR_UNKNOWN;
|
yading@10
|
161 }
|
yading@10
|
162
|
yading@10
|
163 c->frame = av_frame_alloc();
|
yading@10
|
164
|
yading@10
|
165 return 0;
|
yading@10
|
166 }
|
yading@10
|
167
|
yading@10
|
168 static av_cold int decode_end(AVCodecContext *avctx)
|
yading@10
|
169 {
|
yading@10
|
170 CamtasiaContext * const c = avctx->priv_data;
|
yading@10
|
171
|
yading@10
|
172 av_freep(&c->decomp_buf);
|
yading@10
|
173 av_frame_free(&c->frame);
|
yading@10
|
174
|
yading@10
|
175 inflateEnd(&c->zstream);
|
yading@10
|
176
|
yading@10
|
177 return 0;
|
yading@10
|
178 }
|
yading@10
|
179
|
yading@10
|
180 AVCodec ff_tscc_decoder = {
|
yading@10
|
181 .name = "camtasia",
|
yading@10
|
182 .type = AVMEDIA_TYPE_VIDEO,
|
yading@10
|
183 .id = AV_CODEC_ID_TSCC,
|
yading@10
|
184 .priv_data_size = sizeof(CamtasiaContext),
|
yading@10
|
185 .init = decode_init,
|
yading@10
|
186 .close = decode_end,
|
yading@10
|
187 .decode = decode_frame,
|
yading@10
|
188 .capabilities = CODEC_CAP_DR1,
|
yading@10
|
189 .long_name = NULL_IF_CONFIG_SMALL("TechSmith Screen Capture Codec"),
|
yading@10
|
190 };
|