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.

976 lines
36KB

  1. /*
  2. * Apple ProRes encoder
  3. *
  4. * Copyright (c) 2011 Anatoliy Wasserman
  5. * Copyright (c) 2012 Konstantin Shishkov
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file
  25. * Apple ProRes encoder (Anatoliy Wasserman version)
  26. * Known FOURCCs: 'ap4h' (444), 'apch' (HQ), 'apcn' (422), 'apcs' (LT), 'acpo' (Proxy)
  27. */
  28. #include "libavutil/mem_internal.h"
  29. #include "libavutil/opt.h"
  30. #include "avcodec.h"
  31. #include "dct.h"
  32. #include "internal.h"
  33. #include "profiles.h"
  34. #include "proresdata.h"
  35. #include "put_bits.h"
  36. #include "bytestream.h"
  37. #include "fdctdsp.h"
  38. #define DEFAULT_SLICE_MB_WIDTH 8
  39. static const AVProfile profiles[] = {
  40. { FF_PROFILE_PRORES_PROXY, "apco"},
  41. { FF_PROFILE_PRORES_LT, "apcs"},
  42. { FF_PROFILE_PRORES_STANDARD, "apcn"},
  43. { FF_PROFILE_PRORES_HQ, "apch"},
  44. { FF_PROFILE_PRORES_4444, "ap4h"},
  45. { FF_PROFILE_PRORES_XQ, "ap4x"},
  46. { FF_PROFILE_UNKNOWN }
  47. };
  48. static const int qp_start_table[] = { 8, 3, 2, 1, 1, 1};
  49. static const int qp_end_table[] = { 13, 9, 6, 6, 5, 4};
  50. static const int bitrate_table[] = { 1000, 2100, 3500, 5400, 7000, 10000};
  51. static const int valid_primaries[] = { AVCOL_PRI_RESERVED0, AVCOL_PRI_BT709, AVCOL_PRI_UNSPECIFIED, AVCOL_PRI_BT470BG,
  52. AVCOL_PRI_SMPTE170M, AVCOL_PRI_BT2020, AVCOL_PRI_SMPTE431, AVCOL_PRI_SMPTE432, INT_MAX };
  53. static const int valid_trc[] = { AVCOL_TRC_RESERVED0, AVCOL_TRC_BT709, AVCOL_TRC_UNSPECIFIED, AVCOL_TRC_SMPTE2084,
  54. AVCOL_TRC_ARIB_STD_B67, INT_MAX };
  55. static const int valid_colorspace[] = { AVCOL_SPC_BT709, AVCOL_SPC_UNSPECIFIED, AVCOL_SPC_SMPTE170M,
  56. AVCOL_SPC_BT2020_NCL, INT_MAX };
  57. static const uint8_t QMAT_LUMA[6][64] = {
  58. {
  59. 4, 7, 9, 11, 13, 14, 15, 63,
  60. 7, 7, 11, 12, 14, 15, 63, 63,
  61. 9, 11, 13, 14, 15, 63, 63, 63,
  62. 11, 11, 13, 14, 63, 63, 63, 63,
  63. 11, 13, 14, 63, 63, 63, 63, 63,
  64. 13, 14, 63, 63, 63, 63, 63, 63,
  65. 13, 63, 63, 63, 63, 63, 63, 63,
  66. 63, 63, 63, 63, 63, 63, 63, 63
  67. }, {
  68. 4, 5, 6, 7, 9, 11, 13, 15,
  69. 5, 5, 7, 8, 11, 13, 15, 17,
  70. 6, 7, 9, 11, 13, 15, 15, 17,
  71. 7, 7, 9, 11, 13, 15, 17, 19,
  72. 7, 9, 11, 13, 14, 16, 19, 23,
  73. 9, 11, 13, 14, 16, 19, 23, 29,
  74. 9, 11, 13, 15, 17, 21, 28, 35,
  75. 11, 13, 16, 17, 21, 28, 35, 41
  76. }, {
  77. 4, 4, 5, 5, 6, 7, 7, 9,
  78. 4, 4, 5, 6, 7, 7, 9, 9,
  79. 5, 5, 6, 7, 7, 9, 9, 10,
  80. 5, 5, 6, 7, 7, 9, 9, 10,
  81. 5, 6, 7, 7, 8, 9, 10, 12,
  82. 6, 7, 7, 8, 9, 10, 12, 15,
  83. 6, 7, 7, 9, 10, 11, 14, 17,
  84. 7, 7, 9, 10, 11, 14, 17, 21
  85. }, {
  86. 4, 4, 4, 4, 4, 4, 4, 4,
  87. 4, 4, 4, 4, 4, 4, 4, 4,
  88. 4, 4, 4, 4, 4, 4, 4, 4,
  89. 4, 4, 4, 4, 4, 4, 4, 5,
  90. 4, 4, 4, 4, 4, 4, 5, 5,
  91. 4, 4, 4, 4, 4, 5, 5, 6,
  92. 4, 4, 4, 4, 5, 5, 6, 7,
  93. 4, 4, 4, 4, 5, 6, 7, 7
  94. }, { /* 444 */
  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. 4, 4, 4, 4, 4, 4, 4, 5,
  99. 4, 4, 4, 4, 4, 4, 5, 5,
  100. 4, 4, 4, 4, 4, 5, 5, 6,
  101. 4, 4, 4, 4, 5, 5, 6, 7,
  102. 4, 4, 4, 4, 5, 6, 7, 7
  103. }, { /* 444 XQ */
  104. 2, 2, 2, 2, 2, 2, 2, 2,
  105. 2, 2, 2, 2, 2, 2, 2, 2,
  106. 2, 2, 2, 2, 2, 2, 2, 2,
  107. 2, 2, 2, 2, 2, 2, 2, 3,
  108. 2, 2, 2, 2, 2, 2, 3, 3,
  109. 2, 2, 2, 2, 2, 3, 3, 3,
  110. 2, 2, 2, 2, 3, 3, 3, 4,
  111. 2, 2, 2, 2, 3, 3, 4, 4,
  112. }
  113. };
  114. static const uint8_t QMAT_CHROMA[6][64] = {
  115. {
  116. 4, 7, 9, 11, 13, 14, 63, 63,
  117. 7, 7, 11, 12, 14, 63, 63, 63,
  118. 9, 11, 13, 14, 63, 63, 63, 63,
  119. 11, 11, 13, 14, 63, 63, 63, 63,
  120. 11, 13, 14, 63, 63, 63, 63, 63,
  121. 13, 14, 63, 63, 63, 63, 63, 63,
  122. 13, 63, 63, 63, 63, 63, 63, 63,
  123. 63, 63, 63, 63, 63, 63, 63, 63
  124. }, {
  125. 4, 5, 6, 7, 9, 11, 13, 15,
  126. 5, 5, 7, 8, 11, 13, 15, 17,
  127. 6, 7, 9, 11, 13, 15, 15, 17,
  128. 7, 7, 9, 11, 13, 15, 17, 19,
  129. 7, 9, 11, 13, 14, 16, 19, 23,
  130. 9, 11, 13, 14, 16, 19, 23, 29,
  131. 9, 11, 13, 15, 17, 21, 28, 35,
  132. 11, 13, 16, 17, 21, 28, 35, 41
  133. }, {
  134. 4, 4, 5, 5, 6, 7, 7, 9,
  135. 4, 4, 5, 6, 7, 7, 9, 9,
  136. 5, 5, 6, 7, 7, 9, 9, 10,
  137. 5, 5, 6, 7, 7, 9, 9, 10,
  138. 5, 6, 7, 7, 8, 9, 10, 12,
  139. 6, 7, 7, 8, 9, 10, 12, 15,
  140. 6, 7, 7, 9, 10, 11, 14, 17,
  141. 7, 7, 9, 10, 11, 14, 17, 21
  142. }, {
  143. 4, 4, 4, 4, 4, 4, 4, 4,
  144. 4, 4, 4, 4, 4, 4, 4, 4,
  145. 4, 4, 4, 4, 4, 4, 4, 4,
  146. 4, 4, 4, 4, 4, 4, 4, 5,
  147. 4, 4, 4, 4, 4, 4, 5, 5,
  148. 4, 4, 4, 4, 4, 5, 5, 6,
  149. 4, 4, 4, 4, 5, 5, 6, 7,
  150. 4, 4, 4, 4, 5, 6, 7, 7
  151. }, { /* 444 */
  152. 4, 4, 4, 4, 4, 4, 4, 4,
  153. 4, 4, 4, 4, 4, 4, 4, 4,
  154. 4, 4, 4, 4, 4, 4, 4, 4,
  155. 4, 4, 4, 4, 4, 4, 4, 5,
  156. 4, 4, 4, 4, 4, 4, 5, 5,
  157. 4, 4, 4, 4, 4, 5, 5, 6,
  158. 4, 4, 4, 4, 5, 5, 6, 7,
  159. 4, 4, 4, 4, 5, 6, 7, 7
  160. }, { /* 444 xq */
  161. 4, 4, 4, 4, 4, 4, 4, 4,
  162. 4, 4, 4, 4, 4, 4, 4, 4,
  163. 4, 4, 4, 4, 4, 4, 4, 4,
  164. 4, 4, 4, 4, 4, 4, 4, 5,
  165. 4, 4, 4, 4, 4, 4, 5, 5,
  166. 4, 4, 4, 4, 4, 5, 5, 6,
  167. 4, 4, 4, 4, 5, 5, 6, 7,
  168. 4, 4, 4, 4, 5, 6, 7, 7
  169. }
  170. };
  171. typedef struct {
  172. AVClass *class;
  173. FDCTDSPContext fdsp;
  174. uint8_t* fill_y;
  175. uint8_t* fill_u;
  176. uint8_t* fill_v;
  177. uint8_t* fill_a;
  178. int qmat_luma[16][64];
  179. int qmat_chroma[16][64];
  180. const uint8_t *scantable;
  181. int is_422;
  182. int need_alpha;
  183. int is_interlaced;
  184. char *vendor;
  185. } ProresContext;
  186. static void encode_codeword(PutBitContext *pb, int val, int codebook)
  187. {
  188. unsigned int rice_order, exp_order, switch_bits, first_exp, exp, zeros;
  189. /* number of bits to switch between rice and exp golomb */
  190. switch_bits = codebook & 3;
  191. rice_order = codebook >> 5;
  192. exp_order = (codebook >> 2) & 7;
  193. first_exp = ((switch_bits + 1) << rice_order);
  194. if (val >= first_exp) { /* exp golomb */
  195. val -= first_exp;
  196. val += (1 << exp_order);
  197. exp = av_log2(val);
  198. zeros = exp - exp_order + switch_bits + 1;
  199. put_bits(pb, zeros, 0);
  200. put_bits(pb, exp + 1, val);
  201. } else if (rice_order) {
  202. put_bits(pb, (val >> rice_order), 0);
  203. put_bits(pb, 1, 1);
  204. put_sbits(pb, rice_order, val);
  205. } else {
  206. put_bits(pb, val, 0);
  207. put_bits(pb, 1, 1);
  208. }
  209. }
  210. #define QSCALE(qmat,ind,val) ((val) / ((qmat)[ind]))
  211. #define TO_GOLOMB(val) (((val) * 2) ^ ((val) >> 31))
  212. #define DIFF_SIGN(val, sign) (((val) >> 31) ^ (sign))
  213. #define IS_NEGATIVE(val) ((((val) >> 31) ^ -1) + 1)
  214. #define TO_GOLOMB2(val,sign) ((val)==0 ? 0 : ((val) << 1) + (sign))
  215. static av_always_inline int get_level(int val)
  216. {
  217. int sign = (val >> 31);
  218. return (val ^ sign) - sign;
  219. }
  220. #define FIRST_DC_CB 0xB8
  221. static const uint8_t dc_codebook[7] = { 0x04, 0x28, 0x28, 0x4D, 0x4D, 0x70, 0x70};
  222. static void encode_dc_coeffs(PutBitContext *pb, int16_t *in,
  223. int blocks_per_slice, int *qmat)
  224. {
  225. int prev_dc, code;
  226. int i, sign, idx;
  227. int new_dc, delta, diff_sign, new_code;
  228. prev_dc = QSCALE(qmat, 0, in[0] - 16384);
  229. code = TO_GOLOMB(prev_dc);
  230. encode_codeword(pb, code, FIRST_DC_CB);
  231. code = 5; sign = 0; idx = 64;
  232. for (i = 1; i < blocks_per_slice; i++, idx += 64) {
  233. new_dc = QSCALE(qmat, 0, in[idx] - 16384);
  234. delta = new_dc - prev_dc;
  235. diff_sign = DIFF_SIGN(delta, sign);
  236. new_code = TO_GOLOMB2(get_level(delta), diff_sign);
  237. encode_codeword(pb, new_code, dc_codebook[FFMIN(code, 6)]);
  238. code = new_code;
  239. sign = delta >> 31;
  240. prev_dc = new_dc;
  241. }
  242. }
  243. static const uint8_t run_to_cb[16] = { 0x06, 0x06, 0x05, 0x05, 0x04, 0x29,
  244. 0x29, 0x29, 0x29, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x4C };
  245. static const uint8_t lev_to_cb[10] = { 0x04, 0x0A, 0x05, 0x06, 0x04, 0x28,
  246. 0x28, 0x28, 0x28, 0x4C };
  247. static void encode_ac_coeffs(PutBitContext *pb,
  248. int16_t *in, int blocks_per_slice, int *qmat, const uint8_t ff_prores_scan[64])
  249. {
  250. int prev_run = 4;
  251. int prev_level = 2;
  252. int run = 0, level, code, i, j;
  253. for (i = 1; i < 64; i++) {
  254. int indp = ff_prores_scan[i];
  255. for (j = 0; j < blocks_per_slice; j++) {
  256. int val = QSCALE(qmat, indp, in[(j << 6) + indp]);
  257. if (val) {
  258. encode_codeword(pb, run, run_to_cb[FFMIN(prev_run, 15)]);
  259. prev_run = run;
  260. run = 0;
  261. level = get_level(val);
  262. code = level - 1;
  263. encode_codeword(pb, code, lev_to_cb[FFMIN(prev_level, 9)]);
  264. prev_level = level;
  265. put_bits(pb, 1, IS_NEGATIVE(val));
  266. } else {
  267. ++run;
  268. }
  269. }
  270. }
  271. }
  272. static void get(uint8_t *pixels, int stride, int16_t* block)
  273. {
  274. int i;
  275. for (i = 0; i < 8; i++) {
  276. AV_WN64(block, AV_RN64(pixels));
  277. AV_WN64(block+4, AV_RN64(pixels+8));
  278. pixels += stride;
  279. block += 8;
  280. }
  281. }
  282. static void fdct_get(FDCTDSPContext *fdsp, uint8_t *pixels, int stride, int16_t* block)
  283. {
  284. get(pixels, stride, block);
  285. fdsp->fdct(block);
  286. }
  287. static void calc_plane_dct(FDCTDSPContext *fdsp, uint8_t *src, int16_t * blocks, int src_stride, int mb_count, int chroma, int is_422)
  288. {
  289. int16_t *block;
  290. int i;
  291. block = blocks;
  292. if (!chroma) { /* Luma plane */
  293. for (i = 0; i < mb_count; i++) {
  294. fdct_get(fdsp, src, src_stride, block + (0 << 6));
  295. fdct_get(fdsp, src + 16, src_stride, block + (1 << 6));
  296. fdct_get(fdsp, src + 8 * src_stride, src_stride, block + (2 << 6));
  297. fdct_get(fdsp, src + 16 + 8 * src_stride, src_stride, block + (3 << 6));
  298. block += 256;
  299. src += 32;
  300. }
  301. } else if (chroma && is_422){ /* chroma plane 422 */
  302. for (i = 0; i < mb_count; i++) {
  303. fdct_get(fdsp, src, src_stride, block + (0 << 6));
  304. fdct_get(fdsp, src + 8 * src_stride, src_stride, block + (1 << 6));
  305. block += (256 >> 1);
  306. src += (32 >> 1);
  307. }
  308. } else { /* chroma plane 444 */
  309. for (i = 0; i < mb_count; i++) {
  310. fdct_get(fdsp, src, src_stride, block + (0 << 6));
  311. fdct_get(fdsp, src + 8 * src_stride, src_stride, block + (1 << 6));
  312. fdct_get(fdsp, src + 16, src_stride, block + (2 << 6));
  313. fdct_get(fdsp, src + 16 + 8 * src_stride, src_stride, block + (3 << 6));
  314. block += 256;
  315. src += 32;
  316. }
  317. }
  318. }
  319. static int encode_slice_plane(int16_t *blocks, int mb_count, uint8_t *buf, unsigned buf_size, int *qmat, int sub_sample_chroma,
  320. const uint8_t ff_prores_scan[64])
  321. {
  322. int blocks_per_slice;
  323. PutBitContext pb;
  324. blocks_per_slice = mb_count << (2 - sub_sample_chroma);
  325. init_put_bits(&pb, buf, buf_size);
  326. encode_dc_coeffs(&pb, blocks, blocks_per_slice, qmat);
  327. encode_ac_coeffs(&pb, blocks, blocks_per_slice, qmat, ff_prores_scan);
  328. flush_put_bits(&pb);
  329. return put_bits_ptr(&pb) - pb.buf;
  330. }
  331. static av_always_inline unsigned encode_slice_data(AVCodecContext *avctx,
  332. int16_t * blocks_y, int16_t * blocks_u, int16_t * blocks_v,
  333. unsigned mb_count, uint8_t *buf, unsigned data_size,
  334. unsigned* y_data_size, unsigned* u_data_size, unsigned* v_data_size,
  335. int qp)
  336. {
  337. ProresContext* ctx = avctx->priv_data;
  338. *y_data_size = encode_slice_plane(blocks_y, mb_count,
  339. buf, data_size, ctx->qmat_luma[qp - 1], 0, ctx->scantable);
  340. if (!(avctx->flags & AV_CODEC_FLAG_GRAY)) {
  341. *u_data_size = encode_slice_plane(blocks_u, mb_count, buf + *y_data_size, data_size - *y_data_size,
  342. ctx->qmat_chroma[qp - 1], ctx->is_422, ctx->scantable);
  343. *v_data_size = encode_slice_plane(blocks_v, mb_count, buf + *y_data_size + *u_data_size,
  344. data_size - *y_data_size - *u_data_size,
  345. ctx->qmat_chroma[qp - 1], ctx->is_422, ctx->scantable);
  346. }
  347. return *y_data_size + *u_data_size + *v_data_size;
  348. }
  349. static void put_alpha_diff(PutBitContext *pb, int cur, int prev)
  350. {
  351. const int abits = 16;
  352. const int dbits = 7;
  353. const int dsize = 1 << dbits - 1;
  354. int diff = cur - prev;
  355. diff = av_mod_uintp2(diff, abits);
  356. if (diff >= (1 << abits) - dsize)
  357. diff -= 1 << abits;
  358. if (diff < -dsize || diff > dsize || !diff) {
  359. put_bits(pb, 1, 1);
  360. put_bits(pb, abits, diff);
  361. } else {
  362. put_bits(pb, 1, 0);
  363. put_bits(pb, dbits - 1, FFABS(diff) - 1);
  364. put_bits(pb, 1, diff < 0);
  365. }
  366. }
  367. static inline void put_alpha_run(PutBitContext *pb, int run)
  368. {
  369. if (run) {
  370. put_bits(pb, 1, 0);
  371. if (run < 0x10)
  372. put_bits(pb, 4, run);
  373. else
  374. put_bits(pb, 15, run);
  375. } else {
  376. put_bits(pb, 1, 1);
  377. }
  378. }
  379. static av_always_inline int encode_alpha_slice_data(AVCodecContext *avctx, int8_t * src_a,
  380. unsigned mb_count, uint8_t *buf, unsigned data_size, unsigned* a_data_size)
  381. {
  382. const int abits = 16;
  383. const int mask = (1 << abits) - 1;
  384. const int num_coeffs = mb_count * 256;
  385. int prev = mask, cur;
  386. int idx = 0;
  387. int run = 0;
  388. int16_t * blocks = (int16_t *)src_a;
  389. PutBitContext pb;
  390. init_put_bits(&pb, buf, data_size);
  391. cur = blocks[idx++];
  392. put_alpha_diff(&pb, cur, prev);
  393. prev = cur;
  394. do {
  395. cur = blocks[idx++];
  396. if (cur != prev) {
  397. put_alpha_run (&pb, run);
  398. put_alpha_diff(&pb, cur, prev);
  399. prev = cur;
  400. run = 0;
  401. } else {
  402. run++;
  403. }
  404. } while (idx < num_coeffs);
  405. if (run)
  406. put_alpha_run(&pb, run);
  407. flush_put_bits(&pb);
  408. *a_data_size = put_bits_count(&pb) >> 3;
  409. if (put_bits_left(&pb) < 0) {
  410. av_log(avctx, AV_LOG_ERROR,
  411. "Underestimated required buffer size.\n");
  412. return AVERROR_BUG;
  413. } else {
  414. return 0;
  415. }
  416. }
  417. static inline void subimage_with_fill_template(uint16_t *src, unsigned x, unsigned y,
  418. unsigned stride, unsigned width, unsigned height, uint16_t *dst,
  419. unsigned dst_width, unsigned dst_height, int is_alpha_plane,
  420. int is_interlaced, int is_top_field)
  421. {
  422. int box_width = FFMIN(width - x, dst_width);
  423. int i, j, src_stride, box_height;
  424. uint16_t last_pix, *last_line;
  425. if (!is_interlaced) {
  426. src_stride = stride >> 1;
  427. src += y * src_stride + x;
  428. box_height = FFMIN(height - y, dst_height);
  429. } else {
  430. src_stride = stride; /* 2 lines stride */
  431. src += y * src_stride + x;
  432. box_height = FFMIN(height/2 - y, dst_height);
  433. if (!is_top_field)
  434. src += stride >> 1;
  435. }
  436. for (i = 0; i < box_height; ++i) {
  437. for (j = 0; j < box_width; ++j) {
  438. if (!is_alpha_plane) {
  439. dst[j] = src[j];
  440. } else {
  441. dst[j] = src[j] << 6; /* alpha 10b to 16b */
  442. }
  443. }
  444. if (!is_alpha_plane) {
  445. last_pix = dst[j - 1];
  446. } else {
  447. last_pix = dst[j - 1] << 6; /* alpha 10b to 16b */
  448. }
  449. for (; j < dst_width; j++)
  450. dst[j] = last_pix;
  451. src += src_stride;
  452. dst += dst_width;
  453. }
  454. last_line = dst - dst_width;
  455. for (; i < dst_height; i++) {
  456. for (j = 0; j < dst_width; ++j) {
  457. dst[j] = last_line[j];
  458. }
  459. dst += dst_width;
  460. }
  461. }
  462. static void subimage_with_fill(uint16_t *src, unsigned x, unsigned y,
  463. unsigned stride, unsigned width, unsigned height, uint16_t *dst,
  464. unsigned dst_width, unsigned dst_height, int is_interlaced, int is_top_field)
  465. {
  466. subimage_with_fill_template(src, x, y, stride, width, height, dst, dst_width, dst_height, 0, is_interlaced, is_top_field);
  467. }
  468. /* reorganize alpha data and convert 10b -> 16b */
  469. static void subimage_alpha_with_fill(uint16_t *src, unsigned x, unsigned y,
  470. unsigned stride, unsigned width, unsigned height, uint16_t *dst,
  471. unsigned dst_width, unsigned dst_height, int is_interlaced, int is_top_field)
  472. {
  473. subimage_with_fill_template(src, x, y, stride, width, height, dst, dst_width, dst_height, 1, is_interlaced, is_top_field);
  474. }
  475. static int encode_slice(AVCodecContext *avctx, const AVFrame *pic, int mb_x,
  476. int mb_y, unsigned mb_count, uint8_t *buf, unsigned data_size,
  477. int unsafe, int *qp, int is_interlaced, int is_top_field)
  478. {
  479. int luma_stride, chroma_stride, alpha_stride = 0;
  480. ProresContext* ctx = avctx->priv_data;
  481. int hdr_size = 6 + (ctx->need_alpha * 2); /* v data size is write when there is alpha */
  482. int ret = 0, slice_size;
  483. uint8_t *dest_y, *dest_u, *dest_v;
  484. unsigned y_data_size = 0, u_data_size = 0, v_data_size = 0, a_data_size = 0;
  485. FDCTDSPContext *fdsp = &ctx->fdsp;
  486. int tgt_bits = (mb_count * bitrate_table[avctx->profile]) >> 2;
  487. int low_bytes = (tgt_bits - (tgt_bits >> 3)) >> 3; // 12% bitrate fluctuation
  488. int high_bytes = (tgt_bits + (tgt_bits >> 3)) >> 3;
  489. LOCAL_ALIGNED(16, int16_t, blocks_y, [DEFAULT_SLICE_MB_WIDTH << 8]);
  490. LOCAL_ALIGNED(16, int16_t, blocks_u, [DEFAULT_SLICE_MB_WIDTH << 8]);
  491. LOCAL_ALIGNED(16, int16_t, blocks_v, [DEFAULT_SLICE_MB_WIDTH << 8]);
  492. luma_stride = pic->linesize[0];
  493. chroma_stride = pic->linesize[1];
  494. if (ctx->need_alpha)
  495. alpha_stride = pic->linesize[3];
  496. if (!is_interlaced) {
  497. dest_y = pic->data[0] + (mb_y << 4) * luma_stride + (mb_x << 5);
  498. dest_u = pic->data[1] + (mb_y << 4) * chroma_stride + (mb_x << (5 - ctx->is_422));
  499. dest_v = pic->data[2] + (mb_y << 4) * chroma_stride + (mb_x << (5 - ctx->is_422));
  500. } else {
  501. dest_y = pic->data[0] + (mb_y << 4) * luma_stride * 2 + (mb_x << 5);
  502. dest_u = pic->data[1] + (mb_y << 4) * chroma_stride * 2 + (mb_x << (5 - ctx->is_422));
  503. dest_v = pic->data[2] + (mb_y << 4) * chroma_stride * 2 + (mb_x << (5 - ctx->is_422));
  504. if (!is_top_field){ /* bottom field, offset dest */
  505. dest_y += luma_stride;
  506. dest_u += chroma_stride;
  507. dest_v += chroma_stride;
  508. }
  509. }
  510. if (unsafe) {
  511. subimage_with_fill((uint16_t *) pic->data[0], mb_x << 4, mb_y << 4,
  512. luma_stride, avctx->width, avctx->height,
  513. (uint16_t *) ctx->fill_y, mb_count << 4, 16, is_interlaced, is_top_field);
  514. subimage_with_fill((uint16_t *) pic->data[1], mb_x << (4 - ctx->is_422), mb_y << 4,
  515. chroma_stride, avctx->width >> ctx->is_422, avctx->height,
  516. (uint16_t *) ctx->fill_u, mb_count << (4 - ctx->is_422), 16, is_interlaced, is_top_field);
  517. subimage_with_fill((uint16_t *) pic->data[2], mb_x << (4 - ctx->is_422), mb_y << 4,
  518. chroma_stride, avctx->width >> ctx->is_422, avctx->height,
  519. (uint16_t *) ctx->fill_v, mb_count << (4 - ctx->is_422), 16, is_interlaced, is_top_field);
  520. /* no need for interlaced special case, data already reorganized in subimage_with_fill */
  521. calc_plane_dct(fdsp, ctx->fill_y, blocks_y, mb_count << 5, mb_count, 0, 0);
  522. calc_plane_dct(fdsp, ctx->fill_u, blocks_u, mb_count << (5 - ctx->is_422), mb_count, 1, ctx->is_422);
  523. calc_plane_dct(fdsp, ctx->fill_v, blocks_v, mb_count << (5 - ctx->is_422), mb_count, 1, ctx->is_422);
  524. slice_size = encode_slice_data(avctx, blocks_y, blocks_u, blocks_v,
  525. mb_count, buf + hdr_size, data_size - hdr_size,
  526. &y_data_size, &u_data_size, &v_data_size,
  527. *qp);
  528. } else {
  529. if (!is_interlaced) {
  530. calc_plane_dct(fdsp, dest_y, blocks_y, luma_stride, mb_count, 0, 0);
  531. calc_plane_dct(fdsp, dest_u, blocks_u, chroma_stride, mb_count, 1, ctx->is_422);
  532. calc_plane_dct(fdsp, dest_v, blocks_v, chroma_stride, mb_count, 1, ctx->is_422);
  533. } else {
  534. calc_plane_dct(fdsp, dest_y, blocks_y, luma_stride * 2, mb_count, 0, 0);
  535. calc_plane_dct(fdsp, dest_u, blocks_u, chroma_stride * 2, mb_count, 1, ctx->is_422);
  536. calc_plane_dct(fdsp, dest_v, blocks_v, chroma_stride * 2, mb_count, 1, ctx->is_422);
  537. }
  538. slice_size = encode_slice_data(avctx, blocks_y, blocks_u, blocks_v,
  539. mb_count, buf + hdr_size, data_size - hdr_size,
  540. &y_data_size, &u_data_size, &v_data_size,
  541. *qp);
  542. if (slice_size > high_bytes && *qp < qp_end_table[avctx->profile]) {
  543. do {
  544. *qp += 1;
  545. slice_size = encode_slice_data(avctx, blocks_y, blocks_u, blocks_v,
  546. mb_count, buf + hdr_size, data_size - hdr_size,
  547. &y_data_size, &u_data_size, &v_data_size,
  548. *qp);
  549. } while (slice_size > high_bytes && *qp < qp_end_table[avctx->profile]);
  550. } else if (slice_size < low_bytes && *qp
  551. > qp_start_table[avctx->profile]) {
  552. do {
  553. *qp -= 1;
  554. slice_size = encode_slice_data(avctx, blocks_y, blocks_u, blocks_v,
  555. mb_count, buf + hdr_size, data_size - hdr_size,
  556. &y_data_size, &u_data_size, &v_data_size,
  557. *qp);
  558. } while (slice_size < low_bytes && *qp > qp_start_table[avctx->profile]);
  559. }
  560. }
  561. buf[0] = hdr_size << 3;
  562. buf[1] = *qp;
  563. AV_WB16(buf + 2, y_data_size);
  564. AV_WB16(buf + 4, u_data_size);
  565. if (ctx->need_alpha) {
  566. AV_WB16(buf + 6, v_data_size); /* write v data size only if there is alpha */
  567. subimage_alpha_with_fill((uint16_t *) pic->data[3], mb_x << 4, mb_y << 4,
  568. alpha_stride, avctx->width, avctx->height,
  569. (uint16_t *) ctx->fill_a, mb_count << 4, 16, is_interlaced, is_top_field);
  570. ret = encode_alpha_slice_data(avctx, ctx->fill_a, mb_count,
  571. buf + hdr_size + slice_size,
  572. data_size - hdr_size - slice_size, &a_data_size);
  573. }
  574. if (ret != 0) {
  575. return ret;
  576. }
  577. return hdr_size + y_data_size + u_data_size + v_data_size + a_data_size;
  578. }
  579. static int prores_encode_picture(AVCodecContext *avctx, const AVFrame *pic,
  580. uint8_t *buf, const int buf_size, const int picture_index, const int is_top_field)
  581. {
  582. ProresContext *ctx = avctx->priv_data;
  583. int mb_width = (avctx->width + 15) >> 4;
  584. int hdr_size, sl_size, i;
  585. int mb_y, sl_data_size, qp, mb_height, picture_height, unsafe_mb_height_limit;
  586. int unsafe_bot, unsafe_right;
  587. uint8_t *sl_data, *sl_data_sizes;
  588. int slice_per_line = 0, rem = mb_width;
  589. if (!ctx->is_interlaced) { /* progressive encoding */
  590. mb_height = (avctx->height + 15) >> 4;
  591. unsafe_mb_height_limit = mb_height;
  592. } else {
  593. if (is_top_field) {
  594. picture_height = (avctx->height + 1) / 2;
  595. } else {
  596. picture_height = avctx->height / 2;
  597. }
  598. mb_height = (picture_height + 15) >> 4;
  599. unsafe_mb_height_limit = mb_height;
  600. }
  601. for (i = av_log2(DEFAULT_SLICE_MB_WIDTH); i >= 0; --i) {
  602. slice_per_line += rem >> i;
  603. rem &= (1 << i) - 1;
  604. }
  605. qp = qp_start_table[avctx->profile];
  606. hdr_size = 8; sl_data_size = buf_size - hdr_size;
  607. sl_data_sizes = buf + hdr_size;
  608. sl_data = sl_data_sizes + (slice_per_line * mb_height * 2);
  609. for (mb_y = 0; mb_y < mb_height; mb_y++) {
  610. int mb_x = 0;
  611. int slice_mb_count = DEFAULT_SLICE_MB_WIDTH;
  612. while (mb_x < mb_width) {
  613. while (mb_width - mb_x < slice_mb_count)
  614. slice_mb_count >>= 1;
  615. unsafe_bot = (avctx->height & 0xf) && (mb_y == unsafe_mb_height_limit - 1);
  616. unsafe_right = (avctx->width & 0xf) && (mb_x + slice_mb_count == mb_width);
  617. sl_size = encode_slice(avctx, pic, mb_x, mb_y, slice_mb_count,
  618. sl_data, sl_data_size, unsafe_bot || unsafe_right, &qp, ctx->is_interlaced, is_top_field);
  619. if (sl_size < 0){
  620. return sl_size;
  621. }
  622. bytestream_put_be16(&sl_data_sizes, sl_size);
  623. sl_data += sl_size;
  624. sl_data_size -= sl_size;
  625. mb_x += slice_mb_count;
  626. }
  627. }
  628. buf[0] = hdr_size << 3;
  629. AV_WB32(buf + 1, sl_data - buf);
  630. AV_WB16(buf + 5, slice_per_line * mb_height); /* picture size */
  631. buf[7] = av_log2(DEFAULT_SLICE_MB_WIDTH) << 4; /* number of slices */
  632. return sl_data - buf;
  633. }
  634. static int prores_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  635. const AVFrame *pict, int *got_packet)
  636. {
  637. ProresContext *ctx = avctx->priv_data;
  638. int header_size = 148;
  639. uint8_t *buf;
  640. int compress_frame_size, pic_size, ret, is_top_field_first = 0;
  641. uint8_t frame_flags;
  642. int frame_size = FFALIGN(avctx->width, 16) * FFALIGN(avctx->height, 16)*16 + 500 + AV_INPUT_BUFFER_MIN_SIZE; //FIXME choose tighter limit
  643. if ((ret = ff_alloc_packet2(avctx, pkt, frame_size + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
  644. return ret;
  645. buf = pkt->data;
  646. compress_frame_size = 8 + header_size;
  647. bytestream_put_be32(&buf, compress_frame_size);/* frame size will be update after picture(s) encoding */
  648. bytestream_put_buffer(&buf, "icpf", 4);
  649. bytestream_put_be16(&buf, header_size);
  650. bytestream_put_be16(&buf, 0); /* version */
  651. bytestream_put_buffer(&buf, ctx->vendor, 4);
  652. bytestream_put_be16(&buf, avctx->width);
  653. bytestream_put_be16(&buf, avctx->height);
  654. frame_flags = 0x82; /* 422 not interlaced */
  655. if (avctx->profile >= FF_PROFILE_PRORES_4444) /* 4444 or 4444 Xq */
  656. frame_flags |= 0x40; /* 444 chroma */
  657. if (ctx->is_interlaced) {
  658. if (pict->top_field_first || !pict->interlaced_frame) { /* tff frame or progressive frame interpret as tff */
  659. av_log(avctx, AV_LOG_DEBUG, "use interlaced encoding, top field first\n");
  660. frame_flags |= 0x04; /* interlaced tff */
  661. is_top_field_first = 1;
  662. } else {
  663. av_log(avctx, AV_LOG_DEBUG, "use interlaced encoding, bottom field first\n");
  664. frame_flags |= 0x08; /* interlaced bff */
  665. }
  666. } else {
  667. av_log(avctx, AV_LOG_DEBUG, "use progressive encoding\n");
  668. }
  669. *buf++ = frame_flags;
  670. *buf++ = 0; /* reserved */
  671. /* only write color properties, if valid value. set to unspecified otherwise */
  672. *buf++ = ff_int_from_list_or_default(avctx, "frame color primaries", pict->color_primaries, valid_primaries, 0);
  673. *buf++ = ff_int_from_list_or_default(avctx, "frame color trc", pict->color_trc, valid_trc, 0);
  674. *buf++ = ff_int_from_list_or_default(avctx, "frame colorspace", pict->colorspace, valid_colorspace, 0);
  675. if (avctx->profile >= FF_PROFILE_PRORES_4444) {
  676. if (avctx->pix_fmt == AV_PIX_FMT_YUV444P10) {
  677. *buf++ = 0xA0;/* src b64a and no alpha */
  678. } else {
  679. *buf++ = 0xA2;/* src b64a and 16b alpha */
  680. }
  681. } else {
  682. *buf++ = 32;/* src v210 and no alpha */
  683. }
  684. *buf++ = 0; /* reserved */
  685. *buf++ = 3; /* luma and chroma matrix present */
  686. bytestream_put_buffer(&buf, QMAT_LUMA[avctx->profile], 64);
  687. bytestream_put_buffer(&buf, QMAT_CHROMA[avctx->profile], 64);
  688. pic_size = prores_encode_picture(avctx, pict, buf,
  689. pkt->size - compress_frame_size, 0, is_top_field_first);/* encode progressive or first field */
  690. if (pic_size < 0) {
  691. return pic_size;
  692. }
  693. compress_frame_size += pic_size;
  694. if (ctx->is_interlaced) { /* encode second field */
  695. pic_size = prores_encode_picture(avctx, pict, pkt->data + compress_frame_size,
  696. pkt->size - compress_frame_size, 1, !is_top_field_first);
  697. if (pic_size < 0) {
  698. return pic_size;
  699. }
  700. compress_frame_size += pic_size;
  701. }
  702. AV_WB32(pkt->data, compress_frame_size);/* update frame size */
  703. pkt->flags |= AV_PKT_FLAG_KEY;
  704. pkt->size = compress_frame_size;
  705. *got_packet = 1;
  706. return 0;
  707. }
  708. static void scale_mat(const uint8_t* src, int* dst, int scale)
  709. {
  710. int i;
  711. for (i = 0; i < 64; i++)
  712. dst[i] = src[i] * scale;
  713. }
  714. static av_cold int prores_encode_init(AVCodecContext *avctx)
  715. {
  716. int i;
  717. ProresContext* ctx = avctx->priv_data;
  718. avctx->bits_per_raw_sample = 10;
  719. ctx->need_alpha = 0;
  720. ctx->is_interlaced = !!(avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT);
  721. if (ctx->is_interlaced) {
  722. ctx->scantable = ff_prores_interlaced_scan;
  723. } else {
  724. ctx->scantable = ff_prores_progressive_scan;
  725. }
  726. if (avctx->width & 0x1) {
  727. av_log(avctx, AV_LOG_ERROR,
  728. "frame width needs to be multiple of 2\n");
  729. return AVERROR(EINVAL);
  730. }
  731. if (avctx->width > 65534 || avctx->height > 65535) {
  732. av_log(avctx, AV_LOG_ERROR,
  733. "The maximum dimensions are 65534x65535\n");
  734. return AVERROR(EINVAL);
  735. }
  736. if (strlen(ctx->vendor) != 4) {
  737. av_log(avctx, AV_LOG_ERROR, "vendor ID should be 4 bytes\n");
  738. return AVERROR(EINVAL);
  739. }
  740. if (avctx->profile == FF_PROFILE_UNKNOWN) {
  741. if (avctx->pix_fmt == AV_PIX_FMT_YUV422P10) {
  742. avctx->profile = FF_PROFILE_PRORES_STANDARD;
  743. av_log(avctx, AV_LOG_INFO,
  744. "encoding with ProRes standard (apcn) profile\n");
  745. } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P10) {
  746. avctx->profile = FF_PROFILE_PRORES_4444;
  747. av_log(avctx, AV_LOG_INFO,
  748. "encoding with ProRes 4444 (ap4h) profile\n");
  749. } else if (avctx->pix_fmt == AV_PIX_FMT_YUVA444P10) {
  750. avctx->profile = FF_PROFILE_PRORES_4444;
  751. av_log(avctx, AV_LOG_INFO,
  752. "encoding with ProRes 4444+ (ap4h) profile\n");
  753. } else {
  754. av_log(avctx, AV_LOG_ERROR, "Unknown pixel format\n");
  755. return AVERROR(EINVAL);
  756. }
  757. } else if (avctx->profile < FF_PROFILE_PRORES_PROXY
  758. || avctx->profile > FF_PROFILE_PRORES_XQ) {
  759. av_log(
  760. avctx,
  761. AV_LOG_ERROR,
  762. "unknown profile %d, use [0 - apco, 1 - apcs, 2 - apcn (default), 3 - apch, 4 - ap4h, 5 - ap4x]\n",
  763. avctx->profile);
  764. return AVERROR(EINVAL);
  765. } else if ((avctx->pix_fmt == AV_PIX_FMT_YUV422P10) && (avctx->profile > FF_PROFILE_PRORES_HQ)){
  766. av_log(avctx, AV_LOG_ERROR,
  767. "encoding with ProRes 444/Xq (ap4h/ap4x) profile, need YUV444P10 input\n");
  768. return AVERROR(EINVAL);
  769. } else if ((avctx->pix_fmt == AV_PIX_FMT_YUV444P10 || avctx->pix_fmt == AV_PIX_FMT_YUVA444P10)
  770. && (avctx->profile < FF_PROFILE_PRORES_4444)){
  771. av_log(avctx, AV_LOG_ERROR,
  772. "encoding with ProRes Proxy/LT/422/422 HQ (apco, apcs, apcn, ap4h) profile, need YUV422P10 input\n");
  773. return AVERROR(EINVAL);
  774. }
  775. if (avctx->profile < FF_PROFILE_PRORES_4444) { /* 422 versions */
  776. ctx->is_422 = 1;
  777. if ((avctx->height & 0xf) || (avctx->width & 0xf)) {
  778. ctx->fill_y = av_malloc(4 * (DEFAULT_SLICE_MB_WIDTH << 8));
  779. if (!ctx->fill_y)
  780. return AVERROR(ENOMEM);
  781. ctx->fill_u = ctx->fill_y + (DEFAULT_SLICE_MB_WIDTH << 9);
  782. ctx->fill_v = ctx->fill_u + (DEFAULT_SLICE_MB_WIDTH << 8);
  783. }
  784. } else { /* 444 */
  785. ctx->is_422 = 0;
  786. if ((avctx->height & 0xf) || (avctx->width & 0xf)) {
  787. ctx->fill_y = av_malloc(3 * (DEFAULT_SLICE_MB_WIDTH << 9));
  788. if (!ctx->fill_y)
  789. return AVERROR(ENOMEM);
  790. ctx->fill_u = ctx->fill_y + (DEFAULT_SLICE_MB_WIDTH << 9);
  791. ctx->fill_v = ctx->fill_u + (DEFAULT_SLICE_MB_WIDTH << 9);
  792. }
  793. if (avctx->pix_fmt == AV_PIX_FMT_YUVA444P10) {
  794. ctx->need_alpha = 1;
  795. ctx->fill_a = av_malloc(DEFAULT_SLICE_MB_WIDTH << 9); /* 8 blocks x 16px x 16px x sizeof (uint16) */
  796. if (!ctx->fill_a)
  797. return AVERROR(ENOMEM);
  798. }
  799. }
  800. ff_fdctdsp_init(&ctx->fdsp, avctx);
  801. avctx->codec_tag = AV_RL32((const uint8_t*)profiles[avctx->profile].name);
  802. for (i = 1; i <= 16; i++) {
  803. scale_mat(QMAT_LUMA[avctx->profile] , ctx->qmat_luma[i - 1] , i);
  804. scale_mat(QMAT_CHROMA[avctx->profile], ctx->qmat_chroma[i - 1], i);
  805. }
  806. return 0;
  807. }
  808. static av_cold int prores_encode_close(AVCodecContext *avctx)
  809. {
  810. ProresContext* ctx = avctx->priv_data;
  811. av_freep(&ctx->fill_y);
  812. av_freep(&ctx->fill_a);
  813. return 0;
  814. }
  815. #define OFFSET(x) offsetof(ProresContext, x)
  816. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  817. static const AVOption options[] = {
  818. { "vendor", "vendor ID", OFFSET(vendor), AV_OPT_TYPE_STRING, { .str = "fmpg" }, 0, 0, VE },
  819. { NULL }
  820. };
  821. static const AVClass proresaw_enc_class = {
  822. .class_name = "ProResAw encoder",
  823. .item_name = av_default_item_name,
  824. .option = options,
  825. .version = LIBAVUTIL_VERSION_INT,
  826. };
  827. static const AVClass prores_enc_class = {
  828. .class_name = "ProRes encoder",
  829. .item_name = av_default_item_name,
  830. .option = options,
  831. .version = LIBAVUTIL_VERSION_INT,
  832. };
  833. AVCodec ff_prores_aw_encoder = {
  834. .name = "prores_aw",
  835. .long_name = NULL_IF_CONFIG_SMALL("Apple ProRes"),
  836. .type = AVMEDIA_TYPE_VIDEO,
  837. .id = AV_CODEC_ID_PRORES,
  838. .priv_data_size = sizeof(ProresContext),
  839. .init = prores_encode_init,
  840. .close = prores_encode_close,
  841. .encode2 = prores_encode_frame,
  842. .pix_fmts = (const enum AVPixelFormat[]){AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_NONE},
  843. .capabilities = AV_CODEC_CAP_FRAME_THREADS,
  844. .priv_class = &proresaw_enc_class,
  845. .profiles = NULL_IF_CONFIG_SMALL(ff_prores_profiles),
  846. };
  847. AVCodec ff_prores_encoder = {
  848. .name = "prores",
  849. .long_name = NULL_IF_CONFIG_SMALL("Apple ProRes"),
  850. .type = AVMEDIA_TYPE_VIDEO,
  851. .id = AV_CODEC_ID_PRORES,
  852. .priv_data_size = sizeof(ProresContext),
  853. .init = prores_encode_init,
  854. .close = prores_encode_close,
  855. .encode2 = prores_encode_frame,
  856. .pix_fmts = (const enum AVPixelFormat[]){AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_NONE},
  857. .capabilities = AV_CODEC_CAP_FRAME_THREADS,
  858. .priv_class = &prores_enc_class,
  859. .profiles = NULL_IF_CONFIG_SMALL(ff_prores_profiles),
  860. };