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.

184 lines
5.4KB

  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. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include "avcodec.h"
  38. #include "internal.h"
  39. #include "msrledec.h"
  40. #include <zlib.h>
  41. typedef struct TsccContext {
  42. AVCodecContext *avctx;
  43. // Bits per pixel
  44. int bpp;
  45. // Decompressed data size
  46. unsigned int decomp_size;
  47. // Decompression buffer
  48. unsigned char* decomp_buf;
  49. GetByteContext gb;
  50. int height;
  51. z_stream zstream;
  52. uint32_t pal[256];
  53. } CamtasiaContext;
  54. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  55. AVPacket *avpkt)
  56. {
  57. const uint8_t *buf = avpkt->data;
  58. int buf_size = avpkt->size;
  59. CamtasiaContext * const c = avctx->priv_data;
  60. AVFrame *frame = data;
  61. int ret;
  62. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0){
  63. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  64. return ret;
  65. }
  66. ret = inflateReset(&c->zstream);
  67. if (ret != Z_OK) {
  68. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
  69. return AVERROR_UNKNOWN;
  70. }
  71. c->zstream.next_in = buf;
  72. c->zstream.avail_in = buf_size;
  73. c->zstream.next_out = c->decomp_buf;
  74. c->zstream.avail_out = c->decomp_size;
  75. ret = inflate(&c->zstream, Z_FINISH);
  76. // Z_DATA_ERROR means empty picture
  77. if ((ret != Z_OK) && (ret != Z_STREAM_END) && (ret != Z_DATA_ERROR)) {
  78. av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret);
  79. return AVERROR_UNKNOWN;
  80. }
  81. if (ret != Z_DATA_ERROR) {
  82. bytestream2_init(&c->gb, c->decomp_buf,
  83. c->decomp_size - c->zstream.avail_out);
  84. ff_msrle_decode(avctx, frame, c->bpp, &c->gb);
  85. }
  86. /* make the palette available on the way out */
  87. if (c->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  88. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
  89. if (pal) {
  90. frame->palette_has_changed = 1;
  91. memcpy(c->pal, pal, AVPALETTE_SIZE);
  92. }
  93. memcpy(frame->data[1], c->pal, AVPALETTE_SIZE);
  94. }
  95. *got_frame = 1;
  96. /* always report that the buffer was completely consumed */
  97. return buf_size;
  98. }
  99. static av_cold int decode_init(AVCodecContext *avctx)
  100. {
  101. CamtasiaContext * const c = avctx->priv_data;
  102. int zret; // Zlib return code
  103. c->avctx = avctx;
  104. c->height = avctx->height;
  105. // Needed if zlib unused or init aborted before inflateInit
  106. memset(&c->zstream, 0, sizeof(z_stream));
  107. switch(avctx->bits_per_coded_sample){
  108. case 8: avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
  109. case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
  110. case 24:
  111. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  112. break;
  113. case 32: avctx->pix_fmt = AV_PIX_FMT_RGB32; break;
  114. default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_coded_sample);
  115. return AVERROR_INVALIDDATA;
  116. }
  117. c->bpp = avctx->bits_per_coded_sample;
  118. // buffer size for RLE 'best' case when 2-byte code precedes each pixel and there may be padding after it too
  119. c->decomp_size = (((avctx->width * c->bpp + 7) >> 3) + 3 * avctx->width + 2) * avctx->height + 2;
  120. /* Allocate decompression buffer */
  121. if (c->decomp_size) {
  122. if (!(c->decomp_buf = av_malloc(c->decomp_size))) {
  123. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  124. return AVERROR(ENOMEM);
  125. }
  126. }
  127. c->zstream.zalloc = Z_NULL;
  128. c->zstream.zfree = Z_NULL;
  129. c->zstream.opaque = Z_NULL;
  130. zret = inflateInit(&c->zstream);
  131. if (zret != Z_OK) {
  132. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  133. return AVERROR_UNKNOWN;
  134. }
  135. return 0;
  136. }
  137. static av_cold int decode_end(AVCodecContext *avctx)
  138. {
  139. CamtasiaContext * const c = avctx->priv_data;
  140. av_freep(&c->decomp_buf);
  141. inflateEnd(&c->zstream);
  142. return 0;
  143. }
  144. AVCodec ff_tscc_decoder = {
  145. .name = "camtasia",
  146. .long_name = NULL_IF_CONFIG_SMALL("TechSmith Screen Capture Codec"),
  147. .type = AVMEDIA_TYPE_VIDEO,
  148. .id = AV_CODEC_ID_TSCC,
  149. .priv_data_size = sizeof(CamtasiaContext),
  150. .init = decode_init,
  151. .close = decode_end,
  152. .decode = decode_frame,
  153. .capabilities = AV_CODEC_CAP_DR1,
  154. };