proresenc.c 19 KB

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