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.

294 lines
7.7KB

  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 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)
  66. {
  67. unsigned char *src = c->decomp_buf;
  68. unsigned char *output = c->pic.data[0];
  69. int p1, p2, line=c->height, pos=0, i;
  70. while(src < c->decomp_buf + c->decomp_size) {
  71. p1 = *src++;
  72. if(p1 == 0) { //Escape code
  73. p2 = *src++;
  74. if(p2 == 0) { //End-of-line
  75. output = c->pic.data[0] + (--line) * c->pic.linesize[0];
  76. pos = 0;
  77. continue;
  78. } else if(p2 == 1) { //End-of-picture
  79. return 0;
  80. } else if(p2 == 2) { //Skip
  81. p1 = *src++;
  82. p2 = *src++;
  83. line -= p2;
  84. pos += p1;
  85. output = c->pic.data[0] + line * c->pic.linesize[0] + pos * (c->bpp / 8);
  86. continue;
  87. }
  88. // Copy data
  89. for(i = 0; i < p2 * (c->bpp / 8); i++) {
  90. *output++ = *src++;
  91. }
  92. pos += p2;
  93. } else { //Run of pixels
  94. int pix[3]; //original pixel
  95. switch(c->bpp){
  96. case 8: pix[0] = *src++;
  97. break;
  98. case 16: pix[0] = *src++;
  99. pix[1] = *src++;
  100. break;
  101. case 24: pix[0] = *src++;
  102. pix[1] = *src++;
  103. pix[2] = *src++;
  104. break;
  105. }
  106. for(i = 0; i < p1; i++) {
  107. switch(c->bpp){
  108. case 8: *output++ = pix[0];
  109. break;
  110. case 16: *output++ = pix[0];
  111. *output++ = pix[1];
  112. break;
  113. case 24: *output++ = pix[0];
  114. *output++ = pix[1];
  115. *output++ = pix[2];
  116. break;
  117. }
  118. }
  119. pos += p1;
  120. }
  121. }
  122. av_log(c->avctx, AV_LOG_ERROR, "Camtasia warning: no End-of-picture code\n");
  123. return 1;
  124. }
  125. /*
  126. *
  127. * Decode a frame
  128. *
  129. */
  130. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
  131. {
  132. CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
  133. unsigned char *encoded = (unsigned char *)buf;
  134. unsigned char *outptr;
  135. #ifdef CONFIG_ZLIB
  136. int zret; // Zlib return code
  137. #endif
  138. int len = buf_size;
  139. /* no supplementary picture */
  140. if (buf_size == 0)
  141. return 0;
  142. if(c->pic.data[0])
  143. avctx->release_buffer(avctx, &c->pic);
  144. c->pic.reference = 1;
  145. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
  146. if(avctx->get_buffer(avctx, &c->pic) < 0){
  147. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  148. return -1;
  149. }
  150. outptr = c->pic.data[0]; // Output image pointer
  151. #ifdef CONFIG_ZLIB
  152. zret = inflateReset(&(c->zstream));
  153. if (zret != Z_OK) {
  154. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
  155. return -1;
  156. }
  157. c->zstream.next_in = encoded;
  158. c->zstream.avail_in = len;
  159. c->zstream.next_out = c->decomp_buf;
  160. c->zstream.avail_out = c->decomp_size;
  161. zret = inflate(&(c->zstream), Z_FINISH);
  162. // Z_DATA_ERROR means empty picture
  163. if ((zret != Z_OK) && (zret != Z_STREAM_END) && (zret != Z_DATA_ERROR)) {
  164. av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
  165. return -1;
  166. }
  167. encoded = c->decomp_buf;
  168. len = c->decomp_size;
  169. if(zret != Z_DATA_ERROR)
  170. decode_rle(c);
  171. #else
  172. av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
  173. return -1;
  174. #endif
  175. *data_size = sizeof(AVFrame);
  176. *(AVFrame*)data = c->pic;
  177. /* always report that the buffer was completely consumed */
  178. return buf_size;
  179. }
  180. /*
  181. *
  182. * Init tscc decoder
  183. *
  184. */
  185. static int decode_init(AVCodecContext *avctx)
  186. {
  187. CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
  188. int zret; // Zlib return code
  189. c->avctx = avctx;
  190. avctx->has_b_frames = 0;
  191. c->pic.data[0] = NULL;
  192. c->height = avctx->height;
  193. #ifdef CONFIG_ZLIB
  194. // Needed if zlib unused or init aborted before inflateInit
  195. memset(&(c->zstream), 0, sizeof(z_stream));
  196. #else
  197. av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
  198. return 1;
  199. #endif
  200. switch(avctx->bits_per_sample){
  201. case 8: av_log(avctx, AV_LOG_ERROR, "Camtasia warning: RGB8 is just guessed\n");
  202. avctx->pix_fmt = PIX_FMT_PAL8;
  203. break;
  204. case 16: avctx->pix_fmt = PIX_FMT_RGB555;break;
  205. case 24: av_log(avctx, AV_LOG_ERROR, "Camtasia warning: RGB24 is just guessed\n");
  206. avctx->pix_fmt = PIX_FMT_BGR24;
  207. break;
  208. default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_sample);
  209. return -1;
  210. }
  211. c->bpp = avctx->bits_per_sample;
  212. av_log(avctx, AV_LOG_INFO, "bpp=%i \n\n\n", c->bpp);
  213. c->decomp_size = (avctx->width * c->bpp + (avctx->width + 254) / 255 + 2) * avctx->height + 2;//RLE in the best case
  214. /* Allocate decompression buffer */
  215. if (c->decomp_size) {
  216. if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
  217. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  218. return 1;
  219. }
  220. }
  221. #ifdef CONFIG_ZLIB
  222. c->zstream.zalloc = Z_NULL;
  223. c->zstream.zfree = Z_NULL;
  224. c->zstream.opaque = Z_NULL;
  225. zret = inflateInit(&(c->zstream));
  226. if (zret != Z_OK) {
  227. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  228. return 1;
  229. }
  230. #endif
  231. return 0;
  232. }
  233. /*
  234. *
  235. * Uninit tscc decoder
  236. *
  237. */
  238. static int decode_end(AVCodecContext *avctx)
  239. {
  240. CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
  241. if (c->pic.data[0])
  242. avctx->release_buffer(avctx, &c->pic);
  243. #ifdef CONFIG_ZLIB
  244. inflateEnd(&(c->zstream));
  245. #endif
  246. return 0;
  247. }
  248. AVCodec tscc_decoder = {
  249. "camtasia",
  250. CODEC_TYPE_VIDEO,
  251. CODEC_ID_TSCC,
  252. sizeof(CamtasiaContext),
  253. decode_init,
  254. NULL,
  255. decode_end,
  256. decode_frame,
  257. CODEC_CAP_DR1,
  258. };