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.

189 lines
5.5KB

  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 *frame;
  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. AVFrame *frame = c->frame;
  63. int ret;
  64. if ((ret = ff_reget_buffer(avctx, frame)) < 0)
  65. return ret;
  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, (AVPicture*)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. if ((ret = av_frame_ref(data, frame)) < 0)
  96. return ret;
  97. *got_frame = 1;
  98. /* always report that the buffer was completely consumed */
  99. return buf_size;
  100. }
  101. static av_cold int decode_init(AVCodecContext *avctx)
  102. {
  103. CamtasiaContext * const c = avctx->priv_data;
  104. int zret; // Zlib return code
  105. c->avctx = avctx;
  106. c->height = avctx->height;
  107. // Needed if zlib unused or init aborted before inflateInit
  108. memset(&c->zstream, 0, sizeof(z_stream));
  109. switch(avctx->bits_per_coded_sample){
  110. case 8: avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
  111. case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
  112. case 24:
  113. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  114. break;
  115. case 32: avctx->pix_fmt = AV_PIX_FMT_RGB32; break;
  116. default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_coded_sample);
  117. return AVERROR_PATCHWELCOME;
  118. }
  119. c->bpp = avctx->bits_per_coded_sample;
  120. // buffer size for RLE 'best' case when 2-byte code precedes each pixel and there may be padding after it too
  121. c->decomp_size = (((avctx->width * c->bpp + 7) >> 3) + 3 * avctx->width + 2) * avctx->height + 2;
  122. /* Allocate decompression buffer */
  123. if (c->decomp_size) {
  124. if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
  125. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  126. return AVERROR(ENOMEM);
  127. }
  128. }
  129. c->zstream.zalloc = Z_NULL;
  130. c->zstream.zfree = Z_NULL;
  131. c->zstream.opaque = Z_NULL;
  132. zret = inflateInit(&c->zstream);
  133. if (zret != Z_OK) {
  134. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  135. return AVERROR_UNKNOWN;
  136. }
  137. c->frame = av_frame_alloc();
  138. return 0;
  139. }
  140. static av_cold int decode_end(AVCodecContext *avctx)
  141. {
  142. CamtasiaContext * const c = avctx->priv_data;
  143. av_freep(&c->decomp_buf);
  144. av_frame_free(&c->frame);
  145. inflateEnd(&c->zstream);
  146. return 0;
  147. }
  148. AVCodec ff_tscc_decoder = {
  149. .name = "camtasia",
  150. .long_name = NULL_IF_CONFIG_SMALL("TechSmith Screen Capture Codec"),
  151. .type = AVMEDIA_TYPE_VIDEO,
  152. .id = AV_CODEC_ID_TSCC,
  153. .priv_data_size = sizeof(CamtasiaContext),
  154. .init = decode_init,
  155. .close = decode_end,
  156. .decode = decode_frame,
  157. .capabilities = CODEC_CAP_DR1,
  158. };