You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

331 lines
8.9KB

  1. /*
  2. * TechSmith Camtasia decoder
  3. * Copyright (c) 2004 Konstantin Shishkov
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /**
  21. * @file tscc.c
  22. * TechSmith Camtasia decoder
  23. *
  24. * Fourcc: TSCC
  25. *
  26. * Codec is very simple:
  27. * it codes picture (picture difference, really)
  28. * with algorithm almost identical to Windows RLE8,
  29. * only without padding and with greater pixel sizes,
  30. * then this coded picture is packed with ZLib
  31. *
  32. * Supports: BGR8,BGR555,BGR24 - only BGR8 and BGR555 tested
  33. *
  34. */
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include "common.h"
  38. #include "avcodec.h"
  39. #ifdef CONFIG_ZLIB
  40. #include <zlib.h>
  41. #endif
  42. /*
  43. * Decoder context
  44. */
  45. typedef struct TsccContext {
  46. AVCodecContext *avctx;
  47. AVFrame pic;
  48. // Bits per pixel
  49. int bpp;
  50. // Decompressed data size
  51. unsigned int decomp_size;
  52. // Decompression buffer
  53. unsigned char* decomp_buf;
  54. int height;
  55. #ifdef CONFIG_ZLIB
  56. z_stream zstream;
  57. #endif
  58. } CamtasiaContext;
  59. /*
  60. *
  61. * Decode RLE - almost identical to Windows BMP RLE8
  62. * and enhanced to bigger color depths
  63. *
  64. */
  65. static int decode_rle(CamtasiaContext *c, unsigned int srcsize)
  66. {
  67. unsigned char *src = c->decomp_buf;
  68. unsigned char *output, *output_end;
  69. int p1, p2, line=c->height, pos=0, i;
  70. output = c->pic.data[0] + (c->height - 1) * c->pic.linesize[0];
  71. output_end = c->pic.data[0] + (c->height) * c->pic.linesize[0];
  72. while(src < c->decomp_buf + srcsize) {
  73. p1 = *src++;
  74. if(p1 == 0) { //Escape code
  75. p2 = *src++;
  76. if(p2 == 0) { //End-of-line
  77. output = c->pic.data[0] + (--line) * c->pic.linesize[0];
  78. if (line < 0)
  79. return -1;
  80. pos = 0;
  81. continue;
  82. } else if(p2 == 1) { //End-of-picture
  83. return 0;
  84. } else if(p2 == 2) { //Skip
  85. p1 = *src++;
  86. p2 = *src++;
  87. line -= p2;
  88. if (line < 0)
  89. return -1;
  90. pos += p1;
  91. output = c->pic.data[0] + line * c->pic.linesize[0] + pos * (c->bpp / 8);
  92. continue;
  93. }
  94. // Copy data
  95. if (output + p2 * (c->bpp / 8) > output_end) {
  96. src += p2 * (c->bpp / 8);
  97. continue;
  98. }
  99. for(i = 0; i < p2 * (c->bpp / 8); i++) {
  100. *output++ = *src++;
  101. }
  102. // RLE8 copy is actually padded - and runs are not!
  103. if(c->bpp == 8 && (p2 & 1)) {
  104. src++;
  105. }
  106. pos += p2;
  107. } else { //Run of pixels
  108. int pix[4]; //original pixel
  109. switch(c->bpp){
  110. case 8: pix[0] = *src++;
  111. break;
  112. case 16: pix[0] = *src++;
  113. pix[1] = *src++;
  114. break;
  115. case 24: pix[0] = *src++;
  116. pix[1] = *src++;
  117. pix[2] = *src++;
  118. break;
  119. case 32: pix[0] = *src++;
  120. pix[1] = *src++;
  121. pix[2] = *src++;
  122. pix[3] = *src++;
  123. break;
  124. }
  125. if (output + p1 * (c->bpp / 8) > output_end)
  126. continue;
  127. for(i = 0; i < p1; i++) {
  128. switch(c->bpp){
  129. case 8: *output++ = pix[0];
  130. break;
  131. case 16: *output++ = pix[0];
  132. *output++ = pix[1];
  133. break;
  134. case 24: *output++ = pix[0];
  135. *output++ = pix[1];
  136. *output++ = pix[2];
  137. break;
  138. case 32: *output++ = pix[0];
  139. *output++ = pix[1];
  140. *output++ = pix[2];
  141. *output++ = pix[3];
  142. break;
  143. }
  144. }
  145. pos += p1;
  146. }
  147. }
  148. av_log(c->avctx, AV_LOG_ERROR, "Camtasia warning: no End-of-picture code\n");
  149. return 1;
  150. }
  151. /*
  152. *
  153. * Decode a frame
  154. *
  155. */
  156. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
  157. {
  158. CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
  159. unsigned char *encoded = (unsigned char *)buf;
  160. unsigned char *outptr;
  161. #ifdef CONFIG_ZLIB
  162. int zret; // Zlib return code
  163. #endif
  164. int len = buf_size;
  165. if(c->pic.data[0])
  166. avctx->release_buffer(avctx, &c->pic);
  167. c->pic.reference = 1;
  168. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
  169. if(avctx->get_buffer(avctx, &c->pic) < 0){
  170. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  171. return -1;
  172. }
  173. outptr = c->pic.data[0]; // Output image pointer
  174. #ifdef CONFIG_ZLIB
  175. zret = inflateReset(&(c->zstream));
  176. if (zret != Z_OK) {
  177. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
  178. return -1;
  179. }
  180. c->zstream.next_in = encoded;
  181. c->zstream.avail_in = len;
  182. c->zstream.next_out = c->decomp_buf;
  183. c->zstream.avail_out = c->decomp_size;
  184. zret = inflate(&(c->zstream), Z_FINISH);
  185. // Z_DATA_ERROR means empty picture
  186. if ((zret != Z_OK) && (zret != Z_STREAM_END) && (zret != Z_DATA_ERROR)) {
  187. av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
  188. return -1;
  189. }
  190. if(zret != Z_DATA_ERROR)
  191. decode_rle(c, c->zstream.avail_out);
  192. /* make the palette available on the way out */
  193. if (c->avctx->pix_fmt == PIX_FMT_PAL8) {
  194. memcpy(c->pic.data[1], c->avctx->palctrl->palette, AVPALETTE_SIZE);
  195. if (c->avctx->palctrl->palette_changed) {
  196. c->pic.palette_has_changed = 1;
  197. c->avctx->palctrl->palette_changed = 0;
  198. }
  199. }
  200. #else
  201. av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
  202. return -1;
  203. #endif
  204. *data_size = sizeof(AVFrame);
  205. *(AVFrame*)data = c->pic;
  206. /* always report that the buffer was completely consumed */
  207. return buf_size;
  208. }
  209. /*
  210. *
  211. * Init tscc decoder
  212. *
  213. */
  214. static int decode_init(AVCodecContext *avctx)
  215. {
  216. CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
  217. int zret; // Zlib return code
  218. c->avctx = avctx;
  219. avctx->has_b_frames = 0;
  220. c->pic.data[0] = NULL;
  221. c->height = avctx->height;
  222. if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
  223. return 1;
  224. }
  225. #ifdef CONFIG_ZLIB
  226. // Needed if zlib unused or init aborted before inflateInit
  227. memset(&(c->zstream), 0, sizeof(z_stream));
  228. #else
  229. av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
  230. return 1;
  231. #endif
  232. switch(avctx->bits_per_sample){
  233. case 8: avctx->pix_fmt = PIX_FMT_PAL8; break;
  234. case 16: avctx->pix_fmt = PIX_FMT_RGB555; break;
  235. case 24:
  236. avctx->pix_fmt = PIX_FMT_BGR24;
  237. break;
  238. case 32: avctx->pix_fmt = PIX_FMT_RGBA32; break;
  239. default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_sample);
  240. return -1;
  241. }
  242. c->bpp = avctx->bits_per_sample;
  243. c->decomp_size = (avctx->width * c->bpp + (avctx->width + 254) / 255 + 2) * avctx->height + 2;//RLE in the 'best' case
  244. /* Allocate decompression buffer */
  245. if (c->decomp_size) {
  246. if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
  247. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  248. return 1;
  249. }
  250. }
  251. #ifdef CONFIG_ZLIB
  252. c->zstream.zalloc = Z_NULL;
  253. c->zstream.zfree = Z_NULL;
  254. c->zstream.opaque = Z_NULL;
  255. zret = inflateInit(&(c->zstream));
  256. if (zret != Z_OK) {
  257. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  258. return 1;
  259. }
  260. #endif
  261. return 0;
  262. }
  263. /*
  264. *
  265. * Uninit tscc decoder
  266. *
  267. */
  268. static int decode_end(AVCodecContext *avctx)
  269. {
  270. CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
  271. av_freep(&c->decomp_buf);
  272. if (c->pic.data[0])
  273. avctx->release_buffer(avctx, &c->pic);
  274. #ifdef CONFIG_ZLIB
  275. inflateEnd(&(c->zstream));
  276. #endif
  277. return 0;
  278. }
  279. AVCodec tscc_decoder = {
  280. "camtasia",
  281. CODEC_TYPE_VIDEO,
  282. CODEC_ID_TSCC,
  283. sizeof(CamtasiaContext),
  284. decode_init,
  285. NULL,
  286. decode_end,
  287. decode_frame,
  288. CODEC_CAP_DR1,
  289. };