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.

1198 lines
38KB

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