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.

386 lines
11KB

  1. /*
  2. * TechSmith Screen Codec 2 (aka Dora) decoder
  3. * Copyright (c) 2012 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 Screen Codec 2 decoder
  24. */
  25. #define BITSTREAM_READER_LE
  26. #include "avcodec.h"
  27. #include "get_bits.h"
  28. #include "bytestream.h"
  29. #include "internal.h"
  30. #include "tscc2data.h"
  31. typedef struct TSCC2Context {
  32. AVCodecContext *avctx;
  33. AVFrame *pic;
  34. int mb_width, mb_height;
  35. uint8_t *slice_quants;
  36. int quant[2];
  37. int q[2][3];
  38. GetBitContext gb;
  39. VLC dc_vlc, nc_vlc[NUM_VLC_SETS], ac_vlc[NUM_VLC_SETS];
  40. int block[16];
  41. } TSCC2Context;
  42. static av_cold void free_vlcs(TSCC2Context *c)
  43. {
  44. int i;
  45. ff_free_vlc(&c->dc_vlc);
  46. for (i = 0; i < NUM_VLC_SETS; i++) {
  47. ff_free_vlc(c->nc_vlc + i);
  48. ff_free_vlc(c->ac_vlc + i);
  49. }
  50. }
  51. static av_cold int init_vlcs(TSCC2Context *c)
  52. {
  53. int i, ret;
  54. ret = ff_init_vlc_sparse(&c->dc_vlc, 9, DC_VLC_COUNT,
  55. tscc2_dc_vlc_bits, 1, 1,
  56. tscc2_dc_vlc_codes, 2, 2,
  57. tscc2_dc_vlc_syms, 2, 2, INIT_VLC_LE);
  58. if (ret)
  59. return ret;
  60. for (i = 0; i < NUM_VLC_SETS; i++) {
  61. ret = ff_init_vlc_sparse(c->nc_vlc + i, 9, 16,
  62. tscc2_nc_vlc_bits[i], 1, 1,
  63. tscc2_nc_vlc_codes[i], 2, 2,
  64. tscc2_nc_vlc_syms, 1, 1, INIT_VLC_LE);
  65. if (ret) {
  66. free_vlcs(c);
  67. return ret;
  68. }
  69. ret = ff_init_vlc_sparse(c->ac_vlc + i, 9, tscc2_ac_vlc_sizes[i],
  70. tscc2_ac_vlc_bits[i], 1, 1,
  71. tscc2_ac_vlc_codes[i], 2, 2,
  72. tscc2_ac_vlc_syms[i], 2, 2, INIT_VLC_LE);
  73. if (ret) {
  74. free_vlcs(c);
  75. return ret;
  76. }
  77. }
  78. return 0;
  79. }
  80. #define DEQUANT(val, q) ((q * val + 0x80) >> 8)
  81. #define DCT1D(d0, d1, d2, d3, s0, s1, s2, s3, OP) \
  82. OP(d0, 5 * ((s0) + (s1) + (s2)) + 2 * (s3)); \
  83. OP(d1, 5 * ((s0) - (s2) - (s3)) + 2 * (s1)); \
  84. OP(d2, 5 * ((s0) - (s2) + (s3)) - 2 * (s1)); \
  85. OP(d3, 5 * ((s0) - (s1) + (s2)) - 2 * (s3)); \
  86. #define COL_OP(a, b) a = b
  87. #define ROW_OP(a, b) a = ((b) + 0x20) >> 6
  88. static void tscc2_idct4_put(int *in, int q[3], uint8_t *dst, int stride)
  89. {
  90. int i;
  91. int tblk[4 * 4];
  92. int t0, t1, t2, t3;
  93. for (i = 0; i < 4; i++) {
  94. t0 = DEQUANT(q[0 + (i & 1)], in[0 * 4 + i]);
  95. t1 = DEQUANT(q[1 + (i & 1)], in[1 * 4 + i]);
  96. t2 = DEQUANT(q[0 + (i & 1)], in[2 * 4 + i]);
  97. t3 = DEQUANT(q[1 + (i & 1)], in[3 * 4 + i]);
  98. DCT1D(tblk[0 * 4 + i], tblk[1 * 4 + i],
  99. tblk[2 * 4 + i], tblk[3 * 4 + i],
  100. t0, t1, t2, t3, COL_OP);
  101. }
  102. for (i = 0; i < 4; i++) {
  103. DCT1D(dst[0], dst[1], dst[2], dst[3],
  104. tblk[i * 4 + 0], tblk[i * 4 + 1],
  105. tblk[i * 4 + 2], tblk[i * 4 + 3], ROW_OP);
  106. dst += stride;
  107. }
  108. }
  109. static int tscc2_decode_mb(TSCC2Context *c, int *q, int vlc_set,
  110. uint8_t *dst, int stride, int plane)
  111. {
  112. GetBitContext *gb = &c->gb;
  113. int prev_dc, dc, nc, ac, bpos, val;
  114. int i, j, k, l;
  115. if (get_bits1(gb)) {
  116. if (get_bits1(gb)) {
  117. val = get_bits(gb, 8);
  118. for (i = 0; i < 8; i++, dst += stride)
  119. memset(dst, val, 16);
  120. } else {
  121. if (get_bits_left(gb) < 16 * 8 * 8)
  122. return AVERROR_INVALIDDATA;
  123. for (i = 0; i < 8; i++) {
  124. for (j = 0; j < 16; j++)
  125. dst[j] = get_bits(gb, 8);
  126. dst += stride;
  127. }
  128. }
  129. return 0;
  130. }
  131. prev_dc = 0;
  132. for (j = 0; j < 2; j++) {
  133. for (k = 0; k < 4; k++) {
  134. if (!(j | k)) {
  135. dc = get_bits(gb, 8);
  136. } else {
  137. dc = get_vlc2(gb, c->dc_vlc.table, 9, 2);
  138. if (dc == -1)
  139. return AVERROR_INVALIDDATA;
  140. if (dc == 0x100)
  141. dc = get_bits(gb, 8);
  142. }
  143. dc = (dc + prev_dc) & 0xFF;
  144. prev_dc = dc;
  145. c->block[0] = dc;
  146. nc = get_vlc2(gb, c->nc_vlc[vlc_set].table, 9, 1);
  147. if (nc == -1)
  148. return AVERROR_INVALIDDATA;
  149. bpos = 1;
  150. memset(c->block + 1, 0, 15 * sizeof(*c->block));
  151. for (l = 0; l < nc; l++) {
  152. ac = get_vlc2(gb, c->ac_vlc[vlc_set].table, 9, 2);
  153. if (ac == -1)
  154. return AVERROR_INVALIDDATA;
  155. if (ac == 0x1000)
  156. ac = get_bits(gb, 12);
  157. bpos += ac & 0xF;
  158. if (bpos >= 16)
  159. return AVERROR_INVALIDDATA;
  160. val = sign_extend(ac >> 4, 8);
  161. c->block[tscc2_zigzag[bpos++]] = val;
  162. }
  163. tscc2_idct4_put(c->block, q, dst + k * 4, stride);
  164. }
  165. dst += 4 * stride;
  166. }
  167. return 0;
  168. }
  169. static int tscc2_decode_slice(TSCC2Context *c, int mb_y,
  170. const uint8_t *buf, int buf_size)
  171. {
  172. int i, mb_x, q, ret;
  173. int off;
  174. init_get_bits(&c->gb, buf, buf_size * 8);
  175. for (mb_x = 0; mb_x < c->mb_width; mb_x++) {
  176. q = c->slice_quants[mb_x + c->mb_width * mb_y];
  177. if (q == 0 || q == 3) // skip block
  178. continue;
  179. for (i = 0; i < 3; i++) {
  180. off = mb_x * 16 + mb_y * 8 * c->pic->linesize[i];
  181. ret = tscc2_decode_mb(c, c->q[q - 1], c->quant[q - 1] - 2,
  182. c->pic->data[i] + off, c->pic->linesize[i], i);
  183. if (ret)
  184. return ret;
  185. }
  186. }
  187. return 0;
  188. }
  189. static int tscc2_decode_frame(AVCodecContext *avctx, void *data,
  190. int *got_frame, AVPacket *avpkt)
  191. {
  192. const uint8_t *buf = avpkt->data;
  193. int buf_size = avpkt->size;
  194. TSCC2Context *c = avctx->priv_data;
  195. GetByteContext gb;
  196. uint32_t frame_type, size;
  197. int i, val, len, pos = 0;
  198. int num_mb = c->mb_width * c->mb_height;
  199. int ret;
  200. bytestream2_init(&gb, buf, buf_size);
  201. frame_type = bytestream2_get_byte(&gb);
  202. if (frame_type > 1) {
  203. av_log(avctx, AV_LOG_ERROR, "Incorrect frame type %d\n", frame_type);
  204. return AVERROR_INVALIDDATA;
  205. }
  206. if ((ret = ff_reget_buffer(avctx, c->pic)) < 0) {
  207. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  208. return ret;
  209. }
  210. if (frame_type == 0) {
  211. *got_frame = 1;
  212. if ((ret = av_frame_ref(data, c->pic)) < 0)
  213. return ret;
  214. return buf_size;
  215. }
  216. if (bytestream2_get_bytes_left(&gb) < 4) {
  217. av_log(avctx, AV_LOG_ERROR, "Frame is too short\n");
  218. return AVERROR_INVALIDDATA;
  219. }
  220. c->quant[0] = bytestream2_get_byte(&gb);
  221. c->quant[1] = bytestream2_get_byte(&gb);
  222. if (c->quant[0] < 2 || c->quant[0] > NUM_VLC_SETS + 1 ||
  223. c->quant[1] < 2 || c->quant[1] > NUM_VLC_SETS + 1) {
  224. av_log(avctx, AV_LOG_ERROR, "Invalid quantisers %d / %d\n",
  225. c->quant[0], c->quant[1]);
  226. return AVERROR_INVALIDDATA;
  227. }
  228. for (i = 0; i < 3; i++) {
  229. c->q[0][i] = tscc2_quants[c->quant[0] - 2][i];
  230. c->q[1][i] = tscc2_quants[c->quant[1] - 2][i];
  231. }
  232. bytestream2_skip(&gb, 1);
  233. size = bytestream2_get_le32(&gb);
  234. if (size > bytestream2_get_bytes_left(&gb)) {
  235. av_log(avctx, AV_LOG_ERROR, "Slice properties chunk is too large\n");
  236. return AVERROR_INVALIDDATA;
  237. }
  238. for (i = 0; i < size; i++) {
  239. val = bytestream2_get_byte(&gb);
  240. len = val & 0x3F;
  241. val >>= 6;
  242. if (pos + len > num_mb) {
  243. av_log(avctx, AV_LOG_ERROR, "Too many slice properties\n");
  244. return AVERROR_INVALIDDATA;
  245. }
  246. memset(c->slice_quants + pos, val, len);
  247. pos += len;
  248. }
  249. if (pos < num_mb) {
  250. av_log(avctx, AV_LOG_ERROR, "Too few slice properties (%d / %d)\n",
  251. pos, num_mb);
  252. return AVERROR_INVALIDDATA;
  253. }
  254. for (i = 0; i < c->mb_height; i++) {
  255. size = bytestream2_peek_byte(&gb);
  256. if (size & 1) {
  257. size = bytestream2_get_byte(&gb) - 1;
  258. } else {
  259. size = bytestream2_get_le32(&gb) >> 1;
  260. }
  261. if (!size) {
  262. int skip_row = 1, j, off = i * c->mb_width;
  263. for (j = 0; j < c->mb_width; j++) {
  264. if (c->slice_quants[off + j] == 1 ||
  265. c->slice_quants[off + j] == 2) {
  266. skip_row = 0;
  267. break;
  268. }
  269. }
  270. if (!skip_row) {
  271. av_log(avctx, AV_LOG_ERROR, "Non-skip row with zero size\n");
  272. return AVERROR_INVALIDDATA;
  273. }
  274. }
  275. if (bytestream2_get_bytes_left(&gb) < size) {
  276. av_log(avctx, AV_LOG_ERROR, "Invalid slice size (%d/%d)\n",
  277. size, bytestream2_get_bytes_left(&gb));
  278. return AVERROR_INVALIDDATA;
  279. }
  280. ret = tscc2_decode_slice(c, i, buf + bytestream2_tell(&gb), size);
  281. if (ret) {
  282. av_log(avctx, AV_LOG_ERROR, "Error decoding slice %d\n", i);
  283. return ret;
  284. }
  285. bytestream2_skip(&gb, size);
  286. }
  287. *got_frame = 1;
  288. if ((ret = av_frame_ref(data, c->pic)) < 0)
  289. return ret;
  290. /* always report that the buffer was completely consumed */
  291. return buf_size;
  292. }
  293. static av_cold int tscc2_decode_end(AVCodecContext *avctx)
  294. {
  295. TSCC2Context * const c = avctx->priv_data;
  296. av_frame_free(&c->pic);
  297. av_freep(&c->slice_quants);
  298. free_vlcs(c);
  299. return 0;
  300. }
  301. static av_cold int tscc2_decode_init(AVCodecContext *avctx)
  302. {
  303. TSCC2Context * const c = avctx->priv_data;
  304. int ret;
  305. c->avctx = avctx;
  306. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  307. if ((ret = init_vlcs(c)) < 0) {
  308. av_log(avctx, AV_LOG_ERROR, "Cannot initialise VLCs\n");
  309. return ret;
  310. }
  311. c->mb_width = FFALIGN(avctx->width, 16) >> 4;
  312. c->mb_height = FFALIGN(avctx->height, 8) >> 3;
  313. c->slice_quants = av_malloc(c->mb_width * c->mb_height);
  314. if (!c->slice_quants) {
  315. av_log(avctx, AV_LOG_ERROR, "Cannot allocate slice information\n");
  316. free_vlcs(c);
  317. return AVERROR(ENOMEM);
  318. }
  319. c->pic = av_frame_alloc();
  320. if (!c->pic) {
  321. tscc2_decode_end(avctx);
  322. return AVERROR(ENOMEM);
  323. }
  324. return 0;
  325. }
  326. AVCodec ff_tscc2_decoder = {
  327. .name = "tscc2",
  328. .long_name = NULL_IF_CONFIG_SMALL("TechSmith Screen Codec 2"),
  329. .type = AVMEDIA_TYPE_VIDEO,
  330. .id = AV_CODEC_ID_TSCC2,
  331. .priv_data_size = sizeof(TSCC2Context),
  332. .init = tscc2_decode_init,
  333. .close = tscc2_decode_end,
  334. .decode = tscc2_decode_frame,
  335. .capabilities = CODEC_CAP_DR1,
  336. };