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.

1343 lines
45KB

  1. /*
  2. * Apple ProRes encoder
  3. *
  4. * Copyright (c) 2012 Konstantin Shishkov
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/opt.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "avcodec.h"
  25. #include "fdctdsp.h"
  26. #include "put_bits.h"
  27. #include "bytestream.h"
  28. #include "internal.h"
  29. #include "proresdata.h"
  30. #define CFACTOR_Y422 2
  31. #define CFACTOR_Y444 3
  32. #define MAX_MBS_PER_SLICE 8
  33. #define MAX_PLANES 4
  34. enum {
  35. PRORES_PROFILE_PROXY = 0,
  36. PRORES_PROFILE_LT,
  37. PRORES_PROFILE_STANDARD,
  38. PRORES_PROFILE_HQ,
  39. PRORES_PROFILE_4444,
  40. };
  41. enum {
  42. QUANT_MAT_PROXY = 0,
  43. QUANT_MAT_LT,
  44. QUANT_MAT_STANDARD,
  45. QUANT_MAT_HQ,
  46. QUANT_MAT_DEFAULT,
  47. };
  48. static const uint8_t prores_quant_matrices[][64] = {
  49. { // proxy
  50. 4, 7, 9, 11, 13, 14, 15, 63,
  51. 7, 7, 11, 12, 14, 15, 63, 63,
  52. 9, 11, 13, 14, 15, 63, 63, 63,
  53. 11, 11, 13, 14, 63, 63, 63, 63,
  54. 11, 13, 14, 63, 63, 63, 63, 63,
  55. 13, 14, 63, 63, 63, 63, 63, 63,
  56. 13, 63, 63, 63, 63, 63, 63, 63,
  57. 63, 63, 63, 63, 63, 63, 63, 63,
  58. },
  59. { // LT
  60. 4, 5, 6, 7, 9, 11, 13, 15,
  61. 5, 5, 7, 8, 11, 13, 15, 17,
  62. 6, 7, 9, 11, 13, 15, 15, 17,
  63. 7, 7, 9, 11, 13, 15, 17, 19,
  64. 7, 9, 11, 13, 14, 16, 19, 23,
  65. 9, 11, 13, 14, 16, 19, 23, 29,
  66. 9, 11, 13, 15, 17, 21, 28, 35,
  67. 11, 13, 16, 17, 21, 28, 35, 41,
  68. },
  69. { // standard
  70. 4, 4, 5, 5, 6, 7, 7, 9,
  71. 4, 4, 5, 6, 7, 7, 9, 9,
  72. 5, 5, 6, 7, 7, 9, 9, 10,
  73. 5, 5, 6, 7, 7, 9, 9, 10,
  74. 5, 6, 7, 7, 8, 9, 10, 12,
  75. 6, 7, 7, 8, 9, 10, 12, 15,
  76. 6, 7, 7, 9, 10, 11, 14, 17,
  77. 7, 7, 9, 10, 11, 14, 17, 21,
  78. },
  79. { // high quality
  80. 4, 4, 4, 4, 4, 4, 4, 4,
  81. 4, 4, 4, 4, 4, 4, 4, 4,
  82. 4, 4, 4, 4, 4, 4, 4, 4,
  83. 4, 4, 4, 4, 4, 4, 4, 5,
  84. 4, 4, 4, 4, 4, 4, 5, 5,
  85. 4, 4, 4, 4, 4, 5, 5, 6,
  86. 4, 4, 4, 4, 5, 5, 6, 7,
  87. 4, 4, 4, 4, 5, 6, 7, 7,
  88. },
  89. { // codec default
  90. 4, 4, 4, 4, 4, 4, 4, 4,
  91. 4, 4, 4, 4, 4, 4, 4, 4,
  92. 4, 4, 4, 4, 4, 4, 4, 4,
  93. 4, 4, 4, 4, 4, 4, 4, 4,
  94. 4, 4, 4, 4, 4, 4, 4, 4,
  95. 4, 4, 4, 4, 4, 4, 4, 4,
  96. 4, 4, 4, 4, 4, 4, 4, 4,
  97. 4, 4, 4, 4, 4, 4, 4, 4,
  98. },
  99. };
  100. #define NUM_MB_LIMITS 4
  101. static const int prores_mb_limits[NUM_MB_LIMITS] = {
  102. 1620, // up to 720x576
  103. 2700, // up to 960x720
  104. 6075, // up to 1440x1080
  105. 9216, // up to 2048x1152
  106. };
  107. static const struct prores_profile {
  108. const char *full_name;
  109. uint32_t tag;
  110. int min_quant;
  111. int max_quant;
  112. int br_tab[NUM_MB_LIMITS];
  113. int quant;
  114. } prores_profile_info[5] = {
  115. {
  116. .full_name = "proxy",
  117. .tag = MKTAG('a', 'p', 'c', 'o'),
  118. .min_quant = 4,
  119. .max_quant = 8,
  120. .br_tab = { 300, 242, 220, 194 },
  121. .quant = QUANT_MAT_PROXY,
  122. },
  123. {
  124. .full_name = "LT",
  125. .tag = MKTAG('a', 'p', 'c', 's'),
  126. .min_quant = 1,
  127. .max_quant = 9,
  128. .br_tab = { 720, 560, 490, 440 },
  129. .quant = QUANT_MAT_LT,
  130. },
  131. {
  132. .full_name = "standard",
  133. .tag = MKTAG('a', 'p', 'c', 'n'),
  134. .min_quant = 1,
  135. .max_quant = 6,
  136. .br_tab = { 1050, 808, 710, 632 },
  137. .quant = QUANT_MAT_STANDARD,
  138. },
  139. {
  140. .full_name = "high quality",
  141. .tag = MKTAG('a', 'p', 'c', 'h'),
  142. .min_quant = 1,
  143. .max_quant = 6,
  144. .br_tab = { 1566, 1216, 1070, 950 },
  145. .quant = QUANT_MAT_HQ,
  146. },
  147. {
  148. .full_name = "4444",
  149. .tag = MKTAG('a', 'p', '4', 'h'),
  150. .min_quant = 1,
  151. .max_quant = 6,
  152. .br_tab = { 2350, 1828, 1600, 1425 },
  153. .quant = QUANT_MAT_HQ,
  154. }
  155. };
  156. #define TRELLIS_WIDTH 16
  157. #define SCORE_LIMIT INT_MAX / 2
  158. struct TrellisNode {
  159. int prev_node;
  160. int quant;
  161. int bits;
  162. int score;
  163. };
  164. #define MAX_STORED_Q 16
  165. typedef struct ProresThreadData {
  166. DECLARE_ALIGNED(16, int16_t, blocks)[MAX_PLANES][64 * 4 * MAX_MBS_PER_SLICE];
  167. DECLARE_ALIGNED(16, uint16_t, emu_buf)[16 * 16];
  168. int16_t custom_q[64];
  169. struct TrellisNode *nodes;
  170. } ProresThreadData;
  171. typedef struct ProresContext {
  172. AVClass *class;
  173. DECLARE_ALIGNED(16, int16_t, blocks)[MAX_PLANES][64 * 4 * MAX_MBS_PER_SLICE];
  174. DECLARE_ALIGNED(16, uint16_t, emu_buf)[16*16];
  175. int16_t quants[MAX_STORED_Q][64];
  176. int16_t custom_q[64];
  177. const uint8_t *quant_mat;
  178. const uint8_t *scantable;
  179. void (*fdct)(FDCTDSPContext *fdsp, const uint16_t *src,
  180. ptrdiff_t linesize, int16_t *block);
  181. FDCTDSPContext fdsp;
  182. const AVFrame *pic;
  183. int mb_width, mb_height;
  184. int mbs_per_slice;
  185. int num_chroma_blocks, chroma_factor;
  186. int slices_width;
  187. int slices_per_picture;
  188. int pictures_per_frame; // 1 for progressive, 2 for interlaced
  189. int cur_picture_idx;
  190. int num_planes;
  191. int bits_per_mb;
  192. int force_quant;
  193. int alpha_bits;
  194. int warn;
  195. char *vendor;
  196. int quant_sel;
  197. int frame_size_upper_bound;
  198. int profile;
  199. const struct prores_profile *profile_info;
  200. int *slice_q;
  201. ProresThreadData *tdata;
  202. } ProresContext;
  203. static void get_slice_data(ProresContext *ctx, const uint16_t *src,
  204. ptrdiff_t linesize, int x, int y, int w, int h,
  205. int16_t *blocks, uint16_t *emu_buf,
  206. int mbs_per_slice, int blocks_per_mb, int is_chroma)
  207. {
  208. const uint16_t *esrc;
  209. const int mb_width = 4 * blocks_per_mb;
  210. ptrdiff_t elinesize;
  211. int i, j, k;
  212. for (i = 0; i < mbs_per_slice; i++, src += mb_width) {
  213. if (x >= w) {
  214. memset(blocks, 0, 64 * (mbs_per_slice - i) * blocks_per_mb
  215. * sizeof(*blocks));
  216. return;
  217. }
  218. if (x + mb_width <= w && y + 16 <= h) {
  219. esrc = src;
  220. elinesize = linesize;
  221. } else {
  222. int bw, bh, pix;
  223. esrc = emu_buf;
  224. elinesize = 16 * sizeof(*emu_buf);
  225. bw = FFMIN(w - x, mb_width);
  226. bh = FFMIN(h - y, 16);
  227. for (j = 0; j < bh; j++) {
  228. memcpy(emu_buf + j * 16,
  229. (const uint8_t*)src + j * linesize,
  230. bw * sizeof(*src));
  231. pix = emu_buf[j * 16 + bw - 1];
  232. for (k = bw; k < mb_width; k++)
  233. emu_buf[j * 16 + k] = pix;
  234. }
  235. for (; j < 16; j++)
  236. memcpy(emu_buf + j * 16,
  237. emu_buf + (bh - 1) * 16,
  238. mb_width * sizeof(*emu_buf));
  239. }
  240. if (!is_chroma) {
  241. ctx->fdct(&ctx->fdsp, esrc, elinesize, blocks);
  242. blocks += 64;
  243. if (blocks_per_mb > 2) {
  244. ctx->fdct(&ctx->fdsp, esrc + 8, elinesize, blocks);
  245. blocks += 64;
  246. }
  247. ctx->fdct(&ctx->fdsp, esrc + elinesize * 4, elinesize, blocks);
  248. blocks += 64;
  249. if (blocks_per_mb > 2) {
  250. ctx->fdct(&ctx->fdsp, esrc + elinesize * 4 + 8, elinesize, blocks);
  251. blocks += 64;
  252. }
  253. } else {
  254. ctx->fdct(&ctx->fdsp, esrc, elinesize, blocks);
  255. blocks += 64;
  256. ctx->fdct(&ctx->fdsp, esrc + elinesize * 4, elinesize, blocks);
  257. blocks += 64;
  258. if (blocks_per_mb > 2) {
  259. ctx->fdct(&ctx->fdsp, esrc + 8, elinesize, blocks);
  260. blocks += 64;
  261. ctx->fdct(&ctx->fdsp, esrc + elinesize * 4 + 8, elinesize, blocks);
  262. blocks += 64;
  263. }
  264. }
  265. x += mb_width;
  266. }
  267. }
  268. static void get_alpha_data(ProresContext *ctx, const uint16_t *src,
  269. ptrdiff_t linesize, int x, int y, int w, int h,
  270. int16_t *blocks, int mbs_per_slice, int abits)
  271. {
  272. const int slice_width = 16 * mbs_per_slice;
  273. int i, j, copy_w, copy_h;
  274. copy_w = FFMIN(w - x, slice_width);
  275. copy_h = FFMIN(h - y, 16);
  276. for (i = 0; i < copy_h; i++) {
  277. memcpy(blocks, src, copy_w * sizeof(*src));
  278. if (abits == 8)
  279. for (j = 0; j < copy_w; j++)
  280. blocks[j] >>= 2;
  281. else
  282. for (j = 0; j < copy_w; j++)
  283. blocks[j] = (blocks[j] << 6) | (blocks[j] >> 4);
  284. for (j = copy_w; j < slice_width; j++)
  285. blocks[j] = blocks[copy_w - 1];
  286. blocks += slice_width;
  287. src += linesize >> 1;
  288. }
  289. for (; i < 16; i++) {
  290. memcpy(blocks, blocks - slice_width, slice_width * sizeof(*blocks));
  291. blocks += slice_width;
  292. }
  293. }
  294. /**
  295. * Write an unsigned rice/exp golomb codeword.
  296. */
  297. static inline void encode_vlc_codeword(PutBitContext *pb, unsigned codebook, int val)
  298. {
  299. unsigned int rice_order, exp_order, switch_bits, switch_val;
  300. int exponent;
  301. /* number of prefix bits to switch between Rice and expGolomb */
  302. switch_bits = (codebook & 3) + 1;
  303. rice_order = codebook >> 5; /* rice code order */
  304. exp_order = (codebook >> 2) & 7; /* exp golomb code order */
  305. switch_val = switch_bits << rice_order;
  306. if (val >= switch_val) {
  307. val -= switch_val - (1 << exp_order);
  308. exponent = av_log2(val);
  309. put_bits(pb, exponent - exp_order + switch_bits, 0);
  310. put_bits(pb, exponent + 1, val);
  311. } else {
  312. exponent = val >> rice_order;
  313. if (exponent)
  314. put_bits(pb, exponent, 0);
  315. put_bits(pb, 1, 1);
  316. if (rice_order)
  317. put_sbits(pb, rice_order, val);
  318. }
  319. }
  320. #define GET_SIGN(x) ((x) >> 31)
  321. #define MAKE_CODE(x) (((x) << 1) ^ GET_SIGN(x))
  322. static void encode_dcs(PutBitContext *pb, int16_t *blocks,
  323. int blocks_per_slice, int scale)
  324. {
  325. int i;
  326. int codebook = 3, code, dc, prev_dc, delta, sign, new_sign;
  327. prev_dc = (blocks[0] - 0x4000) / scale;
  328. encode_vlc_codeword(pb, FIRST_DC_CB, MAKE_CODE(prev_dc));
  329. sign = 0;
  330. codebook = 3;
  331. blocks += 64;
  332. for (i = 1; i < blocks_per_slice; i++, blocks += 64) {
  333. dc = (blocks[0] - 0x4000) / scale;
  334. delta = dc - prev_dc;
  335. new_sign = GET_SIGN(delta);
  336. delta = (delta ^ sign) - sign;
  337. code = MAKE_CODE(delta);
  338. encode_vlc_codeword(pb, ff_prores_dc_codebook[codebook], code);
  339. codebook = (code + (code & 1)) >> 1;
  340. codebook = FFMIN(codebook, 3);
  341. sign = new_sign;
  342. prev_dc = dc;
  343. }
  344. }
  345. static void encode_acs(PutBitContext *pb, int16_t *blocks,
  346. int blocks_per_slice,
  347. int plane_size_factor,
  348. const uint8_t *scan, const int16_t *qmat)
  349. {
  350. int idx, i;
  351. int run, level, run_cb, lev_cb;
  352. int max_coeffs, abs_level;
  353. max_coeffs = blocks_per_slice << 6;
  354. run_cb = ff_prores_run_to_cb_index[4];
  355. lev_cb = ff_prores_lev_to_cb_index[2];
  356. run = 0;
  357. for (i = 1; i < 64; i++) {
  358. for (idx = scan[i]; idx < max_coeffs; idx += 64) {
  359. level = blocks[idx] / qmat[scan[i]];
  360. if (level) {
  361. abs_level = FFABS(level);
  362. encode_vlc_codeword(pb, ff_prores_ac_codebook[run_cb], run);
  363. encode_vlc_codeword(pb, ff_prores_ac_codebook[lev_cb],
  364. abs_level - 1);
  365. put_sbits(pb, 1, GET_SIGN(level));
  366. run_cb = ff_prores_run_to_cb_index[FFMIN(run, 15)];
  367. lev_cb = ff_prores_lev_to_cb_index[FFMIN(abs_level, 9)];
  368. run = 0;
  369. } else {
  370. run++;
  371. }
  372. }
  373. }
  374. }
  375. static int encode_slice_plane(ProresContext *ctx, PutBitContext *pb,
  376. const uint16_t *src, ptrdiff_t linesize,
  377. int mbs_per_slice, int16_t *blocks,
  378. int blocks_per_mb, int plane_size_factor,
  379. const int16_t *qmat)
  380. {
  381. int blocks_per_slice, saved_pos;
  382. saved_pos = put_bits_count(pb);
  383. blocks_per_slice = mbs_per_slice * blocks_per_mb;
  384. encode_dcs(pb, blocks, blocks_per_slice, qmat[0]);
  385. encode_acs(pb, blocks, blocks_per_slice, plane_size_factor,
  386. ctx->scantable, qmat);
  387. flush_put_bits(pb);
  388. return (put_bits_count(pb) - saved_pos) >> 3;
  389. }
  390. static void put_alpha_diff(PutBitContext *pb, int cur, int prev, int abits)
  391. {
  392. const int mask = (1 << abits) - 1;
  393. const int dbits = (abits == 8) ? 4 : 7;
  394. const int dsize = 1 << dbits - 1;
  395. int diff = cur - prev;
  396. diff &= mask;
  397. if (diff >= (1 << abits) - dsize)
  398. diff -= 1 << abits;
  399. if (diff < -dsize || diff > dsize || !diff) {
  400. put_bits(pb, 1, 1);
  401. put_bits(pb, abits, diff);
  402. } else {
  403. put_bits(pb, 1, 0);
  404. put_bits(pb, dbits - 1, FFABS(diff) - 1);
  405. put_bits(pb, 1, diff < 0);
  406. }
  407. }
  408. static void put_alpha_run(PutBitContext *pb, int run)
  409. {
  410. if (run) {
  411. put_bits(pb, 1, 0);
  412. if (run < 0x10)
  413. put_bits(pb, 4, run);
  414. else
  415. put_bits(pb, 15, run);
  416. } else {
  417. put_bits(pb, 1, 1);
  418. }
  419. }
  420. // todo alpha quantisation for high quants
  421. static int encode_alpha_plane(ProresContext *ctx, PutBitContext *pb,
  422. int mbs_per_slice, uint16_t *blocks,
  423. int quant)
  424. {
  425. const int abits = ctx->alpha_bits;
  426. const int mask = (1 << abits) - 1;
  427. const int num_coeffs = mbs_per_slice * 256;
  428. int saved_pos = put_bits_count(pb);
  429. int prev = mask, cur;
  430. int idx = 0;
  431. int run = 0;
  432. cur = blocks[idx++];
  433. put_alpha_diff(pb, cur, prev, abits);
  434. prev = cur;
  435. do {
  436. cur = blocks[idx++];
  437. if (cur != prev) {
  438. put_alpha_run (pb, run);
  439. put_alpha_diff(pb, cur, prev, abits);
  440. prev = cur;
  441. run = 0;
  442. } else {
  443. run++;
  444. }
  445. } while (idx < num_coeffs);
  446. if (run)
  447. put_alpha_run(pb, run);
  448. flush_put_bits(pb);
  449. return (put_bits_count(pb) - saved_pos) >> 3;
  450. }
  451. static int encode_slice(AVCodecContext *avctx, const AVFrame *pic,
  452. PutBitContext *pb,
  453. int sizes[4], int x, int y, int quant,
  454. int mbs_per_slice)
  455. {
  456. ProresContext *ctx = avctx->priv_data;
  457. int i, xp, yp;
  458. int total_size = 0;
  459. const uint16_t *src;
  460. int slice_width_factor = av_log2(mbs_per_slice);
  461. int num_cblocks, pwidth, line_add;
  462. ptrdiff_t linesize;
  463. int plane_factor, is_chroma;
  464. uint16_t *qmat;
  465. if (ctx->pictures_per_frame == 1)
  466. line_add = 0;
  467. else
  468. line_add = ctx->cur_picture_idx ^ !pic->top_field_first;
  469. if (ctx->force_quant) {
  470. qmat = ctx->quants[0];
  471. } else if (quant < MAX_STORED_Q) {
  472. qmat = ctx->quants[quant];
  473. } else {
  474. qmat = ctx->custom_q;
  475. for (i = 0; i < 64; i++)
  476. qmat[i] = ctx->quant_mat[i] * quant;
  477. }
  478. for (i = 0; i < ctx->num_planes; i++) {
  479. is_chroma = (i == 1 || i == 2);
  480. plane_factor = slice_width_factor + 2;
  481. if (is_chroma)
  482. plane_factor += ctx->chroma_factor - 3;
  483. if (!is_chroma || ctx->chroma_factor == CFACTOR_Y444) {
  484. xp = x << 4;
  485. yp = y << 4;
  486. num_cblocks = 4;
  487. pwidth = avctx->width;
  488. } else {
  489. xp = x << 3;
  490. yp = y << 4;
  491. num_cblocks = 2;
  492. pwidth = avctx->width >> 1;
  493. }
  494. linesize = pic->linesize[i] * ctx->pictures_per_frame;
  495. src = (const uint16_t*)(pic->data[i] + yp * linesize +
  496. line_add * pic->linesize[i]) + xp;
  497. if (i < 3) {
  498. get_slice_data(ctx, src, linesize, xp, yp,
  499. pwidth, avctx->height / ctx->pictures_per_frame,
  500. ctx->blocks[0], ctx->emu_buf,
  501. mbs_per_slice, num_cblocks, is_chroma);
  502. sizes[i] = encode_slice_plane(ctx, pb, src, linesize,
  503. mbs_per_slice, ctx->blocks[0],
  504. num_cblocks, plane_factor,
  505. qmat);
  506. } else {
  507. get_alpha_data(ctx, src, linesize, xp, yp,
  508. pwidth, avctx->height / ctx->pictures_per_frame,
  509. ctx->blocks[0], mbs_per_slice, ctx->alpha_bits);
  510. sizes[i] = encode_alpha_plane(ctx, pb, mbs_per_slice,
  511. ctx->blocks[0], quant);
  512. }
  513. total_size += sizes[i];
  514. if (put_bits_left(pb) < 0) {
  515. av_log(avctx, AV_LOG_ERROR,
  516. "Underestimated required buffer size.\n");
  517. return AVERROR_BUG;
  518. }
  519. }
  520. return total_size;
  521. }
  522. static inline int estimate_vlc(unsigned codebook, int val)
  523. {
  524. unsigned int rice_order, exp_order, switch_bits, switch_val;
  525. int exponent;
  526. /* number of prefix bits to switch between Rice and expGolomb */
  527. switch_bits = (codebook & 3) + 1;
  528. rice_order = codebook >> 5; /* rice code order */
  529. exp_order = (codebook >> 2) & 7; /* exp golomb code order */
  530. switch_val = switch_bits << rice_order;
  531. if (val >= switch_val) {
  532. val -= switch_val - (1 << exp_order);
  533. exponent = av_log2(val);
  534. return exponent * 2 - exp_order + switch_bits + 1;
  535. } else {
  536. return (val >> rice_order) + rice_order + 1;
  537. }
  538. }
  539. static int estimate_dcs(int *error, int16_t *blocks, int blocks_per_slice,
  540. int scale)
  541. {
  542. int i;
  543. int codebook = 3, code, dc, prev_dc, delta, sign, new_sign;
  544. int bits;
  545. prev_dc = (blocks[0] - 0x4000) / scale;
  546. bits = estimate_vlc(FIRST_DC_CB, MAKE_CODE(prev_dc));
  547. sign = 0;
  548. codebook = 3;
  549. blocks += 64;
  550. *error += FFABS(blocks[0] - 0x4000) % scale;
  551. for (i = 1; i < blocks_per_slice; i++, blocks += 64) {
  552. dc = (blocks[0] - 0x4000) / scale;
  553. *error += FFABS(blocks[0] - 0x4000) % scale;
  554. delta = dc - prev_dc;
  555. new_sign = GET_SIGN(delta);
  556. delta = (delta ^ sign) - sign;
  557. code = MAKE_CODE(delta);
  558. bits += estimate_vlc(ff_prores_dc_codebook[codebook], code);
  559. codebook = (code + (code & 1)) >> 1;
  560. codebook = FFMIN(codebook, 3);
  561. sign = new_sign;
  562. prev_dc = dc;
  563. }
  564. return bits;
  565. }
  566. static int estimate_acs(int *error, int16_t *blocks, int blocks_per_slice,
  567. int plane_size_factor,
  568. const uint8_t *scan, const int16_t *qmat)
  569. {
  570. int idx, i;
  571. int run, level, run_cb, lev_cb;
  572. int max_coeffs, abs_level;
  573. int bits = 0;
  574. max_coeffs = blocks_per_slice << 6;
  575. run_cb = ff_prores_run_to_cb_index[4];
  576. lev_cb = ff_prores_lev_to_cb_index[2];
  577. run = 0;
  578. for (i = 1; i < 64; i++) {
  579. for (idx = scan[i]; idx < max_coeffs; idx += 64) {
  580. level = blocks[idx] / qmat[scan[i]];
  581. *error += FFABS(blocks[idx]) % qmat[scan[i]];
  582. if (level) {
  583. abs_level = FFABS(level);
  584. bits += estimate_vlc(ff_prores_ac_codebook[run_cb], run);
  585. bits += estimate_vlc(ff_prores_ac_codebook[lev_cb],
  586. abs_level - 1) + 1;
  587. run_cb = ff_prores_run_to_cb_index[FFMIN(run, 15)];
  588. lev_cb = ff_prores_lev_to_cb_index[FFMIN(abs_level, 9)];
  589. run = 0;
  590. } else {
  591. run++;
  592. }
  593. }
  594. }
  595. return bits;
  596. }
  597. static int estimate_slice_plane(ProresContext *ctx, int *error, int plane,
  598. const uint16_t *src, ptrdiff_t linesize,
  599. int mbs_per_slice,
  600. int blocks_per_mb, int plane_size_factor,
  601. const int16_t *qmat, ProresThreadData *td)
  602. {
  603. int blocks_per_slice;
  604. int bits;
  605. blocks_per_slice = mbs_per_slice * blocks_per_mb;
  606. bits = estimate_dcs(error, td->blocks[plane], blocks_per_slice, qmat[0]);
  607. bits += estimate_acs(error, td->blocks[plane], blocks_per_slice,
  608. plane_size_factor, ctx->scantable, qmat);
  609. return FFALIGN(bits, 8);
  610. }
  611. static int est_alpha_diff(int cur, int prev, int abits)
  612. {
  613. const int mask = (1 << abits) - 1;
  614. const int dbits = (abits == 8) ? 4 : 7;
  615. const int dsize = 1 << dbits - 1;
  616. int diff = cur - prev;
  617. diff &= mask;
  618. if (diff >= (1 << abits) - dsize)
  619. diff -= 1 << abits;
  620. if (diff < -dsize || diff > dsize || !diff)
  621. return abits + 1;
  622. else
  623. return dbits + 1;
  624. }
  625. static int estimate_alpha_plane(ProresContext *ctx, int *error,
  626. const uint16_t *src, ptrdiff_t linesize,
  627. int mbs_per_slice, int quant,
  628. int16_t *blocks)
  629. {
  630. const int abits = ctx->alpha_bits;
  631. const int mask = (1 << abits) - 1;
  632. const int num_coeffs = mbs_per_slice * 256;
  633. int prev = mask, cur;
  634. int idx = 0;
  635. int run = 0;
  636. int bits;
  637. *error = 0;
  638. cur = blocks[idx++];
  639. bits = est_alpha_diff(cur, prev, abits);
  640. prev = cur;
  641. do {
  642. cur = blocks[idx++];
  643. if (cur != prev) {
  644. if (!run)
  645. bits++;
  646. else if (run < 0x10)
  647. bits += 4;
  648. else
  649. bits += 15;
  650. bits += est_alpha_diff(cur, prev, abits);
  651. prev = cur;
  652. run = 0;
  653. } else {
  654. run++;
  655. }
  656. } while (idx < num_coeffs);
  657. if (run) {
  658. if (run < 0x10)
  659. bits += 4;
  660. else
  661. bits += 15;
  662. }
  663. return bits;
  664. }
  665. static int find_slice_quant(AVCodecContext *avctx,
  666. int trellis_node, int x, int y, int mbs_per_slice,
  667. ProresThreadData *td)
  668. {
  669. ProresContext *ctx = avctx->priv_data;
  670. int i, q, pq, xp, yp;
  671. const uint16_t *src;
  672. int slice_width_factor = av_log2(mbs_per_slice);
  673. int num_cblocks[MAX_PLANES], pwidth;
  674. int plane_factor[MAX_PLANES], is_chroma[MAX_PLANES];
  675. const int min_quant = ctx->profile_info->min_quant;
  676. const int max_quant = ctx->profile_info->max_quant;
  677. int error, bits, bits_limit;
  678. int mbs, prev, cur, new_score;
  679. int slice_bits[TRELLIS_WIDTH], slice_score[TRELLIS_WIDTH];
  680. int overquant;
  681. uint16_t *qmat;
  682. int linesize[4], line_add;
  683. if (ctx->pictures_per_frame == 1)
  684. line_add = 0;
  685. else
  686. line_add = ctx->cur_picture_idx ^ !ctx->pic->top_field_first;
  687. mbs = x + mbs_per_slice;
  688. for (i = 0; i < ctx->num_planes; i++) {
  689. is_chroma[i] = (i == 1 || i == 2);
  690. plane_factor[i] = slice_width_factor + 2;
  691. if (is_chroma[i])
  692. plane_factor[i] += ctx->chroma_factor - 3;
  693. if (!is_chroma[i] || ctx->chroma_factor == CFACTOR_Y444) {
  694. xp = x << 4;
  695. yp = y << 4;
  696. num_cblocks[i] = 4;
  697. pwidth = avctx->width;
  698. } else {
  699. xp = x << 3;
  700. yp = y << 4;
  701. num_cblocks[i] = 2;
  702. pwidth = avctx->width >> 1;
  703. }
  704. linesize[i] = ctx->pic->linesize[i] * ctx->pictures_per_frame;
  705. src = (const uint16_t *)(ctx->pic->data[i] + yp * linesize[i] +
  706. line_add * ctx->pic->linesize[i]) + xp;
  707. if (i < 3) {
  708. get_slice_data(ctx, src, linesize[i], xp, yp,
  709. pwidth, avctx->height / ctx->pictures_per_frame,
  710. td->blocks[i], td->emu_buf,
  711. mbs_per_slice, num_cblocks[i], is_chroma[i]);
  712. } else {
  713. get_alpha_data(ctx, src, linesize[i], xp, yp,
  714. pwidth, avctx->height / ctx->pictures_per_frame,
  715. td->blocks[i], mbs_per_slice, ctx->alpha_bits);
  716. }
  717. }
  718. for (q = min_quant; q < max_quant + 2; q++) {
  719. td->nodes[trellis_node + q].prev_node = -1;
  720. td->nodes[trellis_node + q].quant = q;
  721. }
  722. // todo: maybe perform coarser quantising to fit into frame size when needed
  723. for (q = min_quant; q <= max_quant; q++) {
  724. bits = 0;
  725. error = 0;
  726. for (i = 0; i < ctx->num_planes - !!ctx->alpha_bits; i++) {
  727. bits += estimate_slice_plane(ctx, &error, i,
  728. src, linesize[i],
  729. mbs_per_slice,
  730. num_cblocks[i], plane_factor[i],
  731. ctx->quants[q], td);
  732. }
  733. if (ctx->alpha_bits)
  734. bits += estimate_alpha_plane(ctx, &error, src, linesize[3],
  735. mbs_per_slice, q, td->blocks[3]);
  736. if (bits > 65000 * 8)
  737. error = SCORE_LIMIT;
  738. slice_bits[q] = bits;
  739. slice_score[q] = error;
  740. }
  741. if (slice_bits[max_quant] <= ctx->bits_per_mb * mbs_per_slice) {
  742. slice_bits[max_quant + 1] = slice_bits[max_quant];
  743. slice_score[max_quant + 1] = slice_score[max_quant] + 1;
  744. overquant = max_quant;
  745. } else {
  746. for (q = max_quant + 1; q < 128; q++) {
  747. bits = 0;
  748. error = 0;
  749. if (q < MAX_STORED_Q) {
  750. qmat = ctx->quants[q];
  751. } else {
  752. qmat = td->custom_q;
  753. for (i = 0; i < 64; i++)
  754. qmat[i] = ctx->quant_mat[i] * q;
  755. }
  756. for (i = 0; i < ctx->num_planes - !!ctx->alpha_bits; i++) {
  757. bits += estimate_slice_plane(ctx, &error, i,
  758. src, linesize[i],
  759. mbs_per_slice,
  760. num_cblocks[i], plane_factor[i],
  761. qmat, td);
  762. }
  763. if (ctx->alpha_bits)
  764. bits += estimate_alpha_plane(ctx, &error, src, linesize[3],
  765. mbs_per_slice, q, td->blocks[3]);
  766. if (bits <= ctx->bits_per_mb * mbs_per_slice)
  767. break;
  768. }
  769. slice_bits[max_quant + 1] = bits;
  770. slice_score[max_quant + 1] = error;
  771. overquant = q;
  772. }
  773. td->nodes[trellis_node + max_quant + 1].quant = overquant;
  774. bits_limit = mbs * ctx->bits_per_mb;
  775. for (pq = min_quant; pq < max_quant + 2; pq++) {
  776. prev = trellis_node - TRELLIS_WIDTH + pq;
  777. for (q = min_quant; q < max_quant + 2; q++) {
  778. cur = trellis_node + q;
  779. bits = td->nodes[prev].bits + slice_bits[q];
  780. error = slice_score[q];
  781. if (bits > bits_limit)
  782. error = SCORE_LIMIT;
  783. if (td->nodes[prev].score < SCORE_LIMIT && error < SCORE_LIMIT)
  784. new_score = td->nodes[prev].score + error;
  785. else
  786. new_score = SCORE_LIMIT;
  787. if (td->nodes[cur].prev_node == -1 ||
  788. td->nodes[cur].score >= new_score) {
  789. td->nodes[cur].bits = bits;
  790. td->nodes[cur].score = new_score;
  791. td->nodes[cur].prev_node = prev;
  792. }
  793. }
  794. }
  795. error = td->nodes[trellis_node + min_quant].score;
  796. pq = trellis_node + min_quant;
  797. for (q = min_quant + 1; q < max_quant + 2; q++) {
  798. if (td->nodes[trellis_node + q].score <= error) {
  799. error = td->nodes[trellis_node + q].score;
  800. pq = trellis_node + q;
  801. }
  802. }
  803. return pq;
  804. }
  805. static int find_quant_thread(AVCodecContext *avctx, void *arg,
  806. int jobnr, int threadnr)
  807. {
  808. ProresContext *ctx = avctx->priv_data;
  809. ProresThreadData *td = ctx->tdata + threadnr;
  810. int mbs_per_slice = ctx->mbs_per_slice;
  811. int x, y = jobnr, mb, q = 0;
  812. for (x = mb = 0; x < ctx->mb_width; x += mbs_per_slice, mb++) {
  813. while (ctx->mb_width - x < mbs_per_slice)
  814. mbs_per_slice >>= 1;
  815. q = find_slice_quant(avctx,
  816. (mb + 1) * TRELLIS_WIDTH, x, y,
  817. mbs_per_slice, td);
  818. }
  819. for (x = ctx->slices_width - 1; x >= 0; x--) {
  820. ctx->slice_q[x + y * ctx->slices_width] = td->nodes[q].quant;
  821. q = td->nodes[q].prev_node;
  822. }
  823. return 0;
  824. }
  825. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  826. const AVFrame *pic, int *got_packet)
  827. {
  828. ProresContext *ctx = avctx->priv_data;
  829. uint8_t *orig_buf, *buf, *slice_hdr, *slice_sizes, *tmp;
  830. uint8_t *picture_size_pos;
  831. PutBitContext pb;
  832. int x, y, i, mb, q = 0;
  833. int sizes[4] = { 0 };
  834. int slice_hdr_size = 2 + 2 * (ctx->num_planes - 1);
  835. int frame_size, picture_size, slice_size;
  836. int pkt_size, ret, max_slice_size = 0;
  837. uint8_t frame_flags;
  838. ctx->pic = pic;
  839. #if FF_API_CODED_FRAME
  840. FF_DISABLE_DEPRECATION_WARNINGS
  841. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  842. avctx->coded_frame->key_frame = 1;
  843. FF_ENABLE_DEPRECATION_WARNINGS
  844. #endif
  845. pkt_size = ctx->frame_size_upper_bound;
  846. if ((ret = ff_alloc_packet(pkt, pkt_size + AV_INPUT_BUFFER_MIN_SIZE)) < 0) {
  847. av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
  848. return ret;
  849. }
  850. orig_buf = pkt->data;
  851. // frame atom
  852. orig_buf += 4; // frame size
  853. bytestream_put_be32 (&orig_buf, FRAME_ID); // frame container ID
  854. buf = orig_buf;
  855. // frame header
  856. tmp = buf;
  857. buf += 2; // frame header size will be stored here
  858. bytestream_put_be16 (&buf, 0); // version 1
  859. bytestream_put_buffer(&buf, ctx->vendor, 4);
  860. bytestream_put_be16 (&buf, avctx->width);
  861. bytestream_put_be16 (&buf, avctx->height);
  862. frame_flags = ctx->chroma_factor << 6;
  863. if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT)
  864. frame_flags |= pic->top_field_first ? 0x04 : 0x08;
  865. bytestream_put_byte (&buf, frame_flags);
  866. bytestream_put_byte (&buf, 0); // reserved
  867. bytestream_put_byte (&buf, avctx->color_primaries);
  868. bytestream_put_byte (&buf, avctx->color_trc);
  869. bytestream_put_byte (&buf, avctx->colorspace);
  870. bytestream_put_byte (&buf, 0x40 | (ctx->alpha_bits >> 3));
  871. bytestream_put_byte (&buf, 0); // reserved
  872. if (ctx->quant_sel != QUANT_MAT_DEFAULT) {
  873. bytestream_put_byte (&buf, 0x03); // matrix flags - both matrices are present
  874. // luma quantisation matrix
  875. for (i = 0; i < 64; i++)
  876. bytestream_put_byte(&buf, ctx->quant_mat[i]);
  877. // chroma quantisation matrix
  878. for (i = 0; i < 64; i++)
  879. bytestream_put_byte(&buf, ctx->quant_mat[i]);
  880. } else {
  881. bytestream_put_byte (&buf, 0x00); // matrix flags - default matrices are used
  882. }
  883. bytestream_put_be16 (&tmp, buf - orig_buf); // write back frame header size
  884. for (ctx->cur_picture_idx = 0;
  885. ctx->cur_picture_idx < ctx->pictures_per_frame;
  886. ctx->cur_picture_idx++) {
  887. // picture header
  888. picture_size_pos = buf + 1;
  889. bytestream_put_byte (&buf, 0x40); // picture header size (in bits)
  890. buf += 4; // picture data size will be stored here
  891. bytestream_put_be16 (&buf, ctx->slices_per_picture);
  892. bytestream_put_byte (&buf, av_log2(ctx->mbs_per_slice) << 4); // slice width and height in MBs
  893. // seek table - will be filled during slice encoding
  894. slice_sizes = buf;
  895. buf += ctx->slices_per_picture * 2;
  896. // slices
  897. if (!ctx->force_quant) {
  898. ret = avctx->execute2(avctx, find_quant_thread, NULL, NULL,
  899. ctx->mb_height);
  900. if (ret)
  901. return ret;
  902. }
  903. for (y = 0; y < ctx->mb_height; y++) {
  904. int mbs_per_slice = ctx->mbs_per_slice;
  905. for (x = mb = 0; x < ctx->mb_width; x += mbs_per_slice, mb++) {
  906. q = ctx->force_quant ? ctx->force_quant
  907. : ctx->slice_q[mb + y * ctx->slices_width];
  908. while (ctx->mb_width - x < mbs_per_slice)
  909. mbs_per_slice >>= 1;
  910. bytestream_put_byte(&buf, slice_hdr_size << 3);
  911. slice_hdr = buf;
  912. buf += slice_hdr_size - 1;
  913. if (pkt_size <= buf - orig_buf + 2 * max_slice_size) {
  914. uint8_t *start = pkt->data;
  915. // Recompute new size according to max_slice_size
  916. // and deduce delta
  917. int delta = 200 + ctx->pictures_per_frame *
  918. ctx->slices_per_picture * max_slice_size -
  919. pkt_size;
  920. delta = FFMAX(delta, 2 * max_slice_size);
  921. ctx->frame_size_upper_bound += delta;
  922. if (!ctx->warn) {
  923. avpriv_request_sample(avctx,
  924. "Packet too small: is %i,"
  925. " needs %i (slice: %i). "
  926. "Correct allocation",
  927. pkt_size, delta, max_slice_size);
  928. ctx->warn = 1;
  929. }
  930. ret = av_grow_packet(pkt, delta);
  931. if (ret < 0)
  932. return ret;
  933. pkt_size += delta;
  934. // restore pointers
  935. orig_buf = pkt->data + (orig_buf - start);
  936. buf = pkt->data + (buf - start);
  937. picture_size_pos = pkt->data + (picture_size_pos - start);
  938. slice_sizes = pkt->data + (slice_sizes - start);
  939. slice_hdr = pkt->data + (slice_hdr - start);
  940. tmp = pkt->data + (tmp - start);
  941. }
  942. init_put_bits(&pb, buf, (pkt_size - (buf - orig_buf)) * 8);
  943. ret = encode_slice(avctx, pic, &pb, sizes, x, y, q,
  944. mbs_per_slice);
  945. if (ret < 0)
  946. return ret;
  947. bytestream_put_byte(&slice_hdr, q);
  948. slice_size = slice_hdr_size + sizes[ctx->num_planes - 1];
  949. for (i = 0; i < ctx->num_planes - 1; i++) {
  950. bytestream_put_be16(&slice_hdr, sizes[i]);
  951. slice_size += sizes[i];
  952. }
  953. bytestream_put_be16(&slice_sizes, slice_size);
  954. buf += slice_size - slice_hdr_size;
  955. if (max_slice_size < slice_size)
  956. max_slice_size = slice_size;
  957. }
  958. }
  959. if (ctx->pictures_per_frame == 1)
  960. picture_size = buf - picture_size_pos - 6;
  961. else
  962. picture_size = buf - picture_size_pos + 1;
  963. bytestream_put_be32(&picture_size_pos, picture_size);
  964. }
  965. orig_buf -= 8;
  966. frame_size = buf - orig_buf;
  967. bytestream_put_be32(&orig_buf, frame_size);
  968. pkt->size = frame_size;
  969. pkt->flags |= AV_PKT_FLAG_KEY;
  970. *got_packet = 1;
  971. return 0;
  972. }
  973. static av_cold int encode_close(AVCodecContext *avctx)
  974. {
  975. ProresContext *ctx = avctx->priv_data;
  976. int i;
  977. if (ctx->tdata) {
  978. for (i = 0; i < avctx->thread_count; i++)
  979. av_free(ctx->tdata[i].nodes);
  980. }
  981. av_freep(&ctx->tdata);
  982. av_freep(&ctx->slice_q);
  983. return 0;
  984. }
  985. static void prores_fdct(FDCTDSPContext *fdsp, const uint16_t *src,
  986. ptrdiff_t linesize, int16_t *block)
  987. {
  988. int x, y;
  989. const uint16_t *tsrc = src;
  990. for (y = 0; y < 8; y++) {
  991. for (x = 0; x < 8; x++)
  992. block[y * 8 + x] = tsrc[x];
  993. tsrc += linesize >> 1;
  994. }
  995. fdsp->fdct(block);
  996. }
  997. static av_cold int encode_init(AVCodecContext *avctx)
  998. {
  999. ProresContext *ctx = avctx->priv_data;
  1000. int mps;
  1001. int i, j;
  1002. int min_quant, max_quant;
  1003. int interlaced = !!(avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT);
  1004. avctx->bits_per_raw_sample = 10;
  1005. ctx->fdct = prores_fdct;
  1006. ctx->scantable = interlaced ? ff_prores_interlaced_scan
  1007. : ff_prores_progressive_scan;
  1008. ff_fdctdsp_init(&ctx->fdsp, avctx);
  1009. mps = ctx->mbs_per_slice;
  1010. if (mps & (mps - 1)) {
  1011. av_log(avctx, AV_LOG_ERROR,
  1012. "there should be an integer power of two MBs per slice\n");
  1013. return AVERROR(EINVAL);
  1014. }
  1015. if (av_pix_fmt_desc_get(avctx->pix_fmt)->flags & AV_PIX_FMT_FLAG_ALPHA) {
  1016. if (ctx->alpha_bits & 7) {
  1017. av_log(avctx, AV_LOG_ERROR, "alpha bits should be 0, 8 or 16\n");
  1018. return AVERROR(EINVAL);
  1019. }
  1020. avctx->bits_per_coded_sample = 32;
  1021. } else {
  1022. ctx->alpha_bits = 0;
  1023. }
  1024. ctx->chroma_factor = avctx->pix_fmt == AV_PIX_FMT_YUV422P10
  1025. ? CFACTOR_Y422
  1026. : CFACTOR_Y444;
  1027. ctx->profile_info = prores_profile_info + ctx->profile;
  1028. ctx->num_planes = 3 + !!ctx->alpha_bits;
  1029. ctx->mb_width = FFALIGN(avctx->width, 16) >> 4;
  1030. if (interlaced)
  1031. ctx->mb_height = FFALIGN(avctx->height, 32) >> 5;
  1032. else
  1033. ctx->mb_height = FFALIGN(avctx->height, 16) >> 4;
  1034. ctx->slices_width = ctx->mb_width / mps;
  1035. ctx->slices_width += av_popcount(ctx->mb_width - ctx->slices_width * mps);
  1036. ctx->slices_per_picture = ctx->mb_height * ctx->slices_width;
  1037. ctx->pictures_per_frame = 1 + interlaced;
  1038. if (ctx->quant_sel == -1)
  1039. ctx->quant_mat = prores_quant_matrices[ctx->profile_info->quant];
  1040. else
  1041. ctx->quant_mat = prores_quant_matrices[ctx->quant_sel];
  1042. if (strlen(ctx->vendor) != 4) {
  1043. av_log(avctx, AV_LOG_ERROR, "vendor ID should be 4 bytes\n");
  1044. return AVERROR_INVALIDDATA;
  1045. }
  1046. ctx->force_quant = avctx->global_quality / FF_QP2LAMBDA;
  1047. if (!ctx->force_quant) {
  1048. if (!ctx->bits_per_mb) {
  1049. for (i = 0; i < NUM_MB_LIMITS - 1; i++)
  1050. if (prores_mb_limits[i] >= ctx->mb_width * ctx->mb_height *
  1051. ctx->pictures_per_frame)
  1052. break;
  1053. ctx->bits_per_mb = ctx->profile_info->br_tab[i];
  1054. } else if (ctx->bits_per_mb < 128) {
  1055. av_log(avctx, AV_LOG_ERROR, "too few bits per MB, please set at least 128\n");
  1056. return AVERROR_INVALIDDATA;
  1057. }
  1058. min_quant = ctx->profile_info->min_quant;
  1059. max_quant = ctx->profile_info->max_quant;
  1060. for (i = min_quant; i < MAX_STORED_Q; i++) {
  1061. for (j = 0; j < 64; j++)
  1062. ctx->quants[i][j] = ctx->quant_mat[j] * i;
  1063. }
  1064. ctx->slice_q = av_malloc(ctx->slices_per_picture * sizeof(*ctx->slice_q));
  1065. if (!ctx->slice_q) {
  1066. encode_close(avctx);
  1067. return AVERROR(ENOMEM);
  1068. }
  1069. ctx->tdata = av_mallocz(avctx->thread_count * sizeof(*ctx->tdata));
  1070. if (!ctx->tdata) {
  1071. encode_close(avctx);
  1072. return AVERROR(ENOMEM);
  1073. }
  1074. for (j = 0; j < avctx->thread_count; j++) {
  1075. ctx->tdata[j].nodes = av_malloc((ctx->slices_width + 1)
  1076. * TRELLIS_WIDTH
  1077. * sizeof(*ctx->tdata->nodes));
  1078. if (!ctx->tdata[j].nodes) {
  1079. encode_close(avctx);
  1080. return AVERROR(ENOMEM);
  1081. }
  1082. for (i = min_quant; i < max_quant + 2; i++) {
  1083. ctx->tdata[j].nodes[i].prev_node = -1;
  1084. ctx->tdata[j].nodes[i].bits = 0;
  1085. ctx->tdata[j].nodes[i].score = 0;
  1086. }
  1087. }
  1088. } else {
  1089. int ls = 0;
  1090. if (ctx->force_quant > 64) {
  1091. av_log(avctx, AV_LOG_ERROR, "too large quantiser, maximum is 64\n");
  1092. return AVERROR_INVALIDDATA;
  1093. }
  1094. for (j = 0; j < 64; j++) {
  1095. ctx->quants[0][j] = ctx->quant_mat[j] * ctx->force_quant;
  1096. ls += av_log2((1 << 11) / ctx->quants[0][j]) * 2 + 1;
  1097. }
  1098. ctx->bits_per_mb = ls * 8;
  1099. if (ctx->chroma_factor == CFACTOR_Y444)
  1100. ctx->bits_per_mb += ls * 4;
  1101. }
  1102. ctx->frame_size_upper_bound = ctx->pictures_per_frame *
  1103. ctx->slices_per_picture *
  1104. (2 + 2 * ctx->num_planes +
  1105. (mps * ctx->bits_per_mb) / 8)
  1106. + 200;
  1107. if (ctx->alpha_bits) {
  1108. // The alpha plane is run-coded and might exceed the bit budget.
  1109. ctx->frame_size_upper_bound += ctx->pictures_per_frame *
  1110. ctx->slices_per_picture *
  1111. /* num pixels per slice */ (ctx->mbs_per_slice * 256 *
  1112. /* bits per pixel */ (1 + ctx->alpha_bits + 1) + 7 >> 3);
  1113. }
  1114. avctx->codec_tag = ctx->profile_info->tag;
  1115. av_log(avctx, AV_LOG_DEBUG,
  1116. "profile %d, %d slices, interlacing: %s, %d bits per MB\n",
  1117. ctx->profile, ctx->slices_per_picture * ctx->pictures_per_frame,
  1118. interlaced ? "yes" : "no", ctx->bits_per_mb);
  1119. av_log(avctx, AV_LOG_DEBUG, "frame size upper bound: %d\n",
  1120. ctx->frame_size_upper_bound);
  1121. return 0;
  1122. }
  1123. #define OFFSET(x) offsetof(ProresContext, x)
  1124. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  1125. static const AVOption options[] = {
  1126. { "mbs_per_slice", "macroblocks per slice", OFFSET(mbs_per_slice),
  1127. AV_OPT_TYPE_INT, { .i64 = 8 }, 1, MAX_MBS_PER_SLICE, VE },
  1128. { "profile", NULL, OFFSET(profile), AV_OPT_TYPE_INT,
  1129. { .i64 = PRORES_PROFILE_STANDARD },
  1130. PRORES_PROFILE_PROXY, PRORES_PROFILE_4444, VE, "profile" },
  1131. { "proxy", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PRORES_PROFILE_PROXY },
  1132. 0, 0, VE, "profile" },
  1133. { "lt", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PRORES_PROFILE_LT },
  1134. 0, 0, VE, "profile" },
  1135. { "standard", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PRORES_PROFILE_STANDARD },
  1136. 0, 0, VE, "profile" },
  1137. { "hq", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PRORES_PROFILE_HQ },
  1138. 0, 0, VE, "profile" },
  1139. { "4444", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PRORES_PROFILE_4444 },
  1140. 0, 0, VE, "profile" },
  1141. { "vendor", "vendor ID", OFFSET(vendor),
  1142. AV_OPT_TYPE_STRING, { .str = "Lavc" }, CHAR_MIN, CHAR_MAX, VE },
  1143. { "bits_per_mb", "desired bits per macroblock", OFFSET(bits_per_mb),
  1144. AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 8192, VE },
  1145. { "quant_mat", "quantiser matrix", OFFSET(quant_sel), AV_OPT_TYPE_INT,
  1146. { .i64 = -1 }, -1, QUANT_MAT_DEFAULT, VE, "quant_mat" },
  1147. { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = -1 },
  1148. 0, 0, VE, "quant_mat" },
  1149. { "proxy", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = QUANT_MAT_PROXY },
  1150. 0, 0, VE, "quant_mat" },
  1151. { "lt", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = QUANT_MAT_LT },
  1152. 0, 0, VE, "quant_mat" },
  1153. { "standard", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = QUANT_MAT_STANDARD },
  1154. 0, 0, VE, "quant_mat" },
  1155. { "hq", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = QUANT_MAT_HQ },
  1156. 0, 0, VE, "quant_mat" },
  1157. { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = QUANT_MAT_DEFAULT },
  1158. 0, 0, VE, "quant_mat" },
  1159. { "alpha_bits", "bits for alpha plane", OFFSET(alpha_bits), AV_OPT_TYPE_INT,
  1160. { .i64 = 16 }, 0, 16, VE },
  1161. { NULL }
  1162. };
  1163. static const AVClass proresenc_class = {
  1164. .class_name = "ProRes encoder",
  1165. .item_name = av_default_item_name,
  1166. .option = options,
  1167. .version = LIBAVUTIL_VERSION_INT,
  1168. };
  1169. AVCodec ff_prores_encoder = {
  1170. .name = "prores",
  1171. .long_name = NULL_IF_CONFIG_SMALL("Apple ProRes (iCodec Pro)"),
  1172. .type = AVMEDIA_TYPE_VIDEO,
  1173. .id = AV_CODEC_ID_PRORES,
  1174. .priv_data_size = sizeof(ProresContext),
  1175. .init = encode_init,
  1176. .close = encode_close,
  1177. .encode2 = encode_frame,
  1178. .capabilities = AV_CODEC_CAP_SLICE_THREADS,
  1179. .pix_fmts = (const enum AVPixelFormat[]) {
  1180. AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  1181. AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_NONE
  1182. },
  1183. .priv_class = &proresenc_class,
  1184. };