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.

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