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.

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