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.

1265 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(SliceArgs *slice, int quant_idx)
  481. {
  482. int x, y;
  483. uint8_t quants[MAX_DWT_LEVELS][4];
  484. int bits = 0, p, level, orientation;
  485. VC2EncContext *s = slice->ctx;
  486. if (slice->cache[quant_idx])
  487. return slice->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. slice->cache[quant_idx] = bits;
  531. return bits;
  532. }
  533. /* Approaches the best possible quantizer asymptotically, its kinda exaustive
  534. * but we have a LUT to get the coefficient size in bits. Guaranteed to never
  535. * overshoot, which is apparently very important when streaming */
  536. static int rate_control(AVCodecContext *avctx, void *arg)
  537. {
  538. SliceArgs *slice_dat = arg;
  539. VC2EncContext *s = slice_dat->ctx;
  540. const int top = slice_dat->bits_ceil;
  541. const int bottom = slice_dat->bits_floor;
  542. int quant_buf[2] = {-1, -1};
  543. int quant = slice_dat->quant_idx, step = 1;
  544. int bits_last, bits = count_hq_slice(slice_dat, quant);
  545. while ((bits > top) || (bits < bottom)) {
  546. const int signed_step = bits > top ? +step : -step;
  547. quant = av_clip(quant + signed_step, 0, s->q_ceil-1);
  548. bits = count_hq_slice(slice_dat, quant);
  549. if (quant_buf[1] == quant) {
  550. quant = FFMAX(quant_buf[0], quant);
  551. bits = quant == quant_buf[0] ? bits_last : bits;
  552. break;
  553. }
  554. step = av_clip(step/2, 1, (s->q_ceil-1)/2);
  555. quant_buf[1] = quant_buf[0];
  556. quant_buf[0] = quant;
  557. bits_last = bits;
  558. }
  559. slice_dat->quant_idx = av_clip(quant, 0, s->q_ceil-1);
  560. slice_dat->bytes = FFALIGN((bits >> 3), s->size_scaler) + 4 + s->prefix_bytes;
  561. slice_dat->bytes_left = s->slice_max_bytes - slice_dat->bytes;
  562. return 0;
  563. }
  564. static int calc_slice_sizes(VC2EncContext *s)
  565. {
  566. int i, slice_x, slice_y, bytes_left = 0;
  567. int bytes_top[SLICE_REDIST_TOTAL] = {0};
  568. int64_t total_bytes_needed = 0;
  569. int slice_redist_range = FFMIN(SLICE_REDIST_TOTAL, s->num_x*s->num_y);
  570. SliceArgs *enc_args = s->slice_args;
  571. SliceArgs *top_loc[SLICE_REDIST_TOTAL] = {NULL};
  572. init_quant_matrix(s);
  573. for (slice_y = 0; slice_y < s->num_y; slice_y++) {
  574. for (slice_x = 0; slice_x < s->num_x; slice_x++) {
  575. SliceArgs *args = &enc_args[s->num_x*slice_y + slice_x];
  576. args->ctx = s;
  577. args->x = slice_x;
  578. args->y = slice_y;
  579. args->bits_ceil = s->slice_max_bytes << 3;
  580. args->bits_floor = s->slice_min_bytes << 3;
  581. memset(args, 0, s->q_ceil*sizeof(int));
  582. }
  583. }
  584. /* First pass - determine baseline slice sizes w.r.t. max_slice_size */
  585. s->avctx->execute(s->avctx, rate_control, enc_args, NULL, s->num_x*s->num_y,
  586. sizeof(SliceArgs));
  587. for (slice_y = 0; slice_y < s->num_y; slice_y++) {
  588. for (slice_x = 0; slice_x < s->num_x; slice_x++) {
  589. SliceArgs *args = &enc_args[s->num_x*slice_y + slice_x];
  590. bytes_left += args->bytes_left;
  591. for (i = 0; i < slice_redist_range; i++) {
  592. if (args->bytes > bytes_top[i]) {
  593. bytes_top[i] = args->bytes;
  594. top_loc[i] = args;
  595. break;
  596. }
  597. }
  598. }
  599. }
  600. /* Second pass - distribute leftover bytes */
  601. while (1) {
  602. int distributed = 0;
  603. for (i = 0; i < slice_redist_range; i++) {
  604. SliceArgs *args;
  605. int bits, bytes, diff, prev_bytes, new_idx;
  606. if (bytes_left <= 0)
  607. break;
  608. if (!top_loc[i] || !top_loc[i]->quant_idx)
  609. break;
  610. args = top_loc[i];
  611. prev_bytes = args->bytes;
  612. new_idx = FFMAX(args->quant_idx - 1, 0);
  613. bits = count_hq_slice(args, new_idx);
  614. bytes = FFALIGN((bits >> 3), s->size_scaler) + 4 + s->prefix_bytes;
  615. diff = bytes - prev_bytes;
  616. if ((bytes_left - diff) > 0) {
  617. args->quant_idx = new_idx;
  618. args->bytes = bytes;
  619. bytes_left -= diff;
  620. distributed++;
  621. }
  622. }
  623. if (!distributed)
  624. break;
  625. }
  626. for (slice_y = 0; slice_y < s->num_y; slice_y++) {
  627. for (slice_x = 0; slice_x < s->num_x; slice_x++) {
  628. SliceArgs *args = &enc_args[s->num_x*slice_y + slice_x];
  629. total_bytes_needed += args->bytes;
  630. s->q_avg = (s->q_avg + args->quant_idx)/2;
  631. }
  632. }
  633. return total_bytes_needed;
  634. }
  635. /* VC-2 13.5.3 - hq_slice */
  636. static int encode_hq_slice(AVCodecContext *avctx, void *arg)
  637. {
  638. SliceArgs *slice_dat = arg;
  639. VC2EncContext *s = slice_dat->ctx;
  640. PutBitContext *pb = &slice_dat->pb;
  641. const int slice_x = slice_dat->x;
  642. const int slice_y = slice_dat->y;
  643. const int quant_idx = slice_dat->quant_idx;
  644. const int slice_bytes_max = slice_dat->bytes;
  645. uint8_t quants[MAX_DWT_LEVELS][4];
  646. int p, level, orientation;
  647. skip_put_bytes(pb, s->prefix_bytes);
  648. put_bits(pb, 8, quant_idx);
  649. /* Slice quantization (slice_quantizers() in the specs) */
  650. for (level = 0; level < s->wavelet_depth; level++)
  651. for (orientation = !!level; orientation < 4; orientation++)
  652. quants[level][orientation] = FFMAX(quant_idx - s->quant[level][orientation], 0);
  653. /* Luma + 2 Chroma planes */
  654. for (p = 0; p < 3; p++) {
  655. int bytes_start, bytes_len, pad_s, pad_c;
  656. bytes_start = put_bits_count(pb) >> 3;
  657. put_bits(pb, 8, 0);
  658. for (level = 0; level < s->wavelet_depth; level++) {
  659. for (orientation = !!level; orientation < 4; orientation++) {
  660. encode_subband(s, pb, slice_x, slice_y,
  661. &s->plane[p].band[level][orientation],
  662. quants[level][orientation]);
  663. }
  664. }
  665. avpriv_align_put_bits(pb);
  666. bytes_len = (put_bits_count(pb) >> 3) - bytes_start - 1;
  667. if (p == 2) {
  668. int len_diff = slice_bytes_max - (put_bits_count(pb) >> 3);
  669. pad_s = FFALIGN((bytes_len + len_diff), s->size_scaler)/s->size_scaler;
  670. pad_c = (pad_s*s->size_scaler) - bytes_len;
  671. } else {
  672. pad_s = FFALIGN(bytes_len, s->size_scaler)/s->size_scaler;
  673. pad_c = (pad_s*s->size_scaler) - bytes_len;
  674. }
  675. pb->buf[bytes_start] = pad_s;
  676. flush_put_bits(pb);
  677. skip_put_bytes(pb, pad_c);
  678. }
  679. return 0;
  680. }
  681. /* VC-2 13.5.1 - low_delay_transform_data() */
  682. static int encode_slices(VC2EncContext *s)
  683. {
  684. uint8_t *buf;
  685. int slice_x, slice_y, skip = 0;
  686. SliceArgs *enc_args = s->slice_args;
  687. avpriv_align_put_bits(&s->pb);
  688. flush_put_bits(&s->pb);
  689. buf = put_bits_ptr(&s->pb);
  690. for (slice_y = 0; slice_y < s->num_y; slice_y++) {
  691. for (slice_x = 0; slice_x < s->num_x; slice_x++) {
  692. SliceArgs *args = &enc_args[s->num_x*slice_y + slice_x];
  693. init_put_bits(&args->pb, buf + skip, args->bytes+s->prefix_bytes);
  694. skip += args->bytes;
  695. }
  696. }
  697. s->avctx->execute(s->avctx, encode_hq_slice, enc_args, NULL, s->num_x*s->num_y,
  698. sizeof(SliceArgs));
  699. skip_put_bytes(&s->pb, skip);
  700. return 0;
  701. }
  702. /*
  703. * Transform basics for a 3 level transform
  704. * |---------------------------------------------------------------------|
  705. * | LL-0 | HL-0 | | |
  706. * |--------|-------| HL-1 | |
  707. * | LH-0 | HH-0 | | |
  708. * |----------------|-----------------| HL-2 |
  709. * | | | |
  710. * | LH-1 | HH-1 | |
  711. * | | | |
  712. * |----------------------------------|----------------------------------|
  713. * | | |
  714. * | | |
  715. * | | |
  716. * | LH-2 | HH-2 |
  717. * | | |
  718. * | | |
  719. * | | |
  720. * |---------------------------------------------------------------------|
  721. *
  722. * DWT transforms are generally applied by splitting the image in two vertically
  723. * and applying a low pass transform on the left part and a corresponding high
  724. * pass transform on the right hand side. This is known as the horizontal filter
  725. * stage.
  726. * After that, the same operation is performed except the image is divided
  727. * horizontally, with the high pass on the lower and the low pass on the higher
  728. * side.
  729. * Therefore, you're left with 4 subdivisions - known as low-low, low-high,
  730. * high-low and high-high. They're referred to as orientations in the decoder
  731. * and encoder.
  732. *
  733. * The LL (low-low) area contains the original image downsampled by the amount
  734. * of levels. The rest of the areas can be thought as the details needed
  735. * to restore the image perfectly to its original size.
  736. */
  737. static int dwt_plane(AVCodecContext *avctx, void *arg)
  738. {
  739. TransformArgs *transform_dat = arg;
  740. VC2EncContext *s = transform_dat->ctx;
  741. const void *frame_data = transform_dat->idata;
  742. const ptrdiff_t linesize = transform_dat->istride;
  743. const int field = transform_dat->field;
  744. const Plane *p = transform_dat->plane;
  745. VC2TransformContext *t = &transform_dat->t;
  746. dwtcoef *buf = p->coef_buf;
  747. const int idx = s->wavelet_idx;
  748. const int skip = 1 + s->interlaced;
  749. int x, y, level, offset;
  750. ptrdiff_t pix_stride = linesize >> (s->bpp - 1);
  751. if (field == 1) {
  752. offset = 0;
  753. pix_stride <<= 1;
  754. } else if (field == 2) {
  755. offset = pix_stride;
  756. pix_stride <<= 1;
  757. } else {
  758. offset = 0;
  759. }
  760. if (s->bpp == 1) {
  761. const uint8_t *pix = (const uint8_t *)frame_data + offset;
  762. for (y = 0; y < p->height*skip; y+=skip) {
  763. for (x = 0; x < p->width; x++) {
  764. buf[x] = pix[x] - s->diff_offset;
  765. }
  766. buf += p->coef_stride;
  767. pix += pix_stride;
  768. }
  769. } else {
  770. const uint16_t *pix = (const uint16_t *)frame_data + offset;
  771. for (y = 0; y < p->height*skip; y+=skip) {
  772. for (x = 0; x < p->width; x++) {
  773. buf[x] = pix[x] - s->diff_offset;
  774. }
  775. buf += p->coef_stride;
  776. pix += pix_stride;
  777. }
  778. }
  779. memset(buf, 0, p->coef_stride * (p->dwt_height - p->height) * sizeof(dwtcoef));
  780. for (level = s->wavelet_depth-1; level >= 0; level--) {
  781. const SubBand *b = &p->band[level][0];
  782. t->vc2_subband_dwt[idx](t, p->coef_buf, p->coef_stride,
  783. b->width, b->height);
  784. }
  785. return 0;
  786. }
  787. static int encode_frame(VC2EncContext *s, AVPacket *avpkt, const AVFrame *frame,
  788. const char *aux_data, const int header_size, int field)
  789. {
  790. int i, ret;
  791. int64_t max_frame_bytes;
  792. /* Threaded DWT transform */
  793. for (i = 0; i < 3; i++) {
  794. s->transform_args[i].ctx = s;
  795. s->transform_args[i].field = field;
  796. s->transform_args[i].plane = &s->plane[i];
  797. s->transform_args[i].idata = frame->data[i];
  798. s->transform_args[i].istride = frame->linesize[i];
  799. }
  800. s->avctx->execute(s->avctx, dwt_plane, s->transform_args, NULL, 3,
  801. sizeof(TransformArgs));
  802. /* Calculate per-slice quantizers and sizes */
  803. max_frame_bytes = header_size + calc_slice_sizes(s);
  804. if (field < 2) {
  805. ret = ff_alloc_packet2(s->avctx, avpkt,
  806. max_frame_bytes << s->interlaced,
  807. max_frame_bytes << s->interlaced);
  808. if (ret) {
  809. av_log(s->avctx, AV_LOG_ERROR, "Error getting output packet.\n");
  810. return ret;
  811. }
  812. init_put_bits(&s->pb, avpkt->data, avpkt->size);
  813. }
  814. /* Sequence header */
  815. encode_parse_info(s, DIRAC_PCODE_SEQ_HEADER);
  816. encode_seq_header(s);
  817. /* Encoder version */
  818. if (aux_data) {
  819. encode_parse_info(s, DIRAC_PCODE_AUX);
  820. avpriv_put_string(&s->pb, aux_data, 1);
  821. }
  822. /* Picture header */
  823. encode_parse_info(s, DIRAC_PCODE_PICTURE_HQ);
  824. encode_picture_start(s);
  825. /* Encode slices */
  826. encode_slices(s);
  827. /* End sequence */
  828. encode_parse_info(s, DIRAC_PCODE_END_SEQ);
  829. return 0;
  830. }
  831. static av_cold int vc2_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  832. const AVFrame *frame, int *got_packet)
  833. {
  834. int ret = 0;
  835. int sig_size = 256;
  836. VC2EncContext *s = avctx->priv_data;
  837. const char aux_data[] = LIBAVCODEC_IDENT;
  838. const int aux_data_size = sizeof(aux_data);
  839. const int header_size = 100 + aux_data_size;
  840. int64_t max_frame_bytes, r_bitrate = avctx->bit_rate >> (s->interlaced);
  841. s->avctx = avctx;
  842. s->size_scaler = 2;
  843. s->prefix_bytes = 0;
  844. s->last_parse_code = 0;
  845. s->next_parse_offset = 0;
  846. /* Rate control */
  847. max_frame_bytes = (av_rescale(r_bitrate, s->avctx->time_base.num,
  848. s->avctx->time_base.den) >> 3) - header_size;
  849. /* Find an appropriate size scaler */
  850. while (sig_size > 255) {
  851. s->slice_max_bytes = FFALIGN(av_rescale(max_frame_bytes, 1,
  852. s->num_x*s->num_y), s->size_scaler);
  853. s->slice_max_bytes += 4 + s->prefix_bytes;
  854. sig_size = s->slice_max_bytes/s->size_scaler; /* Signalled slize size */
  855. s->size_scaler <<= 1;
  856. }
  857. s->slice_min_bytes = s->slice_max_bytes - s->slice_max_bytes*(s->tolerance/100.0f);
  858. ret = encode_frame(s, avpkt, frame, aux_data, header_size, s->interlaced);
  859. if (ret)
  860. return ret;
  861. if (s->interlaced) {
  862. ret = encode_frame(s, avpkt, frame, aux_data, header_size, 2);
  863. if (ret)
  864. return ret;
  865. }
  866. flush_put_bits(&s->pb);
  867. avpkt->size = put_bits_count(&s->pb) >> 3;
  868. *got_packet = 1;
  869. return 0;
  870. }
  871. static av_cold int vc2_encode_end(AVCodecContext *avctx)
  872. {
  873. int i;
  874. VC2EncContext *s = avctx->priv_data;
  875. av_log(avctx, AV_LOG_INFO, "Qavg: %i\n", s->q_avg);
  876. for (i = 0; i < 3; i++) {
  877. ff_vc2enc_free_transforms(&s->transform_args[i].t);
  878. av_freep(&s->plane[i].coef_buf);
  879. }
  880. av_freep(&s->slice_args);
  881. av_freep(&s->coef_lut_len);
  882. av_freep(&s->coef_lut_val);
  883. return 0;
  884. }
  885. static av_cold int vc2_encode_init(AVCodecContext *avctx)
  886. {
  887. Plane *p;
  888. SubBand *b;
  889. int i, j, level, o, shift;
  890. const AVPixFmtDescriptor *fmt = av_pix_fmt_desc_get(avctx->pix_fmt);
  891. const int depth = fmt->comp[0].depth;
  892. VC2EncContext *s = avctx->priv_data;
  893. s->picture_number = 0;
  894. /* Total allowed quantization range */
  895. s->q_ceil = MAX_QUANT_INDEX;
  896. s->ver.major = 2;
  897. s->ver.minor = 0;
  898. s->profile = 3;
  899. s->level = 3;
  900. s->base_vf = -1;
  901. s->strict_compliance = 1;
  902. s->q_avg = 0;
  903. s->slice_max_bytes = 0;
  904. s->slice_min_bytes = 0;
  905. /* Mark unknown as progressive */
  906. s->interlaced = !((avctx->field_order == AV_FIELD_UNKNOWN) ||
  907. (avctx->field_order == AV_FIELD_PROGRESSIVE));
  908. if (avctx->pix_fmt == AV_PIX_FMT_YUV422P10) {
  909. if (avctx->width == 1280 && avctx->height == 720) {
  910. s->level = 3;
  911. if (avctx->time_base.num == 1001 && avctx->time_base.den == 60000)
  912. s->base_vf = 9;
  913. if (avctx->time_base.num == 1 && avctx->time_base.den == 50)
  914. s->base_vf = 10;
  915. } else if (avctx->width == 1920 && avctx->height == 1080) {
  916. s->level = 3;
  917. if (s->interlaced) {
  918. if (avctx->time_base.num == 1001 && avctx->time_base.den == 30000)
  919. s->base_vf = 11;
  920. if (avctx->time_base.num == 1 && avctx->time_base.den == 50)
  921. s->base_vf = 12;
  922. } else {
  923. if (avctx->time_base.num == 1001 && avctx->time_base.den == 60000)
  924. s->base_vf = 13;
  925. if (avctx->time_base.num == 1 && avctx->time_base.den == 50)
  926. s->base_vf = 14;
  927. if (avctx->time_base.num == 1001 && avctx->time_base.den == 24000)
  928. s->base_vf = 21;
  929. }
  930. } else if (avctx->width == 3840 && avctx->height == 2160) {
  931. s->level = 6;
  932. if (avctx->time_base.num == 1001 && avctx->time_base.den == 60000)
  933. s->base_vf = 17;
  934. if (avctx->time_base.num == 1 && avctx->time_base.den == 50)
  935. s->base_vf = 18;
  936. }
  937. }
  938. if (s->interlaced && s->base_vf <= 0) {
  939. av_log(avctx, AV_LOG_ERROR, "Interlacing not supported with non standard formats!\n");
  940. return AVERROR_UNKNOWN;
  941. }
  942. if (s->interlaced)
  943. av_log(avctx, AV_LOG_WARNING, "Interlacing enabled!\n");
  944. if ((s->slice_width & (s->slice_width - 1)) ||
  945. (s->slice_height & (s->slice_height - 1))) {
  946. av_log(avctx, AV_LOG_ERROR, "Slice size is not a power of two!\n");
  947. return AVERROR_UNKNOWN;
  948. }
  949. if ((s->slice_width > avctx->width) ||
  950. (s->slice_height > avctx->height)) {
  951. av_log(avctx, AV_LOG_ERROR, "Slice size is bigger than the image!\n");
  952. return AVERROR_UNKNOWN;
  953. }
  954. if (s->base_vf <= 0) {
  955. if (avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
  956. s->strict_compliance = s->base_vf = 0;
  957. av_log(avctx, AV_LOG_WARNING, "Disabling strict compliance\n");
  958. } else {
  959. av_log(avctx, AV_LOG_WARNING, "Given format does not strictly comply with "
  960. "the specifications, please add a -strict -1 flag to use it\n");
  961. return AVERROR_UNKNOWN;
  962. }
  963. } else {
  964. av_log(avctx, AV_LOG_INFO, "Selected base video format = %i\n", s->base_vf);
  965. }
  966. /* Chroma subsampling */
  967. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
  968. /* Bit depth and color range index */
  969. if (depth == 8 && avctx->color_range == AVCOL_RANGE_JPEG) {
  970. s->bpp = 1;
  971. s->bpp_idx = 1;
  972. s->diff_offset = 128;
  973. } else if (depth == 8 && (avctx->color_range == AVCOL_RANGE_MPEG ||
  974. avctx->color_range == AVCOL_RANGE_UNSPECIFIED)) {
  975. s->bpp = 1;
  976. s->bpp_idx = 2;
  977. s->diff_offset = 128;
  978. } else if (depth == 10) {
  979. s->bpp = 2;
  980. s->bpp_idx = 3;
  981. s->diff_offset = 512;
  982. } else {
  983. s->bpp = 2;
  984. s->bpp_idx = 4;
  985. s->diff_offset = 2048;
  986. }
  987. /* Planes initialization */
  988. for (i = 0; i < 3; i++) {
  989. int w, h;
  990. p = &s->plane[i];
  991. p->width = avctx->width >> (i ? s->chroma_x_shift : 0);
  992. p->height = avctx->height >> (i ? s->chroma_y_shift : 0);
  993. if (s->interlaced)
  994. p->height >>= 1;
  995. p->dwt_width = w = FFALIGN(p->width, (1 << s->wavelet_depth));
  996. p->dwt_height = h = FFALIGN(p->height, (1 << s->wavelet_depth));
  997. p->coef_stride = FFALIGN(p->dwt_width, 32);
  998. p->coef_buf = av_malloc(p->coef_stride*p->dwt_height*sizeof(dwtcoef));
  999. if (!p->coef_buf)
  1000. goto alloc_fail;
  1001. for (level = s->wavelet_depth-1; level >= 0; level--) {
  1002. w = w >> 1;
  1003. h = h >> 1;
  1004. for (o = 0; o < 4; o++) {
  1005. b = &p->band[level][o];
  1006. b->width = w;
  1007. b->height = h;
  1008. b->stride = p->coef_stride;
  1009. shift = (o > 1)*b->height*b->stride + (o & 1)*b->width;
  1010. b->buf = p->coef_buf + shift;
  1011. }
  1012. }
  1013. /* DWT init */
  1014. if (ff_vc2enc_init_transforms(&s->transform_args[i].t,
  1015. s->plane[i].coef_stride,
  1016. s->plane[i].dwt_height))
  1017. goto alloc_fail;
  1018. }
  1019. /* Slices */
  1020. s->num_x = s->plane[0].dwt_width/s->slice_width;
  1021. s->num_y = s->plane[0].dwt_height/s->slice_height;
  1022. s->slice_args = av_calloc(s->num_x*s->num_y, sizeof(SliceArgs));
  1023. if (!s->slice_args)
  1024. goto alloc_fail;
  1025. /* Lookup tables */
  1026. s->coef_lut_len = av_malloc(COEF_LUT_TAB*(s->q_ceil+1)*sizeof(*s->coef_lut_len));
  1027. if (!s->coef_lut_len)
  1028. goto alloc_fail;
  1029. s->coef_lut_val = av_malloc(COEF_LUT_TAB*(s->q_ceil+1)*sizeof(*s->coef_lut_val));
  1030. if (!s->coef_lut_val)
  1031. goto alloc_fail;
  1032. for (i = 0; i < s->q_ceil; i++) {
  1033. uint8_t *len_lut = &s->coef_lut_len[i*COEF_LUT_TAB];
  1034. uint32_t *val_lut = &s->coef_lut_val[i*COEF_LUT_TAB];
  1035. for (j = 0; j < COEF_LUT_TAB; j++) {
  1036. get_vc2_ue_uint(QUANT(j, ff_dirac_qscale_tab[i]),
  1037. &len_lut[j], &val_lut[j]);
  1038. }
  1039. }
  1040. return 0;
  1041. alloc_fail:
  1042. vc2_encode_end(avctx);
  1043. av_log(avctx, AV_LOG_ERROR, "Unable to allocate memory!\n");
  1044. return AVERROR(ENOMEM);
  1045. }
  1046. #define VC2ENC_FLAGS (AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
  1047. static const AVOption vc2enc_options[] = {
  1048. {"tolerance", "Max undershoot in percent", offsetof(VC2EncContext, tolerance), AV_OPT_TYPE_DOUBLE, {.dbl = 5.0f}, 0.0f, 45.0f, VC2ENC_FLAGS, "tolerance"},
  1049. {"slice_width", "Slice width", offsetof(VC2EncContext, slice_width), AV_OPT_TYPE_INT, {.i64 = 64}, 32, 1024, VC2ENC_FLAGS, "slice_width"},
  1050. {"slice_height", "Slice height", offsetof(VC2EncContext, slice_height), AV_OPT_TYPE_INT, {.i64 = 32}, 8, 1024, VC2ENC_FLAGS, "slice_height"},
  1051. {"wavelet_depth", "Transform depth", offsetof(VC2EncContext, wavelet_depth), AV_OPT_TYPE_INT, {.i64 = 4}, 1, 5, VC2ENC_FLAGS, "wavelet_depth"},
  1052. {"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"},
  1053. {"9_7", "Deslauriers-Dubuc (9,7)", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_TRANSFORM_9_7}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "wavelet_idx"},
  1054. {"5_3", "LeGall (5,3)", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_TRANSFORM_5_3}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "wavelet_idx"},
  1055. {"haar", "Haar (with shift)", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_TRANSFORM_HAAR_S}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "wavelet_idx"},
  1056. {"haar_noshift", "Haar (without shift)", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_TRANSFORM_HAAR}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "wavelet_idx"},
  1057. {"qm", "Custom quantization matrix", offsetof(VC2EncContext, quant_matrix), AV_OPT_TYPE_INT, {.i64 = VC2_QM_DEF}, 0, VC2_QM_NB, VC2ENC_FLAGS, "quant_matrix"},
  1058. {"default", "Default from the specifications", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_QM_DEF}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "quant_matrix"},
  1059. {"color", "Prevents low bitrate discoloration", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_QM_COL}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "quant_matrix"},
  1060. {"flat", "Optimize for PSNR", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_QM_FLAT}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "quant_matrix"},
  1061. {NULL}
  1062. };
  1063. static const AVClass vc2enc_class = {
  1064. .class_name = "SMPTE VC-2 encoder",
  1065. .category = AV_CLASS_CATEGORY_ENCODER,
  1066. .option = vc2enc_options,
  1067. .item_name = av_default_item_name,
  1068. .version = LIBAVUTIL_VERSION_INT
  1069. };
  1070. static const AVCodecDefault vc2enc_defaults[] = {
  1071. { "b", "600000000" },
  1072. { NULL },
  1073. };
  1074. static const enum AVPixelFormat allowed_pix_fmts[] = {
  1075. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
  1076. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  1077. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
  1078. AV_PIX_FMT_NONE
  1079. };
  1080. AVCodec ff_vc2_encoder = {
  1081. .name = "vc2",
  1082. .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-2"),
  1083. .type = AVMEDIA_TYPE_VIDEO,
  1084. .id = AV_CODEC_ID_DIRAC,
  1085. .priv_data_size = sizeof(VC2EncContext),
  1086. .init = vc2_encode_init,
  1087. .close = vc2_encode_end,
  1088. .capabilities = AV_CODEC_CAP_SLICE_THREADS,
  1089. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  1090. .encode2 = vc2_encode_frame,
  1091. .priv_class = &vc2enc_class,
  1092. .defaults = vc2enc_defaults,
  1093. .pix_fmts = allowed_pix_fmts
  1094. };