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.

216 lines
5.6KB

  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 libavcodec/tscc.c
  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 "msrledec.h"
  40. #include <zlib.h>
  41. /*
  42. * Decoder context
  43. */
  44. typedef struct TsccContext {
  45. AVCodecContext *avctx;
  46. AVFrame pic;
  47. // Bits per pixel
  48. int bpp;
  49. // Decompressed data size
  50. unsigned int decomp_size;
  51. // Decompression buffer
  52. unsigned char* decomp_buf;
  53. int height;
  54. z_stream zstream;
  55. } CamtasiaContext;
  56. /*
  57. *
  58. * Decode a frame
  59. *
  60. */
  61. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
  62. {
  63. const uint8_t *buf = avpkt->data;
  64. int buf_size = avpkt->size;
  65. CamtasiaContext * const c = avctx->priv_data;
  66. const unsigned char *encoded = buf;
  67. unsigned char *outptr;
  68. int zret; // Zlib return code
  69. int len = buf_size;
  70. if(c->pic.data[0])
  71. avctx->release_buffer(avctx, &c->pic);
  72. c->pic.reference = 1;
  73. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
  74. if(avctx->get_buffer(avctx, &c->pic) < 0){
  75. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  76. return -1;
  77. }
  78. outptr = c->pic.data[0]; // Output image pointer
  79. zret = inflateReset(&(c->zstream));
  80. if (zret != Z_OK) {
  81. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
  82. return -1;
  83. }
  84. c->zstream.next_in = encoded;
  85. c->zstream.avail_in = len;
  86. c->zstream.next_out = c->decomp_buf;
  87. c->zstream.avail_out = c->decomp_size;
  88. zret = inflate(&(c->zstream), Z_FINISH);
  89. // Z_DATA_ERROR means empty picture
  90. if ((zret != Z_OK) && (zret != Z_STREAM_END) && (zret != Z_DATA_ERROR)) {
  91. av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
  92. return -1;
  93. }
  94. if(zret != Z_DATA_ERROR)
  95. ff_msrle_decode(avctx, (AVPicture*)&c->pic, c->bpp, c->decomp_buf, c->zstream.avail_out);
  96. /* make the palette available on the way out */
  97. if (c->avctx->pix_fmt == PIX_FMT_PAL8) {
  98. memcpy(c->pic.data[1], c->avctx->palctrl->palette, AVPALETTE_SIZE);
  99. if (c->avctx->palctrl->palette_changed) {
  100. c->pic.palette_has_changed = 1;
  101. c->avctx->palctrl->palette_changed = 0;
  102. }
  103. }
  104. *data_size = sizeof(AVFrame);
  105. *(AVFrame*)data = c->pic;
  106. /* always report that the buffer was completely consumed */
  107. return buf_size;
  108. }
  109. /*
  110. *
  111. * Init tscc decoder
  112. *
  113. */
  114. static av_cold int decode_init(AVCodecContext *avctx)
  115. {
  116. CamtasiaContext * const c = avctx->priv_data;
  117. int zret; // Zlib return code
  118. c->avctx = avctx;
  119. c->height = avctx->height;
  120. if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
  121. return 1;
  122. }
  123. // Needed if zlib unused or init aborted before inflateInit
  124. memset(&(c->zstream), 0, sizeof(z_stream));
  125. switch(avctx->bits_per_coded_sample){
  126. case 8: avctx->pix_fmt = PIX_FMT_PAL8; break;
  127. case 16: avctx->pix_fmt = PIX_FMT_RGB555; break;
  128. case 24:
  129. avctx->pix_fmt = PIX_FMT_BGR24;
  130. break;
  131. case 32: avctx->pix_fmt = PIX_FMT_RGB32; break;
  132. default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_coded_sample);
  133. return -1;
  134. }
  135. c->bpp = avctx->bits_per_coded_sample;
  136. c->decomp_size = (avctx->width * c->bpp + (avctx->width + 254) / 255 + 2) * avctx->height + 2;//RLE in the 'best' case
  137. /* Allocate decompression buffer */
  138. if (c->decomp_size) {
  139. if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
  140. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  141. return 1;
  142. }
  143. }
  144. c->zstream.zalloc = Z_NULL;
  145. c->zstream.zfree = Z_NULL;
  146. c->zstream.opaque = Z_NULL;
  147. zret = inflateInit(&(c->zstream));
  148. if (zret != Z_OK) {
  149. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  150. return 1;
  151. }
  152. return 0;
  153. }
  154. /*
  155. *
  156. * Uninit tscc decoder
  157. *
  158. */
  159. static av_cold int decode_end(AVCodecContext *avctx)
  160. {
  161. CamtasiaContext * const c = avctx->priv_data;
  162. av_freep(&c->decomp_buf);
  163. if (c->pic.data[0])
  164. avctx->release_buffer(avctx, &c->pic);
  165. inflateEnd(&(c->zstream));
  166. return 0;
  167. }
  168. AVCodec tscc_decoder = {
  169. "camtasia",
  170. CODEC_TYPE_VIDEO,
  171. CODEC_ID_TSCC,
  172. sizeof(CamtasiaContext),
  173. decode_init,
  174. NULL,
  175. decode_end,
  176. decode_frame,
  177. CODEC_CAP_DR1,
  178. .long_name = NULL_IF_CONFIG_SMALL("TechSmith Screen Capture Codec"),
  179. };