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.

236 lines
5.9KB

  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. #include "msrledec.h"
  40. #ifdef CONFIG_ZLIB
  41. #include <zlib.h>
  42. #endif
  43. /*
  44. * Decoder context
  45. */
  46. typedef struct TsccContext {
  47. AVCodecContext *avctx;
  48. AVFrame pic;
  49. // Bits per pixel
  50. int bpp;
  51. // Decompressed data size
  52. unsigned int decomp_size;
  53. // Decompression buffer
  54. unsigned char* decomp_buf;
  55. int height;
  56. #ifdef CONFIG_ZLIB
  57. z_stream zstream;
  58. #endif
  59. } CamtasiaContext;
  60. /*
  61. *
  62. * Decode a frame
  63. *
  64. */
  65. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, const uint8_t *buf, int buf_size)
  66. {
  67. CamtasiaContext * const c = avctx->priv_data;
  68. const unsigned char *encoded = buf;
  69. unsigned char *outptr;
  70. #ifdef CONFIG_ZLIB
  71. int zret; // Zlib return code
  72. #endif
  73. int len = buf_size;
  74. if(c->pic.data[0])
  75. avctx->release_buffer(avctx, &c->pic);
  76. c->pic.reference = 1;
  77. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
  78. if(avctx->get_buffer(avctx, &c->pic) < 0){
  79. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  80. return -1;
  81. }
  82. outptr = c->pic.data[0]; // Output image pointer
  83. #ifdef CONFIG_ZLIB
  84. zret = inflateReset(&(c->zstream));
  85. if (zret != Z_OK) {
  86. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
  87. return -1;
  88. }
  89. c->zstream.next_in = encoded;
  90. c->zstream.avail_in = len;
  91. c->zstream.next_out = c->decomp_buf;
  92. c->zstream.avail_out = c->decomp_size;
  93. zret = inflate(&(c->zstream), Z_FINISH);
  94. // Z_DATA_ERROR means empty picture
  95. if ((zret != Z_OK) && (zret != Z_STREAM_END) && (zret != Z_DATA_ERROR)) {
  96. av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
  97. return -1;
  98. }
  99. if(zret != Z_DATA_ERROR)
  100. ff_msrle_decode(avctx, &c->pic, c->bpp, c->decomp_buf, c->zstream.avail_out);
  101. /* make the palette available on the way out */
  102. if (c->avctx->pix_fmt == PIX_FMT_PAL8) {
  103. memcpy(c->pic.data[1], c->avctx->palctrl->palette, AVPALETTE_SIZE);
  104. if (c->avctx->palctrl->palette_changed) {
  105. c->pic.palette_has_changed = 1;
  106. c->avctx->palctrl->palette_changed = 0;
  107. }
  108. }
  109. #else
  110. av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
  111. return -1;
  112. #endif
  113. *data_size = sizeof(AVFrame);
  114. *(AVFrame*)data = c->pic;
  115. /* always report that the buffer was completely consumed */
  116. return buf_size;
  117. }
  118. /*
  119. *
  120. * Init tscc decoder
  121. *
  122. */
  123. static av_cold int decode_init(AVCodecContext *avctx)
  124. {
  125. CamtasiaContext * const c = avctx->priv_data;
  126. int zret; // Zlib return code
  127. c->avctx = avctx;
  128. c->pic.data[0] = NULL;
  129. c->height = avctx->height;
  130. if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
  131. return 1;
  132. }
  133. #ifdef CONFIG_ZLIB
  134. // Needed if zlib unused or init aborted before inflateInit
  135. memset(&(c->zstream), 0, sizeof(z_stream));
  136. #else
  137. av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
  138. return 1;
  139. #endif
  140. switch(avctx->bits_per_coded_sample){
  141. case 8: avctx->pix_fmt = PIX_FMT_PAL8; break;
  142. case 16: avctx->pix_fmt = PIX_FMT_RGB555; break;
  143. case 24:
  144. avctx->pix_fmt = PIX_FMT_BGR24;
  145. break;
  146. case 32: avctx->pix_fmt = PIX_FMT_RGB32; break;
  147. default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_coded_sample);
  148. return -1;
  149. }
  150. c->bpp = avctx->bits_per_coded_sample;
  151. c->decomp_size = (avctx->width * c->bpp + (avctx->width + 254) / 255 + 2) * avctx->height + 2;//RLE in the 'best' case
  152. /* Allocate decompression buffer */
  153. if (c->decomp_size) {
  154. if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
  155. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  156. return 1;
  157. }
  158. }
  159. #ifdef CONFIG_ZLIB
  160. c->zstream.zalloc = Z_NULL;
  161. c->zstream.zfree = Z_NULL;
  162. c->zstream.opaque = Z_NULL;
  163. zret = inflateInit(&(c->zstream));
  164. if (zret != Z_OK) {
  165. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  166. return 1;
  167. }
  168. #endif
  169. return 0;
  170. }
  171. /*
  172. *
  173. * Uninit tscc decoder
  174. *
  175. */
  176. static av_cold int decode_end(AVCodecContext *avctx)
  177. {
  178. CamtasiaContext * const c = avctx->priv_data;
  179. av_freep(&c->decomp_buf);
  180. if (c->pic.data[0])
  181. avctx->release_buffer(avctx, &c->pic);
  182. #ifdef CONFIG_ZLIB
  183. inflateEnd(&(c->zstream));
  184. #endif
  185. return 0;
  186. }
  187. AVCodec tscc_decoder = {
  188. "camtasia",
  189. CODEC_TYPE_VIDEO,
  190. CODEC_ID_TSCC,
  191. sizeof(CamtasiaContext),
  192. decode_init,
  193. NULL,
  194. decode_end,
  195. decode_frame,
  196. CODEC_CAP_DR1,
  197. .long_name = NULL_IF_CONFIG_SMALL("TechSmith Screen Capture Codec"),
  198. };