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.

542 lines
17KB

  1. /*
  2. * Canopus HQX decoder
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <inttypes.h>
  21. #include "libavutil/imgutils.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "avcodec.h"
  24. #include "get_bits.h"
  25. #include "internal.h"
  26. #include "hqx.h"
  27. #include "hqxdsp.h"
  28. /* HQX has four modes - 422, 444, 422alpha and 444alpha - all 12-bit */
  29. enum HQXFormat {
  30. HQX_422 = 0,
  31. HQX_444,
  32. HQX_422A,
  33. HQX_444A,
  34. };
  35. #define HQX_HEADER_SIZE 59
  36. typedef int (*mb_decode_func)(HQXContext *ctx, AVFrame *pic,
  37. GetBitContext *gb, int x, int y);
  38. /* macroblock selects a group of 4 possible quants and
  39. * a block can use any of those four quantisers
  40. * one column is powers of 2, the other one is powers of 2 * 3,
  41. * then there is the special one, powers of 2 * 5 */
  42. static const int hqx_quants[16][4] = {
  43. { 0x1, 0x2, 0x4, 0x8 }, { 0x1, 0x3, 0x6, 0xC },
  44. { 0x2, 0x4, 0x8, 0x10 }, { 0x3, 0x6, 0xC, 0x18 },
  45. { 0x4, 0x8, 0x10, 0x20 }, { 0x6, 0xC, 0x18, 0x30 },
  46. { 0x8, 0x10, 0x20, 0x40 },
  47. { 0xA, 0x14, 0x28, 0x50 },
  48. { 0xC, 0x18, 0x30, 0x60 },
  49. { 0x10, 0x20, 0x40, 0x80 }, { 0x18, 0x30, 0x60, 0xC0 },
  50. { 0x20, 0x40, 0x80, 0x100 }, { 0x30, 0x60, 0xC0, 0x180 },
  51. { 0x40, 0x80, 0x100, 0x200 }, { 0x60, 0xC0, 0x180, 0x300 },
  52. { 0x80, 0x100, 0x200, 0x400 }
  53. };
  54. static const uint8_t hqx_quant_luma[64] = {
  55. 16, 16, 16, 19, 19, 19, 42, 44,
  56. 16, 16, 19, 19, 19, 38, 43, 45,
  57. 16, 19, 19, 19, 40, 41, 45, 48,
  58. 19, 19, 19, 40, 41, 42, 46, 49,
  59. 19, 19, 40, 41, 42, 43, 48, 101,
  60. 19, 38, 41, 42, 43, 44, 98, 104,
  61. 42, 43, 45, 46, 48, 98, 109, 116,
  62. 44, 45, 48, 49, 101, 104, 116, 123,
  63. };
  64. static const uint8_t hqx_quant_chroma[64] = {
  65. 16, 16, 19, 25, 26, 26, 42, 44,
  66. 16, 19, 25, 25, 26, 38, 43, 91,
  67. 19, 25, 26, 27, 40, 41, 91, 96,
  68. 25, 25, 27, 40, 41, 84, 93, 197,
  69. 26, 26, 40, 41, 84, 86, 191, 203,
  70. 26, 38, 41, 84, 86, 177, 197, 209,
  71. 42, 43, 91, 93, 191, 197, 219, 232,
  72. 44, 91, 96, 197, 203, 209, 232, 246,
  73. };
  74. static inline void put_blocks(HQXContext *ctx, AVFrame *pic, int plane,
  75. int x, int y, int ilace,
  76. int16_t *block0, int16_t *block1,
  77. const uint8_t *quant)
  78. {
  79. int fields = ilace ? 2 : 1;
  80. int lsize = pic->linesize[plane];
  81. uint8_t *p = pic->data[plane] + x * 2;
  82. ctx->hqxdsp.idct_put((uint16_t *)(p + y * lsize),
  83. lsize * fields, block0, quant);
  84. ctx->hqxdsp.idct_put((uint16_t *)(p + (y + (ilace ? 1 : 8)) * lsize),
  85. lsize * fields, block1, quant);
  86. }
  87. static inline void hqx_get_ac(GetBitContext *gb, const HQXAC *ac,
  88. int *run, int *lev)
  89. {
  90. int val;
  91. val = show_bits(gb, ac->lut_bits);
  92. if (ac->lut[val].bits == -1) {
  93. GetBitContext gb2 = *gb;
  94. skip_bits(&gb2, ac->lut_bits);
  95. val = ac->lut[val].lev + show_bits(&gb2, ac->extra_bits);
  96. }
  97. *run = ac->lut[val].run;
  98. *lev = ac->lut[val].lev;
  99. skip_bits(gb, ac->lut[val].bits);
  100. }
  101. static int decode_block(GetBitContext *gb, VLC *vlc,
  102. const int *quants, int dcb,
  103. int16_t block[64], int *last_dc)
  104. {
  105. int q, dc;
  106. int ac_idx;
  107. int run, lev, pos = 1;
  108. memset(block, 0, 64 * sizeof(*block));
  109. dc = get_vlc2(gb, vlc->table, HQX_DC_VLC_BITS, 2);
  110. if (dc < 0)
  111. return AVERROR_INVALIDDATA;
  112. *last_dc += dc;
  113. block[0] = sign_extend(*last_dc << (12 - dcb), 12);
  114. q = quants[get_bits(gb, 2)];
  115. if (q >= 128)
  116. ac_idx = HQX_AC_Q128;
  117. else if (q >= 64)
  118. ac_idx = HQX_AC_Q64;
  119. else if (q >= 32)
  120. ac_idx = HQX_AC_Q32;
  121. else if (q >= 16)
  122. ac_idx = HQX_AC_Q16;
  123. else if (q >= 8)
  124. ac_idx = HQX_AC_Q8;
  125. else
  126. ac_idx = HQX_AC_Q0;
  127. do {
  128. hqx_get_ac(gb, &ff_hqx_ac[ac_idx], &run, &lev);
  129. pos += run;
  130. if (pos >= 64)
  131. break;
  132. block[ff_zigzag_direct[pos++]] = lev * q;
  133. } while (pos < 64);
  134. return 0;
  135. }
  136. static int hqx_decode_422(HQXContext *ctx, AVFrame *pic,
  137. GetBitContext *gb, int x, int y)
  138. {
  139. const int *quants;
  140. int flag;
  141. int last_dc;
  142. int i, ret;
  143. if (ctx->interlaced)
  144. flag = get_bits1(gb);
  145. else
  146. flag = 0;
  147. quants = hqx_quants[get_bits(gb, 4)];
  148. for (i = 0; i < 8; i++) {
  149. int vlc_index = ctx->dcb - 9;
  150. if (i == 0 || i == 4 || i == 6)
  151. last_dc = 0;
  152. ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,
  153. ctx->dcb, ctx->block[i], &last_dc);
  154. if (ret < 0)
  155. return ret;
  156. }
  157. put_blocks(ctx, pic, 0, x, y, flag, ctx->block[0], ctx->block[2], hqx_quant_luma);
  158. put_blocks(ctx, pic, 0, x + 8, y, flag, ctx->block[1], ctx->block[3], hqx_quant_luma);
  159. put_blocks(ctx, pic, 2, x >> 1, y, flag, ctx->block[4], ctx->block[5], hqx_quant_chroma);
  160. put_blocks(ctx, pic, 1, x >> 1, y, flag, ctx->block[6], ctx->block[7], hqx_quant_chroma);
  161. return 0;
  162. }
  163. static int hqx_decode_422a(HQXContext *ctx, AVFrame *pic,
  164. GetBitContext *gb, int x, int y)
  165. {
  166. const int *quants;
  167. int flag = 0;
  168. int last_dc;
  169. int i, ret;
  170. int cbp;
  171. cbp = get_vlc2(gb, ctx->cbp_vlc.table, ctx->cbp_vlc.bits, 1);
  172. for (i = 0; i < 12; i++)
  173. memset(ctx->block[i], 0, sizeof(**ctx->block) * 64);
  174. for (i = 0; i < 12; i++)
  175. ctx->block[i][0] = -0x800;
  176. if (cbp) {
  177. if (ctx->interlaced)
  178. flag = get_bits1(gb);
  179. quants = hqx_quants[get_bits(gb, 4)];
  180. cbp |= cbp << 4; // alpha CBP
  181. if (cbp & 0x3) // chroma CBP - top
  182. cbp |= 0x500;
  183. if (cbp & 0xC) // chroma CBP - bottom
  184. cbp |= 0xA00;
  185. for (i = 0; i < 12; i++) {
  186. if (i == 0 || i == 4 || i == 8 || i == 10)
  187. last_dc = 0;
  188. if (cbp & (1 << i)) {
  189. int vlc_index = ctx->dcb - 9;
  190. ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,
  191. ctx->dcb, ctx->block[i], &last_dc);
  192. if (ret < 0)
  193. return ret;
  194. }
  195. }
  196. }
  197. put_blocks(ctx, pic, 3, x, y, flag, ctx->block[ 0], ctx->block[ 2], hqx_quant_luma);
  198. put_blocks(ctx, pic, 3, x + 8, y, flag, ctx->block[ 1], ctx->block[ 3], hqx_quant_luma);
  199. put_blocks(ctx, pic, 0, x, y, flag, ctx->block[ 4], ctx->block[ 6], hqx_quant_luma);
  200. put_blocks(ctx, pic, 0, x + 8, y, flag, ctx->block[ 5], ctx->block[ 7], hqx_quant_luma);
  201. put_blocks(ctx, pic, 2, x >> 1, y, flag, ctx->block[ 8], ctx->block[ 9], hqx_quant_chroma);
  202. put_blocks(ctx, pic, 1, x >> 1, y, flag, ctx->block[10], ctx->block[11], hqx_quant_chroma);
  203. return 0;
  204. }
  205. static int hqx_decode_444(HQXContext *ctx, AVFrame *pic,
  206. GetBitContext *gb, int x, int y)
  207. {
  208. const int *quants;
  209. int flag;
  210. int last_dc;
  211. int i, ret;
  212. if (ctx->interlaced)
  213. flag = get_bits1(gb);
  214. else
  215. flag = 0;
  216. quants = hqx_quants[get_bits(gb, 4)];
  217. for (i = 0; i < 12; i++) {
  218. int vlc_index = ctx->dcb - 9;
  219. if (i == 0 || i == 4 || i == 8)
  220. last_dc = 0;
  221. ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,
  222. ctx->dcb, ctx->block[i], &last_dc);
  223. if (ret < 0)
  224. return ret;
  225. }
  226. put_blocks(ctx, pic, 0, x, y, flag, ctx->block[0], ctx->block[ 2], hqx_quant_luma);
  227. put_blocks(ctx, pic, 0, x + 8, y, flag, ctx->block[1], ctx->block[ 3], hqx_quant_luma);
  228. put_blocks(ctx, pic, 2, x, y, flag, ctx->block[4], ctx->block[ 6], hqx_quant_chroma);
  229. put_blocks(ctx, pic, 2, x + 8, y, flag, ctx->block[5], ctx->block[ 7], hqx_quant_chroma);
  230. put_blocks(ctx, pic, 1, x, y, flag, ctx->block[8], ctx->block[10], hqx_quant_chroma);
  231. put_blocks(ctx, pic, 1, x + 8, y, flag, ctx->block[9], ctx->block[11], hqx_quant_chroma);
  232. return 0;
  233. }
  234. static int hqx_decode_444a(HQXContext *ctx, AVFrame *pic,
  235. GetBitContext *gb, int x, int y)
  236. {
  237. const int *quants;
  238. int flag = 0;
  239. int last_dc;
  240. int i, ret;
  241. int cbp;
  242. cbp = get_vlc2(gb, ctx->cbp_vlc.table, ctx->cbp_vlc.bits, 1);
  243. for (i = 0; i < 16; i++)
  244. memset(ctx->block[i], 0, sizeof(**ctx->block) * 64);
  245. for (i = 0; i < 16; i++)
  246. ctx->block[i][0] = -0x800;
  247. if (cbp) {
  248. if (ctx->interlaced)
  249. flag = get_bits1(gb);
  250. quants = hqx_quants[get_bits(gb, 4)];
  251. cbp |= cbp << 4; // alpha CBP
  252. cbp |= cbp << 8; // chroma CBP
  253. for (i = 0; i < 16; i++) {
  254. if (i == 0 || i == 4 || i == 8 || i == 12)
  255. last_dc = 0;
  256. if (cbp & (1 << i)) {
  257. int vlc_index = ctx->dcb - 9;
  258. ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,
  259. ctx->dcb, ctx->block[i], &last_dc);
  260. if (ret < 0)
  261. return ret;
  262. }
  263. }
  264. }
  265. put_blocks(ctx, pic, 3, x, y, flag, ctx->block[ 0], ctx->block[ 2], hqx_quant_luma);
  266. put_blocks(ctx, pic, 3, x + 8, y, flag, ctx->block[ 1], ctx->block[ 3], hqx_quant_luma);
  267. put_blocks(ctx, pic, 0, x, y, flag, ctx->block[ 4], ctx->block[ 6], hqx_quant_luma);
  268. put_blocks(ctx, pic, 0, x + 8, y, flag, ctx->block[ 5], ctx->block[ 7], hqx_quant_luma);
  269. put_blocks(ctx, pic, 2, x, y, flag, ctx->block[ 8], ctx->block[10], hqx_quant_chroma);
  270. put_blocks(ctx, pic, 2, x + 8, y, flag, ctx->block[ 9], ctx->block[11], hqx_quant_chroma);
  271. put_blocks(ctx, pic, 1, x, y, flag, ctx->block[12], ctx->block[14], hqx_quant_chroma);
  272. put_blocks(ctx, pic, 1, x + 8, y, flag, ctx->block[13], ctx->block[15], hqx_quant_chroma);
  273. return 0;
  274. }
  275. static const int shuffle_16[16] = {
  276. 0, 5, 11, 14, 2, 7, 9, 13, 1, 4, 10, 15, 3, 6, 8, 12
  277. };
  278. static int decode_slice(HQXContext *ctx, AVFrame *pic, GetBitContext *gb,
  279. int slice_no, mb_decode_func decode_func)
  280. {
  281. int mb_w = (ctx->width + 15) >> 4;
  282. int mb_h = (ctx->height + 15) >> 4;
  283. int grp_w = (mb_w + 4) / 5;
  284. int grp_h = (mb_h + 4) / 5;
  285. int grp_h_edge = grp_w * (mb_w / grp_w);
  286. int grp_v_edge = grp_h * (mb_h / grp_h);
  287. int grp_v_rest = mb_w - grp_h_edge;
  288. int grp_h_rest = mb_h - grp_v_edge;
  289. int num_mbs = mb_w * mb_h;
  290. int num_tiles = (num_mbs + 479) / 480;
  291. int std_tile_blocks = num_mbs / (16 * num_tiles);
  292. int g_tile = slice_no * num_tiles;
  293. int blk_addr, loc_addr, mb_x, mb_y, pos, loc_row, i;
  294. int tile_blocks, tile_limit, tile_no;
  295. for (tile_no = 0; tile_no < num_tiles; tile_no++, g_tile++) {
  296. tile_blocks = std_tile_blocks;
  297. tile_limit = -1;
  298. if (g_tile < num_mbs - std_tile_blocks * 16 * num_tiles) {
  299. tile_limit = num_mbs / (16 * num_tiles);
  300. tile_blocks++;
  301. }
  302. for (i = 0; i < tile_blocks; i++) {
  303. if (i == tile_limit)
  304. blk_addr = g_tile + 16 * num_tiles * i;
  305. else
  306. blk_addr = tile_no + 16 * num_tiles * i +
  307. num_tiles * shuffle_16[(i + slice_no) & 0xF];
  308. loc_row = grp_h * (blk_addr / (grp_h * mb_w));
  309. loc_addr = blk_addr % (grp_h * mb_w);
  310. if (loc_row >= grp_v_edge) {
  311. mb_x = grp_w * (loc_addr / (grp_h_rest * grp_w));
  312. pos = loc_addr % (grp_h_rest * grp_w);
  313. } else {
  314. mb_x = grp_w * (loc_addr / (grp_h * grp_w));
  315. pos = loc_addr % (grp_h * grp_w);
  316. }
  317. if (mb_x >= grp_h_edge) {
  318. mb_x += pos % grp_v_rest;
  319. mb_y = loc_row + (pos / grp_v_rest);
  320. } else {
  321. mb_x += pos % grp_w;
  322. mb_y = loc_row + (pos / grp_w);
  323. }
  324. decode_func(ctx, pic, gb, mb_x * 16, mb_y * 16);
  325. }
  326. }
  327. return 0;
  328. }
  329. static int hqx_decode_frame(AVCodecContext *avctx, void *data,
  330. int *got_picture_ptr, AVPacket *avpkt)
  331. {
  332. HQXContext *ctx = avctx->priv_data;
  333. AVFrame *pic = data;
  334. uint8_t *src = avpkt->data;
  335. uint32_t info_tag, info_offset;
  336. int data_start;
  337. unsigned data_size;
  338. GetBitContext gb;
  339. int i, ret;
  340. int slice;
  341. uint32_t slice_off[17];
  342. mb_decode_func decode_func = 0;
  343. if (avpkt->size < 8)
  344. return AVERROR_INVALIDDATA;
  345. /* Skip the INFO header if present */
  346. info_offset = 0;
  347. info_tag = AV_RL32(src);
  348. if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
  349. info_offset = AV_RL32(src + 4);
  350. if (info_offset > UINT32_MAX - 8 || info_offset + 8 > avpkt->size) {
  351. av_log(avctx, AV_LOG_ERROR,
  352. "Invalid INFO header offset: 0x%08"PRIX32" is too large.\n",
  353. info_offset);
  354. return AVERROR_INVALIDDATA;
  355. }
  356. info_offset += 8;
  357. src += info_offset;
  358. av_log(avctx, AV_LOG_DEBUG, "Skipping INFO chunk.\n");
  359. }
  360. data_start = src - avpkt->data;
  361. data_size = avpkt->size - data_start;
  362. if (data_size < HQX_HEADER_SIZE) {
  363. av_log(avctx, AV_LOG_ERROR, "Frame too small.\n");
  364. return AVERROR_INVALIDDATA;
  365. }
  366. if (src[0] != 'H' || src[1] != 'Q') {
  367. av_log(avctx, AV_LOG_ERROR, "Not an HQX frame.\n");
  368. return AVERROR_INVALIDDATA;
  369. }
  370. ctx->interlaced = !(src[2] & 0x80);
  371. ctx->format = src[2] & 7;
  372. ctx->dcb = (src[3] & 3) + 8;
  373. ctx->width = AV_RB16(src + 4);
  374. ctx->height = AV_RB16(src + 6);
  375. for (i = 0; i < 17; i++)
  376. slice_off[i] = AV_RB24(src + 8 + i * 3);
  377. if (ctx->dcb == 8) {
  378. av_log(avctx, AV_LOG_ERROR, "Invalid DC precision %d.\n", ctx->dcb);
  379. return AVERROR_INVALIDDATA;
  380. }
  381. ret = av_image_check_size(ctx->width, ctx->height, 0, avctx);
  382. if (ret < 0) {
  383. av_log(avctx, AV_LOG_ERROR, "Invalid stored dimenstions %dx%d.\n",
  384. ctx->width, ctx->height);
  385. return AVERROR_INVALIDDATA;
  386. }
  387. avctx->coded_width = FFALIGN(ctx->width, 16);
  388. avctx->coded_height = FFALIGN(ctx->height, 16);
  389. avctx->width = ctx->width;
  390. avctx->height = ctx->height;
  391. avctx->bits_per_raw_sample = 10;
  392. switch (ctx->format) {
  393. case HQX_422:
  394. avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
  395. decode_func = hqx_decode_422;
  396. break;
  397. case HQX_444:
  398. avctx->pix_fmt = AV_PIX_FMT_YUV444P16;
  399. decode_func = hqx_decode_444;
  400. break;
  401. case HQX_422A:
  402. avctx->pix_fmt = AV_PIX_FMT_YUVA422P16;
  403. decode_func = hqx_decode_422a;
  404. break;
  405. case HQX_444A:
  406. avctx->pix_fmt = AV_PIX_FMT_YUVA444P16;
  407. decode_func = hqx_decode_444a;
  408. break;
  409. default:
  410. av_log(avctx, AV_LOG_ERROR, "Invalid format: %d.\n", ctx->format);
  411. return AVERROR_INVALIDDATA;
  412. }
  413. ret = ff_get_buffer(avctx, pic, 0);
  414. if (ret < 0) {
  415. av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
  416. return ret;
  417. }
  418. for (slice = 0; slice < 16; slice++) {
  419. if (slice_off[slice] < HQX_HEADER_SIZE ||
  420. slice_off[slice] >= slice_off[slice + 1] ||
  421. slice_off[slice + 1] > data_size) {
  422. av_log(avctx, AV_LOG_ERROR, "Invalid slice size.\n");
  423. break;
  424. }
  425. ret = init_get_bits(&gb, src + slice_off[slice],
  426. (slice_off[slice + 1] - slice_off[slice]) * 8);
  427. if (ret < 0)
  428. return ret;
  429. ret = decode_slice(ctx, pic, &gb, slice, decode_func);
  430. if (ret < 0) {
  431. av_log(avctx, AV_LOG_ERROR, "Error decoding slice %d.\n", slice);
  432. }
  433. }
  434. pic->key_frame = 1;
  435. pic->pict_type = AV_PICTURE_TYPE_I;
  436. *got_picture_ptr = 1;
  437. return avpkt->size;
  438. }
  439. static av_cold int hqx_decode_close(AVCodecContext *avctx)
  440. {
  441. int i;
  442. HQXContext *ctx = avctx->priv_data;
  443. ff_free_vlc(&ctx->cbp_vlc);
  444. for (i = 0; i < 3; i++) {
  445. ff_free_vlc(&ctx->dc_vlc[i]);
  446. }
  447. return 0;
  448. }
  449. static av_cold int hqx_decode_init(AVCodecContext *avctx)
  450. {
  451. HQXContext *ctx = avctx->priv_data;
  452. int ret = ff_hqx_init_vlcs(ctx);
  453. if (ret < 0)
  454. hqx_decode_close(avctx);
  455. ff_hqxdsp_init(&ctx->hqxdsp);
  456. return ret;
  457. }
  458. AVCodec ff_hqx_decoder = {
  459. .name = "hqx",
  460. .long_name = NULL_IF_CONFIG_SMALL("Canopus HQX"),
  461. .type = AVMEDIA_TYPE_VIDEO,
  462. .id = AV_CODEC_ID_HQX,
  463. .priv_data_size = sizeof(HQXContext),
  464. .init = hqx_decode_init,
  465. .decode = hqx_decode_frame,
  466. .close = hqx_decode_close,
  467. .capabilities = CODEC_CAP_DR1,
  468. };