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.

214 lines
5.8KB

  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
  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. GetByteContext gb;
  54. int height;
  55. z_stream zstream;
  56. uint32_t pal[256];
  57. } CamtasiaContext;
  58. /*
  59. *
  60. * Decode a frame
  61. *
  62. */
  63. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
  64. {
  65. const uint8_t *buf = avpkt->data;
  66. int buf_size = avpkt->size;
  67. CamtasiaContext * const c = avctx->priv_data;
  68. const unsigned char *encoded = buf;
  69. int zret; // Zlib return code
  70. int ret, len = buf_size;
  71. c->pic.reference = 3;
  72. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
  73. if((ret = avctx->reget_buffer(avctx, &c->pic)) < 0){
  74. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  75. return ret;
  76. }
  77. zret = inflateReset(&c->zstream);
  78. if (zret != Z_OK) {
  79. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
  80. return AVERROR(EINVAL);
  81. }
  82. c->zstream.next_in = encoded;
  83. c->zstream.avail_in = len;
  84. c->zstream.next_out = c->decomp_buf;
  85. c->zstream.avail_out = c->decomp_size;
  86. zret = inflate(&c->zstream, Z_FINISH);
  87. // Z_DATA_ERROR means empty picture
  88. if ((zret != Z_OK) && (zret != Z_STREAM_END) && (zret != Z_DATA_ERROR)) {
  89. av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
  90. return AVERROR(EINVAL);
  91. }
  92. if (zret != Z_DATA_ERROR) {
  93. bytestream2_init(&c->gb, c->decomp_buf,
  94. c->decomp_size - c->zstream.avail_out);
  95. ff_msrle_decode(avctx, (AVPicture*)&c->pic, c->bpp, &c->gb);
  96. }
  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. avcodec_get_frame_defaults(&c->pic);
  123. // Needed if zlib unused or init aborted before inflateInit
  124. memset(&c->zstream, 0, sizeof(z_stream));
  125. switch(avctx->bits_per_coded_sample){
  126. case 8: avctx->pix_fmt = PIX_FMT_PAL8; break;
  127. case 16: avctx->pix_fmt = PIX_FMT_RGB555; break;
  128. case 24:
  129. avctx->pix_fmt = PIX_FMT_BGR24;
  130. break;
  131. case 32: avctx->pix_fmt = PIX_FMT_RGB32; break;
  132. default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_coded_sample);
  133. return AVERROR_PATCHWELCOME;
  134. }
  135. c->bpp = avctx->bits_per_coded_sample;
  136. // buffer size for RLE 'best' case when 2-byte code precedes each pixel and there may be padding after it too
  137. c->decomp_size = (((avctx->width * c->bpp + 7) >> 3) + 3 * avctx->width + 2) * avctx->height + 2;
  138. /* Allocate decompression buffer */
  139. if (c->decomp_size) {
  140. if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
  141. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  142. return AVERROR(ENOMEM);
  143. }
  144. }
  145. c->zstream.zalloc = Z_NULL;
  146. c->zstream.zfree = Z_NULL;
  147. c->zstream.opaque = Z_NULL;
  148. zret = inflateInit(&c->zstream);
  149. if (zret != Z_OK) {
  150. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  151. return AVERROR(ENOMEM);
  152. }
  153. return 0;
  154. }
  155. /*
  156. *
  157. * Uninit tscc decoder
  158. *
  159. */
  160. static av_cold int decode_end(AVCodecContext *avctx)
  161. {
  162. CamtasiaContext * const c = avctx->priv_data;
  163. av_freep(&c->decomp_buf);
  164. if (c->pic.data[0])
  165. avctx->release_buffer(avctx, &c->pic);
  166. inflateEnd(&c->zstream);
  167. return 0;
  168. }
  169. AVCodec ff_tscc_decoder = {
  170. .name = "camtasia",
  171. .type = AVMEDIA_TYPE_VIDEO,
  172. .id = AV_CODEC_ID_TSCC,
  173. .priv_data_size = sizeof(CamtasiaContext),
  174. .init = decode_init,
  175. .close = decode_end,
  176. .decode = decode_frame,
  177. .capabilities = CODEC_CAP_DR1,
  178. .long_name = NULL_IF_CONFIG_SMALL("TechSmith Screen Capture Codec"),
  179. };