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.

193 lines
5.7KB

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