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.

839 lines
28KB

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