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.

346 lines
9.3KB

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