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.

303 lines
8.0KB

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