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.

391 lines
12KB

  1. /*
  2. * Canopus HQ/HQA 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 <stdint.h>
  21. #include "libavutil/attributes.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "avcodec.h"
  24. #include "canopus.h"
  25. #include "internal.h"
  26. #include "hq_hqa.h"
  27. #include "hq_hqadsp.h"
  28. /* HQ/HQA slices are a set of macroblocks belonging to a frame, and
  29. * they usually form a pseudorandom pattern (probably because it is
  30. * nicer to display on partial decode).
  31. *
  32. * For HQA it just happens that each slice is on every 8th macroblock,
  33. * but they can be on any frame width like
  34. * X.......X.
  35. * ......X...
  36. * ....X.....
  37. * ..X.......
  38. * etc.
  39. *
  40. * The original decoder has special handling for edge macroblocks,
  41. * while lavc simply aligns coded_width and coded_height.
  42. */
  43. static inline void put_blocks(HQContext *c, AVFrame *pic,
  44. int plane, int x, int y, int ilace,
  45. int16_t *block0, int16_t *block1)
  46. {
  47. uint8_t *p = pic->data[plane] + x;
  48. c->hqhqadsp.idct_put(p + y * pic->linesize[plane],
  49. pic->linesize[plane] << ilace, block0);
  50. c->hqhqadsp.idct_put(p + (y + (ilace ? 1 : 8)) * pic->linesize[plane],
  51. pic->linesize[plane] << ilace, block1);
  52. }
  53. static int hq_decode_block(HQContext *c, GetBitContext *gb, int16_t block[64],
  54. int qsel, int is_chroma, int is_hqa)
  55. {
  56. const int32_t *q;
  57. int val, pos = 1;
  58. memset(block, 0, 64 * sizeof(*block));
  59. if (!is_hqa) {
  60. block[0] = get_sbits(gb, 9) << 6;
  61. q = ff_hq_quants[qsel][is_chroma][get_bits(gb, 2)];
  62. } else {
  63. q = ff_hq_quants[qsel][is_chroma][get_bits(gb, 2)];
  64. block[0] = get_sbits(gb, 9) << 6;
  65. }
  66. for (;;) {
  67. val = get_vlc2(gb, c->hq_ac_vlc.table, 9, 2);
  68. if (val < 0)
  69. return AVERROR_INVALIDDATA;
  70. pos += ff_hq_ac_skips[val];
  71. if (pos >= 64)
  72. break;
  73. block[ff_zigzag_direct[pos]] = (ff_hq_ac_syms[val] * q[pos]) >> 12;
  74. pos++;
  75. }
  76. return 0;
  77. }
  78. static int hq_decode_mb(HQContext *c, AVFrame *pic,
  79. GetBitContext *gb, int x, int y)
  80. {
  81. int qgroup, flag;
  82. int i, ret;
  83. qgroup = get_bits(gb, 4);
  84. flag = get_bits1(gb);
  85. for (i = 0; i < 8; i++) {
  86. ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 4, 0);
  87. if (ret < 0)
  88. return ret;
  89. }
  90. put_blocks(c, pic, 0, x, y, flag, c->block[0], c->block[2]);
  91. put_blocks(c, pic, 0, x + 8, y, flag, c->block[1], c->block[3]);
  92. put_blocks(c, pic, 2, x >> 1, y, flag, c->block[4], c->block[5]);
  93. put_blocks(c, pic, 1, x >> 1, y, flag, c->block[6], c->block[7]);
  94. return 0;
  95. }
  96. static int hq_decode_frame(HQContext *ctx, AVFrame *pic,
  97. int prof_num, size_t data_size)
  98. {
  99. const HQProfile *profile;
  100. GetBitContext gb;
  101. const uint8_t *perm, *src = ctx->gbc.buffer;
  102. uint32_t slice_off[21];
  103. int slice, start_off, next_off, i, ret;
  104. if (prof_num >= NUM_HQ_PROFILES) {
  105. profile = &ff_hq_profile[0];
  106. avpriv_request_sample(ctx->avctx, "HQ Profile %d", prof_num);
  107. } else {
  108. profile = &ff_hq_profile[prof_num];
  109. av_log(ctx->avctx, AV_LOG_VERBOSE, "HQ Profile %d\n", prof_num);
  110. }
  111. ctx->avctx->coded_width = FFALIGN(profile->width, 16);
  112. ctx->avctx->coded_height = FFALIGN(profile->height, 16);
  113. ctx->avctx->width = profile->width;
  114. ctx->avctx->height = profile->height;
  115. ctx->avctx->bits_per_raw_sample = 8;
  116. ctx->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  117. ret = ff_get_buffer(ctx->avctx, pic, 0);
  118. if (ret < 0) {
  119. av_log(ctx->avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
  120. return ret;
  121. }
  122. /* Offsets are stored from CUV position, so adjust them accordingly. */
  123. for (i = 0; i < profile->num_slices + 1; i++)
  124. slice_off[i] = bytestream2_get_be24(&ctx->gbc) - 4;
  125. next_off = 0;
  126. for (slice = 0; slice < profile->num_slices; slice++) {
  127. start_off = next_off;
  128. next_off = profile->tab_h * (slice + 1) / profile->num_slices;
  129. perm = profile->perm_tab + start_off * profile->tab_w * 2;
  130. if (slice_off[slice] < (profile->num_slices + 1) * 3 ||
  131. slice_off[slice] >= slice_off[slice + 1] ||
  132. slice_off[slice + 1] > data_size) {
  133. av_log(ctx->avctx, AV_LOG_ERROR,
  134. "Invalid slice size %zu.\n", data_size);
  135. break;
  136. }
  137. init_get_bits(&gb, src + slice_off[slice],
  138. (slice_off[slice + 1] - slice_off[slice]) * 8);
  139. for (i = 0; i < (next_off - start_off) * profile->tab_w; i++) {
  140. ret = hq_decode_mb(ctx, pic, &gb, perm[0] * 16, perm[1] * 16);
  141. if (ret < 0) {
  142. av_log(ctx->avctx, AV_LOG_ERROR,
  143. "Error decoding macroblock %d at slice %d.\n", i, slice);
  144. return ret;
  145. }
  146. perm += 2;
  147. }
  148. }
  149. return 0;
  150. }
  151. static int hqa_decode_mb(HQContext *c, AVFrame *pic, int qgroup,
  152. GetBitContext *gb, int x, int y)
  153. {
  154. int flag = 0;
  155. int i, ret, cbp;
  156. cbp = get_vlc2(gb, c->hqa_cbp_vlc.table, 5, 1);
  157. for (i = 0; i < 12; i++)
  158. memset(c->block[i], 0, sizeof(*c->block));
  159. for (i = 0; i < 12; i++)
  160. c->block[i][0] = -128 * (1 << 6);
  161. if (cbp) {
  162. flag = get_bits1(gb);
  163. cbp |= cbp << 4;
  164. if (cbp & 0x3)
  165. cbp |= 0x500;
  166. if (cbp & 0xC)
  167. cbp |= 0xA00;
  168. for (i = 0; i < 12; i++) {
  169. if (!(cbp & (1 << i)))
  170. continue;
  171. ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 8, 1);
  172. if (ret < 0)
  173. return ret;
  174. }
  175. }
  176. put_blocks(c, pic, 3, x, y, flag, c->block[ 0], c->block[ 2]);
  177. put_blocks(c, pic, 3, x + 8, y, flag, c->block[ 1], c->block[ 3]);
  178. put_blocks(c, pic, 0, x, y, flag, c->block[ 4], c->block[ 6]);
  179. put_blocks(c, pic, 0, x + 8, y, flag, c->block[ 5], c->block[ 7]);
  180. put_blocks(c, pic, 2, x >> 1, y, flag, c->block[ 8], c->block[ 9]);
  181. put_blocks(c, pic, 1, x >> 1, y, flag, c->block[10], c->block[11]);
  182. return 0;
  183. }
  184. static int hqa_decode_slice(HQContext *ctx, AVFrame *pic, GetBitContext *gb,
  185. int quant, int slice_no, int w, int h)
  186. {
  187. int i, j, off;
  188. int ret;
  189. for (i = 0; i < h; i += 16) {
  190. off = (slice_no * 16 + i * 3) & 0x70;
  191. for (j = off; j < w; j += 128) {
  192. ret = hqa_decode_mb(ctx, pic, quant, gb, j, i);
  193. if (ret < 0) {
  194. av_log(ctx->avctx, AV_LOG_ERROR,
  195. "Error decoding macroblock at %dx%d.\n", i, j);
  196. return ret;
  197. }
  198. }
  199. }
  200. return 0;
  201. }
  202. static int hqa_decode_frame(HQContext *ctx, AVFrame *pic, size_t data_size)
  203. {
  204. GetBitContext gb;
  205. const int num_slices = 8;
  206. uint32_t slice_off[9];
  207. int i, slice, ret;
  208. int width, height, quant;
  209. const uint8_t *src = ctx->gbc.buffer;
  210. width = bytestream2_get_be16(&ctx->gbc);
  211. height = bytestream2_get_be16(&ctx->gbc);
  212. ctx->avctx->coded_width = FFALIGN(width, 16);
  213. ctx->avctx->coded_height = FFALIGN(height, 16);
  214. ctx->avctx->width = width;
  215. ctx->avctx->height = height;
  216. ctx->avctx->bits_per_raw_sample = 8;
  217. ctx->avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
  218. av_log(ctx->avctx, AV_LOG_VERBOSE, "HQA Profile\n");
  219. quant = bytestream2_get_byte(&ctx->gbc);
  220. bytestream2_skip(&ctx->gbc, 3);
  221. if (quant >= NUM_HQ_QUANTS) {
  222. av_log(ctx->avctx, AV_LOG_ERROR,
  223. "Invalid quantization matrix %d.\n", quant);
  224. return AVERROR_INVALIDDATA;
  225. }
  226. ret = ff_get_buffer(ctx->avctx, pic, 0);
  227. if (ret < 0) {
  228. av_log(ctx->avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
  229. return ret;
  230. }
  231. /* Offsets are stored from HQA1 position, so adjust them accordingly. */
  232. for (i = 0; i < num_slices + 1; i++)
  233. slice_off[i] = bytestream2_get_be32(&ctx->gbc) - 4;
  234. for (slice = 0; slice < num_slices; slice++) {
  235. if (slice_off[slice] < (num_slices + 1) * 3 ||
  236. slice_off[slice] >= slice_off[slice + 1] ||
  237. slice_off[slice + 1] > data_size) {
  238. av_log(ctx->avctx, AV_LOG_ERROR,
  239. "Invalid slice size %zu.\n", data_size);
  240. break;
  241. }
  242. init_get_bits(&gb, src + slice_off[slice],
  243. (slice_off[slice + 1] - slice_off[slice]) * 8);
  244. ret = hqa_decode_slice(ctx, pic, &gb, quant, slice, width, height);
  245. if (ret < 0)
  246. return ret;
  247. }
  248. return 0;
  249. }
  250. static int hq_hqa_decode_frame(AVCodecContext *avctx, void *data,
  251. int *got_frame, AVPacket *avpkt)
  252. {
  253. HQContext *ctx = avctx->priv_data;
  254. AVFrame *pic = data;
  255. uint32_t info_tag;
  256. unsigned int data_size;
  257. int tag, ret;
  258. bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
  259. if (bytestream2_get_bytes_left(&ctx->gbc) < 4 + 4) {
  260. av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", avpkt->size);
  261. return AVERROR_INVALIDDATA;
  262. }
  263. info_tag = bytestream2_peek_le32(&ctx->gbc);
  264. if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
  265. int info_size;
  266. bytestream2_skip(&ctx->gbc, 4);
  267. info_size = bytestream2_get_le32(&ctx->gbc);
  268. if (bytestream2_get_bytes_left(&ctx->gbc) < info_size) {
  269. av_log(avctx, AV_LOG_ERROR, "Invalid INFO size (%d).\n", info_size);
  270. return AVERROR_INVALIDDATA;
  271. }
  272. ff_canopus_parse_info_tag(avctx, ctx->gbc.buffer, info_size);
  273. bytestream2_skip(&ctx->gbc, info_size);
  274. }
  275. data_size = bytestream2_get_bytes_left(&ctx->gbc);
  276. if (data_size < 4) {
  277. av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", data_size);
  278. return AVERROR_INVALIDDATA;
  279. }
  280. /* HQ defines dimensions and number of slices, and thus slice traversal
  281. * order. HQA has no size constraint and a fixed number of slices, so it
  282. * needs a separate scheme for it. */
  283. tag = bytestream2_get_le32(&ctx->gbc);
  284. if ((tag & 0x00FFFFFF) == (MKTAG('U', 'V', 'C', ' ') & 0x00FFFFFF)) {
  285. ret = hq_decode_frame(ctx, pic, tag >> 24, data_size);
  286. } else if (tag == MKTAG('H', 'Q', 'A', '1')) {
  287. ret = hqa_decode_frame(ctx, pic, data_size);
  288. } else {
  289. av_log(avctx, AV_LOG_ERROR, "Not a HQ/HQA frame.\n");
  290. return AVERROR_INVALIDDATA;
  291. }
  292. if (ret < 0) {
  293. av_log(avctx, AV_LOG_ERROR, "Error decoding frame.\n");
  294. return ret;
  295. }
  296. pic->key_frame = 1;
  297. pic->pict_type = AV_PICTURE_TYPE_I;
  298. *got_frame = 1;
  299. return avpkt->size;
  300. }
  301. static av_cold int hq_hqa_decode_init(AVCodecContext *avctx)
  302. {
  303. HQContext *ctx = avctx->priv_data;
  304. ctx->avctx = avctx;
  305. ff_hqdsp_init(&ctx->hqhqadsp);
  306. return ff_hq_init_vlcs(ctx);
  307. }
  308. static av_cold int hq_hqa_decode_close(AVCodecContext *avctx)
  309. {
  310. HQContext *ctx = avctx->priv_data;
  311. ff_free_vlc(&ctx->hq_ac_vlc);
  312. ff_free_vlc(&ctx->hqa_cbp_vlc);
  313. return 0;
  314. }
  315. AVCodec ff_hq_hqa_decoder = {
  316. .name = "hq_hqa",
  317. .long_name = NULL_IF_CONFIG_SMALL("Canopus HQ/HQA"),
  318. .type = AVMEDIA_TYPE_VIDEO,
  319. .id = AV_CODEC_ID_HQ_HQA,
  320. .priv_data_size = sizeof(HQContext),
  321. .init = hq_hqa_decode_init,
  322. .decode = hq_hqa_decode_frame,
  323. .close = hq_hqa_decode_close,
  324. .capabilities = AV_CODEC_CAP_DR1,
  325. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  326. FF_CODEC_CAP_INIT_CLEANUP,
  327. };