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.

604 lines
20KB

  1. /*
  2. * Apple ProRes encoder
  3. *
  4. * Copyright (c) 2011 Anatoliy Wasserman
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file libavcodec/proresenc.c
  24. * Known FOURCCs: 'apch' (HQ), 'apcn' (SD), 'apcs' (LT), 'acpo' (Proxy)
  25. */
  26. #include "avcodec.h"
  27. #include "put_bits.h"
  28. #include "dsputil.h"
  29. #define DEFAULT_SLICE_MB_WIDTH 8
  30. #define FF_PROFILE_PRORES_PROXY 0
  31. #define FF_PROFILE_PRORES_LT 1
  32. #define FF_PROFILE_PRORES_STANDARD 2
  33. #define FF_PROFILE_PRORES_HQ 3
  34. static const AVProfile profiles[] = {
  35. { FF_PROFILE_PRORES_PROXY, "apco"},
  36. { FF_PROFILE_PRORES_LT, "apcs"},
  37. { FF_PROFILE_PRORES_STANDARD, "apcn"},
  38. { FF_PROFILE_PRORES_HQ, "apch"},
  39. { FF_PROFILE_UNKNOWN }
  40. };
  41. static const int qp_start_table[4] = { 4, 1, 1, 1 };
  42. static const int qp_end_table[4] = { 8, 9, 6, 6 };
  43. static const int bitrate_table[5] = { 1000, 2100, 3500, 5400 };
  44. static const uint8_t progressive_scan[64] = {
  45. 0, 1, 8, 9, 2, 3, 10, 11,
  46. 16, 17, 24, 25, 18, 19, 26, 27,
  47. 4, 5, 12, 20, 13, 6, 7, 14,
  48. 21, 28, 29, 22, 15, 23, 30, 31,
  49. 32, 33, 40, 48, 41, 34, 35, 42,
  50. 49, 56, 57, 50, 43, 36, 37, 44,
  51. 51, 58, 59, 52, 45, 38, 39, 46,
  52. 53, 60, 61, 54, 47, 55, 62, 63
  53. };
  54. static const uint8_t QMAT_LUMA[4][64] = {
  55. {
  56. 4, 7, 9, 11, 13, 14, 15, 63,
  57. 7, 7, 11, 12, 14, 15, 63, 63,
  58. 9, 11, 13, 14, 15, 63, 63, 63,
  59. 11, 11, 13, 14, 63, 63, 63, 63,
  60. 11, 13, 14, 63, 63, 63, 63, 63,
  61. 13, 14, 63, 63, 63, 63, 63, 63,
  62. 13, 63, 63, 63, 63, 63, 63, 63,
  63. 63, 63, 63, 63, 63, 63, 63, 63
  64. }, {
  65. 4, 5, 6, 7, 9, 11, 13, 15,
  66. 5, 5, 7, 8, 11, 13, 15, 17,
  67. 6, 7, 9, 11, 13, 15, 15, 17,
  68. 7, 7, 9, 11, 13, 15, 17, 19,
  69. 7, 9, 11, 13, 14, 16, 19, 23,
  70. 9, 11, 13, 14, 16, 19, 23, 29,
  71. 9, 11, 13, 15, 17, 21, 28, 35,
  72. 11, 13, 16, 17, 21, 28, 35, 41
  73. }, {
  74. 4, 4, 5, 5, 6, 7, 7, 9,
  75. 4, 4, 5, 6, 7, 7, 9, 9,
  76. 5, 5, 6, 7, 7, 9, 9, 10,
  77. 5, 5, 6, 7, 7, 9, 9, 10,
  78. 5, 6, 7, 7, 8, 9, 10, 12,
  79. 6, 7, 7, 8, 9, 10, 12, 15,
  80. 6, 7, 7, 9, 10, 11, 14, 17,
  81. 7, 7, 9, 10, 11, 14, 17, 21
  82. }, {
  83. 4, 4, 4, 4, 4, 4, 4, 4,
  84. 4, 4, 4, 4, 4, 4, 4, 4,
  85. 4, 4, 4, 4, 4, 4, 4, 4,
  86. 4, 4, 4, 4, 4, 4, 4, 5,
  87. 4, 4, 4, 4, 4, 4, 5, 5,
  88. 4, 4, 4, 4, 4, 5, 5, 6,
  89. 4, 4, 4, 4, 5, 5, 6, 7,
  90. 4, 4, 4, 4, 5, 6, 7, 7
  91. }
  92. };
  93. static const uint8_t QMAT_CHROMA[4][64] = {
  94. {
  95. 4, 7, 9, 11, 13, 14, 63, 63,
  96. 7, 7, 11, 12, 14, 63, 63, 63,
  97. 9, 11, 13, 14, 63, 63, 63, 63,
  98. 11, 11, 13, 14, 63, 63, 63, 63,
  99. 11, 13, 14, 63, 63, 63, 63, 63,
  100. 13, 14, 63, 63, 63, 63, 63, 63,
  101. 13, 63, 63, 63, 63, 63, 63, 63,
  102. 63, 63, 63, 63, 63, 63, 63, 63
  103. }, {
  104. 4, 5, 6, 7, 9, 11, 13, 15,
  105. 5, 5, 7, 8, 11, 13, 15, 17,
  106. 6, 7, 9, 11, 13, 15, 15, 17,
  107. 7, 7, 9, 11, 13, 15, 17, 19,
  108. 7, 9, 11, 13, 14, 16, 19, 23,
  109. 9, 11, 13, 14, 16, 19, 23, 29,
  110. 9, 11, 13, 15, 17, 21, 28, 35,
  111. 11, 13, 16, 17, 21, 28, 35, 41
  112. }, {
  113. 4, 4, 5, 5, 6, 7, 7, 9,
  114. 4, 4, 5, 6, 7, 7, 9, 9,
  115. 5, 5, 6, 7, 7, 9, 9, 10,
  116. 5, 5, 6, 7, 7, 9, 9, 10,
  117. 5, 6, 7, 7, 8, 9, 10, 12,
  118. 6, 7, 7, 8, 9, 10, 12, 15,
  119. 6, 7, 7, 9, 10, 11, 14, 17,
  120. 7, 7, 9, 10, 11, 14, 17, 21
  121. }, {
  122. 4, 4, 4, 4, 4, 4, 4, 4,
  123. 4, 4, 4, 4, 4, 4, 4, 4,
  124. 4, 4, 4, 4, 4, 4, 4, 4,
  125. 4, 4, 4, 4, 4, 4, 4, 5,
  126. 4, 4, 4, 4, 4, 4, 5, 5,
  127. 4, 4, 4, 4, 4, 5, 5, 6,
  128. 4, 4, 4, 4, 5, 5, 6, 7,
  129. 4, 4, 4, 4, 5, 6, 7, 7
  130. }
  131. };
  132. typedef struct {
  133. uint8_t* fill_y;
  134. uint8_t* fill_u;
  135. uint8_t* fill_v;
  136. int qmat_luma[16][64];
  137. int qmat_chroma[16][64];
  138. } ProresContext;
  139. static void encode_codeword(PutBitContext *pb, int val, int codebook)
  140. {
  141. unsigned int rice_order, exp_order, switch_bits, first_exp, exp, zeros,
  142. mask;
  143. /* number of bits to switch between rice and exp golomb */
  144. switch_bits = codebook & 3;
  145. rice_order = codebook >> 5;
  146. exp_order = (codebook >> 2) & 7;
  147. first_exp = ((switch_bits + 1) << rice_order);
  148. if (val >= first_exp) { /* exp golomb */
  149. val -= first_exp;
  150. val += (1 << exp_order);
  151. exp = av_log2(val);
  152. zeros = exp - exp_order + switch_bits + 1;
  153. put_bits(pb, zeros, 0);
  154. put_bits(pb, 1, 1);
  155. put_bits(pb, exp, val);
  156. } else if (rice_order) {
  157. mask = (1 << rice_order) - 1;
  158. put_bits(pb, (val >> rice_order), 0);
  159. put_bits(pb, 1, 1);
  160. put_bits(pb, rice_order, val & mask);
  161. } else {
  162. put_bits(pb, val, 0);
  163. put_bits(pb, 1, 1);
  164. }
  165. }
  166. #define QSCALE(qmat,ind,val) ((val) / (qmat[ind]))
  167. #define TO_GOLOMB(val) ((val << 1) ^ (val >> 31))
  168. #define DIFF_SIGN(val, sign) ((val >> 31) ^ sign)
  169. #define IS_NEGATIVE(val) (((val >> 31) ^ -1) + 1)
  170. #define TO_GOLOMB2(val,sign) (val==0 ? 0 : (val << 1) + sign)
  171. static av_always_inline int get_level(int val)
  172. {
  173. int sign = (val >> 31);
  174. return (val ^ sign) - sign;
  175. }
  176. #define FIRST_DC_CB 0xB8
  177. static const uint8_t dc_codebook[7] = { 0x04, 0x28, 0x28, 0x4D, 0x4D, 0x70, 0x70};
  178. static void encode_dc_coeffs(PutBitContext *pb, DCTELEM *in,
  179. int blocks_per_slice, int *qmat)
  180. {
  181. int prev_dc, code;
  182. int i, sign, idx;
  183. int new_dc, delta, diff_sign, new_code;
  184. prev_dc = QSCALE(qmat, 0, in[0] - 16384);
  185. code = TO_GOLOMB(prev_dc);
  186. encode_codeword(pb, code, FIRST_DC_CB);
  187. code = 5; sign = 0; idx = 64;
  188. for (i = 1; i < blocks_per_slice; i++, idx += 64) {
  189. new_dc = QSCALE(qmat, 0, in[idx] - 16384);
  190. delta = new_dc - prev_dc;
  191. diff_sign = DIFF_SIGN(delta, sign);
  192. new_code = TO_GOLOMB2(get_level(delta), diff_sign);
  193. encode_codeword(pb, new_code, dc_codebook[FFMIN(code, 6)]);
  194. code = new_code;
  195. sign = delta >> 31;
  196. prev_dc = new_dc;
  197. }
  198. }
  199. static const uint8_t run_to_cb[16] = { 0x06, 0x06, 0x05, 0x05, 0x04, 0x29,
  200. 0x29, 0x29, 0x29, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x4C };
  201. static const uint8_t lev_to_cb[10] = { 0x04, 0x0A, 0x05, 0x06, 0x04, 0x28,
  202. 0x28, 0x28, 0x28, 0x4C };
  203. static void encode_ac_coeffs(AVCodecContext *avctx, PutBitContext *pb,
  204. DCTELEM *in, int blocks_per_slice, int *qmat)
  205. {
  206. int prev_run = 4;
  207. int prev_level = 2;
  208. int run = 0, level, code;
  209. for (int i = 1; i < 64; i++) {
  210. int indp = progressive_scan[i];
  211. for (int j = 0; j < blocks_per_slice; j++) {
  212. int val = QSCALE(qmat, indp, in[(j << 6) + indp]);
  213. if (val) {
  214. encode_codeword(pb, run, run_to_cb[FFMIN(prev_run, 15)]);
  215. prev_run = run;
  216. run = 0;
  217. level = get_level(val);
  218. code = level - 1;
  219. encode_codeword(pb, code, lev_to_cb[FFMIN(prev_level, 9)]);
  220. prev_level = level;
  221. put_bits(pb, 1, IS_NEGATIVE(val));
  222. } else {
  223. ++run;
  224. }
  225. }
  226. }
  227. }
  228. static void get(uint8_t *pixels, int stride, DCTELEM* block)
  229. {
  230. int16_t *p = (int16_t*)pixels;
  231. int i, j;
  232. stride >>= 1;
  233. for (i = 0; i < 8; i++) {
  234. for (j = 0; j < 8; j++) {
  235. block[j] = p[j];
  236. }
  237. p += stride;
  238. block += 8;
  239. }
  240. }
  241. static void fdct_get(uint8_t *pixels, int stride, DCTELEM* block)
  242. {
  243. get(pixels, stride, block);
  244. ff_jpeg_fdct_islow_10(block);
  245. }
  246. static int encode_slice_plane(AVCodecContext *avctx, int mb_count,
  247. uint8_t *src, int src_stride, uint8_t *buf, unsigned buf_size,
  248. int *qmat, int chroma)
  249. {
  250. DECLARE_ALIGNED(16, DCTELEM, blocks)[DEFAULT_SLICE_MB_WIDTH << 8], *block;
  251. int i, blocks_per_slice;
  252. PutBitContext pb;
  253. block = blocks;
  254. for (i = 0; i < mb_count; i++) {
  255. fdct_get(src, src_stride, block + (0 << 6));
  256. fdct_get(src + 8 * src_stride, src_stride, block + ((2 - chroma) << 6));
  257. if (!chroma) {
  258. fdct_get(src + 16, src_stride, block + (1 << 6));
  259. fdct_get(src + 16 + 8 * src_stride, src_stride, block + (3 << 6));
  260. }
  261. block += (256 >> chroma);
  262. src += (32 >> chroma);
  263. }
  264. blocks_per_slice = mb_count << (2 - chroma);
  265. init_put_bits(&pb, buf, buf_size << 3);
  266. encode_dc_coeffs(&pb, blocks, blocks_per_slice, qmat);
  267. encode_ac_coeffs(avctx, &pb, blocks, blocks_per_slice, qmat);
  268. flush_put_bits(&pb);
  269. return put_bits_ptr(&pb) - pb.buf;
  270. }
  271. static av_always_inline unsigned encode_slice_data(AVCodecContext *avctx,
  272. uint8_t *dest_y, uint8_t *dest_u, uint8_t *dest_v, int luma_stride,
  273. int chroma_stride, unsigned mb_count, uint8_t *buf, unsigned data_size,
  274. unsigned* y_data_size, unsigned* u_data_size, unsigned* v_data_size,
  275. int qp)
  276. {
  277. ProresContext* ctx = avctx->priv_data;
  278. *y_data_size = encode_slice_plane(avctx, mb_count, dest_y, luma_stride,
  279. buf, data_size, ctx->qmat_luma[qp - 1], 0);
  280. if (!(avctx->flags & CODEC_FLAG_GRAY)) {
  281. *u_data_size = encode_slice_plane(avctx, mb_count, dest_u,
  282. chroma_stride, buf + *y_data_size, data_size - *y_data_size,
  283. ctx->qmat_chroma[qp - 1], 1);
  284. *v_data_size = encode_slice_plane(avctx, mb_count, dest_v,
  285. chroma_stride, buf + *y_data_size + *u_data_size,
  286. data_size - *y_data_size - *u_data_size,
  287. ctx->qmat_chroma[qp - 1], 1);
  288. }
  289. return *y_data_size + *u_data_size + *v_data_size;
  290. }
  291. static void subimage_with_fill(uint16_t *src, unsigned x, unsigned y,
  292. unsigned stride, unsigned width, unsigned height, uint16_t *dst,
  293. unsigned dst_width, unsigned dst_height)
  294. {
  295. int box_width = FFMIN(width - x, dst_width);
  296. int box_height = FFMIN(height - y, dst_height);
  297. int i, j, src_stride = stride >> 1;
  298. uint16_t last_pix, *last_line;
  299. src += y * src_stride + x;
  300. for (i = 0; i < box_height; ++i) {
  301. for (j = 0; j < box_width; ++j) {
  302. dst[j] = src[j];
  303. }
  304. last_pix = dst[j - 1];
  305. for (; j < dst_width; j++)
  306. dst[j] = last_pix;
  307. src += src_stride;
  308. dst += dst_width;
  309. }
  310. last_line = dst - dst_width;
  311. for (; i < dst_height; i++) {
  312. for (j = 0; j < dst_width; ++j) {
  313. dst[j] = last_line[j];
  314. }
  315. dst += dst_width;
  316. }
  317. }
  318. static int encode_slice(AVCodecContext *avctx, AVFrame *pic, int mb_x,
  319. int mb_y, unsigned mb_count, uint8_t *buf, unsigned data_size,
  320. int unsafe, int *qp)
  321. {
  322. int luma_stride, chroma_stride;
  323. int hdr_size = 6, slice_size;
  324. uint8_t *dest_y, *dest_u, *dest_v;
  325. unsigned y_data_size = 0, u_data_size = 0, v_data_size = 0;
  326. ProresContext* ctx = avctx->priv_data;
  327. int tgt_bits = (mb_count * bitrate_table[avctx->profile]) >> 2;
  328. int low_bytes = (tgt_bits - (tgt_bits >> 3)) >> 3; // 12% bitrate fluctuation
  329. int high_bytes = (tgt_bits + (tgt_bits >> 3)) >> 3;
  330. luma_stride = pic->linesize[0];
  331. chroma_stride = pic->linesize[1];
  332. dest_y = pic->data[0] + (mb_y << 4) * luma_stride + (mb_x << 5);
  333. dest_u = pic->data[1] + (mb_y << 4) * chroma_stride + (mb_x << 4);
  334. dest_v = pic->data[2] + (mb_y << 4) * chroma_stride + (mb_x << 4);
  335. if (unsafe) {
  336. subimage_with_fill((uint16_t *) pic->data[0], mb_x << 4, mb_y << 4,
  337. luma_stride, avctx->width, avctx->height,
  338. (uint16_t *) ctx->fill_y, mb_count << 4, 16);
  339. subimage_with_fill((uint16_t *) pic->data[1], mb_x << 3, mb_y << 4,
  340. chroma_stride, avctx->width >> 1, avctx->height,
  341. (uint16_t *) ctx->fill_u, mb_count << 3, 16);
  342. subimage_with_fill((uint16_t *) pic->data[2], mb_x << 3, mb_y << 4,
  343. chroma_stride, avctx->width >> 1, avctx->height,
  344. (uint16_t *) ctx->fill_v, mb_count << 3, 16);
  345. encode_slice_data(avctx, ctx->fill_y, ctx->fill_u, ctx->fill_v,
  346. mb_count << 5, mb_count << 4, mb_count, buf + hdr_size,
  347. data_size - hdr_size, &y_data_size, &u_data_size, &v_data_size,
  348. *qp);
  349. } else {
  350. slice_size = encode_slice_data(avctx, dest_y, dest_u, dest_v,
  351. luma_stride, chroma_stride, mb_count, buf + hdr_size,
  352. data_size - hdr_size, &y_data_size, &u_data_size, &v_data_size,
  353. *qp);
  354. if (slice_size > high_bytes && *qp < qp_end_table[avctx->profile]) {
  355. do {
  356. *qp += 1;
  357. slice_size = encode_slice_data(avctx, dest_y, dest_u, dest_v,
  358. luma_stride, chroma_stride, mb_count, buf + hdr_size,
  359. data_size - hdr_size, &y_data_size, &u_data_size,
  360. &v_data_size, *qp);
  361. } while (slice_size > high_bytes && *qp < qp_end_table[avctx->profile]);
  362. } else if (slice_size < low_bytes && *qp
  363. > qp_start_table[avctx->profile]) {
  364. do {
  365. *qp -= 1;
  366. slice_size = encode_slice_data(avctx, dest_y, dest_u, dest_v,
  367. luma_stride, chroma_stride, mb_count, buf + hdr_size,
  368. data_size - hdr_size, &y_data_size, &u_data_size,
  369. &v_data_size, *qp);
  370. } while (slice_size < low_bytes && *qp > qp_start_table[avctx->profile]);
  371. }
  372. }
  373. buf[0] = hdr_size << 3;
  374. buf[1] = *qp;
  375. AV_WB16(buf + 2, y_data_size);
  376. AV_WB16(buf + 4, u_data_size);
  377. return hdr_size + y_data_size + u_data_size + v_data_size;
  378. }
  379. static int prores_encode_picture(AVCodecContext *avctx, AVFrame *pic,
  380. uint8_t *buf, const int buf_size)
  381. {
  382. int mb_width = (avctx->width + 15) >> 4;
  383. int mb_height = (avctx->height + 15) >> 4;
  384. int hdr_size, sl_size, *slice_sizes;
  385. int sl, mb_y, sl_data_size, qp;
  386. int unsafe_bot, unsafe_right;
  387. uint8_t *sl_data;
  388. int slice_per_line = 0, rem = mb_width;
  389. for (int i = av_log2(DEFAULT_SLICE_MB_WIDTH); i >= 0; --i) {
  390. slice_per_line += rem >> i;
  391. rem &= (1 << i) - 1;
  392. }
  393. qp = qp_start_table[avctx->profile];
  394. slice_sizes = av_malloc(slice_per_line * mb_height * sizeof(int));
  395. sl = 0; hdr_size = 8; sl_data_size = buf_size - hdr_size;
  396. sl_data = buf + hdr_size + (slice_per_line * mb_height * 2);
  397. for (mb_y = 0; mb_y < mb_height; mb_y++) {
  398. int mb_x = 0;
  399. int slice_mb_count = DEFAULT_SLICE_MB_WIDTH;
  400. while (mb_x < mb_width) {
  401. while (mb_width - mb_x < slice_mb_count)
  402. slice_mb_count >>= 1;
  403. unsafe_bot = (avctx->height & 0xf) && (mb_y == mb_height - 1);
  404. unsafe_right = (avctx->width & 0xf) && (mb_x + slice_mb_count == mb_width);
  405. sl_size = encode_slice(avctx, pic, mb_x, mb_y, slice_mb_count,
  406. sl_data, sl_data_size, unsafe_bot || unsafe_right, &qp);
  407. slice_sizes[sl++] = sl_size;
  408. sl_data += sl_size;
  409. sl_data_size -= sl_size;
  410. mb_x += slice_mb_count;
  411. }
  412. }
  413. buf[0] = hdr_size << 3;
  414. AV_WB32(buf + 1, sl_data - buf);
  415. AV_WB16(buf + 5, slice_per_line * mb_height);
  416. buf[7] = av_log2(DEFAULT_SLICE_MB_WIDTH) << 4;
  417. for (int i = 0; i < slice_per_line * mb_height; i++)
  418. AV_WB16(buf + hdr_size + (i << 1), slice_sizes[i]);
  419. av_free(slice_sizes);
  420. return sl_data - buf;
  421. }
  422. static int prores_encode_frame(AVCodecContext *avctx, unsigned char *buf,
  423. int buf_size, void *data)
  424. {
  425. AVFrame *pic = data;
  426. int header_size = 148;
  427. int pic_size = prores_encode_picture(avctx, pic, buf + header_size + 8,
  428. buf_size - header_size - 8);
  429. bytestream_put_be32(&buf, pic_size + 8 + header_size);
  430. bytestream_put_buffer(&buf, "icpf", 4);
  431. bytestream_put_be16(&buf, header_size);
  432. bytestream_put_be16(&buf, 0);
  433. bytestream_put_buffer(&buf, "fmpg", 4);
  434. bytestream_put_be16(&buf, pic->width);
  435. bytestream_put_be16(&buf, pic->height);
  436. *buf++ = 0x83; // {10}(422){00}{00}(frame){11}
  437. *buf++ = 0;
  438. *buf++ = 2;
  439. *buf++ = 2;
  440. *buf++ = 6;
  441. *buf++ = 32;
  442. *buf++ = 0;
  443. *buf++ = 3;
  444. bytestream_put_buffer(&buf, QMAT_LUMA[avctx->profile], 64);
  445. bytestream_put_buffer(&buf, QMAT_CHROMA[avctx->profile], 64);
  446. return pic_size + 8 + header_size;
  447. }
  448. static void scale_mat(const uint8_t* src, int* dst, int scale)
  449. {
  450. int i;
  451. for (i = 0; i < 64; i++)
  452. dst[i] = src[i] * scale;
  453. }
  454. static av_cold int prores_encode_init(AVCodecContext *avctx)
  455. {
  456. int i;
  457. ProresContext* ctx = avctx->priv_data;
  458. if (avctx->pix_fmt != PIX_FMT_YUV422P10LE) {
  459. av_log(avctx, AV_LOG_ERROR, "need YUV422P10\n");
  460. return -1;
  461. }
  462. if (avctx->width & 0x1) {
  463. av_log(avctx, AV_LOG_ERROR,
  464. "frame width needs to be multiple of 2\n");
  465. return -1;
  466. }
  467. if ((avctx->height & 0xf) || (avctx->width & 0xf)) {
  468. ctx->fill_y = av_malloc(4 * (DEFAULT_SLICE_MB_WIDTH << 8));
  469. if (!ctx->fill_y)
  470. return AVERROR(ENOMEM);
  471. ctx->fill_u = ctx->fill_y + (DEFAULT_SLICE_MB_WIDTH << 9);
  472. ctx->fill_v = ctx->fill_u + (DEFAULT_SLICE_MB_WIDTH << 8);
  473. }
  474. if (avctx->profile == FF_PROFILE_UNKNOWN) {
  475. avctx->profile = FF_PROFILE_PRORES_STANDARD;
  476. av_log(avctx, AV_LOG_INFO,
  477. "encoding with ProRes standard (apcn) profile\n");
  478. } else if (avctx->profile < FF_PROFILE_PRORES_PROXY
  479. || avctx->profile > FF_PROFILE_PRORES_HQ) {
  480. av_log(
  481. avctx,
  482. AV_LOG_ERROR,
  483. "unknown profile %d, use [0 - apco, 1 - apcs, 2 - apcn (default), 3 - apch]\n",
  484. avctx->profile);
  485. return -1;
  486. }
  487. avctx->codec_tag = AV_RL32((const uint8_t*)profiles[avctx->profile].name);
  488. for (i = 1; i <= 16; i++) {
  489. scale_mat(QMAT_LUMA[avctx->profile] , ctx->qmat_luma[i - 1] , i);
  490. scale_mat(QMAT_CHROMA[avctx->profile], ctx->qmat_chroma[i - 1], i);
  491. }
  492. avctx->coded_frame = avcodec_alloc_frame();
  493. avctx->coded_frame->key_frame = 1;
  494. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  495. return 0;
  496. }
  497. static av_cold int prores_encode_close(AVCodecContext *avctx)
  498. {
  499. ProresContext* ctx = avctx->priv_data;
  500. av_freep(&avctx->coded_frame);
  501. av_freep(&ctx->fill_y);
  502. return 0;
  503. }
  504. AVCodec ff_prores_encoder = {
  505. .name = "prores",
  506. .type = AVMEDIA_TYPE_VIDEO,
  507. .id = CODEC_ID_PRORES,
  508. .priv_data_size = sizeof(ProresContext),
  509. .init = prores_encode_init,
  510. .close = prores_encode_close,
  511. .encode = prores_encode_frame,
  512. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV422P10LE, PIX_FMT_NONE},
  513. .long_name = NULL_IF_CONFIG_SMALL("Apple ProRes"),
  514. .capabilities = 0,
  515. .profiles = profiles
  516. };