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.

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