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.

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