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.

202 lines
5.9KB

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