tscc.c
Go to the documentation of this file.
1 /*
2  * TechSmith Camtasia decoder
3  * Copyright (c) 2004 Konstantin Shishkov
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
38 #include <stdio.h>
39 #include <stdlib.h>
40 
41 #include "avcodec.h"
42 #include "internal.h"
43 #include "msrledec.h"
44 
45 #include <zlib.h>
46 
47 
48 /*
49  * Decoder context
50  */
51 typedef struct TsccContext {
52 
55 
56  // Bits per pixel
57  int bpp;
58  // Decompressed data size
59  unsigned int decomp_size;
60  // Decompression buffer
61  unsigned char* decomp_buf;
63  int height;
64  z_stream zstream;
65 
66  uint32_t pal[256];
68 
69 /*
70  *
71  * Decode a frame
72  *
73  */
74 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
75  AVPacket *avpkt)
76 {
77  const uint8_t *buf = avpkt->data;
78  int buf_size = avpkt->size;
79  CamtasiaContext * const c = avctx->priv_data;
80  const unsigned char *encoded = buf;
81  int zret; // Zlib return code
82  int len = buf_size;
83 
84  if(c->pic.data[0])
85  avctx->release_buffer(avctx, &c->pic);
86 
87  c->pic.reference = 1;
89  if(ff_get_buffer(avctx, &c->pic) < 0){
90  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
91  return -1;
92  }
93 
94  zret = inflateReset(&c->zstream);
95  if (zret != Z_OK) {
96  av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
97  return -1;
98  }
99  c->zstream.next_in = encoded;
100  c->zstream.avail_in = len;
101  c->zstream.next_out = c->decomp_buf;
102  c->zstream.avail_out = c->decomp_size;
103  zret = inflate(&c->zstream, Z_FINISH);
104  // Z_DATA_ERROR means empty picture
105  if ((zret != Z_OK) && (zret != Z_STREAM_END) && (zret != Z_DATA_ERROR)) {
106  av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
107  return -1;
108  }
109 
110 
111  if (zret != Z_DATA_ERROR) {
113  c->decomp_size - c->zstream.avail_out);
114  ff_msrle_decode(avctx, (AVPicture*)&c->pic, c->bpp, &c->gb);
115  }
116 
117  /* make the palette available on the way out */
118  if (c->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
120 
121  if (pal) {
122  c->pic.palette_has_changed = 1;
123  memcpy(c->pal, pal, AVPALETTE_SIZE);
124  }
125  memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
126  }
127 
128  *got_frame = 1;
129  *(AVFrame*)data = c->pic;
130 
131  /* always report that the buffer was completely consumed */
132  return buf_size;
133 }
134 
135 
136 
137 /*
138  *
139  * Init tscc decoder
140  *
141  */
143 {
144  CamtasiaContext * const c = avctx->priv_data;
145  int zret; // Zlib return code
146 
147  c->avctx = avctx;
148 
149  c->height = avctx->height;
150 
151  // Needed if zlib unused or init aborted before inflateInit
152  memset(&c->zstream, 0, sizeof(z_stream));
153  switch(avctx->bits_per_coded_sample){
154  case 8: avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
155  case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
156  case 24:
157  avctx->pix_fmt = AV_PIX_FMT_BGR24;
158  break;
159  case 32: avctx->pix_fmt = AV_PIX_FMT_RGB32; break;
160  default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_coded_sample);
161  return -1;
162  }
163  c->bpp = avctx->bits_per_coded_sample;
164  // buffer size for RLE 'best' case when 2-byte code precedes each pixel and there may be padding after it too
165  c->decomp_size = (((avctx->width * c->bpp + 7) >> 3) + 3 * avctx->width + 2) * avctx->height + 2;
166 
167  /* Allocate decompression buffer */
168  if (c->decomp_size) {
169  if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
170  av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
171  return 1;
172  }
173  }
174 
175  c->zstream.zalloc = Z_NULL;
176  c->zstream.zfree = Z_NULL;
177  c->zstream.opaque = Z_NULL;
178  zret = inflateInit(&c->zstream);
179  if (zret != Z_OK) {
180  av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
181  return 1;
182  }
183 
184  return 0;
185 }
186 
187 
188 
189 /*
190  *
191  * Uninit tscc decoder
192  *
193  */
195 {
196  CamtasiaContext * const c = avctx->priv_data;
197 
198  av_freep(&c->decomp_buf);
199 
200  if (c->pic.data[0])
201  avctx->release_buffer(avctx, &c->pic);
202  inflateEnd(&c->zstream);
203 
204  return 0;
205 }
206 
208  .name = "camtasia",
209  .type = AVMEDIA_TYPE_VIDEO,
210  .id = AV_CODEC_ID_TSCC,
211  .priv_data_size = sizeof(CamtasiaContext),
212  .init = decode_init,
213  .close = decode_end,
214  .decode = decode_frame,
215  .capabilities = CODEC_CAP_DR1,
216  .long_name = NULL_IF_CONFIG_SMALL("TechSmith Screen Capture Codec"),
217 };
int height
Definition: tscc.c:63
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:61
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
int bpp
Definition: tscc.c:57
int buffer_hints
codec suggestion on buffer type if != 0
Definition: avcodec.h:1253
AVCodecContext * avctx
Definition: tscc.c:53
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:2259
int size
Definition: avcodec.h:916
unsigned char * decomp_buf
Definition: tscc.c:61
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:130
four components are given, that's all.
Definition: avcodec.h:3153
AVCodec.
Definition: avcodec.h:2960
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:151
unsigned int decomp_size
Definition: tscc.c:59
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
uint8_t
z_stream zstream
Definition: tscc.c:64
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
AVFrame pic
Definition: tscc.c:54
static av_cold int decode_init(AVCodecContext *avctx)
Definition: tscc.c:142
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2704
GetByteContext gb
Definition: tscc.c:62
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
int reference
is this picture used as reference The values for this are the same as the MpegEncContext.picture_structure variable, that is 1->top field, 2->bottom field, 3->frame/both fields.
Definition: avcodec.h:1132
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
AVCodec ff_tscc_decoder
Definition: tscc.c:207
int width
picture width / height.
Definition: avcodec.h:1508
struct TsccContext CamtasiaContext
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
NULL
Definition: eval.c:52
external API header
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
uint32_t pal[256]
Definition: tscc.c:66
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: avcodec.h:1246
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
common internal api header.
void * priv_data
Definition: avcodec.h:1382
int ff_msrle_decode(AVCodecContext *avctx, AVPicture *pic, int depth, GetByteContext *gb)
Decode stream in MS RLE format into frame.
Definition: msrledec.c:246
static av_cold int decode_end(AVCodecContext *avctx)
Definition: tscc.c:194
int len
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:190
This structure stores compressed data.
Definition: avcodec.h:898
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: tscc.c:74
if(!(ptr_align%ac->ptr_align)&&samples_align >=aligned_len)