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.

218 lines
5.8KB

  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 "internal.h"
  40. #include "msrledec.h"
  41. #include <zlib.h>
  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. GetByteContext gb;
  55. int height;
  56. z_stream zstream;
  57. uint32_t pal[256];
  58. } CamtasiaContext;
  59. /*
  60. *
  61. * Decode a frame
  62. *
  63. */
  64. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  65. AVPacket *avpkt)
  66. {
  67. const uint8_t *buf = avpkt->data;
  68. int buf_size = avpkt->size;
  69. CamtasiaContext * const c = avctx->priv_data;
  70. const unsigned char *encoded = buf;
  71. int zret; // Zlib return code
  72. int len = buf_size;
  73. if(c->pic.data[0])
  74. avctx->release_buffer(avctx, &c->pic);
  75. c->pic.reference = 1;
  76. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
  77. if(ff_get_buffer(avctx, &c->pic) < 0){
  78. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  79. return -1;
  80. }
  81. zret = inflateReset(&c->zstream);
  82. if (zret != Z_OK) {
  83. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
  84. return -1;
  85. }
  86. c->zstream.next_in = encoded;
  87. c->zstream.avail_in = len;
  88. c->zstream.next_out = c->decomp_buf;
  89. c->zstream.avail_out = c->decomp_size;
  90. zret = inflate(&c->zstream, Z_FINISH);
  91. // Z_DATA_ERROR means empty picture
  92. if ((zret != Z_OK) && (zret != Z_STREAM_END) && (zret != Z_DATA_ERROR)) {
  93. av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
  94. return -1;
  95. }
  96. if (zret != Z_DATA_ERROR) {
  97. bytestream2_init(&c->gb, c->decomp_buf,
  98. c->decomp_size - c->zstream.avail_out);
  99. ff_msrle_decode(avctx, (AVPicture*)&c->pic, c->bpp, &c->gb);
  100. }
  101. /* make the palette available on the way out */
  102. if (c->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  103. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
  104. if (pal) {
  105. c->pic.palette_has_changed = 1;
  106. memcpy(c->pal, pal, AVPALETTE_SIZE);
  107. }
  108. memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
  109. }
  110. *got_frame = 1;
  111. *(AVFrame*)data = c->pic;
  112. /* always report that the buffer was completely consumed */
  113. return buf_size;
  114. }
  115. /*
  116. *
  117. * Init tscc decoder
  118. *
  119. */
  120. static av_cold int decode_init(AVCodecContext *avctx)
  121. {
  122. CamtasiaContext * const c = avctx->priv_data;
  123. int zret; // Zlib return code
  124. c->avctx = avctx;
  125. c->height = avctx->height;
  126. // Needed if zlib unused or init aborted before inflateInit
  127. memset(&c->zstream, 0, sizeof(z_stream));
  128. switch(avctx->bits_per_coded_sample){
  129. case 8: avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
  130. case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
  131. case 24:
  132. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  133. break;
  134. case 32: avctx->pix_fmt = AV_PIX_FMT_RGB32; break;
  135. default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_coded_sample);
  136. return -1;
  137. }
  138. c->bpp = avctx->bits_per_coded_sample;
  139. // buffer size for RLE 'best' case when 2-byte code precedes each pixel and there may be padding after it too
  140. c->decomp_size = (((avctx->width * c->bpp + 7) >> 3) + 3 * avctx->width + 2) * avctx->height + 2;
  141. /* Allocate decompression buffer */
  142. if (c->decomp_size) {
  143. if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
  144. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  145. return 1;
  146. }
  147. }
  148. c->zstream.zalloc = Z_NULL;
  149. c->zstream.zfree = Z_NULL;
  150. c->zstream.opaque = Z_NULL;
  151. zret = inflateInit(&c->zstream);
  152. if (zret != Z_OK) {
  153. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  154. return 1;
  155. }
  156. return 0;
  157. }
  158. /*
  159. *
  160. * Uninit tscc decoder
  161. *
  162. */
  163. static av_cold int decode_end(AVCodecContext *avctx)
  164. {
  165. CamtasiaContext * const c = avctx->priv_data;
  166. av_freep(&c->decomp_buf);
  167. if (c->pic.data[0])
  168. avctx->release_buffer(avctx, &c->pic);
  169. inflateEnd(&c->zstream);
  170. return 0;
  171. }
  172. AVCodec ff_tscc_decoder = {
  173. .name = "camtasia",
  174. .type = AVMEDIA_TYPE_VIDEO,
  175. .id = AV_CODEC_ID_TSCC,
  176. .priv_data_size = sizeof(CamtasiaContext),
  177. .init = decode_init,
  178. .close = decode_end,
  179. .decode = decode_frame,
  180. .capabilities = CODEC_CAP_DR1,
  181. .long_name = NULL_IF_CONFIG_SMALL("TechSmith Screen Capture Codec"),
  182. };