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,
53 static const uint8_t prores_quant_matrices[][64] = {
55 4, 7, 9, 11, 13, 14, 15, 63,
56 7, 7, 11, 12, 14, 15, 63, 63,
57 9, 11, 13, 14, 15, 63, 63, 63,
58 11, 11, 13, 14, 63, 63, 63, 63,
59 11, 13, 14, 63, 63, 63, 63, 63,
60 13, 14, 63, 63, 63, 63, 63, 63,
61 13, 63, 63, 63, 63, 63, 63, 63,
62 63, 63, 63, 63, 63, 63, 63, 63,
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,
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,
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, 4,
88 4, 4, 4, 4, 4, 4, 4, 5,
89 4, 4, 4, 4, 4, 4, 5, 5,
90 4, 4, 4, 4, 4, 5, 5, 6,
91 4, 4, 4, 4, 5, 5, 6, 7,
92 4, 4, 4, 4, 5, 6, 7, 7,
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, 4,
99 4, 4, 4, 4, 4, 4, 4, 4,
100 4, 4, 4, 4, 4, 4, 4, 4,
101 4, 4, 4, 4, 4, 4, 4, 4,
102 4, 4, 4, 4, 4, 4, 4, 4,
106 #define NUM_MB_LIMITS 4
107 static const int prores_mb_limits[NUM_MB_LIMITS] = {
108 1620, // up to 720x576
109 2700, // up to 960x720
110 6075, // up to 1440x1080
111 9216, // up to 2048x1152
114 static const struct prores_profile {
115 const char *full_name;
119 int br_tab[NUM_MB_LIMITS];
121 } prores_profile_info[4] = {
123 .full_name = "proxy",
124 .tag = MKTAG('a', 'p', 'c', 'o'),
127 .br_tab = { 300, 242, 220, 194 },
128 .quant = QUANT_MAT_PROXY,
132 .tag = MKTAG('a', 'p', 'c', 's'),
135 .br_tab = { 720, 560, 490, 440 },
136 .quant = QUANT_MAT_LT,
139 .full_name = "standard",
140 .tag = MKTAG('a', 'p', 'c', 'n'),
143 .br_tab = { 1050, 808, 710, 632 },
144 .quant = QUANT_MAT_STANDARD,
147 .full_name = "high quality",
148 .tag = MKTAG('a', 'p', 'c', 'h'),
151 .br_tab = { 1566, 1216, 1070, 950 },
152 .quant = QUANT_MAT_HQ,
154 // for 4444 profile bitrate numbers are { 2350, 1828, 1600, 1425 }
157 #define TRELLIS_WIDTH 16
158 #define SCORE_LIMIT INT_MAX / 2
167 #define MAX_STORED_Q 16
169 typedef struct ProresContext {
171 DECLARE_ALIGNED(16, DCTELEM, blocks)[MAX_PLANES][64 * 4 * MAX_MBS_PER_SLICE];
172 DECLARE_ALIGNED(16, uint16_t, emu_buf)[16*16];
173 int16_t quants[MAX_STORED_Q][64];
174 int16_t custom_q[64];
175 const uint8_t *quant_mat;
177 ProresDSPContext dsp;
180 int mb_width, mb_height;
182 int num_chroma_blocks, chroma_factor;
194 const struct prores_profile *profile_info;
196 struct TrellisNode *nodes;
200 static void get_slice_data(ProresContext *ctx, const uint16_t *src,
201 int linesize, int x, int y, int w, int h,
203 int mbs_per_slice, int blocks_per_mb, int is_chroma)
205 const uint16_t *esrc;
206 const int mb_width = 4 * blocks_per_mb;
210 for (i = 0; i < mbs_per_slice; i++, src += mb_width) {
212 memset(blocks, 0, 64 * (mbs_per_slice - i) * blocks_per_mb
216 if (x + mb_width <= w && y + 16 <= h) {
218 elinesize = linesize;
223 elinesize = 16 * sizeof(*ctx->emu_buf);
225 bw = FFMIN(w - x, mb_width);
226 bh = FFMIN(h - y, 16);
228 for (j = 0; j < bh; j++) {
229 memcpy(ctx->emu_buf + j * 16,
230 (const uint8_t*)src + j * linesize,
232 pix = ctx->emu_buf[j * 16 + bw - 1];
233 for (k = bw; k < mb_width; k++)
234 ctx->emu_buf[j * 16 + k] = pix;
237 memcpy(ctx->emu_buf + j * 16,
238 ctx->emu_buf + (bh - 1) * 16,
239 mb_width * sizeof(*ctx->emu_buf));
242 ctx->dsp.fdct(esrc, elinesize, blocks);
244 if (blocks_per_mb > 2) {
245 ctx->dsp.fdct(src + 8, linesize, blocks);
248 ctx->dsp.fdct(src + linesize * 4, linesize, blocks);
250 if (blocks_per_mb > 2) {
251 ctx->dsp.fdct(src + linesize * 4 + 8, linesize, blocks);
255 ctx->dsp.fdct(esrc, elinesize, blocks);
257 ctx->dsp.fdct(src + linesize * 4, linesize, blocks);
259 if (blocks_per_mb > 2) {
260 ctx->dsp.fdct(src + 8, linesize, blocks);
262 ctx->dsp.fdct(src + linesize * 4 + 8, linesize, blocks);
272 * Write an unsigned rice/exp golomb codeword.
274 static inline void encode_vlc_codeword(PutBitContext *pb, unsigned codebook, int val)
276 unsigned int rice_order, exp_order, switch_bits, switch_val;
279 /* number of prefix bits to switch between Rice and expGolomb */
280 switch_bits = (codebook & 3) + 1;
281 rice_order = codebook >> 5; /* rice code order */
282 exp_order = (codebook >> 2) & 7; /* exp golomb code order */
284 switch_val = switch_bits << rice_order;
286 if (val >= switch_val) {
287 val -= switch_val - (1 << exp_order);
288 exponent = av_log2(val);
290 put_bits(pb, exponent - exp_order + switch_bits, 0);
292 put_bits(pb, exponent, val);
294 exponent = val >> rice_order;
297 put_bits(pb, exponent, 0);
300 put_sbits(pb, rice_order, val);
304 #define GET_SIGN(x) ((x) >> 31)
305 #define MAKE_CODE(x) (((x) << 1) ^ GET_SIGN(x))
307 static void encode_dcs(PutBitContext *pb, DCTELEM *blocks,
308 int blocks_per_slice, int scale)
311 int codebook = 3, code, dc, prev_dc, delta, sign, new_sign;
313 prev_dc = (blocks[0] - 0x4000) / scale;
314 encode_vlc_codeword(pb, FIRST_DC_CB, MAKE_CODE(prev_dc));
319 for (i = 1; i < blocks_per_slice; i++, blocks += 64) {
320 dc = (blocks[0] - 0x4000) / scale;
321 delta = dc - prev_dc;
322 new_sign = GET_SIGN(delta);
323 delta = (delta ^ sign) - sign;
324 code = MAKE_CODE(delta);
325 encode_vlc_codeword(pb, ff_prores_dc_codebook[codebook], code);
326 codebook = (code + (code & 1)) >> 1;
327 codebook = FFMIN(codebook, 3);
333 static void encode_acs(PutBitContext *pb, DCTELEM *blocks,
334 int blocks_per_slice,
335 int plane_size_factor,
336 const uint8_t *scan, const int16_t *qmat)
339 int run, level, run_cb, lev_cb;
340 int max_coeffs, abs_level;
342 max_coeffs = blocks_per_slice << 6;
343 run_cb = ff_prores_run_to_cb_index[4];
344 lev_cb = ff_prores_lev_to_cb_index[2];
347 for (i = 1; i < 64; i++) {
348 for (idx = scan[i]; idx < max_coeffs; idx += 64) {
349 level = blocks[idx] / qmat[scan[i]];
351 abs_level = FFABS(level);
352 encode_vlc_codeword(pb, ff_prores_ac_codebook[run_cb], run);
353 encode_vlc_codeword(pb, ff_prores_ac_codebook[lev_cb],
355 put_sbits(pb, 1, GET_SIGN(level));
357 run_cb = ff_prores_run_to_cb_index[FFMIN(run, 15)];
358 lev_cb = ff_prores_lev_to_cb_index[FFMIN(abs_level, 9)];
367 static int encode_slice_plane(ProresContext *ctx, PutBitContext *pb,
368 const uint16_t *src, int linesize,
369 int mbs_per_slice, DCTELEM *blocks,
370 int blocks_per_mb, int plane_size_factor,
373 int blocks_per_slice, saved_pos;
375 saved_pos = put_bits_count(pb);
376 blocks_per_slice = mbs_per_slice * blocks_per_mb;
378 encode_dcs(pb, blocks, blocks_per_slice, qmat[0]);
379 encode_acs(pb, blocks, blocks_per_slice, plane_size_factor,
380 ctx->scantable.permutated, qmat);
383 return (put_bits_count(pb) - saved_pos) >> 3;
386 static int encode_slice(AVCodecContext *avctx, const AVFrame *pic,
388 int sizes[4], int x, int y, int quant,
391 ProresContext *ctx = avctx->priv_data;
395 int slice_width_factor = av_log2(mbs_per_slice);
396 int num_cblocks, pwidth;
397 int plane_factor, is_chroma;
400 if (quant < MAX_STORED_Q) {
401 qmat = ctx->quants[quant];
403 qmat = ctx->custom_q;
404 for (i = 0; i < 64; i++)
405 qmat[i] = ctx->quant_mat[i] * quant;
408 for (i = 0; i < ctx->num_planes; i++) {
409 is_chroma = (i == 1 || i == 2);
410 plane_factor = slice_width_factor + 2;
412 plane_factor += ctx->chroma_factor - 3;
413 if (!is_chroma || ctx->chroma_factor == CFACTOR_Y444) {
417 pwidth = avctx->width;
422 pwidth = avctx->width >> 1;
424 src = (const uint16_t*)(pic->data[i] + yp * pic->linesize[i]) + xp;
426 get_slice_data(ctx, src, pic->linesize[i], xp, yp,
427 pwidth, avctx->height, ctx->blocks[0],
428 mbs_per_slice, num_cblocks, is_chroma);
429 sizes[i] = encode_slice_plane(ctx, pb, src, pic->linesize[i],
430 mbs_per_slice, ctx->blocks[0],
431 num_cblocks, plane_factor,
433 total_size += sizes[i];
438 static inline int estimate_vlc(unsigned codebook, int val)
440 unsigned int rice_order, exp_order, switch_bits, switch_val;
443 /* number of prefix bits to switch between Rice and expGolomb */
444 switch_bits = (codebook & 3) + 1;
445 rice_order = codebook >> 5; /* rice code order */
446 exp_order = (codebook >> 2) & 7; /* exp golomb code order */
448 switch_val = switch_bits << rice_order;
450 if (val >= switch_val) {
451 val -= switch_val - (1 << exp_order);
452 exponent = av_log2(val);
454 return exponent * 2 - exp_order + switch_bits + 1;
456 return (val >> rice_order) + rice_order + 1;
460 static int estimate_dcs(int *error, DCTELEM *blocks, int blocks_per_slice,
464 int codebook = 3, code, dc, prev_dc, delta, sign, new_sign;
467 prev_dc = (blocks[0] - 0x4000) / scale;
468 bits = estimate_vlc(FIRST_DC_CB, MAKE_CODE(prev_dc));
472 *error += FFABS(blocks[0] - 0x4000) % scale;
474 for (i = 1; i < blocks_per_slice; i++, blocks += 64) {
475 dc = (blocks[0] - 0x4000) / scale;
476 *error += FFABS(blocks[0] - 0x4000) % scale;
477 delta = dc - prev_dc;
478 new_sign = GET_SIGN(delta);
479 delta = (delta ^ sign) - sign;
480 code = MAKE_CODE(delta);
481 bits += estimate_vlc(ff_prores_dc_codebook[codebook], code);
482 codebook = (code + (code & 1)) >> 1;
483 codebook = FFMIN(codebook, 3);
491 static int estimate_acs(int *error, DCTELEM *blocks, int blocks_per_slice,
492 int plane_size_factor,
493 const uint8_t *scan, const int16_t *qmat)
496 int run, level, run_cb, lev_cb;
497 int max_coeffs, abs_level;
500 max_coeffs = blocks_per_slice << 6;
501 run_cb = ff_prores_run_to_cb_index[4];
502 lev_cb = ff_prores_lev_to_cb_index[2];
505 for (i = 1; i < 64; i++) {
506 for (idx = scan[i]; idx < max_coeffs; idx += 64) {
507 level = blocks[idx] / qmat[scan[i]];
508 *error += FFABS(blocks[idx]) % qmat[scan[i]];
510 abs_level = FFABS(level);
511 bits += estimate_vlc(ff_prores_ac_codebook[run_cb], run);
512 bits += estimate_vlc(ff_prores_ac_codebook[lev_cb],
515 run_cb = ff_prores_run_to_cb_index[FFMIN(run, 15)];
516 lev_cb = ff_prores_lev_to_cb_index[FFMIN(abs_level, 9)];
527 static int estimate_slice_plane(ProresContext *ctx, int *error, int plane,
528 const uint16_t *src, int linesize,
530 int blocks_per_mb, int plane_size_factor,
533 int blocks_per_slice;
536 blocks_per_slice = mbs_per_slice * blocks_per_mb;
538 bits = estimate_dcs(error, ctx->blocks[plane], blocks_per_slice, qmat[0]);
539 bits += estimate_acs(error, ctx->blocks[plane], blocks_per_slice,
540 plane_size_factor, ctx->scantable.permutated, qmat);
542 return FFALIGN(bits, 8);
545 static int find_slice_quant(AVCodecContext *avctx, const AVFrame *pic,
546 int trellis_node, int x, int y, int mbs_per_slice)
548 ProresContext *ctx = avctx->priv_data;
549 int i, q, pq, xp, yp;
551 int slice_width_factor = av_log2(mbs_per_slice);
552 int num_cblocks[MAX_PLANES], pwidth;
553 int plane_factor[MAX_PLANES], is_chroma[MAX_PLANES];
554 const int min_quant = ctx->profile_info->min_quant;
555 const int max_quant = ctx->profile_info->max_quant;
556 int error, bits, bits_limit;
557 int mbs, prev, cur, new_score;
558 int slice_bits[TRELLIS_WIDTH], slice_score[TRELLIS_WIDTH];
562 mbs = x + mbs_per_slice;
564 for (i = 0; i < ctx->num_planes; i++) {
565 is_chroma[i] = (i == 1 || i == 2);
566 plane_factor[i] = slice_width_factor + 2;
568 plane_factor[i] += ctx->chroma_factor - 3;
569 if (!is_chroma[i] || ctx->chroma_factor == CFACTOR_Y444) {
573 pwidth = avctx->width;
578 pwidth = avctx->width >> 1;
580 src = (const uint16_t*)(pic->data[i] + yp * pic->linesize[i]) + xp;
582 get_slice_data(ctx, src, pic->linesize[i], xp, yp,
583 pwidth, avctx->height, ctx->blocks[i],
584 mbs_per_slice, num_cblocks[i], is_chroma[i]);
587 for (q = min_quant; q < max_quant + 2; q++) {
588 ctx->nodes[trellis_node + q].prev_node = -1;
589 ctx->nodes[trellis_node + q].quant = q;
592 // todo: maybe perform coarser quantising to fit into frame size when needed
593 for (q = min_quant; q <= max_quant; q++) {
596 for (i = 0; i < ctx->num_planes; i++) {
597 bits += estimate_slice_plane(ctx, &error, i,
598 src, pic->linesize[i],
600 num_cblocks[i], plane_factor[i],
603 if (bits > 65000 * 8) {
607 slice_bits[q] = bits;
608 slice_score[q] = error;
610 if (slice_bits[max_quant] <= ctx->bits_per_mb * mbs_per_slice) {
611 slice_bits[max_quant + 1] = slice_bits[max_quant];
612 slice_score[max_quant + 1] = slice_score[max_quant] + 1;
613 overquant = max_quant;
615 for (q = max_quant + 1; q < 128; q++) {
618 if (q < MAX_STORED_Q) {
619 qmat = ctx->quants[q];
621 qmat = ctx->custom_q;
622 for (i = 0; i < 64; i++)
623 qmat[i] = ctx->quant_mat[i] * q;
625 for (i = 0; i < ctx->num_planes; i++) {
626 bits += estimate_slice_plane(ctx, &error, i,
627 src, pic->linesize[i],
629 num_cblocks[i], plane_factor[i],
632 if (bits <= ctx->bits_per_mb * mbs_per_slice)
636 slice_bits[max_quant + 1] = bits;
637 slice_score[max_quant + 1] = error;
640 ctx->nodes[trellis_node + max_quant + 1].quant = overquant;
642 bits_limit = mbs * ctx->bits_per_mb;
643 for (pq = min_quant; pq < max_quant + 2; pq++) {
644 prev = trellis_node - TRELLIS_WIDTH + pq;
646 for (q = min_quant; q < max_quant + 2; q++) {
647 cur = trellis_node + q;
649 bits = ctx->nodes[prev].bits + slice_bits[q];
650 error = slice_score[q];
651 if (bits > bits_limit)
654 if (ctx->nodes[prev].score < SCORE_LIMIT && error < SCORE_LIMIT)
655 new_score = ctx->nodes[prev].score + error;
657 new_score = SCORE_LIMIT;
658 if (ctx->nodes[cur].prev_node == -1 ||
659 ctx->nodes[cur].score >= new_score) {
661 ctx->nodes[cur].bits = bits;
662 ctx->nodes[cur].score = new_score;
663 ctx->nodes[cur].prev_node = prev;
668 error = ctx->nodes[trellis_node + min_quant].score;
669 pq = trellis_node + min_quant;
670 for (q = min_quant + 1; q < max_quant + 2; q++) {
671 if (ctx->nodes[trellis_node + q].score <= error) {
672 error = ctx->nodes[trellis_node + q].score;
673 pq = trellis_node + q;
680 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
681 const AVFrame *pic, int *got_packet)
683 ProresContext *ctx = avctx->priv_data;
684 uint8_t *orig_buf, *buf, *slice_hdr, *slice_sizes, *tmp;
685 uint8_t *picture_size_pos;
687 int x, y, i, mb, q = 0;
688 int sizes[4] = { 0 };
689 int slice_hdr_size = 2 + 2 * (ctx->num_planes - 1);
690 int frame_size, picture_size, slice_size;
691 int mbs_per_slice = ctx->mbs_per_slice;
694 *avctx->coded_frame = *pic;
695 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
696 avctx->coded_frame->key_frame = 1;
698 pkt_size = ctx->frame_size + FF_MIN_BUFFER_SIZE;
700 if ((ret = ff_alloc_packet(pkt, pkt_size)) < 0) {
701 av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
705 orig_buf = pkt->data;
708 orig_buf += 4; // frame size
709 bytestream_put_be32 (&orig_buf, FRAME_ID); // frame container ID
714 buf += 2; // frame header size will be stored here
715 bytestream_put_be16 (&buf, 0); // version 1
716 bytestream_put_buffer(&buf, ctx->vendor, 4);
717 bytestream_put_be16 (&buf, avctx->width);
718 bytestream_put_be16 (&buf, avctx->height);
719 bytestream_put_byte (&buf, ctx->chroma_factor << 6); // frame flags
720 bytestream_put_byte (&buf, 0); // reserved
721 bytestream_put_byte (&buf, avctx->color_primaries);
722 bytestream_put_byte (&buf, avctx->color_trc);
723 bytestream_put_byte (&buf, avctx->colorspace);
724 bytestream_put_byte (&buf, 0x40); // source format and alpha information
725 bytestream_put_byte (&buf, 0); // reserved
726 if (ctx->quant_sel != QUANT_MAT_DEFAULT) {
727 bytestream_put_byte (&buf, 0x03); // matrix flags - both matrices are present
728 // luma quantisation matrix
729 for (i = 0; i < 64; i++)
730 bytestream_put_byte(&buf, ctx->quant_mat[i]);
731 // chroma quantisation matrix
732 for (i = 0; i < 64; i++)
733 bytestream_put_byte(&buf, ctx->quant_mat[i]);
735 bytestream_put_byte (&buf, 0x00); // matrix flags - default matrices are used
737 bytestream_put_be16 (&tmp, buf - orig_buf); // write back frame header size
740 picture_size_pos = buf + 1;
741 bytestream_put_byte (&buf, 0x40); // picture header size (in bits)
742 buf += 4; // picture data size will be stored here
743 bytestream_put_be16 (&buf, ctx->num_slices); // total number of slices
744 bytestream_put_byte (&buf, av_log2(ctx->mbs_per_slice) << 4); // slice width and height in MBs
746 // seek table - will be filled during slice encoding
748 buf += ctx->num_slices * 2;
751 for (y = 0; y < ctx->mb_height; y++) {
752 mbs_per_slice = ctx->mbs_per_slice;
753 for (x = mb = 0; x < ctx->mb_width; x += mbs_per_slice, mb++) {
754 while (ctx->mb_width - x < mbs_per_slice)
756 q = find_slice_quant(avctx, pic, (mb + 1) * TRELLIS_WIDTH, x, y,
760 for (x = ctx->slices_width - 1; x >= 0; x--) {
761 ctx->slice_q[x] = ctx->nodes[q].quant;
762 q = ctx->nodes[q].prev_node;
765 mbs_per_slice = ctx->mbs_per_slice;
766 for (x = mb = 0; x < ctx->mb_width; x += mbs_per_slice, mb++) {
767 q = ctx->slice_q[mb];
769 while (ctx->mb_width - x < mbs_per_slice)
772 bytestream_put_byte(&buf, slice_hdr_size << 3);
774 buf += slice_hdr_size - 1;
775 init_put_bits(&pb, buf, (pkt_size - (buf - orig_buf)) * 8);
776 encode_slice(avctx, pic, &pb, sizes, x, y, q, mbs_per_slice);
778 bytestream_put_byte(&slice_hdr, q);
779 slice_size = slice_hdr_size + sizes[ctx->num_planes - 1];
780 for (i = 0; i < ctx->num_planes - 1; i++) {
781 bytestream_put_be16(&slice_hdr, sizes[i]);
782 slice_size += sizes[i];
784 bytestream_put_be16(&slice_sizes, slice_size);
785 buf += slice_size - slice_hdr_size;
790 frame_size = buf - orig_buf;
791 picture_size = buf - picture_size_pos - 6;
792 bytestream_put_be32(&orig_buf, frame_size);
793 bytestream_put_be32(&picture_size_pos, picture_size);
795 pkt->size = frame_size;
796 pkt->flags |= AV_PKT_FLAG_KEY;
802 static av_cold int encode_close(AVCodecContext *avctx)
804 ProresContext *ctx = avctx->priv_data;
806 if (avctx->coded_frame->data[0])
807 avctx->release_buffer(avctx, avctx->coded_frame);
809 av_freep(&avctx->coded_frame);
811 av_freep(&ctx->nodes);
812 av_freep(&ctx->slice_q);
817 static av_cold int encode_init(AVCodecContext *avctx)
819 ProresContext *ctx = avctx->priv_data;
822 int min_quant, max_quant;
824 avctx->bits_per_raw_sample = 10;
825 avctx->coded_frame = avcodec_alloc_frame();
826 if (!avctx->coded_frame)
827 return AVERROR(ENOMEM);
829 ff_proresdsp_init(&ctx->dsp, avctx);
830 ff_init_scantable(ctx->dsp.dct_permutation, &ctx->scantable,
831 ff_prores_progressive_scan);
833 mps = ctx->mbs_per_slice;
834 if (mps & (mps - 1)) {
835 av_log(avctx, AV_LOG_ERROR,
836 "there should be an integer power of two MBs per slice\n");
837 return AVERROR(EINVAL);
840 ctx->chroma_factor = avctx->pix_fmt == PIX_FMT_YUV422P10
843 ctx->profile_info = prores_profile_info + ctx->profile;
846 ctx->mb_width = FFALIGN(avctx->width, 16) >> 4;
847 ctx->mb_height = FFALIGN(avctx->height, 16) >> 4;
848 ctx->slices_width = ctx->mb_width / mps;
849 ctx->slices_width += av_popcount(ctx->mb_width - ctx->slices_width * mps);
850 ctx->num_slices = ctx->mb_height * ctx->slices_width;
852 if (ctx->quant_sel == -1)
853 ctx->quant_mat = prores_quant_matrices[ctx->profile_info->quant];
855 ctx->quant_mat = prores_quant_matrices[ctx->quant_sel];
857 if (strlen(ctx->vendor) != 4) {
858 av_log(avctx, AV_LOG_ERROR, "vendor ID should be 4 bytes\n");
859 return AVERROR_INVALIDDATA;
862 if (!ctx->bits_per_mb) {
863 for (i = 0; i < NUM_MB_LIMITS - 1; i++)
864 if (prores_mb_limits[i] >= ctx->mb_width * ctx->mb_height)
866 ctx->bits_per_mb = ctx->profile_info->br_tab[i];
867 } else if (ctx->bits_per_mb < 128) {
868 av_log(avctx, AV_LOG_ERROR, "too few bits per MB, please set at least 128\n");
869 return AVERROR_INVALIDDATA;
872 ctx->frame_size = ctx->num_slices * (2 + 2 * ctx->num_planes
873 + (2 * mps * ctx->bits_per_mb) / 8)
876 min_quant = ctx->profile_info->min_quant;
877 max_quant = ctx->profile_info->max_quant;
878 for (i = min_quant; i < MAX_STORED_Q; i++) {
879 for (j = 0; j < 64; j++)
880 ctx->quants[i][j] = ctx->quant_mat[j] * i;
883 avctx->codec_tag = ctx->profile_info->tag;
885 av_log(avctx, AV_LOG_DEBUG, "profile %d, %d slices, %d bits per MB\n",
886 ctx->profile, ctx->num_slices, ctx->bits_per_mb);
887 av_log(avctx, AV_LOG_DEBUG, "estimated frame size %d\n",
890 ctx->nodes = av_malloc((ctx->slices_width + 1) * TRELLIS_WIDTH
891 * sizeof(*ctx->nodes));
894 return AVERROR(ENOMEM);
896 for (i = min_quant; i < max_quant + 2; i++) {
897 ctx->nodes[i].prev_node = -1;
898 ctx->nodes[i].bits = 0;
899 ctx->nodes[i].score = 0;
902 ctx->slice_q = av_malloc(ctx->slices_width * sizeof(*ctx->slice_q));
905 return AVERROR(ENOMEM);
911 #define OFFSET(x) offsetof(ProresContext, x)
912 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
914 static const AVOption options[] = {
915 { "mbs_per_slice", "macroblocks per slice", OFFSET(mbs_per_slice),
916 AV_OPT_TYPE_INT, { 8 }, 1, MAX_MBS_PER_SLICE, VE },
917 { "profile", NULL, OFFSET(profile), AV_OPT_TYPE_INT,
918 { PRORES_PROFILE_STANDARD },
919 PRORES_PROFILE_PROXY, PRORES_PROFILE_HQ, VE, "profile" },
920 { "proxy", NULL, 0, AV_OPT_TYPE_CONST, { PRORES_PROFILE_PROXY },
921 0, 0, VE, "profile" },
922 { "lt", NULL, 0, AV_OPT_TYPE_CONST, { PRORES_PROFILE_LT },
923 0, 0, VE, "profile" },
924 { "standard", NULL, 0, AV_OPT_TYPE_CONST, { PRORES_PROFILE_STANDARD },
925 0, 0, VE, "profile" },
926 { "hq", NULL, 0, AV_OPT_TYPE_CONST, { PRORES_PROFILE_HQ },
927 0, 0, VE, "profile" },
928 { "vendor", "vendor ID", OFFSET(vendor),
929 AV_OPT_TYPE_STRING, { .str = "Lavc" }, CHAR_MIN, CHAR_MAX, VE },
930 { "bits_per_mb", "desired bits per macroblock", OFFSET(bits_per_mb),
931 AV_OPT_TYPE_INT, { 0 }, 0, 8192, VE },
932 { "quant_mat", "quantiser matrix", OFFSET(quant_sel), AV_OPT_TYPE_INT,
933 { -1 }, -1, QUANT_MAT_DEFAULT, VE, "quant_mat" },
934 { "auto", NULL, 0, AV_OPT_TYPE_CONST, { -1 },
935 0, 0, VE, "quant_mat" },
936 { "proxy", NULL, 0, AV_OPT_TYPE_CONST, { QUANT_MAT_PROXY },
937 0, 0, VE, "quant_mat" },
938 { "lt", NULL, 0, AV_OPT_TYPE_CONST, { QUANT_MAT_LT },
939 0, 0, VE, "quant_mat" },
940 { "standard", NULL, 0, AV_OPT_TYPE_CONST, { QUANT_MAT_STANDARD },
941 0, 0, VE, "quant_mat" },
942 { "hq", NULL, 0, AV_OPT_TYPE_CONST, { QUANT_MAT_HQ },
943 0, 0, VE, "quant_mat" },
944 { "default", NULL, 0, AV_OPT_TYPE_CONST, { QUANT_MAT_DEFAULT },
945 0, 0, VE, "quant_mat" },
949 static const AVClass proresenc_class = {
950 .class_name = "ProRes encoder",
951 .item_name = av_default_item_name,
953 .version = LIBAVUTIL_VERSION_INT,
956 AVCodec ff_prores_kostya_encoder = {
957 .name = "prores_kostya",
958 .type = AVMEDIA_TYPE_VIDEO,
959 .id = CODEC_ID_PRORES,
960 .priv_data_size = sizeof(ProresContext),
962 .close = encode_close,
963 .encode2 = encode_frame,
964 .long_name = NULL_IF_CONFIG_SMALL("Apple ProRes (iCodec Pro)"),
965 .pix_fmts = (const enum PixelFormat[]) {
966 PIX_FMT_YUV422P10, PIX_FMT_YUV444P10, PIX_FMT_NONE
968 .priv_class = &proresenc_class,