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.

1268 lines
41KB

  1. /*
  2. * Copyright (C) 2016 Open Broadcast Systems Ltd.
  3. * Author 2016 Rostislav Pehlivanov <atomnuker@gmail.com>
  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. #include "libavutil/pixdesc.h"
  22. #include "libavutil/opt.h"
  23. #include "dirac.h"
  24. #include "put_bits.h"
  25. #include "internal.h"
  26. #include "version.h"
  27. #include "vc2enc_dwt.h"
  28. #include "diractab.h"
  29. /* Quantizations above this usually zero coefficients and lower the quality */
  30. #define MAX_QUANT_INDEX FF_ARRAY_ELEMS(ff_dirac_qscale_tab)
  31. /* Total range is -COEF_LUT_TAB to +COEFF_LUT_TAB, but total tab size is half
  32. * (COEF_LUT_TAB*MAX_QUANT_INDEX) since the sign is appended during encoding */
  33. #define COEF_LUT_TAB 2048
  34. /* Decides the cutoff point in # of slices to distribute the leftover bytes */
  35. #define SLICE_REDIST_TOTAL 150
  36. enum VC2_QM {
  37. VC2_QM_DEF = 0,
  38. VC2_QM_COL,
  39. VC2_QM_FLAT,
  40. VC2_QM_NB
  41. };
  42. typedef struct SubBand {
  43. dwtcoef *buf;
  44. ptrdiff_t stride;
  45. int width;
  46. int height;
  47. } SubBand;
  48. typedef struct Plane {
  49. SubBand band[MAX_DWT_LEVELS][4];
  50. dwtcoef *coef_buf;
  51. int width;
  52. int height;
  53. int dwt_width;
  54. int dwt_height;
  55. ptrdiff_t coef_stride;
  56. } Plane;
  57. typedef struct SliceArgs {
  58. PutBitContext pb;
  59. int cache[MAX_QUANT_INDEX];
  60. void *ctx;
  61. int x;
  62. int y;
  63. int quant_idx;
  64. int bits_ceil;
  65. int bits_floor;
  66. int bytes_left;
  67. int bytes;
  68. } SliceArgs;
  69. typedef struct TransformArgs {
  70. void *ctx;
  71. Plane *plane;
  72. void *idata;
  73. ptrdiff_t istride;
  74. int field;
  75. VC2TransformContext t;
  76. } TransformArgs;
  77. typedef struct VC2EncContext {
  78. AVClass *av_class;
  79. PutBitContext pb;
  80. Plane plane[3];
  81. AVCodecContext *avctx;
  82. DiracVersionInfo ver;
  83. SliceArgs *slice_args;
  84. TransformArgs transform_args[3];
  85. /* For conversion from unsigned pixel values to signed */
  86. int diff_offset;
  87. int bpp;
  88. int bpp_idx;
  89. /* Picture number */
  90. uint32_t picture_number;
  91. /* Base video format */
  92. int base_vf;
  93. int level;
  94. int profile;
  95. /* Quantization matrix */
  96. uint8_t quant[MAX_DWT_LEVELS][4];
  97. int custom_quant_matrix;
  98. /* Coefficient LUT */
  99. uint32_t *coef_lut_val;
  100. uint8_t *coef_lut_len;
  101. int num_x; /* #slices horizontally */
  102. int num_y; /* #slices vertically */
  103. int prefix_bytes;
  104. int size_scaler;
  105. int chroma_x_shift;
  106. int chroma_y_shift;
  107. /* Rate control stuff */
  108. int slice_max_bytes;
  109. int slice_min_bytes;
  110. int q_ceil;
  111. int q_avg;
  112. /* Options */
  113. double tolerance;
  114. int wavelet_idx;
  115. int wavelet_depth;
  116. int strict_compliance;
  117. int slice_height;
  118. int slice_width;
  119. int interlaced;
  120. enum VC2_QM quant_matrix;
  121. /* Parse code state */
  122. uint32_t next_parse_offset;
  123. enum DiracParseCodes last_parse_code;
  124. } VC2EncContext;
  125. static av_always_inline void put_vc2_ue_uint(PutBitContext *pb, uint32_t val)
  126. {
  127. int i;
  128. int pbits = 0, bits = 0, topbit = 1, maxval = 1;
  129. if (!val++) {
  130. put_bits(pb, 1, 1);
  131. return;
  132. }
  133. while (val > maxval) {
  134. topbit <<= 1;
  135. maxval <<= 1;
  136. maxval |= 1;
  137. }
  138. bits = ff_log2(topbit);
  139. for (i = 0; i < bits; i++) {
  140. topbit >>= 1;
  141. pbits <<= 2;
  142. if (val & topbit)
  143. pbits |= 0x1;
  144. }
  145. put_bits(pb, bits*2 + 1, (pbits << 1) | 1);
  146. }
  147. static av_always_inline int count_vc2_ue_uint(uint32_t val)
  148. {
  149. int topbit = 1, maxval = 1;
  150. if (!val++)
  151. return 1;
  152. while (val > maxval) {
  153. topbit <<= 1;
  154. maxval <<= 1;
  155. maxval |= 1;
  156. }
  157. return ff_log2(topbit)*2 + 1;
  158. }
  159. static av_always_inline void get_vc2_ue_uint(int val, uint8_t *nbits,
  160. uint32_t *eval)
  161. {
  162. int i;
  163. int pbits = 0, bits = 0, topbit = 1, maxval = 1;
  164. if (!val++) {
  165. *nbits = 1;
  166. *eval = 1;
  167. return;
  168. }
  169. while (val > maxval) {
  170. topbit <<= 1;
  171. maxval <<= 1;
  172. maxval |= 1;
  173. }
  174. bits = ff_log2(topbit);
  175. for (i = 0; i < bits; i++) {
  176. topbit >>= 1;
  177. pbits <<= 2;
  178. if (val & topbit)
  179. pbits |= 0x1;
  180. }
  181. *nbits = bits*2 + 1;
  182. *eval = (pbits << 1) | 1;
  183. }
  184. /* VC-2 10.4 - parse_info() */
  185. static void encode_parse_info(VC2EncContext *s, enum DiracParseCodes pcode)
  186. {
  187. uint32_t cur_pos, dist;
  188. avpriv_align_put_bits(&s->pb);
  189. cur_pos = put_bits_count(&s->pb) >> 3;
  190. /* Magic string */
  191. avpriv_put_string(&s->pb, "BBCD", 0);
  192. /* Parse code */
  193. put_bits(&s->pb, 8, pcode);
  194. /* Next parse offset */
  195. dist = cur_pos - s->next_parse_offset;
  196. AV_WB32(s->pb.buf + s->next_parse_offset + 5, dist);
  197. s->next_parse_offset = cur_pos;
  198. put_bits32(&s->pb, pcode == DIRAC_PCODE_END_SEQ ? 13 : 0);
  199. /* Last parse offset */
  200. put_bits32(&s->pb, s->last_parse_code == DIRAC_PCODE_END_SEQ ? 13 : dist);
  201. s->last_parse_code = pcode;
  202. }
  203. /* VC-2 11.1 - parse_parameters()
  204. * The level dictates what the decoder should expect in terms of resolution
  205. * and allows it to quickly reject whatever it can't support. Remember,
  206. * this codec kinda targets cheapo FPGAs without much memory. Unfortunately
  207. * it also limits us greatly in our choice of formats, hence the flag to disable
  208. * strict_compliance */
  209. static void encode_parse_params(VC2EncContext *s)
  210. {
  211. put_vc2_ue_uint(&s->pb, s->ver.major); /* VC-2 demands this to be 2 */
  212. put_vc2_ue_uint(&s->pb, s->ver.minor); /* ^^ and this to be 0 */
  213. put_vc2_ue_uint(&s->pb, s->profile); /* 3 to signal HQ profile */
  214. put_vc2_ue_uint(&s->pb, s->level); /* 3 - 1080/720, 6 - 4K */
  215. }
  216. /* VC-2 11.3 - frame_size() */
  217. static void encode_frame_size(VC2EncContext *s)
  218. {
  219. put_bits(&s->pb, 1, !s->strict_compliance);
  220. if (!s->strict_compliance) {
  221. AVCodecContext *avctx = s->avctx;
  222. put_vc2_ue_uint(&s->pb, avctx->width);
  223. put_vc2_ue_uint(&s->pb, avctx->height);
  224. }
  225. }
  226. /* VC-2 11.3.3 - color_diff_sampling_format() */
  227. static void encode_sample_fmt(VC2EncContext *s)
  228. {
  229. put_bits(&s->pb, 1, !s->strict_compliance);
  230. if (!s->strict_compliance) {
  231. int idx;
  232. if (s->chroma_x_shift == 1 && s->chroma_y_shift == 0)
  233. idx = 1; /* 422 */
  234. else if (s->chroma_x_shift == 1 && s->chroma_y_shift == 1)
  235. idx = 2; /* 420 */
  236. else
  237. idx = 0; /* 444 */
  238. put_vc2_ue_uint(&s->pb, idx);
  239. }
  240. }
  241. /* VC-2 11.3.4 - scan_format() */
  242. static void encode_scan_format(VC2EncContext *s)
  243. {
  244. put_bits(&s->pb, 1, !s->strict_compliance);
  245. if (!s->strict_compliance)
  246. put_vc2_ue_uint(&s->pb, s->interlaced);
  247. }
  248. /* VC-2 11.3.5 - frame_rate() */
  249. static void encode_frame_rate(VC2EncContext *s)
  250. {
  251. put_bits(&s->pb, 1, !s->strict_compliance);
  252. if (!s->strict_compliance) {
  253. AVCodecContext *avctx = s->avctx;
  254. put_vc2_ue_uint(&s->pb, 0);
  255. put_vc2_ue_uint(&s->pb, avctx->time_base.den);
  256. put_vc2_ue_uint(&s->pb, avctx->time_base.num);
  257. }
  258. }
  259. /* VC-2 11.3.6 - aspect_ratio() */
  260. static void encode_aspect_ratio(VC2EncContext *s)
  261. {
  262. put_bits(&s->pb, 1, !s->strict_compliance);
  263. if (!s->strict_compliance) {
  264. AVCodecContext *avctx = s->avctx;
  265. put_vc2_ue_uint(&s->pb, 0);
  266. put_vc2_ue_uint(&s->pb, avctx->sample_aspect_ratio.num);
  267. put_vc2_ue_uint(&s->pb, avctx->sample_aspect_ratio.den);
  268. }
  269. }
  270. /* VC-2 11.3.7 - clean_area() */
  271. static void encode_clean_area(VC2EncContext *s)
  272. {
  273. put_bits(&s->pb, 1, 0);
  274. }
  275. /* VC-2 11.3.8 - signal_range() */
  276. static void encode_signal_range(VC2EncContext *s)
  277. {
  278. put_bits(&s->pb, 1, !s->strict_compliance);
  279. if (!s->strict_compliance)
  280. put_vc2_ue_uint(&s->pb, s->bpp_idx);
  281. }
  282. /* VC-2 11.3.9 - color_spec() */
  283. static void encode_color_spec(VC2EncContext *s)
  284. {
  285. AVCodecContext *avctx = s->avctx;
  286. put_bits(&s->pb, 1, !s->strict_compliance);
  287. if (!s->strict_compliance) {
  288. int val;
  289. put_vc2_ue_uint(&s->pb, 0);
  290. /* primaries */
  291. put_bits(&s->pb, 1, 1);
  292. if (avctx->color_primaries == AVCOL_PRI_BT470BG)
  293. val = 2;
  294. else if (avctx->color_primaries == AVCOL_PRI_SMPTE170M)
  295. val = 1;
  296. else if (avctx->color_primaries == AVCOL_PRI_SMPTE240M)
  297. val = 1;
  298. else
  299. val = 0;
  300. put_vc2_ue_uint(&s->pb, val);
  301. /* color matrix */
  302. put_bits(&s->pb, 1, 1);
  303. if (avctx->colorspace == AVCOL_SPC_RGB)
  304. val = 3;
  305. else if (avctx->colorspace == AVCOL_SPC_YCOCG)
  306. val = 2;
  307. else if (avctx->colorspace == AVCOL_SPC_BT470BG)
  308. val = 1;
  309. else
  310. val = 0;
  311. put_vc2_ue_uint(&s->pb, val);
  312. /* transfer function */
  313. put_bits(&s->pb, 1, 1);
  314. if (avctx->color_trc == AVCOL_TRC_LINEAR)
  315. val = 2;
  316. else if (avctx->color_trc == AVCOL_TRC_BT1361_ECG)
  317. val = 1;
  318. else
  319. val = 0;
  320. put_vc2_ue_uint(&s->pb, val);
  321. }
  322. }
  323. /* VC-2 11.3 - source_parameters() */
  324. static void encode_source_params(VC2EncContext *s)
  325. {
  326. encode_frame_size(s);
  327. encode_sample_fmt(s);
  328. encode_scan_format(s);
  329. encode_frame_rate(s);
  330. encode_aspect_ratio(s);
  331. encode_clean_area(s);
  332. encode_signal_range(s);
  333. encode_color_spec(s);
  334. }
  335. /* VC-2 11 - sequence_header() */
  336. static void encode_seq_header(VC2EncContext *s)
  337. {
  338. avpriv_align_put_bits(&s->pb);
  339. encode_parse_params(s);
  340. put_vc2_ue_uint(&s->pb, s->base_vf);
  341. encode_source_params(s);
  342. put_vc2_ue_uint(&s->pb, s->interlaced); /* Frames or fields coding */
  343. }
  344. /* VC-2 12.1 - picture_header() */
  345. static void encode_picture_header(VC2EncContext *s)
  346. {
  347. avpriv_align_put_bits(&s->pb);
  348. put_bits32(&s->pb, s->picture_number++);
  349. }
  350. /* VC-2 12.3.4.1 - slice_parameters() */
  351. static void encode_slice_params(VC2EncContext *s)
  352. {
  353. put_vc2_ue_uint(&s->pb, s->num_x);
  354. put_vc2_ue_uint(&s->pb, s->num_y);
  355. put_vc2_ue_uint(&s->pb, s->prefix_bytes);
  356. put_vc2_ue_uint(&s->pb, s->size_scaler);
  357. }
  358. /* 1st idx = LL, second - vertical, third - horizontal, fourth - total */
  359. const uint8_t vc2_qm_col_tab[][4] = {
  360. {20, 9, 15, 4},
  361. { 0, 6, 6, 4},
  362. { 0, 3, 3, 5},
  363. { 0, 3, 5, 1},
  364. { 0, 11, 10, 11}
  365. };
  366. const uint8_t vc2_qm_flat_tab[][4] = {
  367. { 0, 0, 0, 0},
  368. { 0, 0, 0, 0},
  369. { 0, 0, 0, 0},
  370. { 0, 0, 0, 0},
  371. { 0, 0, 0, 0}
  372. };
  373. static void init_quant_matrix(VC2EncContext *s)
  374. {
  375. int level, orientation;
  376. if (s->wavelet_depth <= 4 && s->quant_matrix == VC2_QM_DEF) {
  377. s->custom_quant_matrix = 0;
  378. for (level = 0; level < s->wavelet_depth; level++) {
  379. s->quant[level][0] = ff_dirac_default_qmat[s->wavelet_idx][level][0];
  380. s->quant[level][1] = ff_dirac_default_qmat[s->wavelet_idx][level][1];
  381. s->quant[level][2] = ff_dirac_default_qmat[s->wavelet_idx][level][2];
  382. s->quant[level][3] = ff_dirac_default_qmat[s->wavelet_idx][level][3];
  383. }
  384. return;
  385. }
  386. s->custom_quant_matrix = 1;
  387. if (s->quant_matrix == VC2_QM_DEF) {
  388. for (level = 0; level < s->wavelet_depth; level++) {
  389. for (orientation = 0; orientation < 4; orientation++) {
  390. if (level <= 3)
  391. s->quant[level][orientation] = ff_dirac_default_qmat[s->wavelet_idx][level][orientation];
  392. else
  393. s->quant[level][orientation] = vc2_qm_col_tab[level][orientation];
  394. }
  395. }
  396. } else if (s->quant_matrix == VC2_QM_COL) {
  397. for (level = 0; level < s->wavelet_depth; level++) {
  398. for (orientation = 0; orientation < 4; orientation++) {
  399. s->quant[level][orientation] = vc2_qm_col_tab[level][orientation];
  400. }
  401. }
  402. } else {
  403. for (level = 0; level < s->wavelet_depth; level++) {
  404. for (orientation = 0; orientation < 4; orientation++) {
  405. s->quant[level][orientation] = vc2_qm_flat_tab[level][orientation];
  406. }
  407. }
  408. }
  409. }
  410. /* VC-2 12.3.4.2 - quant_matrix() */
  411. static void encode_quant_matrix(VC2EncContext *s)
  412. {
  413. int level;
  414. put_bits(&s->pb, 1, s->custom_quant_matrix);
  415. if (s->custom_quant_matrix) {
  416. put_vc2_ue_uint(&s->pb, s->quant[0][0]);
  417. for (level = 0; level < s->wavelet_depth; level++) {
  418. put_vc2_ue_uint(&s->pb, s->quant[level][1]);
  419. put_vc2_ue_uint(&s->pb, s->quant[level][2]);
  420. put_vc2_ue_uint(&s->pb, s->quant[level][3]);
  421. }
  422. }
  423. }
  424. /* VC-2 12.3 - transform_parameters() */
  425. static void encode_transform_params(VC2EncContext *s)
  426. {
  427. put_vc2_ue_uint(&s->pb, s->wavelet_idx);
  428. put_vc2_ue_uint(&s->pb, s->wavelet_depth);
  429. encode_slice_params(s);
  430. encode_quant_matrix(s);
  431. }
  432. /* VC-2 12.2 - wavelet_transform() */
  433. static void encode_wavelet_transform(VC2EncContext *s)
  434. {
  435. encode_transform_params(s);
  436. avpriv_align_put_bits(&s->pb);
  437. }
  438. /* VC-2 12 - picture_parse() */
  439. static void encode_picture_start(VC2EncContext *s)
  440. {
  441. avpriv_align_put_bits(&s->pb);
  442. encode_picture_header(s);
  443. avpriv_align_put_bits(&s->pb);
  444. encode_wavelet_transform(s);
  445. }
  446. #define QUANT(c, qf) (((c) << 2)/(qf))
  447. /* VC-2 13.5.5.2 - slice_band() */
  448. static void encode_subband(VC2EncContext *s, PutBitContext *pb, int sx, int sy,
  449. SubBand *b, int quant)
  450. {
  451. int x, y;
  452. const int left = b->width * (sx+0) / s->num_x;
  453. const int right = b->width * (sx+1) / s->num_x;
  454. const int top = b->height * (sy+0) / s->num_y;
  455. const int bottom = b->height * (sy+1) / s->num_y;
  456. const int qfactor = ff_dirac_qscale_tab[quant];
  457. const uint8_t *len_lut = &s->coef_lut_len[quant*COEF_LUT_TAB];
  458. const uint32_t *val_lut = &s->coef_lut_val[quant*COEF_LUT_TAB];
  459. dwtcoef *coeff = b->buf + top * b->stride;
  460. for (y = top; y < bottom; y++) {
  461. for (x = left; x < right; x++) {
  462. const int neg = coeff[x] < 0;
  463. uint32_t c_abs = FFABS(coeff[x]);
  464. if (c_abs < COEF_LUT_TAB) {
  465. const uint8_t len = len_lut[c_abs];
  466. if (len == 1)
  467. put_bits(pb, 1, 1);
  468. else
  469. put_bits(pb, len + 1, (val_lut[c_abs] << 1) | neg);
  470. } else {
  471. c_abs = QUANT(c_abs, qfactor);
  472. put_vc2_ue_uint(pb, c_abs);
  473. if (c_abs)
  474. put_bits(pb, 1, neg);
  475. }
  476. }
  477. coeff += b->stride;
  478. }
  479. }
  480. static int count_hq_slice(VC2EncContext *s, int *cache,
  481. int slice_x, int slice_y, int quant_idx)
  482. {
  483. int x, y;
  484. uint8_t quants[MAX_DWT_LEVELS][4];
  485. int bits = 0, p, level, orientation;
  486. if (cache && cache[quant_idx])
  487. return cache[quant_idx];
  488. bits += 8*s->prefix_bytes;
  489. bits += 8; /* quant_idx */
  490. for (level = 0; level < s->wavelet_depth; level++)
  491. for (orientation = !!level; orientation < 4; orientation++)
  492. quants[level][orientation] = FFMAX(quant_idx - s->quant[level][orientation], 0);
  493. for (p = 0; p < 3; p++) {
  494. int bytes_start, bytes_len, pad_s, pad_c;
  495. bytes_start = bits >> 3;
  496. bits += 8;
  497. for (level = 0; level < s->wavelet_depth; level++) {
  498. for (orientation = !!level; orientation < 4; orientation++) {
  499. SubBand *b = &s->plane[p].band[level][orientation];
  500. const int q_idx = quants[level][orientation];
  501. const uint8_t *len_lut = &s->coef_lut_len[q_idx*COEF_LUT_TAB];
  502. const int qfactor = ff_dirac_qscale_tab[q_idx];
  503. const int left = b->width * slice_x / s->num_x;
  504. const int right = b->width *(slice_x+1) / s->num_x;
  505. const int top = b->height * slice_y / s->num_y;
  506. const int bottom = b->height *(slice_y+1) / s->num_y;
  507. dwtcoef *buf = b->buf + top * b->stride;
  508. for (y = top; y < bottom; y++) {
  509. for (x = left; x < right; x++) {
  510. uint32_t c_abs = FFABS(buf[x]);
  511. if (c_abs < COEF_LUT_TAB) {
  512. const int len = len_lut[c_abs];
  513. bits += len + (len != 1);
  514. } else {
  515. c_abs = QUANT(c_abs, qfactor);
  516. bits += count_vc2_ue_uint(c_abs);
  517. bits += !!c_abs;
  518. }
  519. }
  520. buf += b->stride;
  521. }
  522. }
  523. }
  524. bits += FFALIGN(bits, 8) - bits;
  525. bytes_len = (bits >> 3) - bytes_start - 1;
  526. pad_s = FFALIGN(bytes_len, s->size_scaler)/s->size_scaler;
  527. pad_c = (pad_s*s->size_scaler) - bytes_len;
  528. bits += pad_c*8;
  529. }
  530. if (cache)
  531. cache[quant_idx] = bits;
  532. return bits;
  533. }
  534. /* Approaches the best possible quantizer asymptotically, its kinda exaustive
  535. * but we have a LUT to get the coefficient size in bits. Guaranteed to never
  536. * overshoot, which is apparently very important when streaming */
  537. static int rate_control(AVCodecContext *avctx, void *arg)
  538. {
  539. SliceArgs *slice_dat = arg;
  540. VC2EncContext *s = slice_dat->ctx;
  541. const int sx = slice_dat->x;
  542. const int sy = slice_dat->y;
  543. const int top = slice_dat->bits_ceil;
  544. const int bottom = slice_dat->bits_floor;
  545. int quant_buf[2] = {-1, -1};
  546. int quant = slice_dat->quant_idx, step = 1;
  547. int bits_last, bits = count_hq_slice(s, slice_dat->cache, sx, sy, quant);
  548. while ((bits > top) || (bits < bottom)) {
  549. const int signed_step = bits > top ? +step : -step;
  550. quant = av_clip(quant + signed_step, 0, s->q_ceil-1);
  551. bits = count_hq_slice(s, slice_dat->cache, sx, sy, quant);
  552. if (quant_buf[1] == quant) {
  553. quant = FFMAX(quant_buf[0], quant);
  554. bits = quant == quant_buf[0] ? bits_last : bits;
  555. break;
  556. }
  557. step = av_clip(step/2, 1, (s->q_ceil-1)/2);
  558. quant_buf[1] = quant_buf[0];
  559. quant_buf[0] = quant;
  560. bits_last = bits;
  561. }
  562. slice_dat->quant_idx = av_clip(quant, 0, s->q_ceil-1);
  563. slice_dat->bytes = FFALIGN((bits >> 3), s->size_scaler) + 4 + s->prefix_bytes;
  564. slice_dat->bytes_left = s->slice_max_bytes - slice_dat->bytes;
  565. return 0;
  566. }
  567. static int calc_slice_sizes(VC2EncContext *s)
  568. {
  569. int i, slice_x, slice_y, bytes_left = 0;
  570. int bytes_top[SLICE_REDIST_TOTAL] = {0};
  571. int64_t total_bytes_needed = 0;
  572. int slice_redist_range = FFMIN(SLICE_REDIST_TOTAL, s->num_x*s->num_y);
  573. SliceArgs *enc_args = s->slice_args;
  574. SliceArgs *top_loc[SLICE_REDIST_TOTAL] = {NULL};
  575. init_quant_matrix(s);
  576. for (slice_y = 0; slice_y < s->num_y; slice_y++) {
  577. for (slice_x = 0; slice_x < s->num_x; slice_x++) {
  578. SliceArgs *args = &enc_args[s->num_x*slice_y + slice_x];
  579. args->ctx = s;
  580. args->x = slice_x;
  581. args->y = slice_y;
  582. args->bits_ceil = s->slice_max_bytes << 3;
  583. args->bits_floor = s->slice_min_bytes << 3;
  584. memset(args, 0, s->q_ceil*sizeof(int));
  585. }
  586. }
  587. /* First pass - determine baseline slice sizes w.r.t. max_slice_size */
  588. s->avctx->execute(s->avctx, rate_control, enc_args, NULL, s->num_x*s->num_y,
  589. sizeof(SliceArgs));
  590. for (slice_y = 0; slice_y < s->num_y; slice_y++) {
  591. for (slice_x = 0; slice_x < s->num_x; slice_x++) {
  592. SliceArgs *args = &enc_args[s->num_x*slice_y + slice_x];
  593. bytes_left += args->bytes_left;
  594. for (i = 0; i < slice_redist_range; i++) {
  595. if (args->bytes > bytes_top[i]) {
  596. bytes_top[i] = args->bytes;
  597. top_loc[i] = args;
  598. break;
  599. }
  600. }
  601. }
  602. }
  603. /* Second pass - distribute leftover bytes */
  604. while (1) {
  605. int distributed = 0;
  606. for (i = 0; i < slice_redist_range; i++) {
  607. SliceArgs *args;
  608. int bits, bytes, diff, prev_bytes, new_idx;
  609. if (bytes_left <= 0)
  610. break;
  611. if (!top_loc[i] || !top_loc[i]->quant_idx)
  612. break;
  613. args = top_loc[i];
  614. prev_bytes = args->bytes;
  615. new_idx = FFMAX(args->quant_idx - 1, 0);
  616. bits = count_hq_slice(s, args->cache, args->x, args->y, new_idx);
  617. bytes = FFALIGN((bits >> 3), s->size_scaler) + 4 + s->prefix_bytes;
  618. diff = bytes - prev_bytes;
  619. if ((bytes_left - diff) > 0) {
  620. args->quant_idx = new_idx;
  621. args->bytes = bytes;
  622. bytes_left -= diff;
  623. distributed++;
  624. }
  625. }
  626. if (!distributed)
  627. break;
  628. }
  629. for (slice_y = 0; slice_y < s->num_y; slice_y++) {
  630. for (slice_x = 0; slice_x < s->num_x; slice_x++) {
  631. SliceArgs *args = &enc_args[s->num_x*slice_y + slice_x];
  632. total_bytes_needed += args->bytes;
  633. s->q_avg = (s->q_avg + args->quant_idx)/2;
  634. }
  635. }
  636. return total_bytes_needed;
  637. }
  638. /* VC-2 13.5.3 - hq_slice */
  639. static int encode_hq_slice(AVCodecContext *avctx, void *arg)
  640. {
  641. SliceArgs *slice_dat = arg;
  642. VC2EncContext *s = slice_dat->ctx;
  643. PutBitContext *pb = &slice_dat->pb;
  644. const int slice_x = slice_dat->x;
  645. const int slice_y = slice_dat->y;
  646. const int quant_idx = slice_dat->quant_idx;
  647. const int slice_bytes_max = slice_dat->bytes;
  648. uint8_t quants[MAX_DWT_LEVELS][4];
  649. int p, level, orientation;
  650. skip_put_bytes(pb, s->prefix_bytes);
  651. put_bits(pb, 8, quant_idx);
  652. /* Slice quantization (slice_quantizers() in the specs) */
  653. for (level = 0; level < s->wavelet_depth; level++)
  654. for (orientation = !!level; orientation < 4; orientation++)
  655. quants[level][orientation] = FFMAX(quant_idx - s->quant[level][orientation], 0);
  656. /* Luma + 2 Chroma planes */
  657. for (p = 0; p < 3; p++) {
  658. int bytes_start, bytes_len, pad_s, pad_c;
  659. bytes_start = put_bits_count(pb) >> 3;
  660. put_bits(pb, 8, 0);
  661. for (level = 0; level < s->wavelet_depth; level++) {
  662. for (orientation = !!level; orientation < 4; orientation++) {
  663. encode_subband(s, pb, slice_x, slice_y,
  664. &s->plane[p].band[level][orientation],
  665. quants[level][orientation]);
  666. }
  667. }
  668. avpriv_align_put_bits(pb);
  669. bytes_len = (put_bits_count(pb) >> 3) - bytes_start - 1;
  670. if (p == 2) {
  671. int len_diff = slice_bytes_max - (put_bits_count(pb) >> 3);
  672. pad_s = FFALIGN((bytes_len + len_diff), s->size_scaler)/s->size_scaler;
  673. pad_c = (pad_s*s->size_scaler) - bytes_len;
  674. } else {
  675. pad_s = FFALIGN(bytes_len, s->size_scaler)/s->size_scaler;
  676. pad_c = (pad_s*s->size_scaler) - bytes_len;
  677. }
  678. pb->buf[bytes_start] = pad_s;
  679. flush_put_bits(pb);
  680. skip_put_bytes(pb, pad_c);
  681. }
  682. return 0;
  683. }
  684. /* VC-2 13.5.1 - low_delay_transform_data() */
  685. static int encode_slices(VC2EncContext *s)
  686. {
  687. uint8_t *buf;
  688. int slice_x, slice_y, skip = 0;
  689. SliceArgs *enc_args = s->slice_args;
  690. avpriv_align_put_bits(&s->pb);
  691. flush_put_bits(&s->pb);
  692. buf = put_bits_ptr(&s->pb);
  693. for (slice_y = 0; slice_y < s->num_y; slice_y++) {
  694. for (slice_x = 0; slice_x < s->num_x; slice_x++) {
  695. SliceArgs *args = &enc_args[s->num_x*slice_y + slice_x];
  696. init_put_bits(&args->pb, buf + skip, args->bytes+s->prefix_bytes);
  697. skip += args->bytes;
  698. }
  699. }
  700. s->avctx->execute(s->avctx, encode_hq_slice, enc_args, NULL, s->num_x*s->num_y,
  701. sizeof(SliceArgs));
  702. skip_put_bytes(&s->pb, skip);
  703. return 0;
  704. }
  705. /*
  706. * Transform basics for a 3 level transform
  707. * |---------------------------------------------------------------------|
  708. * | LL-0 | HL-0 | | |
  709. * |--------|-------| HL-1 | |
  710. * | LH-0 | HH-0 | | |
  711. * |----------------|-----------------| HL-2 |
  712. * | | | |
  713. * | LH-1 | HH-1 | |
  714. * | | | |
  715. * |----------------------------------|----------------------------------|
  716. * | | |
  717. * | | |
  718. * | | |
  719. * | LH-2 | HH-2 |
  720. * | | |
  721. * | | |
  722. * | | |
  723. * |---------------------------------------------------------------------|
  724. *
  725. * DWT transforms are generally applied by splitting the image in two vertically
  726. * and applying a low pass transform on the left part and a corresponding high
  727. * pass transform on the right hand side. This is known as the horizontal filter
  728. * stage.
  729. * After that, the same operation is performed except the image is divided
  730. * horizontally, with the high pass on the lower and the low pass on the higher
  731. * side.
  732. * Therefore, you're left with 4 subdivisions - known as low-low, low-high,
  733. * high-low and high-high. They're referred to as orientations in the decoder
  734. * and encoder.
  735. *
  736. * The LL (low-low) area contains the original image downsampled by the amount
  737. * of levels. The rest of the areas can be thought as the details needed
  738. * to restore the image perfectly to its original size.
  739. */
  740. static int dwt_plane(AVCodecContext *avctx, void *arg)
  741. {
  742. TransformArgs *transform_dat = arg;
  743. VC2EncContext *s = transform_dat->ctx;
  744. const void *frame_data = transform_dat->idata;
  745. const ptrdiff_t linesize = transform_dat->istride;
  746. const int field = transform_dat->field;
  747. const Plane *p = transform_dat->plane;
  748. VC2TransformContext *t = &transform_dat->t;
  749. dwtcoef *buf = p->coef_buf;
  750. const int idx = s->wavelet_idx;
  751. const int skip = 1 + s->interlaced;
  752. int x, y, level, offset;
  753. ptrdiff_t pix_stride = linesize >> (s->bpp - 1);
  754. if (field == 1) {
  755. offset = 0;
  756. pix_stride <<= 1;
  757. } else if (field == 2) {
  758. offset = pix_stride;
  759. pix_stride <<= 1;
  760. } else {
  761. offset = 0;
  762. }
  763. if (s->bpp == 1) {
  764. const uint8_t *pix = (const uint8_t *)frame_data + offset;
  765. for (y = 0; y < p->height*skip; y+=skip) {
  766. for (x = 0; x < p->width; x++) {
  767. buf[x] = pix[x] - s->diff_offset;
  768. }
  769. buf += p->coef_stride;
  770. pix += pix_stride;
  771. }
  772. } else {
  773. const uint16_t *pix = (const uint16_t *)frame_data + offset;
  774. for (y = 0; y < p->height*skip; y+=skip) {
  775. for (x = 0; x < p->width; x++) {
  776. buf[x] = pix[x] - s->diff_offset;
  777. }
  778. buf += p->coef_stride;
  779. pix += pix_stride;
  780. }
  781. }
  782. memset(buf, 0, p->coef_stride * (p->dwt_height - p->height) * sizeof(dwtcoef));
  783. for (level = s->wavelet_depth-1; level >= 0; level--) {
  784. const SubBand *b = &p->band[level][0];
  785. t->vc2_subband_dwt[idx](t, p->coef_buf, p->coef_stride,
  786. b->width, b->height);
  787. }
  788. return 0;
  789. }
  790. static int encode_frame(VC2EncContext *s, AVPacket *avpkt, const AVFrame *frame,
  791. const char *aux_data, const int header_size, int field)
  792. {
  793. int i, ret;
  794. int64_t max_frame_bytes;
  795. /* Threaded DWT transform */
  796. for (i = 0; i < 3; i++) {
  797. s->transform_args[i].ctx = s;
  798. s->transform_args[i].field = field;
  799. s->transform_args[i].plane = &s->plane[i];
  800. s->transform_args[i].idata = frame->data[i];
  801. s->transform_args[i].istride = frame->linesize[i];
  802. }
  803. s->avctx->execute(s->avctx, dwt_plane, s->transform_args, NULL, 3,
  804. sizeof(TransformArgs));
  805. /* Calculate per-slice quantizers and sizes */
  806. max_frame_bytes = header_size + calc_slice_sizes(s);
  807. if (field < 2) {
  808. ret = ff_alloc_packet2(s->avctx, avpkt,
  809. max_frame_bytes << s->interlaced,
  810. max_frame_bytes << s->interlaced);
  811. if (ret) {
  812. av_log(s->avctx, AV_LOG_ERROR, "Error getting output packet.\n");
  813. return ret;
  814. }
  815. init_put_bits(&s->pb, avpkt->data, avpkt->size);
  816. }
  817. /* Sequence header */
  818. encode_parse_info(s, DIRAC_PCODE_SEQ_HEADER);
  819. encode_seq_header(s);
  820. /* Encoder version */
  821. if (aux_data) {
  822. encode_parse_info(s, DIRAC_PCODE_AUX);
  823. avpriv_put_string(&s->pb, aux_data, 1);
  824. }
  825. /* Picture header */
  826. encode_parse_info(s, DIRAC_PCODE_PICTURE_HQ);
  827. encode_picture_start(s);
  828. /* Encode slices */
  829. encode_slices(s);
  830. /* End sequence */
  831. encode_parse_info(s, DIRAC_PCODE_END_SEQ);
  832. return 0;
  833. }
  834. static av_cold int vc2_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  835. const AVFrame *frame, int *got_packet)
  836. {
  837. int ret = 0;
  838. int sig_size = 256;
  839. VC2EncContext *s = avctx->priv_data;
  840. const char aux_data[] = LIBAVCODEC_IDENT;
  841. const int aux_data_size = sizeof(aux_data);
  842. const int header_size = 100 + aux_data_size;
  843. int64_t max_frame_bytes, r_bitrate = avctx->bit_rate >> (s->interlaced);
  844. s->avctx = avctx;
  845. s->size_scaler = 2;
  846. s->prefix_bytes = 0;
  847. s->last_parse_code = 0;
  848. s->next_parse_offset = 0;
  849. /* Rate control */
  850. max_frame_bytes = (av_rescale(r_bitrate, s->avctx->time_base.num,
  851. s->avctx->time_base.den) >> 3) - header_size;
  852. /* Find an appropriate size scaler */
  853. while (sig_size > 255) {
  854. s->slice_max_bytes = FFALIGN(av_rescale(max_frame_bytes, 1,
  855. s->num_x*s->num_y), s->size_scaler);
  856. s->slice_max_bytes += 4 + s->prefix_bytes;
  857. sig_size = s->slice_max_bytes/s->size_scaler; /* Signalled slize size */
  858. s->size_scaler <<= 1;
  859. }
  860. s->slice_min_bytes = s->slice_max_bytes - s->slice_max_bytes*(s->tolerance/100.0f);
  861. ret = encode_frame(s, avpkt, frame, aux_data, header_size, s->interlaced);
  862. if (ret)
  863. return ret;
  864. if (s->interlaced) {
  865. ret = encode_frame(s, avpkt, frame, aux_data, header_size, 2);
  866. if (ret)
  867. return ret;
  868. }
  869. flush_put_bits(&s->pb);
  870. avpkt->size = put_bits_count(&s->pb) >> 3;
  871. *got_packet = 1;
  872. return 0;
  873. }
  874. static av_cold int vc2_encode_end(AVCodecContext *avctx)
  875. {
  876. int i;
  877. VC2EncContext *s = avctx->priv_data;
  878. av_log(avctx, AV_LOG_INFO, "Qavg: %i\n", s->q_avg);
  879. for (i = 0; i < 3; i++) {
  880. ff_vc2enc_free_transforms(&s->transform_args[i].t);
  881. av_freep(&s->plane[i].coef_buf);
  882. }
  883. av_freep(&s->slice_args);
  884. av_freep(&s->coef_lut_len);
  885. av_freep(&s->coef_lut_val);
  886. return 0;
  887. }
  888. static av_cold int vc2_encode_init(AVCodecContext *avctx)
  889. {
  890. Plane *p;
  891. SubBand *b;
  892. int i, j, level, o, shift;
  893. const AVPixFmtDescriptor *fmt = av_pix_fmt_desc_get(avctx->pix_fmt);
  894. const int depth = fmt->comp[0].depth;
  895. VC2EncContext *s = avctx->priv_data;
  896. s->picture_number = 0;
  897. /* Total allowed quantization range */
  898. s->q_ceil = MAX_QUANT_INDEX;
  899. s->ver.major = 2;
  900. s->ver.minor = 0;
  901. s->profile = 3;
  902. s->level = 3;
  903. s->base_vf = -1;
  904. s->strict_compliance = 1;
  905. s->q_avg = 0;
  906. s->slice_max_bytes = 0;
  907. s->slice_min_bytes = 0;
  908. /* Mark unknown as progressive */
  909. s->interlaced = !((avctx->field_order == AV_FIELD_UNKNOWN) ||
  910. (avctx->field_order == AV_FIELD_PROGRESSIVE));
  911. if (avctx->pix_fmt == AV_PIX_FMT_YUV422P10) {
  912. if (avctx->width == 1280 && avctx->height == 720) {
  913. s->level = 3;
  914. if (avctx->time_base.num == 1001 && avctx->time_base.den == 60000)
  915. s->base_vf = 9;
  916. if (avctx->time_base.num == 1 && avctx->time_base.den == 50)
  917. s->base_vf = 10;
  918. } else if (avctx->width == 1920 && avctx->height == 1080) {
  919. s->level = 3;
  920. if (s->interlaced) {
  921. if (avctx->time_base.num == 1001 && avctx->time_base.den == 30000)
  922. s->base_vf = 11;
  923. if (avctx->time_base.num == 1 && avctx->time_base.den == 50)
  924. s->base_vf = 12;
  925. } else {
  926. if (avctx->time_base.num == 1001 && avctx->time_base.den == 60000)
  927. s->base_vf = 13;
  928. if (avctx->time_base.num == 1 && avctx->time_base.den == 50)
  929. s->base_vf = 14;
  930. if (avctx->time_base.num == 1001 && avctx->time_base.den == 24000)
  931. s->base_vf = 21;
  932. }
  933. } else if (avctx->width == 3840 && avctx->height == 2160) {
  934. s->level = 6;
  935. if (avctx->time_base.num == 1001 && avctx->time_base.den == 60000)
  936. s->base_vf = 17;
  937. if (avctx->time_base.num == 1 && avctx->time_base.den == 50)
  938. s->base_vf = 18;
  939. }
  940. }
  941. if (s->interlaced && s->base_vf <= 0) {
  942. av_log(avctx, AV_LOG_ERROR, "Interlacing not supported with non standard formats!\n");
  943. return AVERROR_UNKNOWN;
  944. }
  945. if (s->interlaced)
  946. av_log(avctx, AV_LOG_WARNING, "Interlacing enabled!\n");
  947. if ((s->slice_width & (s->slice_width - 1)) ||
  948. (s->slice_height & (s->slice_height - 1))) {
  949. av_log(avctx, AV_LOG_ERROR, "Slice size is not a power of two!\n");
  950. return AVERROR_UNKNOWN;
  951. }
  952. if ((s->slice_width > avctx->width) ||
  953. (s->slice_height > avctx->height)) {
  954. av_log(avctx, AV_LOG_ERROR, "Slice size is bigger than the image!\n");
  955. return AVERROR_UNKNOWN;
  956. }
  957. if (s->base_vf <= 0) {
  958. if (avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
  959. s->strict_compliance = s->base_vf = 0;
  960. av_log(avctx, AV_LOG_WARNING, "Disabling strict compliance\n");
  961. } else {
  962. av_log(avctx, AV_LOG_WARNING, "Given format does not strictly comply with "
  963. "the specifications, please add a -strict -1 flag to use it\n");
  964. return AVERROR_UNKNOWN;
  965. }
  966. } else {
  967. av_log(avctx, AV_LOG_INFO, "Selected base video format = %i\n", s->base_vf);
  968. }
  969. /* Chroma subsampling */
  970. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
  971. /* Bit depth and color range index */
  972. if (depth == 8 && avctx->color_range == AVCOL_RANGE_JPEG) {
  973. s->bpp = 1;
  974. s->bpp_idx = 1;
  975. s->diff_offset = 128;
  976. } else if (depth == 8 && (avctx->color_range == AVCOL_RANGE_MPEG ||
  977. avctx->color_range == AVCOL_RANGE_UNSPECIFIED)) {
  978. s->bpp = 1;
  979. s->bpp_idx = 2;
  980. s->diff_offset = 128;
  981. } else if (depth == 10) {
  982. s->bpp = 2;
  983. s->bpp_idx = 3;
  984. s->diff_offset = 512;
  985. } else {
  986. s->bpp = 2;
  987. s->bpp_idx = 4;
  988. s->diff_offset = 2048;
  989. }
  990. /* Planes initialization */
  991. for (i = 0; i < 3; i++) {
  992. int w, h;
  993. p = &s->plane[i];
  994. p->width = avctx->width >> (i ? s->chroma_x_shift : 0);
  995. p->height = avctx->height >> (i ? s->chroma_y_shift : 0);
  996. if (s->interlaced)
  997. p->height >>= 1;
  998. p->dwt_width = w = FFALIGN(p->width, (1 << s->wavelet_depth));
  999. p->dwt_height = h = FFALIGN(p->height, (1 << s->wavelet_depth));
  1000. p->coef_stride = FFALIGN(p->dwt_width, 32);
  1001. p->coef_buf = av_malloc(p->coef_stride*p->dwt_height*sizeof(dwtcoef));
  1002. if (!p->coef_buf)
  1003. goto alloc_fail;
  1004. for (level = s->wavelet_depth-1; level >= 0; level--) {
  1005. w = w >> 1;
  1006. h = h >> 1;
  1007. for (o = 0; o < 4; o++) {
  1008. b = &p->band[level][o];
  1009. b->width = w;
  1010. b->height = h;
  1011. b->stride = p->coef_stride;
  1012. shift = (o > 1)*b->height*b->stride + (o & 1)*b->width;
  1013. b->buf = p->coef_buf + shift;
  1014. }
  1015. }
  1016. /* DWT init */
  1017. if (ff_vc2enc_init_transforms(&s->transform_args[i].t,
  1018. s->plane[i].coef_stride,
  1019. s->plane[i].dwt_height))
  1020. goto alloc_fail;
  1021. }
  1022. /* Slices */
  1023. s->num_x = s->plane[0].dwt_width/s->slice_width;
  1024. s->num_y = s->plane[0].dwt_height/s->slice_height;
  1025. s->slice_args = av_calloc(s->num_x*s->num_y, sizeof(SliceArgs));
  1026. if (!s->slice_args)
  1027. goto alloc_fail;
  1028. /* Lookup tables */
  1029. s->coef_lut_len = av_malloc(COEF_LUT_TAB*(s->q_ceil+1)*sizeof(*s->coef_lut_len));
  1030. if (!s->coef_lut_len)
  1031. goto alloc_fail;
  1032. s->coef_lut_val = av_malloc(COEF_LUT_TAB*(s->q_ceil+1)*sizeof(*s->coef_lut_val));
  1033. if (!s->coef_lut_val)
  1034. goto alloc_fail;
  1035. for (i = 0; i < s->q_ceil; i++) {
  1036. uint8_t *len_lut = &s->coef_lut_len[i*COEF_LUT_TAB];
  1037. uint32_t *val_lut = &s->coef_lut_val[i*COEF_LUT_TAB];
  1038. for (j = 0; j < COEF_LUT_TAB; j++) {
  1039. get_vc2_ue_uint(QUANT(j, ff_dirac_qscale_tab[i]),
  1040. &len_lut[j], &val_lut[j]);
  1041. }
  1042. }
  1043. return 0;
  1044. alloc_fail:
  1045. vc2_encode_end(avctx);
  1046. av_log(avctx, AV_LOG_ERROR, "Unable to allocate memory!\n");
  1047. return AVERROR(ENOMEM);
  1048. }
  1049. #define VC2ENC_FLAGS (AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
  1050. static const AVOption vc2enc_options[] = {
  1051. {"tolerance", "Max undershoot in percent", offsetof(VC2EncContext, tolerance), AV_OPT_TYPE_DOUBLE, {.dbl = 5.0f}, 0.0f, 45.0f, VC2ENC_FLAGS, "tolerance"},
  1052. {"slice_width", "Slice width", offsetof(VC2EncContext, slice_width), AV_OPT_TYPE_INT, {.i64 = 64}, 32, 1024, VC2ENC_FLAGS, "slice_width"},
  1053. {"slice_height", "Slice height", offsetof(VC2EncContext, slice_height), AV_OPT_TYPE_INT, {.i64 = 32}, 8, 1024, VC2ENC_FLAGS, "slice_height"},
  1054. {"wavelet_depth", "Transform depth", offsetof(VC2EncContext, wavelet_depth), AV_OPT_TYPE_INT, {.i64 = 4}, 1, 5, VC2ENC_FLAGS, "wavelet_depth"},
  1055. {"wavelet_type", "Transform type", offsetof(VC2EncContext, wavelet_idx), AV_OPT_TYPE_INT, {.i64 = VC2_TRANSFORM_9_7}, 0, VC2_TRANSFORMS_NB, VC2ENC_FLAGS, "wavelet_idx"},
  1056. {"9_7", "Deslauriers-Dubuc (9,7)", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_TRANSFORM_9_7}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "wavelet_idx"},
  1057. {"5_3", "LeGall (5,3)", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_TRANSFORM_5_3}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "wavelet_idx"},
  1058. {"haar", "Haar (with shift)", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_TRANSFORM_HAAR_S}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "wavelet_idx"},
  1059. {"haar_noshift", "Haar (without shift)", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_TRANSFORM_HAAR}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "wavelet_idx"},
  1060. {"qm", "Custom quantization matrix", offsetof(VC2EncContext, quant_matrix), AV_OPT_TYPE_INT, {.i64 = VC2_QM_DEF}, 0, VC2_QM_NB, VC2ENC_FLAGS, "quant_matrix"},
  1061. {"default", "Default from the specifications", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_QM_DEF}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "quant_matrix"},
  1062. {"color", "Prevents low bitrate discoloration", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_QM_COL}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "quant_matrix"},
  1063. {"flat", "Optimize for PSNR", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_QM_FLAT}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "quant_matrix"},
  1064. {NULL}
  1065. };
  1066. static const AVClass vc2enc_class = {
  1067. .class_name = "SMPTE VC-2 encoder",
  1068. .category = AV_CLASS_CATEGORY_ENCODER,
  1069. .option = vc2enc_options,
  1070. .item_name = av_default_item_name,
  1071. .version = LIBAVUTIL_VERSION_INT
  1072. };
  1073. static const AVCodecDefault vc2enc_defaults[] = {
  1074. { "b", "600000000" },
  1075. { NULL },
  1076. };
  1077. static const enum AVPixelFormat allowed_pix_fmts[] = {
  1078. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
  1079. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  1080. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
  1081. AV_PIX_FMT_NONE
  1082. };
  1083. AVCodec ff_vc2_encoder = {
  1084. .name = "vc2",
  1085. .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-2"),
  1086. .type = AVMEDIA_TYPE_VIDEO,
  1087. .id = AV_CODEC_ID_DIRAC,
  1088. .priv_data_size = sizeof(VC2EncContext),
  1089. .init = vc2_encode_init,
  1090. .close = vc2_encode_end,
  1091. .capabilities = AV_CODEC_CAP_SLICE_THREADS,
  1092. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  1093. .encode2 = vc2_encode_frame,
  1094. .priv_class = &vc2enc_class,
  1095. .defaults = vc2enc_defaults,
  1096. .pix_fmts = allowed_pix_fmts
  1097. };