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.

393 lines
13KB

  1. /*
  2. * ClearVideo 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. * ClearVideo decoder
  24. */
  25. #include "avcodec.h"
  26. #include "bitstream.h"
  27. #include "bytestream.h"
  28. #include "idctdsp.h"
  29. #include "internal.h"
  30. #include "vlc.h"
  31. #define NUM_DC_CODES 127
  32. #define NUM_AC_CODES 103
  33. static const uint8_t clv_dc_codes[NUM_DC_CODES] = {
  34. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  35. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  36. 0x10, 0x11, 0x12, 0x13, 0x13, 0x14, 0x07, 0x0B,
  37. 0x0C, 0x08, 0x08, 0x09, 0x04, 0x06, 0x07, 0x05,
  38. 0x04, 0x05, 0x04, 0x06, 0x05, 0x06, 0x07, 0x05,
  39. 0x06, 0x07, 0x06, 0x07, 0x08, 0x06, 0x07, 0x08,
  40. 0x09, 0x0A, 0x0B, 0x07, 0x08, 0x09, 0x07, 0x08,
  41. 0x06, 0x07, 0x08, 0x06, 0x04, 0x05, 0x02, 0x01,
  42. 0x03, 0x06, 0x07, 0x07, 0x09, 0x0A, 0x0B, 0x09,
  43. 0x0A, 0x0B, 0x0A, 0x0B, 0x0C, 0x0D, 0x0C, 0x09,
  44. 0x0D, 0x0A, 0x0B, 0x08, 0x09, 0x0A, 0x0B, 0x07,
  45. 0x08, 0x09, 0x0A, 0x0B, 0x06, 0x07, 0x06, 0x08,
  46. 0x07, 0x09, 0x0A, 0x0B, 0x09, 0x0A, 0x0B, 0x0C,
  47. 0x14, 0x0D, 0x0D, 0x0E, 0x0F, 0x15, 0x15, 0x16,
  48. 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E,
  49. 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25,
  50. };
  51. static const uint8_t clv_dc_bits[NUM_DC_CODES] = {
  52. 22, 22, 22, 22, 22, 22, 22, 22,
  53. 22, 22, 22, 22, 22, 22, 22, 22,
  54. 22, 22, 22, 21, 22, 22, 19, 20,
  55. 20, 19, 18, 18, 15, 17, 17, 16,
  56. 14, 15, 12, 13, 14, 14, 14, 12,
  57. 12, 12, 11, 11, 11, 10, 10, 10,
  58. 10, 10, 10, 9, 9, 9, 8, 8,
  59. 7, 7, 7, 6, 5, 5, 3, 1,
  60. 3, 5, 5, 6, 7, 7, 7, 8,
  61. 8, 8, 9, 9, 9, 9, 10, 11,
  62. 10, 11, 11, 12, 12, 12, 12, 13,
  63. 14, 14, 14, 14, 15, 15, 16, 17,
  64. 16, 17, 18, 18, 19, 19, 19, 19,
  65. 21, 19, 20, 19, 19, 21, 22, 22,
  66. 22, 22, 22, 22, 22, 22, 22, 22,
  67. 22, 22, 22, 22, 22, 22, 22,
  68. };
  69. static const uint16_t clv_ac_syms[NUM_AC_CODES] = {
  70. 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008,
  71. 0x0009, 0x000A, 0x000B, 0x000C, 0x0011, 0x0012, 0x0013, 0x0014,
  72. 0x0015, 0x0016, 0x0021, 0x0022, 0x0023, 0x0024, 0x0031, 0x0032,
  73. 0x0033, 0x0041, 0x0042, 0x0043, 0x0051, 0x0052, 0x0053, 0x0061,
  74. 0x0062, 0x0063, 0x0071, 0x0072, 0x0081, 0x0082, 0x0091, 0x0092,
  75. 0x00A1, 0x00A2, 0x00B1, 0x00C1, 0x00D1, 0x00E1, 0x00F1, 0x0101,
  76. 0x0111, 0x0121, 0x0131, 0x0141, 0x0151, 0x0161, 0x0171, 0x0181,
  77. 0x0191, 0x01A1, 0x1001, 0x1002, 0x1003, 0x1011, 0x1012, 0x1021,
  78. 0x1031, 0x1041, 0x1051, 0x1061, 0x1071, 0x1081, 0x1091, 0x10A1,
  79. 0x10B1, 0x10C1, 0x10D1, 0x10E1, 0x10F1, 0x1101, 0x1111, 0x1121,
  80. 0x1131, 0x1141, 0x1151, 0x1161, 0x1171, 0x1181, 0x1191, 0x11A1,
  81. 0x11B1, 0x11C1, 0x11D1, 0x11E1, 0x11F1, 0x1201, 0x1211, 0x1221,
  82. 0x1231, 0x1241, 0x1251, 0x1261, 0x1271, 0x1281, 0x1BFF,
  83. };
  84. static const uint8_t clv_ac_codes[NUM_AC_CODES] = {
  85. 0x02, 0x0F, 0x15, 0x17, 0x1F, 0x25, 0x24, 0x21,
  86. 0x20, 0x07, 0x06, 0x20, 0x06, 0x14, 0x1E, 0x0F,
  87. 0x21, 0x50, 0x0E, 0x1D, 0x0E, 0x51, 0x0D, 0x23,
  88. 0x0D, 0x0C, 0x22, 0x52, 0x0B, 0x0C, 0x53, 0x13,
  89. 0x0B, 0x54, 0x12, 0x0A, 0x11, 0x09, 0x10, 0x08,
  90. 0x16, 0x55, 0x15, 0x14, 0x1C, 0x1B, 0x21, 0x20,
  91. 0x1F, 0x1E, 0x1D, 0x1C, 0x1B, 0x1A, 0x22, 0x23,
  92. 0x56, 0x57, 0x07, 0x19, 0x05, 0x0F, 0x04, 0x0E,
  93. 0x0D, 0x0C, 0x13, 0x12, 0x11, 0x10, 0x1A, 0x19,
  94. 0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x18, 0x17,
  95. 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x07, 0x06,
  96. 0x05, 0x04, 0x24, 0x25, 0x26, 0x27, 0x58, 0x59,
  97. 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x03,
  98. };
  99. static const uint8_t clv_ac_bits[NUM_AC_CODES] = {
  100. 2, 4, 6, 7, 8, 9, 9, 10,
  101. 10, 11, 11, 11, 3, 6, 8, 10,
  102. 11, 12, 4, 8, 10, 12, 5, 9,
  103. 10, 5, 9, 12, 5, 10, 12, 6,
  104. 10, 12, 6, 10, 6, 10, 6, 10,
  105. 7, 12, 7, 7, 8, 8, 9, 9,
  106. 9, 9, 9, 9, 9, 9, 11, 11,
  107. 12, 12, 4, 9, 11, 6, 11, 6,
  108. 6, 6, 7, 7, 7, 7, 8, 8,
  109. 8, 8, 8, 8, 8, 8, 9, 9,
  110. 9, 9, 9, 9, 9, 9, 10, 10,
  111. 10, 10, 11, 11, 11, 11, 12, 12,
  112. 12, 12, 12, 12, 12, 12, 7,
  113. };
  114. typedef struct CLVContext {
  115. AVCodecContext *avctx;
  116. IDCTDSPContext idsp;
  117. AVFrame *pic;
  118. BitstreamContext bc;
  119. int mb_width, mb_height;
  120. VLC dc_vlc, ac_vlc;
  121. int luma_dc_quant, chroma_dc_quant, ac_quant;
  122. DECLARE_ALIGNED(16, int16_t, block)[64];
  123. int top_dc[3], left_dc[4];
  124. int iframes_warning;
  125. } CLVContext;
  126. static inline int decode_block(CLVContext *ctx, int16_t *blk, int has_ac,
  127. int ac_quant)
  128. {
  129. BitstreamContext *bc = &ctx->bc;
  130. int idx = 1, last = 0, val, skip;
  131. memset(blk, 0, sizeof(*blk) * 64);
  132. blk[0] = bitstream_read_vlc(bc, ctx->dc_vlc.table, 9, 3);
  133. if (blk[0] < 0)
  134. return AVERROR_INVALIDDATA;
  135. blk[0] -= 63;
  136. if (!has_ac)
  137. return 0;
  138. while (idx < 64 && !last) {
  139. val = bitstream_read_vlc(bc, ctx->ac_vlc.table, 9, 2);
  140. if (val < 0)
  141. return AVERROR_INVALIDDATA;
  142. if (val != 0x1BFF) {
  143. last = val >> 12;
  144. skip = (val >> 4) & 0xFF;
  145. val &= 0xF;
  146. if (bitstream_read_bit(bc))
  147. val = -val;
  148. } else {
  149. last = bitstream_read_bit(bc);
  150. skip = bitstream_read(bc, 6);
  151. val = bitstream_read_signed(bc, 8);
  152. }
  153. if (val) {
  154. int aval = FFABS(val), sign = val < 0;
  155. val = ac_quant * (2 * aval + 1);
  156. if (!(ac_quant & 1))
  157. val--;
  158. if (sign)
  159. val = -val;
  160. }
  161. idx += skip;
  162. if (idx >= 64)
  163. return AVERROR_INVALIDDATA;
  164. blk[ff_zigzag_direct[idx++]] = val;
  165. }
  166. return (idx <= 64 && last) ? 0 : -1;
  167. }
  168. #define DCT_TEMPLATE(blk, step, bias, shift, dshift, OP) \
  169. const int t0 = OP(2841 * blk[1 * step] + 565 * blk[7 * step]); \
  170. const int t1 = OP( 565 * blk[1 * step] - 2841 * blk[7 * step]); \
  171. const int t2 = OP(1609 * blk[5 * step] + 2408 * blk[3 * step]); \
  172. const int t3 = OP(2408 * blk[5 * step] - 1609 * blk[3 * step]); \
  173. const int t4 = OP(1108 * blk[2 * step] - 2676 * blk[6 * step]); \
  174. const int t5 = OP(2676 * blk[2 * step] + 1108 * blk[6 * step]); \
  175. const int t6 = ((blk[0 * step] + blk[4 * step]) << dshift) + bias; \
  176. const int t7 = ((blk[0 * step] - blk[4 * step]) << dshift) + bias; \
  177. const int t8 = t0 + t2; \
  178. const int t9 = t0 - t2; \
  179. const int tA = 181 * (t9 + (t1 - t3)) + 0x80 >> 8; \
  180. const int tB = 181 * (t9 - (t1 - t3)) + 0x80 >> 8; \
  181. const int tC = t1 + t3; \
  182. \
  183. blk[0 * step] = (t6 + t5 + t8) >> shift; \
  184. blk[1 * step] = (t7 + t4 + tA) >> shift; \
  185. blk[2 * step] = (t7 - t4 + tB) >> shift; \
  186. blk[3 * step] = (t6 - t5 + tC) >> shift; \
  187. blk[4 * step] = (t6 - t5 - tC) >> shift; \
  188. blk[5 * step] = (t7 - t4 - tB) >> shift; \
  189. blk[6 * step] = (t7 + t4 - tA) >> shift; \
  190. blk[7 * step] = (t6 + t5 - t8) >> shift; \
  191. #define ROP(x) x
  192. #define COP(x) (((x) + 4) >> 3)
  193. static void clv_dct(int16_t *block)
  194. {
  195. int i;
  196. int16_t *ptr;
  197. ptr = block;
  198. for (i = 0; i < 8; i++) {
  199. DCT_TEMPLATE(ptr, 1, 0x80, 8, 11, ROP);
  200. ptr += 8;
  201. }
  202. ptr = block;
  203. for (i = 0; i < 8; i++) {
  204. DCT_TEMPLATE(ptr, 8, 0x2000, 14, 8, COP);
  205. ptr++;
  206. }
  207. }
  208. static int decode_mb(CLVContext *c, int x, int y)
  209. {
  210. int i, has_ac[6], off;
  211. for (i = 0; i < 6; i++)
  212. has_ac[i] = bitstream_read_bit(&c->bc);
  213. off = x * 16 + y * 16 * c->pic->linesize[0];
  214. for (i = 0; i < 4; i++) {
  215. if (decode_block(c, c->block, has_ac[i], c->ac_quant) < 0)
  216. return AVERROR_INVALIDDATA;
  217. if (!x && !(i & 1)) {
  218. c->block[0] += c->top_dc[0];
  219. c->top_dc[0] = c->block[0];
  220. } else {
  221. c->block[0] += c->left_dc[(i & 2) >> 1];
  222. }
  223. c->left_dc[(i & 2) >> 1] = c->block[0];
  224. c->block[0] *= c->luma_dc_quant;
  225. clv_dct(c->block);
  226. if (i == 2)
  227. off += c->pic->linesize[0] * 8;
  228. c->idsp.put_pixels_clamped(c->block,
  229. c->pic->data[0] + off + (i & 1) * 8,
  230. c->pic->linesize[0]);
  231. }
  232. off = x * 8 + y * 8 * c->pic->linesize[1];
  233. for (i = 1; i < 3; i++) {
  234. if (decode_block(c, c->block, has_ac[i + 3], c->ac_quant) < 0)
  235. return AVERROR_INVALIDDATA;
  236. if (!x) {
  237. c->block[0] += c->top_dc[i];
  238. c->top_dc[i] = c->block[0];
  239. } else {
  240. c->block[0] += c->left_dc[i + 1];
  241. }
  242. c->left_dc[i + 1] = c->block[0];
  243. c->block[0] *= c->chroma_dc_quant;
  244. clv_dct(c->block);
  245. c->idsp.put_pixels_clamped(c->block, c->pic->data[i] + off,
  246. c->pic->linesize[i]);
  247. }
  248. return 0;
  249. }
  250. static int clv_decode_frame(AVCodecContext *avctx, void *data,
  251. int *got_frame, AVPacket *avpkt)
  252. {
  253. const uint8_t *buf = avpkt->data;
  254. int buf_size = avpkt->size;
  255. CLVContext *c = avctx->priv_data;
  256. GetByteContext gb;
  257. uint32_t frame_type;
  258. int i, j, ret;
  259. bytestream2_init(&gb, buf, buf_size);
  260. if (avctx->codec_tag == MKTAG('C', 'L', 'V', '1')) {
  261. int skip = bytestream2_get_byte(&gb);
  262. bytestream2_skip(&gb, (skip + 1) * 8);
  263. }
  264. frame_type = bytestream2_get_byte(&gb);
  265. if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
  266. return ret;
  267. c->pic->key_frame = frame_type & 0x20 ? 1 : 0;
  268. c->pic->pict_type = frame_type & 0x20 ? AV_PICTURE_TYPE_I
  269. : AV_PICTURE_TYPE_P;
  270. if (frame_type & 0x2) {
  271. bytestream2_get_be32(&gb); // frame size;
  272. c->ac_quant = bytestream2_get_byte(&gb);
  273. c->luma_dc_quant = 32;
  274. c->chroma_dc_quant = 32;
  275. if ((ret = bitstream_init8(&c->bc, buf + bytestream2_tell(&gb),
  276. buf_size - bytestream2_tell(&gb))) < 0)
  277. return ret;
  278. for (i = 0; i < 3; i++)
  279. c->top_dc[i] = 32;
  280. for (i = 0; i < 4; i++)
  281. c->left_dc[i] = 32;
  282. for (j = 0; j < c->mb_height; j++) {
  283. for (i = 0; i < c->mb_width; i++) {
  284. ret |= decode_mb(c, i, j);
  285. }
  286. }
  287. } else {
  288. if (!c->iframes_warning)
  289. avpriv_report_missing_feature(avctx, "Non-I-frames in Clearvideo");
  290. c->iframes_warning = 1;
  291. return AVERROR_PATCHWELCOME;
  292. }
  293. if ((ret = av_frame_ref(data, c->pic)) < 0)
  294. return ret;
  295. *got_frame = 1;
  296. return ret < 0 ? ret : buf_size;
  297. }
  298. static av_cold int clv_decode_init(AVCodecContext *avctx)
  299. {
  300. CLVContext *const c = avctx->priv_data;
  301. int ret;
  302. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  303. c->avctx = avctx;
  304. c->mb_width = FFALIGN(avctx->width, 16) >> 4;
  305. c->mb_height = FFALIGN(avctx->height, 16) >> 4;
  306. c->iframes_warning = 0;
  307. c->pic = av_frame_alloc();
  308. if (!c->pic)
  309. return AVERROR(ENOMEM);
  310. ff_idctdsp_init(&c->idsp, avctx);
  311. ret = init_vlc(&c->dc_vlc, 9, NUM_DC_CODES,
  312. clv_dc_bits, 1, 1,
  313. clv_dc_codes, 1, 1, 0);
  314. if (ret) {
  315. av_log(avctx, AV_LOG_ERROR, "Error initialising DC VLC\n");
  316. return ret;
  317. }
  318. ret = ff_init_vlc_sparse(&c->ac_vlc, 9, NUM_AC_CODES,
  319. clv_ac_bits, 1, 1,
  320. clv_ac_codes, 1, 1,
  321. clv_ac_syms, 2, 2, 0);
  322. if (ret) {
  323. av_log(avctx, AV_LOG_ERROR, "Error initialising AC VLC\n");
  324. return ret;
  325. }
  326. return 0;
  327. }
  328. static av_cold int clv_decode_end(AVCodecContext *avctx)
  329. {
  330. CLVContext *const c = avctx->priv_data;
  331. av_frame_free(&c->pic);
  332. ff_free_vlc(&c->dc_vlc);
  333. ff_free_vlc(&c->ac_vlc);
  334. return 0;
  335. }
  336. AVCodec ff_clearvideo_decoder = {
  337. .name = "clearvideo",
  338. .long_name = NULL_IF_CONFIG_SMALL("Iterated Systems ClearVideo"),
  339. .type = AVMEDIA_TYPE_VIDEO,
  340. .id = AV_CODEC_ID_CLEARVIDEO,
  341. .priv_data_size = sizeof(CLVContext),
  342. .init = clv_decode_init,
  343. .close = clv_decode_end,
  344. .decode = clv_decode_frame,
  345. .capabilities = AV_CODEC_CAP_DR1,
  346. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  347. };