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.

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