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.

691 lines
23KB

  1. /*
  2. * NewTek SpeedHQ codec
  3. * Copyright 2017 Steinar H. Gunderson
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * NewTek SpeedHQ decoder.
  24. */
  25. #define BITSTREAM_READER_LE
  26. #include "libavutil/attributes.h"
  27. #include "avcodec.h"
  28. #include "get_bits.h"
  29. #include "internal.h"
  30. #include "libavutil/thread.h"
  31. #include "mathops.h"
  32. #include "mpeg12.h"
  33. #include "mpeg12data.h"
  34. #include "mpeg12vlc.h"
  35. #define MAX_INDEX (64 - 1)
  36. /*
  37. * 5 bits makes for very small tables, with no more than two lookups needed
  38. * for the longest (10-bit) codes.
  39. */
  40. #define ALPHA_VLC_BITS 5
  41. typedef struct SHQContext {
  42. AVCodecContext *avctx;
  43. BlockDSPContext bdsp;
  44. IDCTDSPContext idsp;
  45. ScanTable intra_scantable;
  46. int quant_matrix[64];
  47. enum { SHQ_SUBSAMPLING_420, SHQ_SUBSAMPLING_422, SHQ_SUBSAMPLING_444 }
  48. subsampling;
  49. enum { SHQ_NO_ALPHA, SHQ_RLE_ALPHA, SHQ_DCT_ALPHA } alpha_type;
  50. } SHQContext;
  51. /* AC codes: Very similar but not identical to MPEG-2. */
  52. static uint16_t speedhq_vlc[123][2] = {
  53. {0x02, 2}, {0x06, 3}, {0x07, 4}, {0x1c, 5},
  54. {0x1d, 5}, {0x05, 6}, {0x04, 6}, {0x7b, 7},
  55. {0x7c, 7}, {0x23, 8}, {0x22, 8}, {0xfa, 8},
  56. {0xfb, 8}, {0xfe, 8}, {0xff, 8}, {0x1f,14},
  57. {0x1e,14}, {0x1d,14}, {0x1c,14}, {0x1b,14},
  58. {0x1a,14}, {0x19,14}, {0x18,14}, {0x17,14},
  59. {0x16,14}, {0x15,14}, {0x14,14}, {0x13,14},
  60. {0x12,14}, {0x11,14}, {0x10,14}, {0x18,15},
  61. {0x17,15}, {0x16,15}, {0x15,15}, {0x14,15},
  62. {0x13,15}, {0x12,15}, {0x11,15}, {0x10,15},
  63. {0x02, 3}, {0x06, 5}, {0x79, 7}, {0x27, 8},
  64. {0x20, 8}, {0x16,13}, {0x15,13}, {0x1f,15},
  65. {0x1e,15}, {0x1d,15}, {0x1c,15}, {0x1b,15},
  66. {0x1a,15}, {0x19,15}, {0x13,16}, {0x12,16},
  67. {0x11,16}, {0x10,16}, {0x18,13}, {0x17,13},
  68. {0x05, 5}, {0x07, 7}, {0xfc, 8}, {0x0c,10},
  69. {0x14,13}, {0x18,12}, {0x14,12}, {0x13,12},
  70. {0x10,12}, {0x1a,13}, {0x19,13}, {0x07, 5},
  71. {0x26, 8}, {0x1c,12}, {0x13,13}, {0x1b,12},
  72. {0x06, 6}, {0xfd, 8}, {0x12,12}, {0x1d,12},
  73. {0x07, 6}, {0x04, 9}, {0x12,13}, {0x06, 7},
  74. {0x1e,12}, {0x14,16}, {0x04, 7}, {0x15,12},
  75. {0x05, 7}, {0x11,12}, {0x78, 7}, {0x11,13},
  76. {0x7a, 7}, {0x10,13}, {0x21, 8}, {0x1a,16},
  77. {0x25, 8}, {0x19,16}, {0x24, 8}, {0x18,16},
  78. {0x05, 9}, {0x17,16}, {0x07, 9}, {0x16,16},
  79. {0x0d,10}, {0x15,16}, {0x1f,12}, {0x1a,12},
  80. {0x19,12}, {0x17,12}, {0x16,12}, {0x1f,13},
  81. {0x1e,13}, {0x1d,13}, {0x1c,13}, {0x1b,13},
  82. {0x1f,16}, {0x1e,16}, {0x1d,16}, {0x1c,16},
  83. {0x1b,16},
  84. {0x01,6}, /* escape */
  85. {0x06,4}, /* EOB */
  86. };
  87. static const uint8_t speedhq_level[121] = {
  88. 1, 2, 3, 4, 5, 6, 7, 8,
  89. 9, 10, 11, 12, 13, 14, 15, 16,
  90. 17, 18, 19, 20, 21, 22, 23, 24,
  91. 25, 26, 27, 28, 29, 30, 31, 32,
  92. 33, 34, 35, 36, 37, 38, 39, 40,
  93. 1, 2, 3, 4, 5, 6, 7, 8,
  94. 9, 10, 11, 12, 13, 14, 15, 16,
  95. 17, 18, 19, 20, 1, 2, 3, 4,
  96. 5, 6, 7, 8, 9, 10, 11, 1,
  97. 2, 3, 4, 5, 1, 2, 3, 4,
  98. 1, 2, 3, 1, 2, 3, 1, 2,
  99. 1, 2, 1, 2, 1, 2, 1, 2,
  100. 1, 2, 1, 2, 1, 2, 1, 2,
  101. 1, 2, 1, 1, 1, 1, 1, 1,
  102. 1, 1, 1, 1, 1, 1, 1, 1,
  103. 1,
  104. };
  105. static const uint8_t speedhq_run[121] = {
  106. 0, 0, 0, 0, 0, 0, 0, 0,
  107. 0, 0, 0, 0, 0, 0, 0, 0,
  108. 0, 0, 0, 0, 0, 0, 0, 0,
  109. 0, 0, 0, 0, 0, 0, 0, 0,
  110. 0, 0, 0, 0, 0, 0, 0, 0,
  111. 1, 1, 1, 1, 1, 1, 1, 1,
  112. 1, 1, 1, 1, 1, 1, 1, 1,
  113. 1, 1, 1, 1, 2, 2, 2, 2,
  114. 2, 2, 2, 2, 2, 2, 2, 3,
  115. 3, 3, 3, 3, 4, 4, 4, 4,
  116. 5, 5, 5, 6, 6, 6, 7, 7,
  117. 8, 8, 9, 9, 10, 10, 11, 11,
  118. 12, 12, 13, 13, 14, 14, 15, 15,
  119. 16, 16, 17, 18, 19, 20, 21, 22,
  120. 23, 24, 25, 26, 27, 28, 29, 30,
  121. 31,
  122. };
  123. static RLTable ff_rl_speedhq = {
  124. 121,
  125. 121,
  126. (const uint16_t (*)[])speedhq_vlc,
  127. speedhq_run,
  128. speedhq_level,
  129. };
  130. /* NOTE: The first element is always 16, unscaled. */
  131. static const uint8_t unscaled_quant_matrix[64] = {
  132. 16, 16, 19, 22, 26, 27, 29, 34,
  133. 16, 16, 22, 24, 27, 29, 34, 37,
  134. 19, 22, 26, 27, 29, 34, 34, 38,
  135. 22, 22, 26, 27, 29, 34, 37, 40,
  136. 22, 26, 27, 29, 32, 35, 40, 48,
  137. 26, 27, 29, 32, 35, 40, 48, 58,
  138. 26, 27, 29, 34, 38, 46, 56, 69,
  139. 27, 29, 35, 38, 46, 56, 69, 83
  140. };
  141. static uint8_t ff_speedhq_static_rl_table_store[2][2*MAX_RUN + MAX_LEVEL + 3];
  142. static VLC ff_dc_lum_vlc_le;
  143. static VLC ff_dc_chroma_vlc_le;
  144. static VLC ff_dc_alpha_run_vlc_le;
  145. static VLC ff_dc_alpha_level_vlc_le;
  146. static inline int decode_dc_le(GetBitContext *gb, int component)
  147. {
  148. int code, diff;
  149. if (component == 0 || component == 3) {
  150. code = get_vlc2(gb, ff_dc_lum_vlc_le.table, DC_VLC_BITS, 2);
  151. } else {
  152. code = get_vlc2(gb, ff_dc_chroma_vlc_le.table, DC_VLC_BITS, 2);
  153. }
  154. if (code < 0) {
  155. av_log(NULL, AV_LOG_ERROR, "invalid dc code at\n");
  156. return 0xffff;
  157. }
  158. if (!code) {
  159. diff = 0;
  160. } else {
  161. diff = get_xbits_le(gb, code);
  162. }
  163. return diff;
  164. }
  165. static inline int decode_alpha_block(const SHQContext *s, GetBitContext *gb, uint8_t last_alpha[16], uint8_t *dest, int linesize)
  166. {
  167. uint8_t block[128];
  168. int i = 0, x, y;
  169. memset(block, 0, sizeof(block));
  170. {
  171. OPEN_READER(re, gb);
  172. for ( ;; ) {
  173. int run, level;
  174. UPDATE_CACHE_LE(re, gb);
  175. GET_VLC(run, re, gb, ff_dc_alpha_run_vlc_le.table, ALPHA_VLC_BITS, 2);
  176. if (run < 0) break;
  177. i += run;
  178. if (i >= 128)
  179. return AVERROR_INVALIDDATA;
  180. UPDATE_CACHE_LE(re, gb);
  181. GET_VLC(level, re, gb, ff_dc_alpha_level_vlc_le.table, ALPHA_VLC_BITS, 2);
  182. block[i++] = level;
  183. }
  184. CLOSE_READER(re, gb);
  185. }
  186. for (y = 0; y < 8; y++) {
  187. for (x = 0; x < 16; x++) {
  188. last_alpha[x] -= block[y * 16 + x];
  189. }
  190. memcpy(dest, last_alpha, 16);
  191. dest += linesize;
  192. }
  193. return 0;
  194. }
  195. static inline int decode_dct_block(const SHQContext *s, GetBitContext *gb, int last_dc[4], int component, uint8_t *dest, int linesize)
  196. {
  197. const int *quant_matrix = s->quant_matrix;
  198. const uint8_t *scantable = s->intra_scantable.permutated;
  199. LOCAL_ALIGNED_32(int16_t, block, [64]);
  200. int dc_offset;
  201. s->bdsp.clear_block(block);
  202. dc_offset = decode_dc_le(gb, component);
  203. last_dc[component] -= dc_offset; /* Note: Opposite of most codecs. */
  204. block[scantable[0]] = last_dc[component]; /* quant_matrix[0] is always 16. */
  205. /* Read AC coefficients. */
  206. {
  207. int i = 0;
  208. OPEN_READER(re, gb);
  209. for ( ;; ) {
  210. int level, run;
  211. UPDATE_CACHE_LE(re, gb);
  212. GET_RL_VLC(level, run, re, gb, ff_rl_speedhq.rl_vlc[0],
  213. TEX_VLC_BITS, 2, 0);
  214. if (level == 127) {
  215. break;
  216. } else if (level) {
  217. i += run;
  218. if (i > MAX_INDEX)
  219. return AVERROR_INVALIDDATA;
  220. /* If next bit is 1, level = -level */
  221. level = (level ^ SHOW_SBITS(re, gb, 1)) -
  222. SHOW_SBITS(re, gb, 1);
  223. LAST_SKIP_BITS(re, gb, 1);
  224. } else {
  225. /* Escape. */
  226. #if MIN_CACHE_BITS < 6 + 6 + 12
  227. #error MIN_CACHE_BITS is too small for the escape code, add UPDATE_CACHE
  228. #endif
  229. run = SHOW_UBITS(re, gb, 6) + 1;
  230. SKIP_BITS(re, gb, 6);
  231. level = SHOW_UBITS(re, gb, 12) - 2048;
  232. LAST_SKIP_BITS(re, gb, 12);
  233. i += run;
  234. if (i > MAX_INDEX)
  235. return AVERROR_INVALIDDATA;
  236. }
  237. block[scantable[i]] = (level * quant_matrix[i]) >> 4;
  238. }
  239. CLOSE_READER(re, gb);
  240. }
  241. s->idsp.idct_put(dest, linesize, block);
  242. return 0;
  243. }
  244. static int decode_speedhq_field(const SHQContext *s, const uint8_t *buf, int buf_size, AVFrame *frame, int field_number, int start, int end, int line_stride)
  245. {
  246. int ret, slice_number, slice_offsets[5];
  247. int linesize_y = frame->linesize[0] * line_stride;
  248. int linesize_cb = frame->linesize[1] * line_stride;
  249. int linesize_cr = frame->linesize[2] * line_stride;
  250. int linesize_a;
  251. if (s->alpha_type != SHQ_NO_ALPHA)
  252. linesize_a = frame->linesize[3] * line_stride;
  253. if (end < start || end - start < 3 || end > buf_size)
  254. return AVERROR_INVALIDDATA;
  255. slice_offsets[0] = start;
  256. slice_offsets[4] = end;
  257. for (slice_number = 1; slice_number < 4; slice_number++) {
  258. uint32_t last_offset, slice_len;
  259. last_offset = slice_offsets[slice_number - 1];
  260. slice_len = AV_RL24(buf + last_offset);
  261. slice_offsets[slice_number] = last_offset + slice_len;
  262. if (slice_len < 3 || slice_offsets[slice_number] > end - 3)
  263. return AVERROR_INVALIDDATA;
  264. }
  265. for (slice_number = 0; slice_number < 4; slice_number++) {
  266. GetBitContext gb;
  267. uint32_t slice_begin, slice_end;
  268. int x, y;
  269. slice_begin = slice_offsets[slice_number];
  270. slice_end = slice_offsets[slice_number + 1];
  271. if ((ret = init_get_bits8(&gb, buf + slice_begin + 3, slice_end - slice_begin - 3)) < 0)
  272. return ret;
  273. for (y = slice_number * 16 * line_stride; y < frame->height; y += line_stride * 64) {
  274. uint8_t *dest_y, *dest_cb, *dest_cr, *dest_a;
  275. int last_dc[4] = { 1024, 1024, 1024, 1024 };
  276. uint8_t last_alpha[16];
  277. memset(last_alpha, 255, sizeof(last_alpha));
  278. dest_y = frame->data[0] + frame->linesize[0] * (y + field_number);
  279. if (s->subsampling == SHQ_SUBSAMPLING_420) {
  280. dest_cb = frame->data[1] + frame->linesize[1] * (y/2 + field_number);
  281. dest_cr = frame->data[2] + frame->linesize[2] * (y/2 + field_number);
  282. } else {
  283. dest_cb = frame->data[1] + frame->linesize[1] * (y + field_number);
  284. dest_cr = frame->data[2] + frame->linesize[2] * (y + field_number);
  285. }
  286. if (s->alpha_type != SHQ_NO_ALPHA) {
  287. dest_a = frame->data[3] + frame->linesize[3] * (y + field_number);
  288. }
  289. for (x = 0; x < frame->width; x += 16) {
  290. /* Decode the four luma blocks. */
  291. if ((ret = decode_dct_block(s, &gb, last_dc, 0, dest_y, linesize_y)) < 0)
  292. return ret;
  293. if ((ret = decode_dct_block(s, &gb, last_dc, 0, dest_y + 8, linesize_y)) < 0)
  294. return ret;
  295. if ((ret = decode_dct_block(s, &gb, last_dc, 0, dest_y + 8 * linesize_y, linesize_y)) < 0)
  296. return ret;
  297. if ((ret = decode_dct_block(s, &gb, last_dc, 0, dest_y + 8 * linesize_y + 8, linesize_y)) < 0)
  298. return ret;
  299. /*
  300. * Decode the first chroma block. For 4:2:0, this is the only one;
  301. * for 4:2:2, it's the top block; for 4:4:4, it's the top-left block.
  302. */
  303. if ((ret = decode_dct_block(s, &gb, last_dc, 1, dest_cb, linesize_cb)) < 0)
  304. return ret;
  305. if ((ret = decode_dct_block(s, &gb, last_dc, 2, dest_cr, linesize_cr)) < 0)
  306. return ret;
  307. if (s->subsampling != SHQ_SUBSAMPLING_420) {
  308. /* For 4:2:2, this is the bottom block; for 4:4:4, it's the bottom-left block. */
  309. if ((ret = decode_dct_block(s, &gb, last_dc, 1, dest_cb + 8 * linesize_cb, linesize_cb)) < 0)
  310. return ret;
  311. if ((ret = decode_dct_block(s, &gb, last_dc, 2, dest_cr + 8 * linesize_cr, linesize_cr)) < 0)
  312. return ret;
  313. if (s->subsampling == SHQ_SUBSAMPLING_444) {
  314. /* Top-right and bottom-right blocks. */
  315. if ((ret = decode_dct_block(s, &gb, last_dc, 1, dest_cb + 8, linesize_cb)) < 0)
  316. return ret;
  317. if ((ret = decode_dct_block(s, &gb, last_dc, 2, dest_cr + 8, linesize_cr)) < 0)
  318. return ret;
  319. if ((ret = decode_dct_block(s, &gb, last_dc, 1, dest_cb + 8 * linesize_cb + 8, linesize_cb)) < 0)
  320. return ret;
  321. if ((ret = decode_dct_block(s, &gb, last_dc, 2, dest_cr + 8 * linesize_cr + 8, linesize_cr)) < 0)
  322. return ret;
  323. dest_cb += 8;
  324. dest_cr += 8;
  325. }
  326. }
  327. dest_y += 16;
  328. dest_cb += 8;
  329. dest_cr += 8;
  330. if (s->alpha_type == SHQ_RLE_ALPHA) {
  331. /* Alpha coded using 16x8 RLE blocks. */
  332. if ((ret = decode_alpha_block(s, &gb, last_alpha, dest_a, linesize_a)) < 0)
  333. return ret;
  334. if ((ret = decode_alpha_block(s, &gb, last_alpha, dest_a + 8 * linesize_a, linesize_a)) < 0)
  335. return ret;
  336. dest_a += 16;
  337. } else if (s->alpha_type == SHQ_DCT_ALPHA) {
  338. /* Alpha encoded exactly like luma. */
  339. if ((ret = decode_dct_block(s, &gb, last_dc, 3, dest_a, linesize_a)) < 0)
  340. return ret;
  341. if ((ret = decode_dct_block(s, &gb, last_dc, 3, dest_a + 8, linesize_a)) < 0)
  342. return ret;
  343. if ((ret = decode_dct_block(s, &gb, last_dc, 3, dest_a + 8 * linesize_a, linesize_a)) < 0)
  344. return ret;
  345. if ((ret = decode_dct_block(s, &gb, last_dc, 3, dest_a + 8 * linesize_a + 8, linesize_a)) < 0)
  346. return ret;
  347. dest_a += 16;
  348. }
  349. }
  350. }
  351. }
  352. return 0;
  353. }
  354. static void compute_quant_matrix(int *output, int qscale)
  355. {
  356. int i;
  357. for (i = 0; i < 64; i++) output[i] = unscaled_quant_matrix[ff_zigzag_direct[i]] * qscale;
  358. }
  359. static int speedhq_decode_frame(AVCodecContext *avctx,
  360. void *data, int *got_frame,
  361. AVPacket *avpkt)
  362. {
  363. SHQContext * const s = avctx->priv_data;
  364. const uint8_t *buf = avpkt->data;
  365. int buf_size = avpkt->size;
  366. AVFrame *frame = data;
  367. uint8_t quality;
  368. uint32_t second_field_offset;
  369. int ret;
  370. if (buf_size < 4)
  371. return AVERROR_INVALIDDATA;
  372. quality = buf[0];
  373. if (quality >= 100) {
  374. return AVERROR_INVALIDDATA;
  375. }
  376. compute_quant_matrix(s->quant_matrix, 100 - quality);
  377. second_field_offset = AV_RL24(buf + 1);
  378. if (second_field_offset >= buf_size - 3) {
  379. return AVERROR_INVALIDDATA;
  380. }
  381. avctx->coded_width = FFALIGN(avctx->width, 16);
  382. avctx->coded_height = FFALIGN(avctx->height, 16);
  383. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  384. return ret;
  385. }
  386. frame->key_frame = 1;
  387. if (second_field_offset == 4) {
  388. /*
  389. * Overlapping first and second fields is used to signal
  390. * encoding only a single field. In this case, "height"
  391. * is ambiguous; it could mean either the height of the
  392. * frame as a whole, or of the field. The former would make
  393. * more sense for compatibility with legacy decoders,
  394. * but this matches the convention used in NDI, which is
  395. * the primary user of this trick.
  396. */
  397. if ((ret = decode_speedhq_field(s, buf, buf_size, frame, 0, 4, buf_size, 1)) < 0)
  398. return ret;
  399. } else {
  400. if ((ret = decode_speedhq_field(s, buf, buf_size, frame, 0, 4, second_field_offset, 2)) < 0)
  401. return ret;
  402. if ((ret = decode_speedhq_field(s, buf, buf_size, frame, 1, second_field_offset, buf_size, 2)) < 0)
  403. return ret;
  404. }
  405. *got_frame = 1;
  406. return buf_size;
  407. }
  408. /*
  409. * Alpha VLC. Run and level are independently coded, and would be
  410. * outside the default limits for MAX_RUN/MAX_LEVEL, so we don't
  411. * bother with combining them into one table.
  412. */
  413. static av_cold void compute_alpha_vlcs(void)
  414. {
  415. uint16_t run_code[134], level_code[266];
  416. uint8_t run_bits[134], level_bits[266];
  417. int16_t run_symbols[134], level_symbols[266];
  418. int entry, i, sign;
  419. /* Initialize VLC for alpha run. */
  420. entry = 0;
  421. /* 0 -> 0. */
  422. run_code[entry] = 0;
  423. run_bits[entry] = 1;
  424. run_symbols[entry] = 0;
  425. ++entry;
  426. /* 10xx -> xx plus 1. */
  427. for (i = 0; i < 4; ++i) {
  428. run_code[entry] = (i << 2) | 1;
  429. run_bits[entry] = 4;
  430. run_symbols[entry] = i + 1;
  431. ++entry;
  432. }
  433. /* 111xxxxxxx -> xxxxxxx. */
  434. for (i = 0; i < 128; ++i) {
  435. run_code[entry] = (i << 3) | 7;
  436. run_bits[entry] = 10;
  437. run_symbols[entry] = i;
  438. ++entry;
  439. }
  440. /* 110 -> EOB. */
  441. run_code[entry] = 3;
  442. run_bits[entry] = 3;
  443. run_symbols[entry] = -1;
  444. ++entry;
  445. av_assert0(entry == FF_ARRAY_ELEMS(run_code));
  446. INIT_LE_VLC_SPARSE_STATIC(&ff_dc_alpha_run_vlc_le, ALPHA_VLC_BITS,
  447. FF_ARRAY_ELEMS(run_code),
  448. run_bits, 1, 1,
  449. run_code, 2, 2,
  450. run_symbols, 2, 2, 160);
  451. /* Initialize VLC for alpha level. */
  452. entry = 0;
  453. for (sign = 0; sign <= 1; ++sign) {
  454. /* 1s -> -1 or +1 (depending on sign bit). */
  455. level_code[entry] = (sign << 1) | 1;
  456. level_bits[entry] = 2;
  457. level_symbols[entry] = sign ? -1 : 1;
  458. ++entry;
  459. /* 01sxx -> xx plus 2 (2..5 or -2..-5, depending on sign bit). */
  460. for (i = 0; i < 4; ++i) {
  461. level_code[entry] = (i << 3) | (sign << 2) | 2;
  462. level_bits[entry] = 5;
  463. level_symbols[entry] = sign ? -(i + 2) : (i + 2);
  464. ++entry;
  465. }
  466. }
  467. /*
  468. * 00xxxxxxxx -> xxxxxxxx, in two's complement. There are many codes
  469. * here that would better be encoded in other ways (e.g. 0 would be
  470. * encoded by increasing run, and +/- 1 would be encoded with a
  471. * shorter code), but it doesn't hurt to allow everything.
  472. */
  473. for (i = 0; i < 256; ++i) {
  474. level_code[entry] = i << 2;
  475. level_bits[entry] = 10;
  476. level_symbols[entry] = i;
  477. ++entry;
  478. }
  479. av_assert0(entry == FF_ARRAY_ELEMS(level_code));
  480. INIT_LE_VLC_SPARSE_STATIC(&ff_dc_alpha_level_vlc_le, ALPHA_VLC_BITS,
  481. FF_ARRAY_ELEMS(level_code),
  482. level_bits, 1, 1,
  483. level_code, 2, 2,
  484. level_symbols, 2, 2, 288);
  485. }
  486. static uint32_t reverse(uint32_t num, int bits)
  487. {
  488. return bitswap_32(num) >> (32 - bits);
  489. }
  490. static void reverse_code(const uint16_t *code, const uint8_t *bits,
  491. uint16_t *reversed_code, int num_entries)
  492. {
  493. int i;
  494. for (i = 0; i < num_entries; i++) {
  495. reversed_code[i] = reverse(code[i], bits[i]);
  496. }
  497. }
  498. static av_cold void speedhq_static_init(void)
  499. {
  500. uint16_t ff_mpeg12_vlc_dc_lum_code_reversed[12];
  501. uint16_t ff_mpeg12_vlc_dc_chroma_code_reversed[12];
  502. int i;
  503. /* Exactly the same as MPEG-2, except little-endian. */
  504. reverse_code(ff_mpeg12_vlc_dc_lum_code,
  505. ff_mpeg12_vlc_dc_lum_bits,
  506. ff_mpeg12_vlc_dc_lum_code_reversed,
  507. 12);
  508. INIT_LE_VLC_STATIC(&ff_dc_lum_vlc_le, DC_VLC_BITS, 12,
  509. ff_mpeg12_vlc_dc_lum_bits, 1, 1,
  510. ff_mpeg12_vlc_dc_lum_code_reversed, 2, 2, 512);
  511. reverse_code(ff_mpeg12_vlc_dc_chroma_code,
  512. ff_mpeg12_vlc_dc_chroma_bits,
  513. ff_mpeg12_vlc_dc_chroma_code_reversed,
  514. 12);
  515. INIT_LE_VLC_STATIC(&ff_dc_chroma_vlc_le, DC_VLC_BITS, 12,
  516. ff_mpeg12_vlc_dc_chroma_bits, 1, 1,
  517. ff_mpeg12_vlc_dc_chroma_code_reversed, 2, 2, 514);
  518. /* Reverse the AC VLC, because INIT_VLC_LE wants it in that order. */
  519. for (i = 0; i < FF_ARRAY_ELEMS(speedhq_vlc); ++i) {
  520. speedhq_vlc[i][0] = reverse(speedhq_vlc[i][0], speedhq_vlc[i][1]);
  521. }
  522. ff_rl_init(&ff_rl_speedhq, ff_speedhq_static_rl_table_store);
  523. INIT_2D_VLC_RL(ff_rl_speedhq, 674, INIT_VLC_LE);
  524. compute_alpha_vlcs();
  525. }
  526. static av_cold int speedhq_decode_init(AVCodecContext *avctx)
  527. {
  528. int ret;
  529. static AVOnce init_once = AV_ONCE_INIT;
  530. SHQContext * const s = avctx->priv_data;
  531. s->avctx = avctx;
  532. ret = ff_thread_once(&init_once, speedhq_static_init);
  533. if (ret)
  534. return AVERROR_UNKNOWN;
  535. ff_blockdsp_init(&s->bdsp, avctx);
  536. ff_idctdsp_init(&s->idsp, avctx);
  537. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
  538. switch (avctx->codec_tag) {
  539. case MKTAG('S', 'H', 'Q', '0'):
  540. s->subsampling = SHQ_SUBSAMPLING_420;
  541. s->alpha_type = SHQ_NO_ALPHA;
  542. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  543. break;
  544. case MKTAG('S', 'H', 'Q', '1'):
  545. s->subsampling = SHQ_SUBSAMPLING_420;
  546. s->alpha_type = SHQ_RLE_ALPHA;
  547. avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
  548. break;
  549. case MKTAG('S', 'H', 'Q', '2'):
  550. s->subsampling = SHQ_SUBSAMPLING_422;
  551. s->alpha_type = SHQ_NO_ALPHA;
  552. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  553. break;
  554. case MKTAG('S', 'H', 'Q', '3'):
  555. s->subsampling = SHQ_SUBSAMPLING_422;
  556. s->alpha_type = SHQ_RLE_ALPHA;
  557. avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
  558. break;
  559. case MKTAG('S', 'H', 'Q', '4'):
  560. s->subsampling = SHQ_SUBSAMPLING_444;
  561. s->alpha_type = SHQ_NO_ALPHA;
  562. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  563. break;
  564. case MKTAG('S', 'H', 'Q', '5'):
  565. s->subsampling = SHQ_SUBSAMPLING_444;
  566. s->alpha_type = SHQ_RLE_ALPHA;
  567. avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
  568. break;
  569. case MKTAG('S', 'H', 'Q', '7'):
  570. s->subsampling = SHQ_SUBSAMPLING_422;
  571. s->alpha_type = SHQ_DCT_ALPHA;
  572. avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
  573. break;
  574. case MKTAG('S', 'H', 'Q', '9'):
  575. s->subsampling = SHQ_SUBSAMPLING_444;
  576. s->alpha_type = SHQ_DCT_ALPHA;
  577. avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
  578. break;
  579. default:
  580. av_log(avctx, AV_LOG_ERROR, "Unknown NewTek SpeedHQ FOURCC provided (%08X)\n",
  581. avctx->codec_tag);
  582. return AVERROR_INVALIDDATA;
  583. }
  584. /* This matches what NDI's RGB -> Y'CbCr 4:2:2 converter uses. */
  585. avctx->colorspace = AVCOL_SPC_BT470BG;
  586. avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
  587. return 0;
  588. }
  589. AVCodec ff_speedhq_decoder = {
  590. .name = "speedhq",
  591. .long_name = NULL_IF_CONFIG_SMALL("NewTek SpeedHQ"),
  592. .type = AVMEDIA_TYPE_VIDEO,
  593. .id = AV_CODEC_ID_SPEEDHQ,
  594. .priv_data_size = sizeof(SHQContext),
  595. .init = speedhq_decode_init,
  596. .decode = speedhq_decode_frame,
  597. .capabilities = AV_CODEC_CAP_DR1,
  598. };