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.

318 lines
8.5KB

  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[3]; //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. }
  120. if (output + p1 * (c->bpp / 8) > output_end)
  121. continue;
  122. for(i = 0; i < p1; i++) {
  123. switch(c->bpp){
  124. case 8: *output++ = pix[0];
  125. break;
  126. case 16: *output++ = pix[0];
  127. *output++ = pix[1];
  128. break;
  129. case 24: *output++ = pix[0];
  130. *output++ = pix[1];
  131. *output++ = pix[2];
  132. break;
  133. }
  134. }
  135. pos += p1;
  136. }
  137. }
  138. av_log(c->avctx, AV_LOG_ERROR, "Camtasia warning: no End-of-picture code\n");
  139. return 1;
  140. }
  141. /*
  142. *
  143. * Decode a frame
  144. *
  145. */
  146. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
  147. {
  148. CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
  149. unsigned char *encoded = (unsigned char *)buf;
  150. unsigned char *outptr;
  151. #ifdef CONFIG_ZLIB
  152. int zret; // Zlib return code
  153. #endif
  154. int len = buf_size;
  155. if(c->pic.data[0])
  156. avctx->release_buffer(avctx, &c->pic);
  157. c->pic.reference = 1;
  158. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
  159. if(avctx->get_buffer(avctx, &c->pic) < 0){
  160. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  161. return -1;
  162. }
  163. outptr = c->pic.data[0]; // Output image pointer
  164. #ifdef CONFIG_ZLIB
  165. zret = inflateReset(&(c->zstream));
  166. if (zret != Z_OK) {
  167. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
  168. return -1;
  169. }
  170. c->zstream.next_in = encoded;
  171. c->zstream.avail_in = len;
  172. c->zstream.next_out = c->decomp_buf;
  173. c->zstream.avail_out = c->decomp_size;
  174. zret = inflate(&(c->zstream), Z_FINISH);
  175. // Z_DATA_ERROR means empty picture
  176. if ((zret != Z_OK) && (zret != Z_STREAM_END) && (zret != Z_DATA_ERROR)) {
  177. av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
  178. return -1;
  179. }
  180. if(zret != Z_DATA_ERROR)
  181. decode_rle(c, c->zstream.avail_out);
  182. /* make the palette available on the way out */
  183. if (c->avctx->pix_fmt == PIX_FMT_PAL8) {
  184. memcpy(c->pic.data[1], c->avctx->palctrl->palette, AVPALETTE_SIZE);
  185. if (c->avctx->palctrl->palette_changed) {
  186. c->pic.palette_has_changed = 1;
  187. c->avctx->palctrl->palette_changed = 0;
  188. }
  189. }
  190. #else
  191. av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
  192. return -1;
  193. #endif
  194. *data_size = sizeof(AVFrame);
  195. *(AVFrame*)data = c->pic;
  196. /* always report that the buffer was completely consumed */
  197. return buf_size;
  198. }
  199. /*
  200. *
  201. * Init tscc decoder
  202. *
  203. */
  204. static int decode_init(AVCodecContext *avctx)
  205. {
  206. CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
  207. int zret; // Zlib return code
  208. c->avctx = avctx;
  209. avctx->has_b_frames = 0;
  210. c->pic.data[0] = NULL;
  211. c->height = avctx->height;
  212. if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
  213. return 1;
  214. }
  215. #ifdef CONFIG_ZLIB
  216. // Needed if zlib unused or init aborted before inflateInit
  217. memset(&(c->zstream), 0, sizeof(z_stream));
  218. #else
  219. av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
  220. return 1;
  221. #endif
  222. switch(avctx->bits_per_sample){
  223. case 8: avctx->pix_fmt = PIX_FMT_PAL8; break;
  224. case 16: avctx->pix_fmt = PIX_FMT_RGB555; break;
  225. case 24: av_log(avctx, AV_LOG_ERROR, "Camtasia warning: RGB24 is just guessed\n");
  226. avctx->pix_fmt = PIX_FMT_BGR24;
  227. break;
  228. default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_sample);
  229. return -1;
  230. }
  231. c->bpp = avctx->bits_per_sample;
  232. c->decomp_size = (avctx->width * c->bpp + (avctx->width + 254) / 255 + 2) * avctx->height + 2;//RLE in the 'best' case
  233. /* Allocate decompression buffer */
  234. if (c->decomp_size) {
  235. if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
  236. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  237. return 1;
  238. }
  239. }
  240. #ifdef CONFIG_ZLIB
  241. c->zstream.zalloc = Z_NULL;
  242. c->zstream.zfree = Z_NULL;
  243. c->zstream.opaque = Z_NULL;
  244. zret = inflateInit(&(c->zstream));
  245. if (zret != Z_OK) {
  246. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  247. return 1;
  248. }
  249. #endif
  250. return 0;
  251. }
  252. /*
  253. *
  254. * Uninit tscc decoder
  255. *
  256. */
  257. static int decode_end(AVCodecContext *avctx)
  258. {
  259. CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
  260. if (c->pic.data[0])
  261. avctx->release_buffer(avctx, &c->pic);
  262. #ifdef CONFIG_ZLIB
  263. inflateEnd(&(c->zstream));
  264. #endif
  265. return 0;
  266. }
  267. AVCodec tscc_decoder = {
  268. "camtasia",
  269. CODEC_TYPE_VIDEO,
  270. CODEC_ID_TSCC,
  271. sizeof(CamtasiaContext),
  272. decode_init,
  273. NULL,
  274. decode_end,
  275. decode_frame,
  276. CODEC_CAP_DR1,
  277. };