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, int is_chroma)
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;
194 elinesize = 16 * sizeof(*ctx->emu_buf);
196 bw = FFMIN(w - x, mb_width);
197 bh = FFMIN(h - y, 16);
199 for (j = 0; j < bh; j++) {
200 memcpy(ctx->emu_buf + j * 16,
201 (const uint8_t*)src + j * linesize,
203 pix = ctx->emu_buf[j * 16 + bw - 1];
204 for (k = bw; k < mb_width; k++)
205 ctx->emu_buf[j * 16 + k] = pix;
208 memcpy(ctx->emu_buf + j * 16,
209 ctx->emu_buf + (bh - 1) * 16,
210 mb_width * sizeof(*ctx->emu_buf));
213 ctx->dsp.fdct(esrc, elinesize, blocks);
215 if (blocks_per_mb > 2) {
216 ctx->dsp.fdct(src + 8, linesize, blocks);
219 ctx->dsp.fdct(src + linesize * 4, linesize, blocks);
221 if (blocks_per_mb > 2) {
222 ctx->dsp.fdct(src + linesize * 4 + 8, linesize, blocks);
226 ctx->dsp.fdct(esrc, elinesize, blocks);
228 ctx->dsp.fdct(src + linesize * 4, linesize, blocks);
230 if (blocks_per_mb > 2) {
231 ctx->dsp.fdct(src + 8, linesize, blocks);
233 ctx->dsp.fdct(src + linesize * 4 + 8, linesize, blocks);
243 * Write an unsigned rice/exp golomb codeword.
245 static inline void encode_vlc_codeword(PutBitContext *pb, unsigned codebook, int val)
247 unsigned int rice_order, exp_order, switch_bits, switch_val;
250 /* number of prefix bits to switch between Rice and expGolomb */
251 switch_bits = (codebook & 3) + 1;
252 rice_order = codebook >> 5; /* rice code order */
253 exp_order = (codebook >> 2) & 7; /* exp golomb code order */
255 switch_val = switch_bits << rice_order;
257 if (val >= switch_val) {
258 val -= switch_val - (1 << exp_order);
259 exponent = av_log2(val);
261 put_bits(pb, exponent - exp_order + switch_bits, 0);
263 put_bits(pb, exponent, val);
265 exponent = val >> rice_order;
268 put_bits(pb, exponent, 0);
271 put_sbits(pb, rice_order, val);
275 #define GET_SIGN(x) ((x) >> 31)
276 #define MAKE_CODE(x) (((x) << 1) ^ GET_SIGN(x))
278 static void encode_dcs(PutBitContext *pb, DCTELEM *blocks,
279 int blocks_per_slice, int scale)
282 int codebook = 3, code, dc, prev_dc, delta, sign, new_sign;
284 prev_dc = (blocks[0] - 0x4000) / scale;
285 encode_vlc_codeword(pb, FIRST_DC_CB, MAKE_CODE(prev_dc));
290 for (i = 1; i < blocks_per_slice; i++, blocks += 64) {
291 dc = (blocks[0] - 0x4000) / scale;
292 delta = dc - prev_dc;
293 new_sign = GET_SIGN(delta);
294 delta = (delta ^ sign) - sign;
295 code = MAKE_CODE(delta);
296 encode_vlc_codeword(pb, ff_prores_dc_codebook[codebook], code);
297 codebook = (code + (code & 1)) >> 1;
298 codebook = FFMIN(codebook, 3);
304 static void encode_acs(PutBitContext *pb, DCTELEM *blocks,
305 int blocks_per_slice,
306 int plane_size_factor,
307 const uint8_t *scan, const int16_t *qmat)
310 int run, level, run_cb, lev_cb;
311 int max_coeffs, abs_level;
313 max_coeffs = blocks_per_slice << 6;
314 run_cb = ff_prores_run_to_cb_index[4];
315 lev_cb = ff_prores_lev_to_cb_index[2];
318 for (i = 1; i < 64; i++) {
319 for (idx = scan[i]; idx < max_coeffs; idx += 64) {
320 level = blocks[idx] / qmat[scan[i]];
322 abs_level = FFABS(level);
323 encode_vlc_codeword(pb, ff_prores_ac_codebook[run_cb], run);
324 encode_vlc_codeword(pb, ff_prores_ac_codebook[lev_cb],
326 put_sbits(pb, 1, GET_SIGN(level));
328 run_cb = ff_prores_run_to_cb_index[FFMIN(run, 15)];
329 lev_cb = ff_prores_lev_to_cb_index[FFMIN(abs_level, 9)];
338 static int encode_slice_plane(ProresContext *ctx, PutBitContext *pb,
339 const uint16_t *src, int linesize,
340 int mbs_per_slice, DCTELEM *blocks,
341 int blocks_per_mb, int plane_size_factor,
344 int blocks_per_slice, saved_pos;
346 saved_pos = put_bits_count(pb);
347 blocks_per_slice = mbs_per_slice * blocks_per_mb;
349 encode_dcs(pb, blocks, blocks_per_slice, qmat[0]);
350 encode_acs(pb, blocks, blocks_per_slice, plane_size_factor,
351 ctx->scantable.permutated, qmat);
354 return (put_bits_count(pb) - saved_pos) >> 3;
357 static int encode_slice(AVCodecContext *avctx, const AVFrame *pic,
359 int sizes[4], int x, int y, int quant,
362 ProresContext *ctx = avctx->priv_data;
366 int slice_width_factor = av_log2(mbs_per_slice);
367 int num_cblocks, pwidth;
368 int plane_factor, is_chroma;
371 if (quant < MAX_STORED_Q) {
372 qmat = ctx->quants[quant];
374 qmat = ctx->custom_q;
375 for (i = 0; i < 64; i++)
376 qmat[i] = ctx->profile_info->quant[i] * quant;
379 for (i = 0; i < ctx->num_planes; i++) {
380 is_chroma = (i == 1 || i == 2);
381 plane_factor = slice_width_factor + 2;
383 plane_factor += ctx->chroma_factor - 3;
384 if (!is_chroma || ctx->chroma_factor == CFACTOR_Y444) {
388 pwidth = avctx->width;
393 pwidth = avctx->width >> 1;
395 src = (const uint16_t*)(pic->data[i] + yp * pic->linesize[i]) + xp;
397 get_slice_data(ctx, src, pic->linesize[i], xp, yp,
398 pwidth, avctx->height, ctx->blocks[0],
399 mbs_per_slice, num_cblocks, is_chroma);
400 sizes[i] = encode_slice_plane(ctx, pb, src, pic->linesize[i],
401 mbs_per_slice, ctx->blocks[0],
402 num_cblocks, plane_factor,
404 total_size += sizes[i];
409 static inline int estimate_vlc(unsigned codebook, int val)
411 unsigned int rice_order, exp_order, switch_bits, switch_val;
414 /* number of prefix bits to switch between Rice and expGolomb */
415 switch_bits = (codebook & 3) + 1;
416 rice_order = codebook >> 5; /* rice code order */
417 exp_order = (codebook >> 2) & 7; /* exp golomb code order */
419 switch_val = switch_bits << rice_order;
421 if (val >= switch_val) {
422 val -= switch_val - (1 << exp_order);
423 exponent = av_log2(val);
425 return exponent * 2 - exp_order + switch_bits + 1;
427 return (val >> rice_order) + rice_order + 1;
431 static int estimate_dcs(int *error, DCTELEM *blocks, int blocks_per_slice,
435 int codebook = 3, code, dc, prev_dc, delta, sign, new_sign;
438 prev_dc = (blocks[0] - 0x4000) / scale;
439 bits = estimate_vlc(FIRST_DC_CB, MAKE_CODE(prev_dc));
443 *error += FFABS(blocks[0] - 0x4000) % scale;
445 for (i = 1; i < blocks_per_slice; i++, blocks += 64) {
446 dc = (blocks[0] - 0x4000) / scale;
447 *error += FFABS(blocks[0] - 0x4000) % scale;
448 delta = dc - prev_dc;
449 new_sign = GET_SIGN(delta);
450 delta = (delta ^ sign) - sign;
451 code = MAKE_CODE(delta);
452 bits += estimate_vlc(ff_prores_dc_codebook[codebook], code);
453 codebook = (code + (code & 1)) >> 1;
454 codebook = FFMIN(codebook, 3);
462 static int estimate_acs(int *error, DCTELEM *blocks, int blocks_per_slice,
463 int plane_size_factor,
464 const uint8_t *scan, const int16_t *qmat)
467 int run, level, run_cb, lev_cb;
468 int max_coeffs, abs_level;
471 max_coeffs = blocks_per_slice << 6;
472 run_cb = ff_prores_run_to_cb_index[4];
473 lev_cb = ff_prores_lev_to_cb_index[2];
476 for (i = 1; i < 64; i++) {
477 for (idx = scan[i]; idx < max_coeffs; idx += 64) {
478 level = blocks[idx] / qmat[scan[i]];
479 *error += FFABS(blocks[idx]) % qmat[scan[i]];
481 abs_level = FFABS(level);
482 bits += estimate_vlc(ff_prores_ac_codebook[run_cb], run);
483 bits += estimate_vlc(ff_prores_ac_codebook[lev_cb],
486 run_cb = ff_prores_run_to_cb_index[FFMIN(run, 15)];
487 lev_cb = ff_prores_lev_to_cb_index[FFMIN(abs_level, 9)];
498 static int estimate_slice_plane(ProresContext *ctx, int *error, int plane,
499 const uint16_t *src, int linesize,
501 int blocks_per_mb, int plane_size_factor,
504 int blocks_per_slice;
507 blocks_per_slice = mbs_per_slice * blocks_per_mb;
509 bits = estimate_dcs(error, ctx->blocks[plane], blocks_per_slice, qmat[0]);
510 bits += estimate_acs(error, ctx->blocks[plane], blocks_per_slice,
511 plane_size_factor, ctx->scantable.permutated, qmat);
513 return FFALIGN(bits, 8);
516 static int find_slice_quant(AVCodecContext *avctx, const AVFrame *pic,
517 int trellis_node, int x, int y, int mbs_per_slice)
519 ProresContext *ctx = avctx->priv_data;
520 int i, q, pq, xp, yp;
522 int slice_width_factor = av_log2(mbs_per_slice);
523 int num_cblocks[MAX_PLANES], pwidth;
524 int plane_factor[MAX_PLANES], is_chroma[MAX_PLANES];
525 const int min_quant = ctx->profile_info->min_quant;
526 const int max_quant = ctx->profile_info->max_quant;
527 int error, bits, bits_limit;
528 int mbs, prev, cur, new_score;
529 int slice_bits[TRELLIS_WIDTH], slice_score[TRELLIS_WIDTH];
533 mbs = x + mbs_per_slice;
535 for (i = 0; i < ctx->num_planes; i++) {
536 is_chroma[i] = (i == 1 || i == 2);
537 plane_factor[i] = slice_width_factor + 2;
539 plane_factor[i] += ctx->chroma_factor - 3;
540 if (!is_chroma[i] || ctx->chroma_factor == CFACTOR_Y444) {
544 pwidth = avctx->width;
549 pwidth = avctx->width >> 1;
551 src = (const uint16_t*)(pic->data[i] + yp * pic->linesize[i]) + xp;
553 get_slice_data(ctx, src, pic->linesize[i], xp, yp,
554 pwidth, avctx->height, ctx->blocks[i],
555 mbs_per_slice, num_cblocks[i], is_chroma[i]);
558 for (q = min_quant; q < max_quant + 2; q++) {
559 ctx->nodes[trellis_node + q].prev_node = -1;
560 ctx->nodes[trellis_node + q].quant = q;
563 // todo: maybe perform coarser quantising to fit into frame size when needed
564 for (q = min_quant; q <= max_quant; q++) {
567 for (i = 0; i < ctx->num_planes; i++) {
568 bits += estimate_slice_plane(ctx, &error, i,
569 src, pic->linesize[i],
571 num_cblocks[i], plane_factor[i],
574 if (bits > 65000 * 8) {
578 slice_bits[q] = bits;
579 slice_score[q] = error;
581 if (slice_bits[max_quant] <= ctx->bits_per_mb * mbs_per_slice) {
582 slice_bits[max_quant + 1] = slice_bits[max_quant];
583 slice_score[max_quant + 1] = slice_score[max_quant] + 1;
584 overquant = max_quant;
586 for (q = max_quant + 1; q < 128; q++) {
589 if (q < MAX_STORED_Q) {
590 qmat = ctx->quants[q];
592 qmat = ctx->custom_q;
593 for (i = 0; i < 64; i++)
594 qmat[i] = ctx->profile_info->quant[i] * 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 <= ctx->bits_per_mb * mbs_per_slice)
607 slice_bits[max_quant + 1] = bits;
608 slice_score[max_quant + 1] = error;
611 ctx->nodes[trellis_node + max_quant + 1].quant = overquant;
613 bits_limit = mbs * ctx->bits_per_mb;
614 for (pq = min_quant; pq < max_quant + 2; pq++) {
615 prev = trellis_node - TRELLIS_WIDTH + pq;
617 for (q = min_quant; q < max_quant + 2; q++) {
618 cur = trellis_node + q;
620 bits = ctx->nodes[prev].bits + slice_bits[q];
621 error = slice_score[q];
622 if (bits > bits_limit)
625 if (ctx->nodes[prev].score < SCORE_LIMIT && error < SCORE_LIMIT)
626 new_score = ctx->nodes[prev].score + error;
628 new_score = SCORE_LIMIT;
629 if (ctx->nodes[cur].prev_node == -1 ||
630 ctx->nodes[cur].score >= new_score) {
632 ctx->nodes[cur].bits = bits;
633 ctx->nodes[cur].score = new_score;
634 ctx->nodes[cur].prev_node = prev;
639 error = ctx->nodes[trellis_node + min_quant].score;
640 pq = trellis_node + min_quant;
641 for (q = min_quant + 1; q < max_quant + 2; q++) {
642 if (ctx->nodes[trellis_node + q].score <= error) {
643 error = ctx->nodes[trellis_node + q].score;
644 pq = trellis_node + q;
651 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
652 const AVFrame *pic, int *got_packet)
654 ProresContext *ctx = avctx->priv_data;
655 uint8_t *orig_buf, *buf, *slice_hdr, *slice_sizes, *tmp;
656 uint8_t *picture_size_pos;
658 int x, y, i, mb, q = 0;
659 int sizes[4] = { 0 };
660 int slice_hdr_size = 2 + 2 * (ctx->num_planes - 1);
661 int frame_size, picture_size, slice_size;
662 int mbs_per_slice = ctx->mbs_per_slice;
665 *avctx->coded_frame = *pic;
666 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
667 avctx->coded_frame->key_frame = 1;
669 pkt_size = ctx->frame_size + FF_MIN_BUFFER_SIZE;
671 if ((ret = ff_alloc_packet(pkt, pkt_size)) < 0) {
672 av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
676 orig_buf = pkt->data;
679 orig_buf += 4; // frame size
680 bytestream_put_be32 (&orig_buf, FRAME_ID); // frame container ID
685 buf += 2; // frame header size will be stored here
686 bytestream_put_be16 (&buf, 0); // version 1
687 bytestream_put_buffer(&buf, "Lavc", 4); // creator
688 bytestream_put_be16 (&buf, avctx->width);
689 bytestream_put_be16 (&buf, avctx->height);
690 bytestream_put_byte (&buf, ctx->chroma_factor << 6); // frame flags
691 bytestream_put_byte (&buf, 0); // reserved
692 bytestream_put_byte (&buf, avctx->color_primaries);
693 bytestream_put_byte (&buf, avctx->color_trc);
694 bytestream_put_byte (&buf, avctx->colorspace);
695 bytestream_put_byte (&buf, 0x40); // source format and alpha information
696 bytestream_put_byte (&buf, 0); // reserved
697 bytestream_put_byte (&buf, 0x03); // matrix flags - both matrices are present
698 // luma quantisation matrix
699 for (i = 0; i < 64; i++)
700 bytestream_put_byte(&buf, ctx->profile_info->quant[i]);
701 // chroma quantisation matrix
702 for (i = 0; i < 64; i++)
703 bytestream_put_byte(&buf, ctx->profile_info->quant[i]);
704 bytestream_put_be16 (&tmp, buf - orig_buf); // write back frame header size
707 picture_size_pos = buf + 1;
708 bytestream_put_byte (&buf, 0x40); // picture header size (in bits)
709 buf += 4; // picture data size will be stored here
710 bytestream_put_be16 (&buf, ctx->num_slices); // total number of slices
711 bytestream_put_byte (&buf, av_log2(ctx->mbs_per_slice) << 4); // slice width and height in MBs
713 // seek table - will be filled during slice encoding
715 buf += ctx->num_slices * 2;
718 for (y = 0; y < ctx->mb_height; y++) {
719 mbs_per_slice = ctx->mbs_per_slice;
720 for (x = mb = 0; x < ctx->mb_width; x += mbs_per_slice, mb++) {
721 while (ctx->mb_width - x < mbs_per_slice)
723 q = find_slice_quant(avctx, pic, (mb + 1) * TRELLIS_WIDTH, x, y,
727 for (x = ctx->slices_width - 1; x >= 0; x--) {
728 ctx->slice_q[x] = ctx->nodes[q].quant;
729 q = ctx->nodes[q].prev_node;
732 mbs_per_slice = ctx->mbs_per_slice;
733 for (x = mb = 0; x < ctx->mb_width; x += mbs_per_slice, mb++) {
734 q = ctx->slice_q[mb];
736 while (ctx->mb_width - x < mbs_per_slice)
739 bytestream_put_byte(&buf, slice_hdr_size << 3);
741 buf += slice_hdr_size - 1;
742 init_put_bits(&pb, buf, (pkt_size - (buf - orig_buf)) * 8);
743 encode_slice(avctx, pic, &pb, sizes, x, y, q, mbs_per_slice);
745 bytestream_put_byte(&slice_hdr, q);
746 slice_size = slice_hdr_size + sizes[ctx->num_planes - 1];
747 for (i = 0; i < ctx->num_planes - 1; i++) {
748 bytestream_put_be16(&slice_hdr, sizes[i]);
749 slice_size += sizes[i];
751 bytestream_put_be16(&slice_sizes, slice_size);
752 buf += slice_size - slice_hdr_size;
757 frame_size = buf - orig_buf;
758 picture_size = buf - picture_size_pos - 6;
759 bytestream_put_be32(&orig_buf, frame_size);
760 bytestream_put_be32(&picture_size_pos, picture_size);
762 pkt->size = frame_size;
763 pkt->flags |= AV_PKT_FLAG_KEY;
769 static av_cold int encode_close(AVCodecContext *avctx)
771 ProresContext *ctx = avctx->priv_data;
773 if (avctx->coded_frame->data[0])
774 avctx->release_buffer(avctx, avctx->coded_frame);
776 av_freep(&avctx->coded_frame);
778 av_freep(&ctx->nodes);
779 av_freep(&ctx->slice_q);
784 static av_cold int encode_init(AVCodecContext *avctx)
786 ProresContext *ctx = avctx->priv_data;
789 int min_quant, max_quant;
791 avctx->bits_per_raw_sample = 10;
792 avctx->coded_frame = avcodec_alloc_frame();
793 if (!avctx->coded_frame)
794 return AVERROR(ENOMEM);
796 ff_proresdsp_init(&ctx->dsp, avctx);
797 ff_init_scantable(ctx->dsp.dct_permutation, &ctx->scantable,
798 ff_prores_progressive_scan);
800 mps = ctx->mbs_per_slice;
801 if (mps & (mps - 1)) {
802 av_log(avctx, AV_LOG_ERROR,
803 "there should be an integer power of two MBs per slice\n");
804 return AVERROR(EINVAL);
807 ctx->chroma_factor = avctx->pix_fmt == PIX_FMT_YUV422P10
810 ctx->profile_info = prores_profile_info + ctx->profile;
813 ctx->mb_width = FFALIGN(avctx->width, 16) >> 4;
814 ctx->mb_height = FFALIGN(avctx->height, 16) >> 4;
815 ctx->slices_width = ctx->mb_width / mps;
816 ctx->slices_width += av_popcount(ctx->mb_width - ctx->slices_width * mps);
817 ctx->num_slices = ctx->mb_height * ctx->slices_width;
819 for (i = 0; i < NUM_MB_LIMITS - 1; i++)
820 if (prores_mb_limits[i] >= ctx->mb_width * ctx->mb_height)
822 ctx->bits_per_mb = ctx->profile_info->br_tab[i];
824 ctx->frame_size = ctx->num_slices * (2 + 2 * ctx->num_planes
825 + (2 * mps * ctx->bits_per_mb) / 8)
828 min_quant = ctx->profile_info->min_quant;
829 max_quant = ctx->profile_info->max_quant;
830 for (i = min_quant; i < MAX_STORED_Q; i++) {
831 for (j = 0; j < 64; j++)
832 ctx->quants[i][j] = ctx->profile_info->quant[j] * i;
835 avctx->codec_tag = ctx->profile_info->tag;
837 av_log(avctx, AV_LOG_DEBUG, "profile %d, %d slices, %d bits per MB\n",
838 ctx->profile, ctx->num_slices, ctx->bits_per_mb);
839 av_log(avctx, AV_LOG_DEBUG, "estimated frame size %d\n",
842 ctx->nodes = av_malloc((ctx->slices_width + 1) * TRELLIS_WIDTH
843 * sizeof(*ctx->nodes));
846 return AVERROR(ENOMEM);
848 for (i = min_quant; i < max_quant + 2; i++) {
849 ctx->nodes[i].prev_node = -1;
850 ctx->nodes[i].bits = 0;
851 ctx->nodes[i].score = 0;
854 ctx->slice_q = av_malloc(ctx->slices_width * sizeof(*ctx->slice_q));
857 return AVERROR(ENOMEM);
863 #define OFFSET(x) offsetof(ProresContext, x)
864 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
866 static const AVOption options[] = {
867 { "mbs_per_slice", "macroblocks per slice", OFFSET(mbs_per_slice),
868 AV_OPT_TYPE_INT, { 8 }, 1, MAX_MBS_PER_SLICE, VE },
869 { "profile", NULL, OFFSET(profile), AV_OPT_TYPE_INT,
870 { PRORES_PROFILE_STANDARD },
871 PRORES_PROFILE_PROXY, PRORES_PROFILE_HQ, VE, "profile" },
872 { "proxy", NULL, 0, AV_OPT_TYPE_CONST, { PRORES_PROFILE_PROXY },
873 0, 0, VE, "profile" },
874 { "lt", NULL, 0, AV_OPT_TYPE_CONST, { PRORES_PROFILE_LT },
875 0, 0, VE, "profile" },
876 { "standard", NULL, 0, AV_OPT_TYPE_CONST, { PRORES_PROFILE_STANDARD },
877 0, 0, VE, "profile" },
878 { "hq", NULL, 0, AV_OPT_TYPE_CONST, { PRORES_PROFILE_HQ },
879 0, 0, VE, "profile" },
883 static const AVClass proresenc_class = {
884 .class_name = "ProRes encoder",
885 .item_name = av_default_item_name,
887 .version = LIBAVUTIL_VERSION_INT,
890 AVCodec ff_prores_kostya_encoder = {
891 .name = "prores_kostya",
892 .type = AVMEDIA_TYPE_VIDEO,
893 .id = CODEC_ID_PRORES,
894 .priv_data_size = sizeof(ProresContext),
896 .close = encode_close,
897 .encode2 = encode_frame,
898 .long_name = NULL_IF_CONFIG_SMALL("Apple ProRes (iCodec Pro)"),
899 .pix_fmts = (const enum PixelFormat[]) {
900 PIX_FMT_YUV422P10, PIX_FMT_YUV444P10, PIX_FMT_NONE
902 .priv_class = &proresenc_class,