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.

682 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 const uint16_t speedhq_vlc[123][2] = {
  53. {0x0001, 2}, {0x0003, 3}, {0x000E, 4}, {0x0007, 5},
  54. {0x0017, 5}, {0x0028, 6}, {0x0008, 6}, {0x006F, 7},
  55. {0x001F, 7}, {0x00C4, 8}, {0x0044, 8}, {0x005F, 8},
  56. {0x00DF, 8}, {0x007F, 8}, {0x00FF, 8}, {0x3E00, 14},
  57. {0x1E00, 14}, {0x2E00, 14}, {0x0E00, 14}, {0x3600, 14},
  58. {0x1600, 14}, {0x2600, 14}, {0x0600, 14}, {0x3A00, 14},
  59. {0x1A00, 14}, {0x2A00, 14}, {0x0A00, 14}, {0x3200, 14},
  60. {0x1200, 14}, {0x2200, 14}, {0x0200, 14}, {0x0C00, 15},
  61. {0x7400, 15}, {0x3400, 15}, {0x5400, 15}, {0x1400, 15},
  62. {0x6400, 15}, {0x2400, 15}, {0x4400, 15}, {0x0400, 15},
  63. {0x0002, 3}, {0x000C, 5}, {0x004F, 7}, {0x00E4, 8},
  64. {0x0004, 8}, {0x0D00, 13}, {0x1500, 13}, {0x7C00, 15},
  65. {0x3C00, 15}, {0x5C00, 15}, {0x1C00, 15}, {0x6C00, 15},
  66. {0x2C00, 15}, {0x4C00, 15}, {0xC800, 16}, {0x4800, 16},
  67. {0x8800, 16}, {0x0800, 16}, {0x0300, 13}, {0x1D00, 13},
  68. {0x0014, 5}, {0x0070, 7}, {0x003F, 8}, {0x00C0, 10},
  69. {0x0500, 13}, {0x0180, 12}, {0x0280, 12}, {0x0C80, 12},
  70. {0x0080, 12}, {0x0B00, 13}, {0x1300, 13}, {0x001C, 5},
  71. {0x0064, 8}, {0x0380, 12}, {0x1900, 13}, {0x0D80, 12},
  72. {0x0018, 6}, {0x00BF, 8}, {0x0480, 12}, {0x0B80, 12},
  73. {0x0038, 6}, {0x0040, 9}, {0x0900, 13}, {0x0030, 7},
  74. {0x0780, 12}, {0x2800, 16}, {0x0010, 7}, {0x0A80, 12},
  75. {0x0050, 7}, {0x0880, 12}, {0x000F, 7}, {0x1100, 13},
  76. {0x002F, 7}, {0x0100, 13}, {0x0084, 8}, {0x5800, 16},
  77. {0x00A4, 8}, {0x9800, 16}, {0x0024, 8}, {0x1800, 16},
  78. {0x0140, 9}, {0xE800, 16}, {0x01C0, 9}, {0x6800, 16},
  79. {0x02C0, 10}, {0xA800, 16}, {0x0F80, 12}, {0x0580, 12},
  80. {0x0980, 12}, {0x0E80, 12}, {0x0680, 12}, {0x1F00, 13},
  81. {0x0F00, 13}, {0x1700, 13}, {0x0700, 13}, {0x1B00, 13},
  82. {0xF800, 16}, {0x7800, 16}, {0xB800, 16}, {0x3800, 16},
  83. {0xD800, 16},
  84. {0x0020, 6}, /* escape */
  85. {0x0006, 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) {
  155. diff = 0;
  156. } else {
  157. diff = get_xbits_le(gb, code);
  158. }
  159. return diff;
  160. }
  161. static inline int decode_alpha_block(const SHQContext *s, GetBitContext *gb, uint8_t last_alpha[16], uint8_t *dest, int linesize)
  162. {
  163. uint8_t block[128];
  164. int i = 0, x, y;
  165. memset(block, 0, sizeof(block));
  166. {
  167. OPEN_READER(re, gb);
  168. for ( ;; ) {
  169. int run, level;
  170. UPDATE_CACHE_LE(re, gb);
  171. GET_VLC(run, re, gb, ff_dc_alpha_run_vlc_le.table, ALPHA_VLC_BITS, 2);
  172. if (run < 0) break;
  173. i += run;
  174. if (i >= 128)
  175. return AVERROR_INVALIDDATA;
  176. UPDATE_CACHE_LE(re, gb);
  177. GET_VLC(level, re, gb, ff_dc_alpha_level_vlc_le.table, ALPHA_VLC_BITS, 2);
  178. block[i++] = level;
  179. }
  180. CLOSE_READER(re, gb);
  181. }
  182. for (y = 0; y < 8; y++) {
  183. for (x = 0; x < 16; x++) {
  184. last_alpha[x] -= block[y * 16 + x];
  185. }
  186. memcpy(dest, last_alpha, 16);
  187. dest += linesize;
  188. }
  189. return 0;
  190. }
  191. static inline int decode_dct_block(const SHQContext *s, GetBitContext *gb, int last_dc[4], int component, uint8_t *dest, int linesize)
  192. {
  193. const int *quant_matrix = s->quant_matrix;
  194. const uint8_t *scantable = s->intra_scantable.permutated;
  195. LOCAL_ALIGNED_32(int16_t, block, [64]);
  196. int dc_offset;
  197. s->bdsp.clear_block(block);
  198. dc_offset = decode_dc_le(gb, component);
  199. last_dc[component] -= dc_offset; /* Note: Opposite of most codecs. */
  200. block[scantable[0]] = last_dc[component]; /* quant_matrix[0] is always 16. */
  201. /* Read AC coefficients. */
  202. {
  203. int i = 0;
  204. OPEN_READER(re, gb);
  205. for ( ;; ) {
  206. int level, run;
  207. UPDATE_CACHE_LE(re, gb);
  208. GET_RL_VLC(level, run, re, gb, ff_rl_speedhq.rl_vlc[0],
  209. TEX_VLC_BITS, 2, 0);
  210. if (level == 127) {
  211. break;
  212. } else if (level) {
  213. i += run;
  214. if (i > MAX_INDEX)
  215. return AVERROR_INVALIDDATA;
  216. /* If next bit is 1, level = -level */
  217. level = (level ^ SHOW_SBITS(re, gb, 1)) -
  218. SHOW_SBITS(re, gb, 1);
  219. LAST_SKIP_BITS(re, gb, 1);
  220. } else {
  221. /* Escape. */
  222. #if MIN_CACHE_BITS < 6 + 6 + 12
  223. #error MIN_CACHE_BITS is too small for the escape code, add UPDATE_CACHE
  224. #endif
  225. run = SHOW_UBITS(re, gb, 6) + 1;
  226. SKIP_BITS(re, gb, 6);
  227. level = SHOW_UBITS(re, gb, 12) - 2048;
  228. LAST_SKIP_BITS(re, gb, 12);
  229. i += run;
  230. if (i > MAX_INDEX)
  231. return AVERROR_INVALIDDATA;
  232. }
  233. block[scantable[i]] = (level * quant_matrix[i]) >> 4;
  234. }
  235. CLOSE_READER(re, gb);
  236. }
  237. s->idsp.idct_put(dest, linesize, block);
  238. return 0;
  239. }
  240. 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)
  241. {
  242. int ret, slice_number, slice_offsets[5];
  243. int linesize_y = frame->linesize[0] * line_stride;
  244. int linesize_cb = frame->linesize[1] * line_stride;
  245. int linesize_cr = frame->linesize[2] * line_stride;
  246. int linesize_a;
  247. if (s->alpha_type != SHQ_NO_ALPHA)
  248. linesize_a = frame->linesize[3] * line_stride;
  249. if (end < start || end - start < 3 || end > buf_size)
  250. return AVERROR_INVALIDDATA;
  251. slice_offsets[0] = start;
  252. slice_offsets[4] = end;
  253. for (slice_number = 1; slice_number < 4; slice_number++) {
  254. uint32_t last_offset, slice_len;
  255. last_offset = slice_offsets[slice_number - 1];
  256. slice_len = AV_RL24(buf + last_offset);
  257. slice_offsets[slice_number] = last_offset + slice_len;
  258. if (slice_len < 3 || slice_offsets[slice_number] > end - 3)
  259. return AVERROR_INVALIDDATA;
  260. }
  261. for (slice_number = 0; slice_number < 4; slice_number++) {
  262. GetBitContext gb;
  263. uint32_t slice_begin, slice_end;
  264. int x, y;
  265. slice_begin = slice_offsets[slice_number];
  266. slice_end = slice_offsets[slice_number + 1];
  267. if ((ret = init_get_bits8(&gb, buf + slice_begin + 3, slice_end - slice_begin - 3)) < 0)
  268. return ret;
  269. for (y = slice_number * 16 * line_stride; y < frame->height; y += line_stride * 64) {
  270. uint8_t *dest_y, *dest_cb, *dest_cr, *dest_a;
  271. int last_dc[4] = { 1024, 1024, 1024, 1024 };
  272. uint8_t last_alpha[16];
  273. memset(last_alpha, 255, sizeof(last_alpha));
  274. dest_y = frame->data[0] + frame->linesize[0] * (y + field_number);
  275. if (s->subsampling == SHQ_SUBSAMPLING_420) {
  276. dest_cb = frame->data[1] + frame->linesize[1] * (y/2 + field_number);
  277. dest_cr = frame->data[2] + frame->linesize[2] * (y/2 + field_number);
  278. } else {
  279. dest_cb = frame->data[1] + frame->linesize[1] * (y + field_number);
  280. dest_cr = frame->data[2] + frame->linesize[2] * (y + field_number);
  281. }
  282. if (s->alpha_type != SHQ_NO_ALPHA) {
  283. dest_a = frame->data[3] + frame->linesize[3] * (y + field_number);
  284. }
  285. for (x = 0; x < frame->width; x += 16) {
  286. /* Decode the four luma blocks. */
  287. if ((ret = decode_dct_block(s, &gb, last_dc, 0, dest_y, linesize_y)) < 0)
  288. return ret;
  289. if ((ret = decode_dct_block(s, &gb, last_dc, 0, dest_y + 8, linesize_y)) < 0)
  290. return ret;
  291. if ((ret = decode_dct_block(s, &gb, last_dc, 0, dest_y + 8 * linesize_y, linesize_y)) < 0)
  292. return ret;
  293. if ((ret = decode_dct_block(s, &gb, last_dc, 0, dest_y + 8 * linesize_y + 8, linesize_y)) < 0)
  294. return ret;
  295. /*
  296. * Decode the first chroma block. For 4:2:0, this is the only one;
  297. * for 4:2:2, it's the top block; for 4:4:4, it's the top-left block.
  298. */
  299. if ((ret = decode_dct_block(s, &gb, last_dc, 1, dest_cb, linesize_cb)) < 0)
  300. return ret;
  301. if ((ret = decode_dct_block(s, &gb, last_dc, 2, dest_cr, linesize_cr)) < 0)
  302. return ret;
  303. if (s->subsampling != SHQ_SUBSAMPLING_420) {
  304. /* For 4:2:2, this is the bottom block; for 4:4:4, it's the bottom-left block. */
  305. if ((ret = decode_dct_block(s, &gb, last_dc, 1, dest_cb + 8 * linesize_cb, linesize_cb)) < 0)
  306. return ret;
  307. if ((ret = decode_dct_block(s, &gb, last_dc, 2, dest_cr + 8 * linesize_cr, linesize_cr)) < 0)
  308. return ret;
  309. if (s->subsampling == SHQ_SUBSAMPLING_444) {
  310. /* Top-right and bottom-right blocks. */
  311. if ((ret = decode_dct_block(s, &gb, last_dc, 1, dest_cb + 8, linesize_cb)) < 0)
  312. return ret;
  313. if ((ret = decode_dct_block(s, &gb, last_dc, 2, dest_cr + 8, linesize_cr)) < 0)
  314. return ret;
  315. if ((ret = decode_dct_block(s, &gb, last_dc, 1, dest_cb + 8 * linesize_cb + 8, linesize_cb)) < 0)
  316. return ret;
  317. if ((ret = decode_dct_block(s, &gb, last_dc, 2, dest_cr + 8 * linesize_cr + 8, linesize_cr)) < 0)
  318. return ret;
  319. dest_cb += 8;
  320. dest_cr += 8;
  321. }
  322. }
  323. dest_y += 16;
  324. dest_cb += 8;
  325. dest_cr += 8;
  326. if (s->alpha_type == SHQ_RLE_ALPHA) {
  327. /* Alpha coded using 16x8 RLE blocks. */
  328. if ((ret = decode_alpha_block(s, &gb, last_alpha, dest_a, linesize_a)) < 0)
  329. return ret;
  330. if ((ret = decode_alpha_block(s, &gb, last_alpha, dest_a + 8 * linesize_a, linesize_a)) < 0)
  331. return ret;
  332. dest_a += 16;
  333. } else if (s->alpha_type == SHQ_DCT_ALPHA) {
  334. /* Alpha encoded exactly like luma. */
  335. if ((ret = decode_dct_block(s, &gb, last_dc, 3, dest_a, linesize_a)) < 0)
  336. return ret;
  337. if ((ret = decode_dct_block(s, &gb, last_dc, 3, dest_a + 8, linesize_a)) < 0)
  338. return ret;
  339. if ((ret = decode_dct_block(s, &gb, last_dc, 3, dest_a + 8 * linesize_a, linesize_a)) < 0)
  340. return ret;
  341. if ((ret = decode_dct_block(s, &gb, last_dc, 3, dest_a + 8 * linesize_a + 8, linesize_a)) < 0)
  342. return ret;
  343. dest_a += 16;
  344. }
  345. }
  346. }
  347. }
  348. return 0;
  349. }
  350. static void compute_quant_matrix(int *output, int qscale)
  351. {
  352. int i;
  353. for (i = 0; i < 64; i++) output[i] = unscaled_quant_matrix[ff_zigzag_direct[i]] * qscale;
  354. }
  355. static int speedhq_decode_frame(AVCodecContext *avctx,
  356. void *data, int *got_frame,
  357. AVPacket *avpkt)
  358. {
  359. SHQContext * const s = avctx->priv_data;
  360. const uint8_t *buf = avpkt->data;
  361. int buf_size = avpkt->size;
  362. AVFrame *frame = data;
  363. uint8_t quality;
  364. uint32_t second_field_offset;
  365. int ret;
  366. if (buf_size < 4)
  367. return AVERROR_INVALIDDATA;
  368. quality = buf[0];
  369. if (quality >= 100) {
  370. return AVERROR_INVALIDDATA;
  371. }
  372. compute_quant_matrix(s->quant_matrix, 100 - quality);
  373. second_field_offset = AV_RL24(buf + 1);
  374. if (second_field_offset >= buf_size - 3) {
  375. return AVERROR_INVALIDDATA;
  376. }
  377. avctx->coded_width = FFALIGN(avctx->width, 16);
  378. avctx->coded_height = FFALIGN(avctx->height, 16);
  379. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  380. return ret;
  381. }
  382. frame->key_frame = 1;
  383. if (second_field_offset == 4 || second_field_offset == (buf_size-4)) {
  384. /*
  385. * Overlapping first and second fields is used to signal
  386. * encoding only a single field. In this case, "height"
  387. * is ambiguous; it could mean either the height of the
  388. * frame as a whole, or of the field. The former would make
  389. * more sense for compatibility with legacy decoders,
  390. * but this matches the convention used in NDI, which is
  391. * the primary user of this trick.
  392. */
  393. if ((ret = decode_speedhq_field(s, buf, buf_size, frame, 0, 4, buf_size, 1)) < 0)
  394. return ret;
  395. } else {
  396. if ((ret = decode_speedhq_field(s, buf, buf_size, frame, 0, 4, second_field_offset, 2)) < 0)
  397. return ret;
  398. if ((ret = decode_speedhq_field(s, buf, buf_size, frame, 1, second_field_offset, buf_size, 2)) < 0)
  399. return ret;
  400. }
  401. *got_frame = 1;
  402. return buf_size;
  403. }
  404. /*
  405. * Alpha VLC. Run and level are independently coded, and would be
  406. * outside the default limits for MAX_RUN/MAX_LEVEL, so we don't
  407. * bother with combining them into one table.
  408. */
  409. static av_cold void compute_alpha_vlcs(void)
  410. {
  411. uint16_t run_code[134], level_code[266];
  412. uint8_t run_bits[134], level_bits[266];
  413. int16_t run_symbols[134], level_symbols[266];
  414. int entry, i, sign;
  415. /* Initialize VLC for alpha run. */
  416. entry = 0;
  417. /* 0 -> 0. */
  418. run_code[entry] = 0;
  419. run_bits[entry] = 1;
  420. run_symbols[entry] = 0;
  421. ++entry;
  422. /* 10xx -> xx plus 1. */
  423. for (i = 0; i < 4; ++i) {
  424. run_code[entry] = (i << 2) | 1;
  425. run_bits[entry] = 4;
  426. run_symbols[entry] = i + 1;
  427. ++entry;
  428. }
  429. /* 111xxxxxxx -> xxxxxxx. */
  430. for (i = 0; i < 128; ++i) {
  431. run_code[entry] = (i << 3) | 7;
  432. run_bits[entry] = 10;
  433. run_symbols[entry] = i;
  434. ++entry;
  435. }
  436. /* 110 -> EOB. */
  437. run_code[entry] = 3;
  438. run_bits[entry] = 3;
  439. run_symbols[entry] = -1;
  440. ++entry;
  441. av_assert0(entry == FF_ARRAY_ELEMS(run_code));
  442. INIT_LE_VLC_SPARSE_STATIC(&ff_dc_alpha_run_vlc_le, ALPHA_VLC_BITS,
  443. FF_ARRAY_ELEMS(run_code),
  444. run_bits, 1, 1,
  445. run_code, 2, 2,
  446. run_symbols, 2, 2, 160);
  447. /* Initialize VLC for alpha level. */
  448. entry = 0;
  449. for (sign = 0; sign <= 1; ++sign) {
  450. /* 1s -> -1 or +1 (depending on sign bit). */
  451. level_code[entry] = (sign << 1) | 1;
  452. level_bits[entry] = 2;
  453. level_symbols[entry] = sign ? -1 : 1;
  454. ++entry;
  455. /* 01sxx -> xx plus 2 (2..5 or -2..-5, depending on sign bit). */
  456. for (i = 0; i < 4; ++i) {
  457. level_code[entry] = (i << 3) | (sign << 2) | 2;
  458. level_bits[entry] = 5;
  459. level_symbols[entry] = sign ? -(i + 2) : (i + 2);
  460. ++entry;
  461. }
  462. }
  463. /*
  464. * 00xxxxxxxx -> xxxxxxxx, in two's complement. There are many codes
  465. * here that would better be encoded in other ways (e.g. 0 would be
  466. * encoded by increasing run, and +/- 1 would be encoded with a
  467. * shorter code), but it doesn't hurt to allow everything.
  468. */
  469. for (i = 0; i < 256; ++i) {
  470. level_code[entry] = i << 2;
  471. level_bits[entry] = 10;
  472. level_symbols[entry] = i;
  473. ++entry;
  474. }
  475. av_assert0(entry == FF_ARRAY_ELEMS(level_code));
  476. INIT_LE_VLC_SPARSE_STATIC(&ff_dc_alpha_level_vlc_le, ALPHA_VLC_BITS,
  477. FF_ARRAY_ELEMS(level_code),
  478. level_bits, 1, 1,
  479. level_code, 2, 2,
  480. level_symbols, 2, 2, 288);
  481. }
  482. static uint32_t reverse(uint32_t num, int bits)
  483. {
  484. return bitswap_32(num) >> (32 - bits);
  485. }
  486. static void reverse_code(const uint16_t *code, const uint8_t *bits,
  487. uint16_t *reversed_code, int num_entries)
  488. {
  489. int i;
  490. for (i = 0; i < num_entries; i++) {
  491. reversed_code[i] = reverse(code[i], bits[i]);
  492. }
  493. }
  494. static av_cold void speedhq_static_init(void)
  495. {
  496. uint16_t ff_mpeg12_vlc_dc_lum_code_reversed[12];
  497. uint16_t ff_mpeg12_vlc_dc_chroma_code_reversed[12];
  498. /* Exactly the same as MPEG-2, except little-endian. */
  499. reverse_code(ff_mpeg12_vlc_dc_lum_code,
  500. ff_mpeg12_vlc_dc_lum_bits,
  501. ff_mpeg12_vlc_dc_lum_code_reversed,
  502. 12);
  503. INIT_LE_VLC_STATIC(&ff_dc_lum_vlc_le, DC_VLC_BITS, 12,
  504. ff_mpeg12_vlc_dc_lum_bits, 1, 1,
  505. ff_mpeg12_vlc_dc_lum_code_reversed, 2, 2, 512);
  506. reverse_code(ff_mpeg12_vlc_dc_chroma_code,
  507. ff_mpeg12_vlc_dc_chroma_bits,
  508. ff_mpeg12_vlc_dc_chroma_code_reversed,
  509. 12);
  510. INIT_LE_VLC_STATIC(&ff_dc_chroma_vlc_le, DC_VLC_BITS, 12,
  511. ff_mpeg12_vlc_dc_chroma_bits, 1, 1,
  512. ff_mpeg12_vlc_dc_chroma_code_reversed, 2, 2, 514);
  513. ff_rl_init(&ff_rl_speedhq, ff_speedhq_static_rl_table_store);
  514. INIT_2D_VLC_RL(ff_rl_speedhq, 674, INIT_VLC_LE);
  515. compute_alpha_vlcs();
  516. }
  517. static av_cold int speedhq_decode_init(AVCodecContext *avctx)
  518. {
  519. int ret;
  520. static AVOnce init_once = AV_ONCE_INIT;
  521. SHQContext * const s = avctx->priv_data;
  522. s->avctx = avctx;
  523. ret = ff_thread_once(&init_once, speedhq_static_init);
  524. if (ret)
  525. return AVERROR_UNKNOWN;
  526. ff_blockdsp_init(&s->bdsp, avctx);
  527. ff_idctdsp_init(&s->idsp, avctx);
  528. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
  529. switch (avctx->codec_tag) {
  530. case MKTAG('S', 'H', 'Q', '0'):
  531. s->subsampling = SHQ_SUBSAMPLING_420;
  532. s->alpha_type = SHQ_NO_ALPHA;
  533. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  534. break;
  535. case MKTAG('S', 'H', 'Q', '1'):
  536. s->subsampling = SHQ_SUBSAMPLING_420;
  537. s->alpha_type = SHQ_RLE_ALPHA;
  538. avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
  539. break;
  540. case MKTAG('S', 'H', 'Q', '2'):
  541. s->subsampling = SHQ_SUBSAMPLING_422;
  542. s->alpha_type = SHQ_NO_ALPHA;
  543. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  544. break;
  545. case MKTAG('S', 'H', 'Q', '3'):
  546. s->subsampling = SHQ_SUBSAMPLING_422;
  547. s->alpha_type = SHQ_RLE_ALPHA;
  548. avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
  549. break;
  550. case MKTAG('S', 'H', 'Q', '4'):
  551. s->subsampling = SHQ_SUBSAMPLING_444;
  552. s->alpha_type = SHQ_NO_ALPHA;
  553. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  554. break;
  555. case MKTAG('S', 'H', 'Q', '5'):
  556. s->subsampling = SHQ_SUBSAMPLING_444;
  557. s->alpha_type = SHQ_RLE_ALPHA;
  558. avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
  559. break;
  560. case MKTAG('S', 'H', 'Q', '7'):
  561. s->subsampling = SHQ_SUBSAMPLING_422;
  562. s->alpha_type = SHQ_DCT_ALPHA;
  563. avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
  564. break;
  565. case MKTAG('S', 'H', 'Q', '9'):
  566. s->subsampling = SHQ_SUBSAMPLING_444;
  567. s->alpha_type = SHQ_DCT_ALPHA;
  568. avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
  569. break;
  570. default:
  571. av_log(avctx, AV_LOG_ERROR, "Unknown NewTek SpeedHQ FOURCC provided (%08X)\n",
  572. avctx->codec_tag);
  573. return AVERROR_INVALIDDATA;
  574. }
  575. /* This matches what NDI's RGB -> Y'CbCr 4:2:2 converter uses. */
  576. avctx->colorspace = AVCOL_SPC_BT470BG;
  577. avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
  578. return 0;
  579. }
  580. AVCodec ff_speedhq_decoder = {
  581. .name = "speedhq",
  582. .long_name = NULL_IF_CONFIG_SMALL("NewTek SpeedHQ"),
  583. .type = AVMEDIA_TYPE_VIDEO,
  584. .id = AV_CODEC_ID_SPEEDHQ,
  585. .priv_data_size = sizeof(SHQContext),
  586. .init = speedhq_decode_init,
  587. .decode = speedhq_decode_frame,
  588. .capabilities = AV_CODEC_CAP_DR1,
  589. };