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.

217 lines
5.6KB

  1. /*
  2. * TechSmith Camtasia decoder
  3. * Copyright (c) 2004 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  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. #include <zlib.h>
  41. /*
  42. * Decoder context
  43. */
  44. typedef struct TsccContext {
  45. AVCodecContext *avctx;
  46. AVFrame pic;
  47. // Bits per pixel
  48. int bpp;
  49. // Decompressed data size
  50. unsigned int decomp_size;
  51. // Decompression buffer
  52. unsigned char* decomp_buf;
  53. int height;
  54. z_stream zstream;
  55. uint32_t pal[256];
  56. } CamtasiaContext;
  57. /*
  58. *
  59. * Decode a frame
  60. *
  61. */
  62. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
  63. {
  64. const uint8_t *buf = avpkt->data;
  65. int buf_size = avpkt->size;
  66. CamtasiaContext * const c = avctx->priv_data;
  67. const unsigned char *encoded = buf;
  68. unsigned char *outptr;
  69. int zret; // Zlib return code
  70. int len = buf_size;
  71. if(c->pic.data[0])
  72. avctx->release_buffer(avctx, &c->pic);
  73. c->pic.reference = 1;
  74. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
  75. if(avctx->get_buffer(avctx, &c->pic) < 0){
  76. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  77. return -1;
  78. }
  79. outptr = c->pic.data[0]; // Output image pointer
  80. zret = inflateReset(&(c->zstream));
  81. if (zret != Z_OK) {
  82. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
  83. return -1;
  84. }
  85. c->zstream.next_in = encoded;
  86. c->zstream.avail_in = len;
  87. c->zstream.next_out = c->decomp_buf;
  88. c->zstream.avail_out = c->decomp_size;
  89. zret = inflate(&(c->zstream), Z_FINISH);
  90. // Z_DATA_ERROR means empty picture
  91. if ((zret != Z_OK) && (zret != Z_STREAM_END) && (zret != Z_DATA_ERROR)) {
  92. av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
  93. return -1;
  94. }
  95. if(zret != Z_DATA_ERROR)
  96. ff_msrle_decode(avctx, (AVPicture*)&c->pic, c->bpp, c->decomp_buf, c->decomp_size - c->zstream.avail_out);
  97. /* make the palette available on the way out */
  98. if (c->avctx->pix_fmt == PIX_FMT_PAL8) {
  99. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
  100. if (pal) {
  101. c->pic.palette_has_changed = 1;
  102. memcpy(c->pal, pal, AVPALETTE_SIZE);
  103. }
  104. memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
  105. }
  106. *data_size = sizeof(AVFrame);
  107. *(AVFrame*)data = c->pic;
  108. /* always report that the buffer was completely consumed */
  109. return buf_size;
  110. }
  111. /*
  112. *
  113. * Init tscc decoder
  114. *
  115. */
  116. static av_cold int decode_init(AVCodecContext *avctx)
  117. {
  118. CamtasiaContext * const c = avctx->priv_data;
  119. int zret; // Zlib return code
  120. c->avctx = avctx;
  121. c->height = avctx->height;
  122. // Needed if zlib unused or init aborted before inflateInit
  123. memset(&(c->zstream), 0, sizeof(z_stream));
  124. switch(avctx->bits_per_coded_sample){
  125. case 8: avctx->pix_fmt = PIX_FMT_PAL8; break;
  126. case 16: avctx->pix_fmt = PIX_FMT_RGB555; break;
  127. case 24:
  128. avctx->pix_fmt = PIX_FMT_BGR24;
  129. break;
  130. case 32: avctx->pix_fmt = PIX_FMT_RGB32; break;
  131. default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_coded_sample);
  132. return -1;
  133. }
  134. c->bpp = avctx->bits_per_coded_sample;
  135. // buffer size for RLE 'best' case when 2-byte code preceeds each pixel and there may be padding after it too
  136. c->decomp_size = (((avctx->width * c->bpp + 7) >> 3) + 3 * avctx->width + 2) * avctx->height + 2;
  137. /* Allocate decompression buffer */
  138. if (c->decomp_size) {
  139. if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
  140. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  141. return 1;
  142. }
  143. }
  144. c->zstream.zalloc = Z_NULL;
  145. c->zstream.zfree = Z_NULL;
  146. c->zstream.opaque = Z_NULL;
  147. zret = inflateInit(&(c->zstream));
  148. if (zret != Z_OK) {
  149. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  150. return 1;
  151. }
  152. return 0;
  153. }
  154. /*
  155. *
  156. * Uninit tscc decoder
  157. *
  158. */
  159. static av_cold int decode_end(AVCodecContext *avctx)
  160. {
  161. CamtasiaContext * const c = avctx->priv_data;
  162. av_freep(&c->decomp_buf);
  163. if (c->pic.data[0])
  164. avctx->release_buffer(avctx, &c->pic);
  165. inflateEnd(&(c->zstream));
  166. return 0;
  167. }
  168. AVCodec ff_tscc_decoder = {
  169. "camtasia",
  170. AVMEDIA_TYPE_VIDEO,
  171. CODEC_ID_TSCC,
  172. sizeof(CamtasiaContext),
  173. decode_init,
  174. NULL,
  175. decode_end,
  176. decode_frame,
  177. CODEC_CAP_DR1,
  178. .long_name = NULL_IF_CONFIG_SMALL("TechSmith Screen Capture Codec"),
  179. };