4 * Copyright (c) 2012 Konstantin Shishkov
6 * This file is part of Libav.
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.
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.
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
23 #include "libavutil/opt.h"
26 #include "bytestream.h"
28 #include "proresdsp.h"
29 #include "proresdata.h"
31 #define CFACTOR_Y422 2
32 #define CFACTOR_Y444 3
34 #define MAX_MBS_PER_SLICE 8
36 #define MAX_PLANES 3 // should be increased to 4 when there's PIX_FMT_YUV444AP10
39 PRORES_PROFILE_PROXY = 0,
41 PRORES_PROFILE_STANDARD,
45 #define NUM_MB_LIMITS 4
46 static const int prores_mb_limits[NUM_MB_LIMITS] = {
47 1620, // up to 720x576
48 2700, // up to 960x720
49 6075, // up to 1440x1080
50 9216, // up to 2048x1152
53 static const struct prores_profile {
54 const char *full_name;
58 int br_tab[NUM_MB_LIMITS];
60 } prores_profile_info[4] = {
63 .tag = MKTAG('a', 'p', 'c', 'o'),
66 .br_tab = { 300, 242, 220, 194 },
68 4, 7, 9, 11, 13, 14, 15, 63,
69 7, 7, 11, 12, 14, 15, 63, 63,
70 9, 11, 13, 14, 15, 63, 63, 63,
71 11, 11, 13, 14, 63, 63, 63, 63,
72 11, 13, 14, 63, 63, 63, 63, 63,
73 13, 14, 63, 63, 63, 63, 63, 63,
74 13, 63, 63, 63, 63, 63, 63, 63,
75 63, 63, 63, 63, 63, 63, 63, 63,
80 .tag = MKTAG('a', 'p', 'c', 's'),
83 .br_tab = { 720, 560, 490, 440 },
85 4, 5, 6, 7, 9, 11, 13, 15,
86 5, 5, 7, 8, 11, 13, 15, 17,
87 6, 7, 9, 11, 13, 15, 15, 17,
88 7, 7, 9, 11, 13, 15, 17, 19,
89 7, 9, 11, 13, 14, 16, 19, 23,
90 9, 11, 13, 14, 16, 19, 23, 29,
91 9, 11, 13, 15, 17, 21, 28, 35,
92 11, 13, 16, 17, 21, 28, 35, 41,
96 .full_name = "standard",
97 .tag = MKTAG('a', 'p', 'c', 'n'),
100 .br_tab = { 1050, 808, 710, 632 },
102 4, 4, 5, 5, 6, 7, 7, 9,
103 4, 4, 5, 6, 7, 7, 9, 9,
104 5, 5, 6, 7, 7, 9, 9, 10,
105 5, 5, 6, 7, 7, 9, 9, 10,
106 5, 6, 7, 7, 8, 9, 10, 12,
107 6, 7, 7, 8, 9, 10, 12, 15,
108 6, 7, 7, 9, 10, 11, 14, 17,
109 7, 7, 9, 10, 11, 14, 17, 21,
113 .full_name = "high quality",
114 .tag = MKTAG('a', 'p', 'c', 'h'),
117 .br_tab = { 1566, 1216, 1070, 950 },
119 4, 4, 4, 4, 4, 4, 4, 4,
120 4, 4, 4, 4, 4, 4, 4, 4,
121 4, 4, 4, 4, 4, 4, 4, 4,
122 4, 4, 4, 4, 4, 4, 4, 5,
123 4, 4, 4, 4, 4, 4, 5, 5,
124 4, 4, 4, 4, 4, 5, 5, 6,
125 4, 4, 4, 4, 5, 5, 6, 7,
126 4, 4, 4, 4, 5, 6, 7, 7,
129 // for 4444 profile bitrate numbers are { 2350, 1828, 1600, 1425 }
132 #define TRELLIS_WIDTH 16
133 #define SCORE_LIMIT INT_MAX / 2
142 #define MAX_STORED_Q 16
144 typedef struct ProresContext {
146 DECLARE_ALIGNED(16, DCTELEM, blocks)[MAX_PLANES][64 * 4 * MAX_MBS_PER_SLICE];
147 DECLARE_ALIGNED(16, uint16_t, emu_buf)[16*16];
148 int16_t quants[MAX_STORED_Q][64];
149 int16_t custom_q[64];
151 ProresDSPContext dsp;
154 int mb_width, mb_height;
156 int num_chroma_blocks, chroma_factor;
165 const struct prores_profile *profile_info;
167 struct TrellisNode *nodes;
171 static void get_slice_data(ProresContext *ctx, const uint16_t *src,
172 int linesize, int x, int y, int w, int h,
174 int mbs_per_slice, int blocks_per_mb)
176 const uint16_t *esrc;
177 const int mb_width = 4 * blocks_per_mb;
181 for (i = 0; i < mbs_per_slice; i++, src += mb_width) {
183 memset(blocks, 0, 64 * (mbs_per_slice - i) * blocks_per_mb
187 if (x + mb_width <= w && y + 16 <= h) {
189 elinesize = linesize;
192 const int estride = 16 / sizeof(*ctx->emu_buf);
197 bw = FFMIN(w - x, mb_width);
198 bh = FFMIN(h - y, 16);
200 for (j = 0; j < bh; j++) {
201 memcpy(ctx->emu_buf + j * estride, src + j * linesize,
203 pix = ctx->emu_buf[j * estride + bw - 1];
204 for (k = bw; k < mb_width; k++)
205 ctx->emu_buf[j * estride + k] = pix;
208 memcpy(ctx->emu_buf + j * estride,
209 ctx->emu_buf + (bh - 1) * estride,
210 mb_width * sizeof(*ctx->emu_buf));
212 ctx->dsp.fdct(esrc, elinesize, blocks);
214 if (blocks_per_mb > 2) {
215 ctx->dsp.fdct(src + 8, linesize, blocks);
218 ctx->dsp.fdct(src + linesize * 4, linesize, blocks);
220 if (blocks_per_mb > 2) {
221 ctx->dsp.fdct(src + linesize * 4 + 8, linesize, blocks);
230 * Write an unsigned rice/exp golomb codeword.
232 static inline void encode_vlc_codeword(PutBitContext *pb, uint8_t codebook, int val)
234 unsigned int rice_order, exp_order, switch_bits, switch_val;
237 /* number of prefix bits to switch between Rice and expGolomb */
238 switch_bits = (codebook & 3) + 1;
239 rice_order = codebook >> 5; /* rice code order */
240 exp_order = (codebook >> 2) & 7; /* exp golomb code order */
242 switch_val = switch_bits << rice_order;
244 if (val >= switch_val) {
245 val -= switch_val - (1 << exp_order);
246 exponent = av_log2(val);
248 put_bits(pb, exponent - exp_order + switch_bits, 0);
250 put_bits(pb, exponent, val);
252 exponent = val >> rice_order;
255 put_bits(pb, exponent, 0);
258 put_sbits(pb, rice_order, val);
262 #define GET_SIGN(x) ((x) >> 31)
263 #define MAKE_CODE(x) (((x) << 1) ^ GET_SIGN(x))
265 static void encode_dcs(PutBitContext *pb, DCTELEM *blocks,
266 int blocks_per_slice, int scale)
269 int codebook = 3, code, dc, prev_dc, delta, sign, new_sign;
271 prev_dc = (blocks[0] - 0x4000) / scale;
272 encode_vlc_codeword(pb, FIRST_DC_CB, MAKE_CODE(prev_dc));
277 for (i = 1; i < blocks_per_slice; i++, blocks += 64) {
278 dc = (blocks[0] - 0x4000) / scale;
279 delta = dc - prev_dc;
280 new_sign = GET_SIGN(delta);
281 delta = (delta ^ sign) - sign;
282 code = MAKE_CODE(delta);
283 encode_vlc_codeword(pb, ff_prores_dc_codebook[codebook], code);
284 codebook = (code + (code & 1)) >> 1;
285 codebook = FFMIN(codebook, 3);
291 static void encode_acs(PutBitContext *pb, DCTELEM *blocks,
292 int blocks_per_slice,
293 int plane_size_factor,
294 const uint8_t *scan, const int16_t *qmat)
297 int run, level, run_cb, lev_cb;
298 int max_coeffs, abs_level;
300 max_coeffs = blocks_per_slice << 6;
301 run_cb = ff_prores_run_to_cb_index[4];
302 lev_cb = ff_prores_lev_to_cb_index[2];
305 for (i = 1; i < 64; i++) {
306 for (idx = scan[i]; idx < max_coeffs; idx += 64) {
307 level = blocks[idx] / qmat[scan[i]];
309 abs_level = FFABS(level);
310 encode_vlc_codeword(pb, ff_prores_ac_codebook[run_cb], run);
311 encode_vlc_codeword(pb, ff_prores_ac_codebook[lev_cb],
313 put_sbits(pb, 1, GET_SIGN(level));
315 run_cb = ff_prores_run_to_cb_index[FFMIN(run, 15)];
316 lev_cb = ff_prores_lev_to_cb_index[FFMIN(abs_level, 9)];
325 static int encode_slice_plane(ProresContext *ctx, PutBitContext *pb,
326 const uint16_t *src, int linesize,
327 int mbs_per_slice, DCTELEM *blocks,
328 int blocks_per_mb, int plane_size_factor,
331 int blocks_per_slice, saved_pos;
333 saved_pos = put_bits_count(pb);
334 blocks_per_slice = mbs_per_slice * blocks_per_mb;
336 encode_dcs(pb, blocks, blocks_per_slice, qmat[0]);
337 encode_acs(pb, blocks, blocks_per_slice, plane_size_factor,
338 ctx->scantable.permutated, qmat);
341 return (put_bits_count(pb) - saved_pos) >> 3;
344 static int encode_slice(AVCodecContext *avctx, const AVFrame *pic,
346 int sizes[4], int x, int y, int quant,
349 ProresContext *ctx = avctx->priv_data;
353 int slice_width_factor = av_log2(mbs_per_slice);
354 int num_cblocks, pwidth;
355 int plane_factor, is_chroma;
358 if (quant < MAX_STORED_Q) {
359 qmat = ctx->quants[quant];
361 qmat = ctx->custom_q;
362 for (i = 0; i < 64; i++)
363 qmat[i] = ctx->profile_info->quant[i] * quant;
366 for (i = 0; i < ctx->num_planes; i++) {
367 is_chroma = (i == 1 || i == 2);
368 plane_factor = slice_width_factor + 2;
370 plane_factor += ctx->chroma_factor - 3;
371 if (!is_chroma || ctx->chroma_factor == CFACTOR_Y444) {
375 pwidth = avctx->width;
380 pwidth = avctx->width >> 1;
382 src = (const uint16_t*)(pic->data[i] + yp * pic->linesize[i]) + xp;
384 get_slice_data(ctx, src, pic->linesize[i], xp, yp,
385 pwidth, avctx->height, ctx->blocks[0],
386 mbs_per_slice, num_cblocks);
387 sizes[i] = encode_slice_plane(ctx, pb, src, pic->linesize[i],
388 mbs_per_slice, ctx->blocks[0],
389 num_cblocks, plane_factor,
391 total_size += sizes[i];
396 static inline int estimate_vlc(uint8_t codebook, int val)
398 unsigned int rice_order, exp_order, switch_bits, switch_val;
401 /* number of prefix bits to switch between Rice and expGolomb */
402 switch_bits = (codebook & 3) + 1;
403 rice_order = codebook >> 5; /* rice code order */
404 exp_order = (codebook >> 2) & 7; /* exp golomb code order */
406 switch_val = switch_bits << rice_order;
408 if (val >= switch_val) {
409 val -= switch_val - (1 << exp_order);
410 exponent = av_log2(val);
412 return exponent * 2 - exp_order + switch_bits + 1;
414 return (val >> rice_order) + rice_order + 1;
418 static int estimate_dcs(int *error, DCTELEM *blocks, int blocks_per_slice,
422 int codebook = 3, code, dc, prev_dc, delta, sign, new_sign;
425 prev_dc = (blocks[0] - 0x4000) / scale;
426 bits = estimate_vlc(FIRST_DC_CB, MAKE_CODE(prev_dc));
430 *error += FFABS(blocks[0] - 0x4000) % scale;
432 for (i = 1; i < blocks_per_slice; i++, blocks += 64) {
433 dc = (blocks[0] - 0x4000) / scale;
434 *error += FFABS(blocks[0] - 0x4000) % scale;
435 delta = dc - prev_dc;
436 new_sign = GET_SIGN(delta);
437 delta = (delta ^ sign) - sign;
438 code = MAKE_CODE(delta);
439 bits += estimate_vlc(ff_prores_dc_codebook[codebook], code);
440 codebook = (code + (code & 1)) >> 1;
441 codebook = FFMIN(codebook, 3);
449 static int estimate_acs(int *error, DCTELEM *blocks, int blocks_per_slice,
450 int plane_size_factor,
451 const uint8_t *scan, const int16_t *qmat)
454 int run, level, run_cb, lev_cb;
455 int max_coeffs, abs_level;
458 max_coeffs = blocks_per_slice << 6;
459 run_cb = ff_prores_run_to_cb_index[4];
460 lev_cb = ff_prores_lev_to_cb_index[2];
463 for (i = 1; i < 64; i++) {
464 for (idx = scan[i]; idx < max_coeffs; idx += 64) {
465 level = blocks[idx] / qmat[scan[i]];
466 *error += FFABS(blocks[idx]) % qmat[scan[i]];
468 abs_level = FFABS(level);
469 bits += estimate_vlc(ff_prores_ac_codebook[run_cb], run);
470 bits += estimate_vlc(ff_prores_ac_codebook[lev_cb],
473 run_cb = ff_prores_run_to_cb_index[FFMIN(run, 15)];
474 lev_cb = ff_prores_lev_to_cb_index[FFMIN(abs_level, 9)];
485 static int estimate_slice_plane(ProresContext *ctx, int *error, int plane,
486 const uint16_t *src, int linesize,
488 int blocks_per_mb, int plane_size_factor,
491 int blocks_per_slice;
494 blocks_per_slice = mbs_per_slice * blocks_per_mb;
496 bits = estimate_dcs(error, ctx->blocks[plane], blocks_per_slice, qmat[0]);
497 bits += estimate_acs(error, ctx->blocks[plane], blocks_per_slice,
498 plane_size_factor, ctx->scantable.permutated, qmat);
500 return FFALIGN(bits, 8);
503 static int find_slice_quant(AVCodecContext *avctx, const AVFrame *pic,
504 int trellis_node, int x, int y, int mbs_per_slice)
506 ProresContext *ctx = avctx->priv_data;
507 int i, q, pq, xp, yp;
509 int slice_width_factor = av_log2(mbs_per_slice);
510 int num_cblocks[MAX_PLANES], pwidth;
511 int plane_factor[MAX_PLANES], is_chroma[MAX_PLANES];
512 const int min_quant = ctx->profile_info->min_quant;
513 const int max_quant = ctx->profile_info->max_quant;
514 int error, bits, bits_limit;
515 int mbs, prev, cur, new_score;
516 int slice_bits[TRELLIS_WIDTH], slice_score[TRELLIS_WIDTH];
520 mbs = x + mbs_per_slice;
522 for (i = 0; i < ctx->num_planes; i++) {
523 is_chroma[i] = (i == 1 || i == 2);
524 plane_factor[i] = slice_width_factor + 2;
526 plane_factor[i] += ctx->chroma_factor - 3;
527 if (!is_chroma[i] || ctx->chroma_factor == CFACTOR_Y444) {
531 pwidth = avctx->width;
536 pwidth = avctx->width >> 1;
538 src = (const uint16_t*)(pic->data[i] + yp * pic->linesize[i]) + xp;
540 get_slice_data(ctx, src, pic->linesize[i], xp, yp,
541 pwidth, avctx->height, ctx->blocks[i],
542 mbs_per_slice, num_cblocks[i]);
545 for (q = min_quant; q < max_quant + 2; q++) {
546 ctx->nodes[trellis_node + q].prev_node = -1;
547 ctx->nodes[trellis_node + q].quant = q;
550 // todo: maybe perform coarser quantising to fit into frame size when needed
551 for (q = min_quant; q <= max_quant; q++) {
554 for (i = 0; i < ctx->num_planes; i++) {
555 bits += estimate_slice_plane(ctx, &error, i,
556 src, pic->linesize[i],
558 num_cblocks[i], plane_factor[i],
561 if (bits > 65000 * 8) {
565 slice_bits[q] = bits;
566 slice_score[q] = error;
568 if (slice_bits[max_quant] <= ctx->bits_per_mb * mbs_per_slice) {
569 slice_bits[max_quant + 1] = slice_bits[max_quant];
570 slice_score[max_quant + 1] = slice_score[max_quant] + 1;
571 overquant = max_quant;
573 for (q = max_quant + 1; q < 128; q++) {
576 if (q < MAX_STORED_Q) {
577 qmat = ctx->quants[q];
579 qmat = ctx->custom_q;
580 for (i = 0; i < 64; i++)
581 qmat[i] = ctx->profile_info->quant[i] * q;
583 for (i = 0; i < ctx->num_planes; i++) {
584 bits += estimate_slice_plane(ctx, &error, i,
585 src, pic->linesize[i],
587 num_cblocks[i], plane_factor[i],
590 if (bits <= ctx->bits_per_mb * mbs_per_slice)
594 slice_bits[max_quant + 1] = bits;
595 slice_score[max_quant + 1] = error;
598 ctx->nodes[trellis_node + max_quant + 1].quant = overquant;
600 bits_limit = mbs * ctx->bits_per_mb;
601 for (pq = min_quant; pq < max_quant + 2; pq++) {
602 prev = trellis_node - TRELLIS_WIDTH + pq;
604 for (q = min_quant; q < max_quant + 2; q++) {
605 cur = trellis_node + q;
607 bits = ctx->nodes[prev].bits + slice_bits[q];
608 error = slice_score[q];
609 if (bits > bits_limit)
612 if (ctx->nodes[prev].score < SCORE_LIMIT && error < SCORE_LIMIT)
613 new_score = ctx->nodes[prev].score + error;
615 new_score = SCORE_LIMIT;
616 if (ctx->nodes[cur].prev_node == -1 ||
617 ctx->nodes[cur].score >= new_score) {
619 ctx->nodes[cur].bits = bits;
620 ctx->nodes[cur].score = new_score;
621 ctx->nodes[cur].prev_node = prev;
626 error = ctx->nodes[trellis_node + min_quant].score;
627 pq = trellis_node + min_quant;
628 for (q = min_quant + 1; q < max_quant + 2; q++) {
629 if (ctx->nodes[trellis_node + q].score <= error) {
630 error = ctx->nodes[trellis_node + q].score;
631 pq = trellis_node + q;
638 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
639 const AVFrame *pic, int *got_packet)
641 ProresContext *ctx = avctx->priv_data;
642 uint8_t *orig_buf, *buf, *slice_hdr, *slice_sizes, *tmp;
643 uint8_t *picture_size_pos;
645 int x, y, i, mb, q = 0;
646 int sizes[4] = { 0 };
647 int slice_hdr_size = 2 + 2 * (ctx->num_planes - 1);
648 int frame_size, picture_size, slice_size;
649 int mbs_per_slice = ctx->mbs_per_slice;
652 *avctx->coded_frame = *pic;
653 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
654 avctx->coded_frame->key_frame = 1;
656 pkt_size = ctx->frame_size + FF_MIN_BUFFER_SIZE;
658 if ((ret = ff_alloc_packet(pkt, pkt_size)) < 0) {
659 av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
663 orig_buf = pkt->data;
666 orig_buf += 4; // frame size
667 bytestream_put_be32 (&orig_buf, FRAME_ID); // frame container ID
672 buf += 2; // frame header size will be stored here
673 bytestream_put_be16 (&buf, 0); // version 1
674 bytestream_put_buffer(&buf, "Lavc", 4); // creator
675 bytestream_put_be16 (&buf, avctx->width);
676 bytestream_put_be16 (&buf, avctx->height);
677 bytestream_put_byte (&buf, ctx->chroma_factor << 6); // frame flags
678 bytestream_put_byte (&buf, 0); // reserved
679 bytestream_put_byte (&buf, 0); // primaries
680 bytestream_put_byte (&buf, 0); // transfer function
681 bytestream_put_byte (&buf, 6); // colour matrix - ITU-R BT.601-4
682 bytestream_put_byte (&buf, 0x40); // source format and alpha information
683 bytestream_put_byte (&buf, 0); // reserved
684 bytestream_put_byte (&buf, 0x03); // matrix flags - both matrices are present
685 // luma quantisation matrix
686 for (i = 0; i < 64; i++)
687 bytestream_put_byte(&buf, ctx->profile_info->quant[i]);
688 // chroma quantisation matrix
689 for (i = 0; i < 64; i++)
690 bytestream_put_byte(&buf, ctx->profile_info->quant[i]);
691 bytestream_put_be16 (&tmp, buf - orig_buf); // write back frame header size
694 picture_size_pos = buf + 1;
695 bytestream_put_byte (&buf, 0x40); // picture header size (in bits)
696 buf += 4; // picture data size will be stored here
697 bytestream_put_be16 (&buf, ctx->num_slices); // total number of slices
698 bytestream_put_byte (&buf, av_log2(ctx->mbs_per_slice) << 4); // slice width and height in MBs
700 // seek table - will be filled during slice encoding
702 buf += ctx->num_slices * 2;
705 for (y = 0; y < ctx->mb_height; y++) {
706 mbs_per_slice = ctx->mbs_per_slice;
707 for (x = mb = 0; x < ctx->mb_width; x += mbs_per_slice, mb++) {
708 while (ctx->mb_width - x < mbs_per_slice)
710 q = find_slice_quant(avctx, pic, (mb + 1) * TRELLIS_WIDTH, x, y,
714 for (x = ctx->slices_width - 1; x >= 0; x--) {
715 ctx->slice_q[x] = ctx->nodes[q].quant;
716 q = ctx->nodes[q].prev_node;
719 mbs_per_slice = ctx->mbs_per_slice;
720 for (x = mb = 0; x < ctx->mb_width; x += mbs_per_slice, mb++) {
721 q = ctx->slice_q[mb];
723 while (ctx->mb_width - x < mbs_per_slice)
726 bytestream_put_byte(&buf, slice_hdr_size << 3);
728 buf += slice_hdr_size - 1;
729 init_put_bits(&pb, buf, (pkt_size - (buf - orig_buf)) * 8);
730 encode_slice(avctx, pic, &pb, sizes, x, y, q, mbs_per_slice);
732 bytestream_put_byte(&slice_hdr, q);
733 slice_size = slice_hdr_size + sizes[ctx->num_planes - 1];
734 for (i = 0; i < ctx->num_planes - 1; i++) {
735 bytestream_put_be16(&slice_hdr, sizes[i]);
736 slice_size += sizes[i];
738 bytestream_put_be16(&slice_sizes, slice_size);
739 buf += slice_size - slice_hdr_size;
744 frame_size = buf - orig_buf;
745 picture_size = buf - picture_size_pos - 6;
746 bytestream_put_be32(&orig_buf, frame_size);
747 bytestream_put_be32(&picture_size_pos, picture_size);
749 pkt->size = frame_size;
750 pkt->flags |= AV_PKT_FLAG_KEY;
756 static av_cold int encode_close(AVCodecContext *avctx)
758 ProresContext *ctx = avctx->priv_data;
760 if (avctx->coded_frame->data[0])
761 avctx->release_buffer(avctx, avctx->coded_frame);
763 av_freep(&avctx->coded_frame);
765 av_freep(&ctx->nodes);
766 av_freep(&ctx->slice_q);
771 static av_cold int encode_init(AVCodecContext *avctx)
773 ProresContext *ctx = avctx->priv_data;
776 int min_quant, max_quant;
778 avctx->bits_per_raw_sample = 10;
779 avctx->coded_frame = avcodec_alloc_frame();
780 if (!avctx->coded_frame)
781 return AVERROR(ENOMEM);
783 ff_proresdsp_init(&ctx->dsp);
784 ff_init_scantable(ctx->dsp.dct_permutation, &ctx->scantable,
785 ff_prores_progressive_scan);
787 mps = ctx->mbs_per_slice;
788 if (mps & (mps - 1)) {
789 av_log(avctx, AV_LOG_ERROR,
790 "there should be an integer power of two MBs per slice\n");
791 return AVERROR(EINVAL);
794 ctx->chroma_factor = avctx->pix_fmt == PIX_FMT_YUV422P10
797 ctx->profile_info = prores_profile_info + ctx->profile;
800 ctx->mb_width = FFALIGN(avctx->width, 16) >> 4;
801 ctx->mb_height = FFALIGN(avctx->height, 16) >> 4;
802 ctx->slices_width = ctx->mb_width / mps;
803 ctx->slices_width += av_popcount(ctx->mb_width - ctx->slices_width * mps);
804 ctx->num_slices = ctx->mb_height * ctx->slices_width;
806 for (i = 0; i < NUM_MB_LIMITS - 1; i++)
807 if (prores_mb_limits[i] >= ctx->mb_width * ctx->mb_height)
809 ctx->bits_per_mb = ctx->profile_info->br_tab[i];
811 ctx->frame_size = ctx->num_slices * (2 + 2 * ctx->num_planes
812 + (2 * mps * ctx->bits_per_mb) / 8)
815 min_quant = ctx->profile_info->min_quant;
816 max_quant = ctx->profile_info->max_quant;
817 for (i = min_quant; i < MAX_STORED_Q; i++) {
818 for (j = 0; j < 64; j++)
819 ctx->quants[i][j] = ctx->profile_info->quant[j] * i;
822 avctx->codec_tag = ctx->profile_info->tag;
824 av_log(avctx, AV_LOG_DEBUG, "profile %d, %d slices, %d bits per MB\n",
825 ctx->profile, ctx->num_slices, ctx->bits_per_mb);
826 av_log(avctx, AV_LOG_DEBUG, "estimated frame size %d\n",
829 ctx->nodes = av_malloc((ctx->slices_width + 1) * TRELLIS_WIDTH
830 * sizeof(*ctx->nodes));
833 return AVERROR(ENOMEM);
835 for (i = min_quant; i < max_quant + 2; i++) {
836 ctx->nodes[i].prev_node = -1;
837 ctx->nodes[i].bits = 0;
838 ctx->nodes[i].score = 0;
841 ctx->slice_q = av_malloc(ctx->slices_width * sizeof(*ctx->slice_q));
844 return AVERROR(ENOMEM);
850 #define OFFSET(x) offsetof(ProresContext, x)
851 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
853 static const AVOption options[] = {
854 { "mbs_per_slice", "macroblocks per slice", OFFSET(mbs_per_slice),
855 AV_OPT_TYPE_INT, { 8 }, 1, MAX_MBS_PER_SLICE, VE },
856 { "profile", NULL, OFFSET(profile), AV_OPT_TYPE_INT,
857 { PRORES_PROFILE_STANDARD },
858 PRORES_PROFILE_PROXY, PRORES_PROFILE_HQ, VE, "profile" },
859 { "proxy", NULL, 0, AV_OPT_TYPE_CONST, { PRORES_PROFILE_PROXY },
860 0, 0, VE, "profile" },
861 { "lt", NULL, 0, AV_OPT_TYPE_CONST, { PRORES_PROFILE_LT },
862 0, 0, VE, "profile" },
863 { "standard", NULL, 0, AV_OPT_TYPE_CONST, { PRORES_PROFILE_STANDARD },
864 0, 0, VE, "profile" },
865 { "hq", NULL, 0, AV_OPT_TYPE_CONST, { PRORES_PROFILE_HQ },
866 0, 0, VE, "profile" },
870 static const AVClass proresenc_class = {
871 .class_name = "ProRes encoder",
872 .item_name = av_default_item_name,
874 .version = LIBAVUTIL_VERSION_INT,
877 AVCodec ff_prores_encoder = {
879 .type = AVMEDIA_TYPE_VIDEO,
880 .id = CODEC_ID_PRORES,
881 .priv_data_size = sizeof(ProresContext),
883 .close = encode_close,
884 .encode2 = encode_frame,
885 .long_name = NULL_IF_CONFIG_SMALL("Apple ProRes (iCodec Pro)"),
886 .pix_fmts = (const enum PixelFormat[]) {
887 PIX_FMT_YUV422P10, PIX_FMT_YUV444P10, PIX_FMT_NONE
889 .priv_class = &proresenc_class,