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.

1248 lines
40KB

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