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.

660 lines
22KB

  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. 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 == 128) 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. 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[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 (the second field then comes
  391. * as a separate, later frame).
  392. */
  393. frame->height >>= 1;
  394. if ((ret = decode_speedhq_field(s, buf, buf_size, frame, 0, 4, buf_size, 1)) < 0)
  395. return ret;
  396. } else {
  397. if ((ret = decode_speedhq_field(s, buf, buf_size, frame, 0, 4, second_field_offset, 2)) < 0)
  398. return ret;
  399. if ((ret = decode_speedhq_field(s, buf, buf_size, frame, 1, second_field_offset, buf_size, 2)) < 0)
  400. return ret;
  401. }
  402. *got_frame = 1;
  403. return buf_size;
  404. }
  405. /*
  406. * Alpha VLC. Run and level are independently coded, and would be
  407. * outside the default limits for MAX_RUN/MAX_LEVEL, so we don't
  408. * bother with combining them into one table.
  409. */
  410. static av_cold void compute_alpha_vlcs(void)
  411. {
  412. uint16_t run_code[129], level_code[256];
  413. uint8_t run_bits[129], level_bits[256];
  414. int run, level;
  415. for (run = 0; run < 128; run++) {
  416. if (!run) {
  417. /* 0 -> 0. */
  418. run_code[run] = 0;
  419. run_bits[run] = 1;
  420. } else if (run <= 4) {
  421. /* 10xx -> xx plus 1. */
  422. run_code[run] = ((run - 1) << 2) | 1;
  423. run_bits[run] = 4;
  424. } else {
  425. /* 111xxxxxxx -> xxxxxxxx. */
  426. run_code[run] = (run << 3) | 7;
  427. run_bits[run] = 10;
  428. }
  429. }
  430. /* 110 -> EOB. */
  431. run_code[128] = 3;
  432. run_bits[128] = 3;
  433. INIT_LE_VLC_STATIC(&ff_dc_alpha_run_vlc_le, ALPHA_VLC_BITS, 129,
  434. run_bits, 1, 1,
  435. run_code, 2, 2, 160);
  436. for (level = 0; level < 256; level++) {
  437. int8_t signed_level = (int8_t)level;
  438. int abs_signed_level = abs(signed_level);
  439. int sign = (signed_level < 0) ? 1 : 0;
  440. if (abs_signed_level == 1) {
  441. /* 1s -> -1 or +1 (depending on sign bit). */
  442. level_code[level] = (sign << 1) | 1;
  443. level_bits[level] = 2;
  444. } else if (abs_signed_level >= 2 && abs_signed_level <= 5) {
  445. /* 01sxx -> xx plus 2 (2..5 or -2..-5, depending on sign bit). */
  446. level_code[level] = ((abs_signed_level - 2) << 3) | (sign << 2) | 2;
  447. level_bits[level] = 5;
  448. } else {
  449. /*
  450. * 00xxxxxxxx -> xxxxxxxx, in two's complement. 0 is technically an
  451. * illegal code (that would be encoded by increasing run), but it
  452. * doesn't hurt and simplifies indexing.
  453. */
  454. level_code[level] = level << 2;
  455. level_bits[level] = 10;
  456. }
  457. }
  458. INIT_LE_VLC_STATIC(&ff_dc_alpha_level_vlc_le, ALPHA_VLC_BITS, 256,
  459. level_bits, 1, 1,
  460. level_code, 2, 2, 288);
  461. }
  462. static uint32_t reverse(uint32_t num, int bits)
  463. {
  464. return bitswap_32(num) >> (32 - bits);
  465. }
  466. static void reverse_code(const uint16_t *code, const uint8_t *bits,
  467. uint16_t *reversed_code, int num_entries)
  468. {
  469. int i;
  470. for (i = 0; i < num_entries; i++) {
  471. reversed_code[i] = reverse(code[i], bits[i]);
  472. }
  473. }
  474. static av_cold void speedhq_static_init(void)
  475. {
  476. uint16_t ff_mpeg12_vlc_dc_lum_code_reversed[12];
  477. uint16_t ff_mpeg12_vlc_dc_chroma_code_reversed[12];
  478. int i;
  479. /* Exactly the same as MPEG-2, except little-endian. */
  480. reverse_code(ff_mpeg12_vlc_dc_lum_code,
  481. ff_mpeg12_vlc_dc_lum_bits,
  482. ff_mpeg12_vlc_dc_lum_code_reversed,
  483. 12);
  484. INIT_LE_VLC_STATIC(&ff_dc_lum_vlc_le, DC_VLC_BITS, 12,
  485. ff_mpeg12_vlc_dc_lum_bits, 1, 1,
  486. ff_mpeg12_vlc_dc_lum_code_reversed, 2, 2, 512);
  487. reverse_code(ff_mpeg12_vlc_dc_chroma_code,
  488. ff_mpeg12_vlc_dc_chroma_bits,
  489. ff_mpeg12_vlc_dc_chroma_code_reversed,
  490. 12);
  491. INIT_LE_VLC_STATIC(&ff_dc_chroma_vlc_le, DC_VLC_BITS, 12,
  492. ff_mpeg12_vlc_dc_chroma_bits, 1, 1,
  493. ff_mpeg12_vlc_dc_chroma_code_reversed, 2, 2, 514);
  494. /* Reverse the AC VLC, because INIT_VLC_LE wants it in that order. */
  495. for (i = 0; i < FF_ARRAY_ELEMS(speedhq_vlc); ++i) {
  496. speedhq_vlc[i][0] = reverse(speedhq_vlc[i][0], speedhq_vlc[i][1]);
  497. }
  498. ff_rl_init(&ff_rl_speedhq, ff_speedhq_static_rl_table_store);
  499. INIT_2D_VLC_RL(ff_rl_speedhq, 674, INIT_VLC_LE);
  500. compute_alpha_vlcs();
  501. }
  502. static av_cold int speedhq_decode_init(AVCodecContext *avctx)
  503. {
  504. int ret;
  505. static AVOnce init_once = AV_ONCE_INIT;
  506. SHQContext * const s = avctx->priv_data;
  507. s->avctx = avctx;
  508. ret = ff_thread_once(&init_once, speedhq_static_init);
  509. if (ret)
  510. return AVERROR_UNKNOWN;
  511. ff_blockdsp_init(&s->bdsp, avctx);
  512. ff_idctdsp_init(&s->idsp, avctx);
  513. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
  514. switch (avctx->codec_tag) {
  515. case MKTAG('S', 'H', 'Q', '0'):
  516. s->subsampling = SHQ_SUBSAMPLING_420;
  517. s->alpha_type = SHQ_NO_ALPHA;
  518. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  519. break;
  520. case MKTAG('S', 'H', 'Q', '1'):
  521. s->subsampling = SHQ_SUBSAMPLING_420;
  522. s->alpha_type = SHQ_RLE_ALPHA;
  523. avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
  524. break;
  525. case MKTAG('S', 'H', 'Q', '2'):
  526. s->subsampling = SHQ_SUBSAMPLING_422;
  527. s->alpha_type = SHQ_NO_ALPHA;
  528. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  529. break;
  530. case MKTAG('S', 'H', 'Q', '3'):
  531. s->subsampling = SHQ_SUBSAMPLING_422;
  532. s->alpha_type = SHQ_RLE_ALPHA;
  533. avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
  534. break;
  535. case MKTAG('S', 'H', 'Q', '4'):
  536. s->subsampling = SHQ_SUBSAMPLING_444;
  537. s->alpha_type = SHQ_NO_ALPHA;
  538. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  539. break;
  540. case MKTAG('S', 'H', 'Q', '5'):
  541. s->subsampling = SHQ_SUBSAMPLING_444;
  542. s->alpha_type = SHQ_RLE_ALPHA;
  543. avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
  544. break;
  545. case MKTAG('S', 'H', 'Q', '7'):
  546. s->subsampling = SHQ_SUBSAMPLING_422;
  547. s->alpha_type = SHQ_DCT_ALPHA;
  548. avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
  549. break;
  550. case MKTAG('S', 'H', 'Q', '9'):
  551. s->subsampling = SHQ_SUBSAMPLING_444;
  552. s->alpha_type = SHQ_DCT_ALPHA;
  553. avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
  554. break;
  555. default:
  556. av_log(avctx, AV_LOG_ERROR, "Unknown NewTek SpeedHQ FOURCC provided (%08X)\n",
  557. avctx->codec_tag);
  558. return AVERROR_INVALIDDATA;
  559. }
  560. /* This matches what NDI's RGB -> Y'CbCr 4:2:2 converter uses. */
  561. avctx->colorspace = AVCOL_SPC_BT470BG;
  562. avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
  563. return 0;
  564. }
  565. AVCodec ff_speedhq_decoder = {
  566. .name = "speedhq",
  567. .long_name = NULL_IF_CONFIG_SMALL("NewTek SpeedHQ"),
  568. .type = AVMEDIA_TYPE_VIDEO,
  569. .id = AV_CODEC_ID_SPEEDHQ,
  570. .priv_data_size = sizeof(SHQContext),
  571. .init = speedhq_decode_init,
  572. .decode = speedhq_decode_frame,
  573. .capabilities = AV_CODEC_CAP_DR1,
  574. };