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.

307 lines
8.1KB

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