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.

389 lines
12KB

  1. /*
  2. * Canopus HQ/HQA decoder
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 "get_bits.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, GetBitContext *gb, 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] = get_sbits(gb, 9) * 64;
  62. q = ff_hq_quants[qsel][is_chroma][get_bits(gb, 2)];
  63. } else {
  64. q = ff_hq_quants[qsel][is_chroma][get_bits(gb, 2)];
  65. block[0] = get_sbits(gb, 9) * 64;
  66. }
  67. for (;;) {
  68. val = get_vlc2(gb, 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]] = (int)(ff_hq_ac_syms[val] * (unsigned)q[pos]) >> 12;
  75. pos++;
  76. }
  77. return 0;
  78. }
  79. static int hq_decode_mb(HQContext *c, AVFrame *pic,
  80. GetBitContext *gb, int x, int y)
  81. {
  82. int qgroup, flag;
  83. int i, ret;
  84. qgroup = get_bits(gb, 4);
  85. flag = get_bits1(gb);
  86. for (i = 0; i < 8; i++) {
  87. ret = hq_decode_block(c, gb, 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. GetBitContext gb;
  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 ((unsigned)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. return ret;
  121. /* Offsets are stored from CUV position, so adjust them accordingly. */
  122. for (i = 0; i < profile->num_slices + 1; i++)
  123. slice_off[i] = bytestream2_get_be24(&ctx->gbc) - 4;
  124. next_off = 0;
  125. for (slice = 0; slice < profile->num_slices; slice++) {
  126. start_off = next_off;
  127. next_off = profile->tab_h * (slice + 1) / profile->num_slices;
  128. perm = profile->perm_tab + start_off * profile->tab_w * 2;
  129. if (slice_off[slice] < (profile->num_slices + 1) * 3 ||
  130. slice_off[slice] >= slice_off[slice + 1] ||
  131. slice_off[slice + 1] > data_size) {
  132. av_log(ctx->avctx, AV_LOG_ERROR,
  133. "Invalid slice size %"SIZE_SPECIFIER".\n", data_size);
  134. break;
  135. }
  136. init_get_bits(&gb, src + slice_off[slice],
  137. (slice_off[slice + 1] - slice_off[slice]) * 8);
  138. for (i = 0; i < (next_off - start_off) * profile->tab_w; i++) {
  139. ret = hq_decode_mb(ctx, pic, &gb, perm[0] * 16, perm[1] * 16);
  140. if (ret < 0) {
  141. av_log(ctx->avctx, AV_LOG_ERROR,
  142. "Error decoding macroblock %d at slice %d.\n", i, slice);
  143. return ret;
  144. }
  145. perm += 2;
  146. }
  147. }
  148. return 0;
  149. }
  150. static int hqa_decode_mb(HQContext *c, AVFrame *pic, int qgroup,
  151. GetBitContext *gb, int x, int y)
  152. {
  153. int flag = 0;
  154. int i, ret, cbp;
  155. cbp = get_vlc2(gb, c->hqa_cbp_vlc.table, 5, 1);
  156. for (i = 0; i < 12; i++)
  157. memset(c->block[i], 0, sizeof(*c->block));
  158. for (i = 0; i < 12; i++)
  159. c->block[i][0] = -128 * (1 << 6);
  160. if (cbp) {
  161. flag = get_bits1(gb);
  162. cbp |= cbp << 4;
  163. if (cbp & 0x3)
  164. cbp |= 0x500;
  165. if (cbp & 0xC)
  166. cbp |= 0xA00;
  167. for (i = 0; i < 12; i++) {
  168. if (!(cbp & (1 << i)))
  169. continue;
  170. ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 8, 1);
  171. if (ret < 0)
  172. return ret;
  173. }
  174. }
  175. put_blocks(c, pic, 3, x, y, flag, c->block[ 0], c->block[ 2]);
  176. put_blocks(c, pic, 3, x + 8, y, flag, c->block[ 1], c->block[ 3]);
  177. put_blocks(c, pic, 0, x, y, flag, c->block[ 4], c->block[ 6]);
  178. put_blocks(c, pic, 0, x + 8, y, flag, c->block[ 5], c->block[ 7]);
  179. put_blocks(c, pic, 2, x >> 1, y, flag, c->block[ 8], c->block[ 9]);
  180. put_blocks(c, pic, 1, x >> 1, y, flag, c->block[10], c->block[11]);
  181. return 0;
  182. }
  183. static int hqa_decode_slice(HQContext *ctx, AVFrame *pic, GetBitContext *gb,
  184. int quant, int slice_no, int w, int h)
  185. {
  186. int i, j, off;
  187. int ret;
  188. for (i = 0; i < h; i += 16) {
  189. off = (slice_no * 16 + i * 3) & 0x70;
  190. for (j = off; j < w; j += 128) {
  191. ret = hqa_decode_mb(ctx, pic, quant, gb, j, i);
  192. if (ret < 0) {
  193. av_log(ctx->avctx, AV_LOG_ERROR,
  194. "Error decoding macroblock at %dx%d.\n", i, j);
  195. return ret;
  196. }
  197. }
  198. }
  199. return 0;
  200. }
  201. static int hqa_decode_frame(HQContext *ctx, AVFrame *pic, size_t data_size)
  202. {
  203. GetBitContext gb;
  204. const int num_slices = 8;
  205. uint32_t slice_off[9];
  206. int i, slice, ret;
  207. int width, height, quant;
  208. const uint8_t *src = ctx->gbc.buffer;
  209. width = bytestream2_get_be16(&ctx->gbc);
  210. height = bytestream2_get_be16(&ctx->gbc);
  211. ctx->avctx->coded_width = FFALIGN(width, 16);
  212. ctx->avctx->coded_height = FFALIGN(height, 16);
  213. ctx->avctx->width = width;
  214. ctx->avctx->height = height;
  215. ctx->avctx->bits_per_raw_sample = 8;
  216. ctx->avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
  217. av_log(ctx->avctx, AV_LOG_VERBOSE, "HQA Profile\n");
  218. quant = bytestream2_get_byte(&ctx->gbc);
  219. bytestream2_skip(&ctx->gbc, 3);
  220. if (quant >= NUM_HQ_QUANTS) {
  221. av_log(ctx->avctx, AV_LOG_ERROR,
  222. "Invalid quantization matrix %d.\n", quant);
  223. return AVERROR_INVALIDDATA;
  224. }
  225. ret = ff_get_buffer(ctx->avctx, pic, 0);
  226. if (ret < 0)
  227. return ret;
  228. /* Offsets are stored from HQA1 position, so adjust them accordingly. */
  229. for (i = 0; i < num_slices + 1; i++)
  230. slice_off[i] = bytestream2_get_be32(&ctx->gbc) - 4;
  231. for (slice = 0; slice < num_slices; slice++) {
  232. if (slice_off[slice] < (num_slices + 1) * 3 ||
  233. slice_off[slice] >= slice_off[slice + 1] ||
  234. slice_off[slice + 1] > data_size) {
  235. av_log(ctx->avctx, AV_LOG_ERROR,
  236. "Invalid slice size %"SIZE_SPECIFIER".\n", data_size);
  237. break;
  238. }
  239. init_get_bits(&gb, src + slice_off[slice],
  240. (slice_off[slice + 1] - slice_off[slice]) * 8);
  241. ret = hqa_decode_slice(ctx, pic, &gb, quant, slice, width, height);
  242. if (ret < 0)
  243. return ret;
  244. }
  245. return 0;
  246. }
  247. static int hq_hqa_decode_frame(AVCodecContext *avctx, void *data,
  248. int *got_frame, AVPacket *avpkt)
  249. {
  250. HQContext *ctx = avctx->priv_data;
  251. AVFrame *pic = data;
  252. uint32_t info_tag;
  253. unsigned int data_size;
  254. int ret;
  255. unsigned tag;
  256. bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
  257. if (bytestream2_get_bytes_left(&ctx->gbc) < 4 + 4) {
  258. av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", avpkt->size);
  259. return AVERROR_INVALIDDATA;
  260. }
  261. info_tag = bytestream2_peek_le32(&ctx->gbc);
  262. if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
  263. int info_size;
  264. bytestream2_skip(&ctx->gbc, 4);
  265. info_size = bytestream2_get_le32(&ctx->gbc);
  266. if (bytestream2_get_bytes_left(&ctx->gbc) < info_size) {
  267. av_log(avctx, AV_LOG_ERROR, "Invalid INFO size (%d).\n", info_size);
  268. return AVERROR_INVALIDDATA;
  269. }
  270. ff_canopus_parse_info_tag(avctx, ctx->gbc.buffer, info_size);
  271. bytestream2_skip(&ctx->gbc, info_size);
  272. }
  273. data_size = bytestream2_get_bytes_left(&ctx->gbc);
  274. if (data_size < 4) {
  275. av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", data_size);
  276. return AVERROR_INVALIDDATA;
  277. }
  278. /* HQ defines dimensions and number of slices, and thus slice traversal
  279. * order. HQA has no size constraint and a fixed number of slices, so it
  280. * needs a separate scheme for it. */
  281. tag = bytestream2_get_le32(&ctx->gbc);
  282. if ((tag & 0x00FFFFFF) == (MKTAG('U', 'V', 'C', ' ') & 0x00FFFFFF)) {
  283. ret = hq_decode_frame(ctx, pic, tag >> 24, data_size);
  284. } else if (tag == MKTAG('H', 'Q', 'A', '1')) {
  285. ret = hqa_decode_frame(ctx, pic, data_size);
  286. } else {
  287. av_log(avctx, AV_LOG_ERROR, "Not a HQ/HQA frame.\n");
  288. return AVERROR_INVALIDDATA;
  289. }
  290. if (ret < 0) {
  291. av_log(avctx, AV_LOG_ERROR, "Error decoding frame.\n");
  292. return ret;
  293. }
  294. pic->key_frame = 1;
  295. pic->pict_type = AV_PICTURE_TYPE_I;
  296. *got_frame = 1;
  297. return avpkt->size;
  298. }
  299. static av_cold int hq_hqa_decode_init(AVCodecContext *avctx)
  300. {
  301. HQContext *ctx = avctx->priv_data;
  302. ctx->avctx = avctx;
  303. ff_hqdsp_init(&ctx->hqhqadsp);
  304. return ff_hq_init_vlcs(ctx);
  305. }
  306. static av_cold int hq_hqa_decode_close(AVCodecContext *avctx)
  307. {
  308. HQContext *ctx = avctx->priv_data;
  309. ff_free_vlc(&ctx->hq_ac_vlc);
  310. ff_free_vlc(&ctx->hqa_cbp_vlc);
  311. return 0;
  312. }
  313. AVCodec ff_hq_hqa_decoder = {
  314. .name = "hq_hqa",
  315. .long_name = NULL_IF_CONFIG_SMALL("Canopus HQ/HQA"),
  316. .type = AVMEDIA_TYPE_VIDEO,
  317. .id = AV_CODEC_ID_HQ_HQA,
  318. .priv_data_size = sizeof(HQContext),
  319. .init = hq_hqa_decode_init,
  320. .decode = hq_hqa_decode_frame,
  321. .close = hq_hqa_decode_close,
  322. .capabilities = AV_CODEC_CAP_DR1,
  323. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  324. FF_CODEC_CAP_INIT_CLEANUP,
  325. };