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.

545 lines
18KB

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