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.

891 lines
29KB

  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 "avcodec.h"
  24. #include "put_bits.h"
  25. #include "bytestream.h"
  26. #include "internal.h"
  27. #include "proresdsp.h"
  28. #include "proresdata.h"
  29. #define CFACTOR_Y422 2
  30. #define CFACTOR_Y444 3
  31. #define MAX_MBS_PER_SLICE 8
  32. #define MAX_PLANES 3 // should be increased to 4 when there's PIX_FMT_YUV444AP10
  33. enum {
  34. PRORES_PROFILE_PROXY = 0,
  35. PRORES_PROFILE_LT,
  36. PRORES_PROFILE_STANDARD,
  37. PRORES_PROFILE_HQ,
  38. };
  39. #define NUM_MB_LIMITS 4
  40. static const int prores_mb_limits[NUM_MB_LIMITS] = {
  41. 1620, // up to 720x576
  42. 2700, // up to 960x720
  43. 6075, // up to 1440x1080
  44. 9216, // up to 2048x1152
  45. };
  46. static const struct prores_profile {
  47. const char *full_name;
  48. uint32_t tag;
  49. int min_quant;
  50. int max_quant;
  51. int br_tab[NUM_MB_LIMITS];
  52. uint8_t quant[64];
  53. } prores_profile_info[4] = {
  54. {
  55. .full_name = "proxy",
  56. .tag = MKTAG('a', 'p', 'c', 'o'),
  57. .min_quant = 4,
  58. .max_quant = 8,
  59. .br_tab = { 300, 242, 220, 194 },
  60. .quant = {
  61. 4, 7, 9, 11, 13, 14, 15, 63,
  62. 7, 7, 11, 12, 14, 15, 63, 63,
  63. 9, 11, 13, 14, 15, 63, 63, 63,
  64. 11, 11, 13, 14, 63, 63, 63, 63,
  65. 11, 13, 14, 63, 63, 63, 63, 63,
  66. 13, 14, 63, 63, 63, 63, 63, 63,
  67. 13, 63, 63, 63, 63, 63, 63, 63,
  68. 63, 63, 63, 63, 63, 63, 63, 63,
  69. },
  70. },
  71. {
  72. .full_name = "LT",
  73. .tag = MKTAG('a', 'p', 'c', 's'),
  74. .min_quant = 1,
  75. .max_quant = 9,
  76. .br_tab = { 720, 560, 490, 440 },
  77. .quant = {
  78. 4, 5, 6, 7, 9, 11, 13, 15,
  79. 5, 5, 7, 8, 11, 13, 15, 17,
  80. 6, 7, 9, 11, 13, 15, 15, 17,
  81. 7, 7, 9, 11, 13, 15, 17, 19,
  82. 7, 9, 11, 13, 14, 16, 19, 23,
  83. 9, 11, 13, 14, 16, 19, 23, 29,
  84. 9, 11, 13, 15, 17, 21, 28, 35,
  85. 11, 13, 16, 17, 21, 28, 35, 41,
  86. },
  87. },
  88. {
  89. .full_name = "standard",
  90. .tag = MKTAG('a', 'p', 'c', 'n'),
  91. .min_quant = 1,
  92. .max_quant = 6,
  93. .br_tab = { 1050, 808, 710, 632 },
  94. .quant = {
  95. 4, 4, 5, 5, 6, 7, 7, 9,
  96. 4, 4, 5, 6, 7, 7, 9, 9,
  97. 5, 5, 6, 7, 7, 9, 9, 10,
  98. 5, 5, 6, 7, 7, 9, 9, 10,
  99. 5, 6, 7, 7, 8, 9, 10, 12,
  100. 6, 7, 7, 8, 9, 10, 12, 15,
  101. 6, 7, 7, 9, 10, 11, 14, 17,
  102. 7, 7, 9, 10, 11, 14, 17, 21,
  103. },
  104. },
  105. {
  106. .full_name = "high quality",
  107. .tag = MKTAG('a', 'p', 'c', 'h'),
  108. .min_quant = 1,
  109. .max_quant = 6,
  110. .br_tab = { 1566, 1216, 1070, 950 },
  111. .quant = {
  112. 4, 4, 4, 4, 4, 4, 4, 4,
  113. 4, 4, 4, 4, 4, 4, 4, 4,
  114. 4, 4, 4, 4, 4, 4, 4, 4,
  115. 4, 4, 4, 4, 4, 4, 4, 5,
  116. 4, 4, 4, 4, 4, 4, 5, 5,
  117. 4, 4, 4, 4, 4, 5, 5, 6,
  118. 4, 4, 4, 4, 5, 5, 6, 7,
  119. 4, 4, 4, 4, 5, 6, 7, 7,
  120. },
  121. }
  122. // for 4444 profile bitrate numbers are { 2350, 1828, 1600, 1425 }
  123. };
  124. #define TRELLIS_WIDTH 16
  125. #define SCORE_LIMIT INT_MAX / 2
  126. struct TrellisNode {
  127. int prev_node;
  128. int quant;
  129. int bits;
  130. int score;
  131. };
  132. #define MAX_STORED_Q 16
  133. typedef struct ProresContext {
  134. AVClass *class;
  135. DECLARE_ALIGNED(16, DCTELEM, blocks)[MAX_PLANES][64 * 4 * MAX_MBS_PER_SLICE];
  136. DECLARE_ALIGNED(16, uint16_t, emu_buf)[16*16];
  137. int16_t quants[MAX_STORED_Q][64];
  138. int16_t custom_q[64];
  139. ProresDSPContext dsp;
  140. ScanTable scantable;
  141. int mb_width, mb_height;
  142. int mbs_per_slice;
  143. int num_chroma_blocks, chroma_factor;
  144. int slices_width;
  145. int num_slices;
  146. int num_planes;
  147. int bits_per_mb;
  148. int frame_size;
  149. int profile;
  150. const struct prores_profile *profile_info;
  151. struct TrellisNode *nodes;
  152. int *slice_q;
  153. } ProresContext;
  154. static void get_slice_data(ProresContext *ctx, const uint16_t *src,
  155. int linesize, int x, int y, int w, int h,
  156. DCTELEM *blocks,
  157. int mbs_per_slice, int blocks_per_mb)
  158. {
  159. const uint16_t *esrc;
  160. const int mb_width = 4 * blocks_per_mb;
  161. int elinesize;
  162. int i, j, k;
  163. for (i = 0; i < mbs_per_slice; i++, src += mb_width) {
  164. if (x >= w) {
  165. memset(blocks, 0, 64 * (mbs_per_slice - i) * blocks_per_mb
  166. * sizeof(*blocks));
  167. return;
  168. }
  169. if (x + mb_width <= w && y + 16 <= h) {
  170. esrc = src;
  171. elinesize = linesize;
  172. } else {
  173. int bw, bh, pix;
  174. const int estride = 16 / sizeof(*ctx->emu_buf);
  175. esrc = ctx->emu_buf;
  176. elinesize = 16;
  177. bw = FFMIN(w - x, mb_width);
  178. bh = FFMIN(h - y, 16);
  179. for (j = 0; j < bh; j++) {
  180. memcpy(ctx->emu_buf + j * estride, src + j * linesize,
  181. bw * sizeof(*src));
  182. pix = ctx->emu_buf[j * estride + bw - 1];
  183. for (k = bw; k < mb_width; k++)
  184. ctx->emu_buf[j * estride + k] = pix;
  185. }
  186. for (; j < 16; j++)
  187. memcpy(ctx->emu_buf + j * estride,
  188. ctx->emu_buf + (bh - 1) * estride,
  189. mb_width * sizeof(*ctx->emu_buf));
  190. }
  191. ctx->dsp.fdct(esrc, elinesize, blocks);
  192. blocks += 64;
  193. if (blocks_per_mb > 2) {
  194. ctx->dsp.fdct(src + 8, linesize, blocks);
  195. blocks += 64;
  196. }
  197. ctx->dsp.fdct(src + linesize * 4, linesize, blocks);
  198. blocks += 64;
  199. if (blocks_per_mb > 2) {
  200. ctx->dsp.fdct(src + linesize * 4 + 8, linesize, blocks);
  201. blocks += 64;
  202. }
  203. x += mb_width;
  204. }
  205. }
  206. /**
  207. * Write an unsigned rice/exp golomb codeword.
  208. */
  209. static inline void encode_vlc_codeword(PutBitContext *pb, unsigned codebook, int val)
  210. {
  211. unsigned int rice_order, exp_order, switch_bits, switch_val;
  212. int exponent;
  213. /* number of prefix bits to switch between Rice and expGolomb */
  214. switch_bits = (codebook & 3) + 1;
  215. rice_order = codebook >> 5; /* rice code order */
  216. exp_order = (codebook >> 2) & 7; /* exp golomb code order */
  217. switch_val = switch_bits << rice_order;
  218. if (val >= switch_val) {
  219. val -= switch_val - (1 << exp_order);
  220. exponent = av_log2(val);
  221. put_bits(pb, exponent - exp_order + switch_bits, 0);
  222. put_bits(pb, 1, 1);
  223. put_bits(pb, exponent, val);
  224. } else {
  225. exponent = val >> rice_order;
  226. if (exponent)
  227. put_bits(pb, exponent, 0);
  228. put_bits(pb, 1, 1);
  229. if (rice_order)
  230. put_sbits(pb, rice_order, val);
  231. }
  232. }
  233. #define GET_SIGN(x) ((x) >> 31)
  234. #define MAKE_CODE(x) (((x) << 1) ^ GET_SIGN(x))
  235. static void encode_dcs(PutBitContext *pb, DCTELEM *blocks,
  236. int blocks_per_slice, int scale)
  237. {
  238. int i;
  239. int codebook = 3, code, dc, prev_dc, delta, sign, new_sign;
  240. prev_dc = (blocks[0] - 0x4000) / scale;
  241. encode_vlc_codeword(pb, FIRST_DC_CB, MAKE_CODE(prev_dc));
  242. sign = 0;
  243. codebook = 3;
  244. blocks += 64;
  245. for (i = 1; i < blocks_per_slice; i++, blocks += 64) {
  246. dc = (blocks[0] - 0x4000) / scale;
  247. delta = dc - prev_dc;
  248. new_sign = GET_SIGN(delta);
  249. delta = (delta ^ sign) - sign;
  250. code = MAKE_CODE(delta);
  251. encode_vlc_codeword(pb, ff_prores_dc_codebook[codebook], code);
  252. codebook = (code + (code & 1)) >> 1;
  253. codebook = FFMIN(codebook, 3);
  254. sign = new_sign;
  255. prev_dc = dc;
  256. }
  257. }
  258. static void encode_acs(PutBitContext *pb, DCTELEM *blocks,
  259. int blocks_per_slice,
  260. int plane_size_factor,
  261. const uint8_t *scan, const int16_t *qmat)
  262. {
  263. int idx, i;
  264. int run, level, run_cb, lev_cb;
  265. int max_coeffs, abs_level;
  266. max_coeffs = blocks_per_slice << 6;
  267. run_cb = ff_prores_run_to_cb_index[4];
  268. lev_cb = ff_prores_lev_to_cb_index[2];
  269. run = 0;
  270. for (i = 1; i < 64; i++) {
  271. for (idx = scan[i]; idx < max_coeffs; idx += 64) {
  272. level = blocks[idx] / qmat[scan[i]];
  273. if (level) {
  274. abs_level = FFABS(level);
  275. encode_vlc_codeword(pb, ff_prores_ac_codebook[run_cb], run);
  276. encode_vlc_codeword(pb, ff_prores_ac_codebook[lev_cb],
  277. abs_level - 1);
  278. put_sbits(pb, 1, GET_SIGN(level));
  279. run_cb = ff_prores_run_to_cb_index[FFMIN(run, 15)];
  280. lev_cb = ff_prores_lev_to_cb_index[FFMIN(abs_level, 9)];
  281. run = 0;
  282. } else {
  283. run++;
  284. }
  285. }
  286. }
  287. }
  288. static int encode_slice_plane(ProresContext *ctx, PutBitContext *pb,
  289. const uint16_t *src, int linesize,
  290. int mbs_per_slice, DCTELEM *blocks,
  291. int blocks_per_mb, int plane_size_factor,
  292. const int16_t *qmat)
  293. {
  294. int blocks_per_slice, saved_pos;
  295. saved_pos = put_bits_count(pb);
  296. blocks_per_slice = mbs_per_slice * blocks_per_mb;
  297. encode_dcs(pb, blocks, blocks_per_slice, qmat[0]);
  298. encode_acs(pb, blocks, blocks_per_slice, plane_size_factor,
  299. ctx->scantable.permutated, qmat);
  300. flush_put_bits(pb);
  301. return (put_bits_count(pb) - saved_pos) >> 3;
  302. }
  303. static int encode_slice(AVCodecContext *avctx, const AVFrame *pic,
  304. PutBitContext *pb,
  305. int sizes[4], int x, int y, int quant,
  306. int mbs_per_slice)
  307. {
  308. ProresContext *ctx = avctx->priv_data;
  309. int i, xp, yp;
  310. int total_size = 0;
  311. const uint16_t *src;
  312. int slice_width_factor = av_log2(mbs_per_slice);
  313. int num_cblocks, pwidth;
  314. int plane_factor, is_chroma;
  315. uint16_t *qmat;
  316. if (quant < MAX_STORED_Q) {
  317. qmat = ctx->quants[quant];
  318. } else {
  319. qmat = ctx->custom_q;
  320. for (i = 0; i < 64; i++)
  321. qmat[i] = ctx->profile_info->quant[i] * quant;
  322. }
  323. for (i = 0; i < ctx->num_planes; i++) {
  324. is_chroma = (i == 1 || i == 2);
  325. plane_factor = slice_width_factor + 2;
  326. if (is_chroma)
  327. plane_factor += ctx->chroma_factor - 3;
  328. if (!is_chroma || ctx->chroma_factor == CFACTOR_Y444) {
  329. xp = x << 4;
  330. yp = y << 4;
  331. num_cblocks = 4;
  332. pwidth = avctx->width;
  333. } else {
  334. xp = x << 3;
  335. yp = y << 4;
  336. num_cblocks = 2;
  337. pwidth = avctx->width >> 1;
  338. }
  339. src = (const uint16_t*)(pic->data[i] + yp * pic->linesize[i]) + xp;
  340. get_slice_data(ctx, src, pic->linesize[i], xp, yp,
  341. pwidth, avctx->height, ctx->blocks[0],
  342. mbs_per_slice, num_cblocks);
  343. sizes[i] = encode_slice_plane(ctx, pb, src, pic->linesize[i],
  344. mbs_per_slice, ctx->blocks[0],
  345. num_cblocks, plane_factor,
  346. qmat);
  347. total_size += sizes[i];
  348. }
  349. return total_size;
  350. }
  351. static inline int estimate_vlc(unsigned codebook, int val)
  352. {
  353. unsigned int rice_order, exp_order, switch_bits, switch_val;
  354. int exponent;
  355. /* number of prefix bits to switch between Rice and expGolomb */
  356. switch_bits = (codebook & 3) + 1;
  357. rice_order = codebook >> 5; /* rice code order */
  358. exp_order = (codebook >> 2) & 7; /* exp golomb code order */
  359. switch_val = switch_bits << rice_order;
  360. if (val >= switch_val) {
  361. val -= switch_val - (1 << exp_order);
  362. exponent = av_log2(val);
  363. return exponent * 2 - exp_order + switch_bits + 1;
  364. } else {
  365. return (val >> rice_order) + rice_order + 1;
  366. }
  367. }
  368. static int estimate_dcs(int *error, DCTELEM *blocks, int blocks_per_slice,
  369. int scale)
  370. {
  371. int i;
  372. int codebook = 3, code, dc, prev_dc, delta, sign, new_sign;
  373. int bits;
  374. prev_dc = (blocks[0] - 0x4000) / scale;
  375. bits = estimate_vlc(FIRST_DC_CB, MAKE_CODE(prev_dc));
  376. sign = 0;
  377. codebook = 3;
  378. blocks += 64;
  379. *error += FFABS(blocks[0] - 0x4000) % scale;
  380. for (i = 1; i < blocks_per_slice; i++, blocks += 64) {
  381. dc = (blocks[0] - 0x4000) / scale;
  382. *error += FFABS(blocks[0] - 0x4000) % scale;
  383. delta = dc - prev_dc;
  384. new_sign = GET_SIGN(delta);
  385. delta = (delta ^ sign) - sign;
  386. code = MAKE_CODE(delta);
  387. bits += estimate_vlc(ff_prores_dc_codebook[codebook], code);
  388. codebook = (code + (code & 1)) >> 1;
  389. codebook = FFMIN(codebook, 3);
  390. sign = new_sign;
  391. prev_dc = dc;
  392. }
  393. return bits;
  394. }
  395. static int estimate_acs(int *error, DCTELEM *blocks, int blocks_per_slice,
  396. int plane_size_factor,
  397. const uint8_t *scan, const int16_t *qmat)
  398. {
  399. int idx, i;
  400. int run, level, run_cb, lev_cb;
  401. int max_coeffs, abs_level;
  402. int bits = 0;
  403. max_coeffs = blocks_per_slice << 6;
  404. run_cb = ff_prores_run_to_cb_index[4];
  405. lev_cb = ff_prores_lev_to_cb_index[2];
  406. run = 0;
  407. for (i = 1; i < 64; i++) {
  408. for (idx = scan[i]; idx < max_coeffs; idx += 64) {
  409. level = blocks[idx] / qmat[scan[i]];
  410. *error += FFABS(blocks[idx]) % qmat[scan[i]];
  411. if (level) {
  412. abs_level = FFABS(level);
  413. bits += estimate_vlc(ff_prores_ac_codebook[run_cb], run);
  414. bits += estimate_vlc(ff_prores_ac_codebook[lev_cb],
  415. abs_level - 1) + 1;
  416. run_cb = ff_prores_run_to_cb_index[FFMIN(run, 15)];
  417. lev_cb = ff_prores_lev_to_cb_index[FFMIN(abs_level, 9)];
  418. run = 0;
  419. } else {
  420. run++;
  421. }
  422. }
  423. }
  424. return bits;
  425. }
  426. static int estimate_slice_plane(ProresContext *ctx, int *error, int plane,
  427. const uint16_t *src, int linesize,
  428. int mbs_per_slice,
  429. int blocks_per_mb, int plane_size_factor,
  430. const int16_t *qmat)
  431. {
  432. int blocks_per_slice;
  433. int bits;
  434. blocks_per_slice = mbs_per_slice * blocks_per_mb;
  435. bits = estimate_dcs(error, ctx->blocks[plane], blocks_per_slice, qmat[0]);
  436. bits += estimate_acs(error, ctx->blocks[plane], blocks_per_slice,
  437. plane_size_factor, ctx->scantable.permutated, qmat);
  438. return FFALIGN(bits, 8);
  439. }
  440. static int find_slice_quant(AVCodecContext *avctx, const AVFrame *pic,
  441. int trellis_node, int x, int y, int mbs_per_slice)
  442. {
  443. ProresContext *ctx = avctx->priv_data;
  444. int i, q, pq, xp, yp;
  445. const uint16_t *src;
  446. int slice_width_factor = av_log2(mbs_per_slice);
  447. int num_cblocks[MAX_PLANES], pwidth;
  448. int plane_factor[MAX_PLANES], is_chroma[MAX_PLANES];
  449. const int min_quant = ctx->profile_info->min_quant;
  450. const int max_quant = ctx->profile_info->max_quant;
  451. int error, bits, bits_limit;
  452. int mbs, prev, cur, new_score;
  453. int slice_bits[TRELLIS_WIDTH], slice_score[TRELLIS_WIDTH];
  454. int overquant;
  455. uint16_t *qmat;
  456. mbs = x + mbs_per_slice;
  457. for (i = 0; i < ctx->num_planes; i++) {
  458. is_chroma[i] = (i == 1 || i == 2);
  459. plane_factor[i] = slice_width_factor + 2;
  460. if (is_chroma[i])
  461. plane_factor[i] += ctx->chroma_factor - 3;
  462. if (!is_chroma[i] || ctx->chroma_factor == CFACTOR_Y444) {
  463. xp = x << 4;
  464. yp = y << 4;
  465. num_cblocks[i] = 4;
  466. pwidth = avctx->width;
  467. } else {
  468. xp = x << 3;
  469. yp = y << 4;
  470. num_cblocks[i] = 2;
  471. pwidth = avctx->width >> 1;
  472. }
  473. src = (const uint16_t*)(pic->data[i] + yp * pic->linesize[i]) + xp;
  474. get_slice_data(ctx, src, pic->linesize[i], xp, yp,
  475. pwidth, avctx->height, ctx->blocks[i],
  476. mbs_per_slice, num_cblocks[i]);
  477. }
  478. for (q = min_quant; q < max_quant + 2; q++) {
  479. ctx->nodes[trellis_node + q].prev_node = -1;
  480. ctx->nodes[trellis_node + q].quant = q;
  481. }
  482. // todo: maybe perform coarser quantising to fit into frame size when needed
  483. for (q = min_quant; q <= max_quant; q++) {
  484. bits = 0;
  485. error = 0;
  486. for (i = 0; i < ctx->num_planes; i++) {
  487. bits += estimate_slice_plane(ctx, &error, i,
  488. src, pic->linesize[i],
  489. mbs_per_slice,
  490. num_cblocks[i], plane_factor[i],
  491. ctx->quants[q]);
  492. }
  493. if (bits > 65000 * 8) {
  494. error = SCORE_LIMIT;
  495. break;
  496. }
  497. slice_bits[q] = bits;
  498. slice_score[q] = error;
  499. }
  500. if (slice_bits[max_quant] <= ctx->bits_per_mb * mbs_per_slice) {
  501. slice_bits[max_quant + 1] = slice_bits[max_quant];
  502. slice_score[max_quant + 1] = slice_score[max_quant] + 1;
  503. overquant = max_quant;
  504. } else {
  505. for (q = max_quant + 1; q < 128; q++) {
  506. bits = 0;
  507. error = 0;
  508. if (q < MAX_STORED_Q) {
  509. qmat = ctx->quants[q];
  510. } else {
  511. qmat = ctx->custom_q;
  512. for (i = 0; i < 64; i++)
  513. qmat[i] = ctx->profile_info->quant[i] * q;
  514. }
  515. for (i = 0; i < ctx->num_planes; i++) {
  516. bits += estimate_slice_plane(ctx, &error, i,
  517. src, pic->linesize[i],
  518. mbs_per_slice,
  519. num_cblocks[i], plane_factor[i],
  520. qmat);
  521. }
  522. if (bits <= ctx->bits_per_mb * mbs_per_slice)
  523. break;
  524. }
  525. slice_bits[max_quant + 1] = bits;
  526. slice_score[max_quant + 1] = error;
  527. overquant = q;
  528. }
  529. ctx->nodes[trellis_node + max_quant + 1].quant = overquant;
  530. bits_limit = mbs * ctx->bits_per_mb;
  531. for (pq = min_quant; pq < max_quant + 2; pq++) {
  532. prev = trellis_node - TRELLIS_WIDTH + pq;
  533. for (q = min_quant; q < max_quant + 2; q++) {
  534. cur = trellis_node + q;
  535. bits = ctx->nodes[prev].bits + slice_bits[q];
  536. error = slice_score[q];
  537. if (bits > bits_limit)
  538. error = SCORE_LIMIT;
  539. if (ctx->nodes[prev].score < SCORE_LIMIT && error < SCORE_LIMIT)
  540. new_score = ctx->nodes[prev].score + error;
  541. else
  542. new_score = SCORE_LIMIT;
  543. if (ctx->nodes[cur].prev_node == -1 ||
  544. ctx->nodes[cur].score >= new_score) {
  545. ctx->nodes[cur].bits = bits;
  546. ctx->nodes[cur].score = new_score;
  547. ctx->nodes[cur].prev_node = prev;
  548. }
  549. }
  550. }
  551. error = ctx->nodes[trellis_node + min_quant].score;
  552. pq = trellis_node + min_quant;
  553. for (q = min_quant + 1; q < max_quant + 2; q++) {
  554. if (ctx->nodes[trellis_node + q].score <= error) {
  555. error = ctx->nodes[trellis_node + q].score;
  556. pq = trellis_node + q;
  557. }
  558. }
  559. return pq;
  560. }
  561. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  562. const AVFrame *pic, int *got_packet)
  563. {
  564. ProresContext *ctx = avctx->priv_data;
  565. uint8_t *orig_buf, *buf, *slice_hdr, *slice_sizes, *tmp;
  566. uint8_t *picture_size_pos;
  567. PutBitContext pb;
  568. int x, y, i, mb, q = 0;
  569. int sizes[4] = { 0 };
  570. int slice_hdr_size = 2 + 2 * (ctx->num_planes - 1);
  571. int frame_size, picture_size, slice_size;
  572. int mbs_per_slice = ctx->mbs_per_slice;
  573. int pkt_size, ret;
  574. *avctx->coded_frame = *pic;
  575. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  576. avctx->coded_frame->key_frame = 1;
  577. pkt_size = ctx->frame_size + FF_MIN_BUFFER_SIZE;
  578. if ((ret = ff_alloc_packet(pkt, pkt_size)) < 0) {
  579. av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
  580. return ret;
  581. }
  582. orig_buf = pkt->data;
  583. // frame atom
  584. orig_buf += 4; // frame size
  585. bytestream_put_be32 (&orig_buf, FRAME_ID); // frame container ID
  586. buf = orig_buf;
  587. // frame header
  588. tmp = buf;
  589. buf += 2; // frame header size will be stored here
  590. bytestream_put_be16 (&buf, 0); // version 1
  591. bytestream_put_buffer(&buf, "Lavc", 4); // creator
  592. bytestream_put_be16 (&buf, avctx->width);
  593. bytestream_put_be16 (&buf, avctx->height);
  594. bytestream_put_byte (&buf, ctx->chroma_factor << 6); // frame flags
  595. bytestream_put_byte (&buf, 0); // reserved
  596. bytestream_put_byte (&buf, 0); // primaries
  597. bytestream_put_byte (&buf, 0); // transfer function
  598. bytestream_put_byte (&buf, 6); // colour matrix - ITU-R BT.601-4
  599. bytestream_put_byte (&buf, 0x40); // source format and alpha information
  600. bytestream_put_byte (&buf, 0); // reserved
  601. bytestream_put_byte (&buf, 0x03); // matrix flags - both matrices are present
  602. // luma quantisation matrix
  603. for (i = 0; i < 64; i++)
  604. bytestream_put_byte(&buf, ctx->profile_info->quant[i]);
  605. // chroma quantisation matrix
  606. for (i = 0; i < 64; i++)
  607. bytestream_put_byte(&buf, ctx->profile_info->quant[i]);
  608. bytestream_put_be16 (&tmp, buf - orig_buf); // write back frame header size
  609. // picture header
  610. picture_size_pos = buf + 1;
  611. bytestream_put_byte (&buf, 0x40); // picture header size (in bits)
  612. buf += 4; // picture data size will be stored here
  613. bytestream_put_be16 (&buf, ctx->num_slices); // total number of slices
  614. bytestream_put_byte (&buf, av_log2(ctx->mbs_per_slice) << 4); // slice width and height in MBs
  615. // seek table - will be filled during slice encoding
  616. slice_sizes = buf;
  617. buf += ctx->num_slices * 2;
  618. // slices
  619. for (y = 0; y < ctx->mb_height; y++) {
  620. mbs_per_slice = ctx->mbs_per_slice;
  621. for (x = mb = 0; x < ctx->mb_width; x += mbs_per_slice, mb++) {
  622. while (ctx->mb_width - x < mbs_per_slice)
  623. mbs_per_slice >>= 1;
  624. q = find_slice_quant(avctx, pic, (mb + 1) * TRELLIS_WIDTH, x, y,
  625. mbs_per_slice);
  626. }
  627. for (x = ctx->slices_width - 1; x >= 0; x--) {
  628. ctx->slice_q[x] = ctx->nodes[q].quant;
  629. q = ctx->nodes[q].prev_node;
  630. }
  631. mbs_per_slice = ctx->mbs_per_slice;
  632. for (x = mb = 0; x < ctx->mb_width; x += mbs_per_slice, mb++) {
  633. q = ctx->slice_q[mb];
  634. while (ctx->mb_width - x < mbs_per_slice)
  635. mbs_per_slice >>= 1;
  636. bytestream_put_byte(&buf, slice_hdr_size << 3);
  637. slice_hdr = buf;
  638. buf += slice_hdr_size - 1;
  639. init_put_bits(&pb, buf, (pkt_size - (buf - orig_buf)) * 8);
  640. encode_slice(avctx, pic, &pb, sizes, x, y, q, mbs_per_slice);
  641. bytestream_put_byte(&slice_hdr, q);
  642. slice_size = slice_hdr_size + sizes[ctx->num_planes - 1];
  643. for (i = 0; i < ctx->num_planes - 1; i++) {
  644. bytestream_put_be16(&slice_hdr, sizes[i]);
  645. slice_size += sizes[i];
  646. }
  647. bytestream_put_be16(&slice_sizes, slice_size);
  648. buf += slice_size - slice_hdr_size;
  649. }
  650. }
  651. orig_buf -= 8;
  652. frame_size = buf - orig_buf;
  653. picture_size = buf - picture_size_pos - 6;
  654. bytestream_put_be32(&orig_buf, frame_size);
  655. bytestream_put_be32(&picture_size_pos, picture_size);
  656. pkt->size = frame_size;
  657. pkt->flags |= AV_PKT_FLAG_KEY;
  658. *got_packet = 1;
  659. return 0;
  660. }
  661. static av_cold int encode_close(AVCodecContext *avctx)
  662. {
  663. ProresContext *ctx = avctx->priv_data;
  664. if (avctx->coded_frame->data[0])
  665. avctx->release_buffer(avctx, avctx->coded_frame);
  666. av_freep(&avctx->coded_frame);
  667. av_freep(&ctx->nodes);
  668. av_freep(&ctx->slice_q);
  669. return 0;
  670. }
  671. static av_cold int encode_init(AVCodecContext *avctx)
  672. {
  673. ProresContext *ctx = avctx->priv_data;
  674. int mps;
  675. int i, j;
  676. int min_quant, max_quant;
  677. avctx->bits_per_raw_sample = 10;
  678. avctx->coded_frame = avcodec_alloc_frame();
  679. if (!avctx->coded_frame)
  680. return AVERROR(ENOMEM);
  681. ff_proresdsp_init(&ctx->dsp);
  682. ff_init_scantable(ctx->dsp.dct_permutation, &ctx->scantable,
  683. ff_prores_progressive_scan);
  684. mps = ctx->mbs_per_slice;
  685. if (mps & (mps - 1)) {
  686. av_log(avctx, AV_LOG_ERROR,
  687. "there should be an integer power of two MBs per slice\n");
  688. return AVERROR(EINVAL);
  689. }
  690. ctx->chroma_factor = avctx->pix_fmt == PIX_FMT_YUV422P10
  691. ? CFACTOR_Y422
  692. : CFACTOR_Y444;
  693. ctx->profile_info = prores_profile_info + ctx->profile;
  694. ctx->num_planes = 3;
  695. ctx->mb_width = FFALIGN(avctx->width, 16) >> 4;
  696. ctx->mb_height = FFALIGN(avctx->height, 16) >> 4;
  697. ctx->slices_width = ctx->mb_width / mps;
  698. ctx->slices_width += av_popcount(ctx->mb_width - ctx->slices_width * mps);
  699. ctx->num_slices = ctx->mb_height * ctx->slices_width;
  700. for (i = 0; i < NUM_MB_LIMITS - 1; i++)
  701. if (prores_mb_limits[i] >= ctx->mb_width * ctx->mb_height)
  702. break;
  703. ctx->bits_per_mb = ctx->profile_info->br_tab[i];
  704. ctx->frame_size = ctx->num_slices * (2 + 2 * ctx->num_planes
  705. + (2 * mps * ctx->bits_per_mb) / 8)
  706. + 200;
  707. min_quant = ctx->profile_info->min_quant;
  708. max_quant = ctx->profile_info->max_quant;
  709. for (i = min_quant; i < MAX_STORED_Q; i++) {
  710. for (j = 0; j < 64; j++)
  711. ctx->quants[i][j] = ctx->profile_info->quant[j] * i;
  712. }
  713. avctx->codec_tag = ctx->profile_info->tag;
  714. av_log(avctx, AV_LOG_DEBUG, "profile %d, %d slices, %d bits per MB\n",
  715. ctx->profile, ctx->num_slices, ctx->bits_per_mb);
  716. av_log(avctx, AV_LOG_DEBUG, "estimated frame size %d\n",
  717. ctx->frame_size);
  718. ctx->nodes = av_malloc((ctx->slices_width + 1) * TRELLIS_WIDTH
  719. * sizeof(*ctx->nodes));
  720. if (!ctx->nodes) {
  721. encode_close(avctx);
  722. return AVERROR(ENOMEM);
  723. }
  724. for (i = min_quant; i < max_quant + 2; i++) {
  725. ctx->nodes[i].prev_node = -1;
  726. ctx->nodes[i].bits = 0;
  727. ctx->nodes[i].score = 0;
  728. }
  729. ctx->slice_q = av_malloc(ctx->slices_width * sizeof(*ctx->slice_q));
  730. if (!ctx->slice_q) {
  731. encode_close(avctx);
  732. return AVERROR(ENOMEM);
  733. }
  734. return 0;
  735. }
  736. #define OFFSET(x) offsetof(ProresContext, x)
  737. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  738. static const AVOption options[] = {
  739. { "mbs_per_slice", "macroblocks per slice", OFFSET(mbs_per_slice),
  740. AV_OPT_TYPE_INT, { 8 }, 1, MAX_MBS_PER_SLICE, VE },
  741. { "profile", NULL, OFFSET(profile), AV_OPT_TYPE_INT,
  742. { PRORES_PROFILE_STANDARD },
  743. PRORES_PROFILE_PROXY, PRORES_PROFILE_HQ, VE, "profile" },
  744. { "proxy", NULL, 0, AV_OPT_TYPE_CONST, { PRORES_PROFILE_PROXY },
  745. 0, 0, VE, "profile" },
  746. { "lt", NULL, 0, AV_OPT_TYPE_CONST, { PRORES_PROFILE_LT },
  747. 0, 0, VE, "profile" },
  748. { "standard", NULL, 0, AV_OPT_TYPE_CONST, { PRORES_PROFILE_STANDARD },
  749. 0, 0, VE, "profile" },
  750. { "hq", NULL, 0, AV_OPT_TYPE_CONST, { PRORES_PROFILE_HQ },
  751. 0, 0, VE, "profile" },
  752. { NULL }
  753. };
  754. static const AVClass proresenc_class = {
  755. .class_name = "ProRes encoder",
  756. .item_name = av_default_item_name,
  757. .option = options,
  758. .version = LIBAVUTIL_VERSION_INT,
  759. };
  760. AVCodec ff_prores_encoder = {
  761. .name = "prores",
  762. .type = AVMEDIA_TYPE_VIDEO,
  763. .id = CODEC_ID_PRORES,
  764. .priv_data_size = sizeof(ProresContext),
  765. .init = encode_init,
  766. .close = encode_close,
  767. .encode2 = encode_frame,
  768. .long_name = NULL_IF_CONFIG_SMALL("Apple ProRes (iCodec Pro)"),
  769. .pix_fmts = (const enum PixelFormat[]) {
  770. PIX_FMT_YUV422P10, PIX_FMT_YUV444P10, PIX_FMT_NONE
  771. },
  772. .priv_class = &proresenc_class,
  773. };