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.

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