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.

1288 lines
42KB

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