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 AV_PIX_FMT_YUV444AP10
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
124 .tag =
MKTAG(
'a',
'p',
'c',
'o'),
127 .br_tab = { 300, 242, 220, 194 },
132 .tag =
MKTAG(
'a',
'p',
'c',
's'),
135 .br_tab = { 720, 560, 490, 440 },
139 .full_name =
"standard",
140 .tag =
MKTAG(
'a',
'p',
'c',
'n'),
143 .br_tab = { 1050, 808, 710, 632 },
147 .full_name =
"high quality",
148 .tag =
MKTAG(
'a',
'p',
'c',
'h'),
151 .br_tab = { 1566, 1216, 1070, 950 },
157 #define TRELLIS_WIDTH 16
158 #define SCORE_LIMIT INT_MAX / 2
167 #define MAX_STORED_Q 16
212 int linesize,
int x,
int y,
int w,
int h,
213 DCTELEM *blocks, uint16_t *emu_buf,
214 int mbs_per_slice,
int blocks_per_mb,
int is_chroma)
216 const uint16_t *esrc;
217 const int mb_width = 4 * blocks_per_mb;
221 for (i = 0; i < mbs_per_slice; i++, src += mb_width) {
223 memset(blocks, 0, 64 * (mbs_per_slice - i) * blocks_per_mb
227 if (x + mb_width <= w && y + 16 <= h) {
229 elinesize = linesize;
234 elinesize = 16 *
sizeof(*emu_buf);
236 bw =
FFMIN(w - x, mb_width);
237 bh =
FFMIN(h - y, 16);
239 for (j = 0; j < bh; j++) {
240 memcpy(emu_buf + j * 16,
241 (
const uint8_t*)src + j * linesize,
243 pix = emu_buf[j * 16 + bw - 1];
244 for (k = bw; k < mb_width; k++)
245 emu_buf[j * 16 + k] = pix;
248 memcpy(emu_buf + j * 16,
249 emu_buf + (bh - 1) * 16,
250 mb_width *
sizeof(*emu_buf));
253 ctx->
dsp.
fdct(esrc, elinesize, blocks);
255 if (blocks_per_mb > 2) {
256 ctx->
dsp.
fdct(esrc + 8, elinesize, blocks);
259 ctx->
dsp.
fdct(esrc + elinesize * 4, elinesize, blocks);
261 if (blocks_per_mb > 2) {
262 ctx->
dsp.
fdct(esrc + elinesize * 4 + 8, elinesize, blocks);
266 ctx->
dsp.
fdct(esrc, elinesize, blocks);
268 ctx->
dsp.
fdct(esrc + elinesize * 4, elinesize, blocks);
270 if (blocks_per_mb > 2) {
271 ctx->
dsp.
fdct(esrc + 8, elinesize, blocks);
273 ctx->
dsp.
fdct(esrc + elinesize * 4 + 8, elinesize, blocks);
287 unsigned int rice_order, exp_order, switch_bits, switch_val;
291 switch_bits = (codebook & 3) + 1;
292 rice_order = codebook >> 5;
293 exp_order = (codebook >> 2) & 7;
295 switch_val = switch_bits << rice_order;
297 if (val >= switch_val) {
298 val -= switch_val - (1 << exp_order);
301 put_bits(pb, exponent - exp_order + switch_bits, 0);
304 exponent = val >> rice_order;
314 #define GET_SIGN(x) ((x) >> 31)
315 #define MAKE_CODE(x) (((x) << 1) ^ GET_SIGN(x))
318 int blocks_per_slice,
int scale)
321 int codebook = 3, code, dc, prev_dc,
delta, sign, new_sign;
323 prev_dc = (blocks[0] - 0x4000) / scale;
329 for (i = 1; i < blocks_per_slice; i++, blocks += 64) {
330 dc = (blocks[0] - 0x4000) / scale;
331 delta = dc - prev_dc;
333 delta = (delta ^ sign) - sign;
336 codebook = (code + (code & 1)) >> 1;
337 codebook =
FFMIN(codebook, 3);
344 int blocks_per_slice,
345 int plane_size_factor,
346 const uint8_t *scan,
const int16_t *qmat)
350 int max_coeffs, abs_level;
352 max_coeffs = blocks_per_slice << 6;
357 for (i = 1; i < 64; i++) {
358 for (idx = scan[i]; idx < max_coeffs; idx += 64) {
359 level = blocks[idx] / qmat[scan[i]];
361 abs_level =
FFABS(level);
378 const uint16_t *src,
int linesize,
379 int mbs_per_slice,
DCTELEM *blocks,
380 int blocks_per_mb,
int plane_size_factor,
383 int blocks_per_slice, saved_pos;
386 blocks_per_slice = mbs_per_slice * blocks_per_mb;
388 encode_dcs(pb, blocks, blocks_per_slice, qmat[0]);
389 encode_acs(pb, blocks, blocks_per_slice, plane_size_factor,
405 int slice_width_factor =
av_log2(mbs_per_slice);
406 int num_cblocks, pwidth, linesize, line_add;
407 int plane_factor, is_chroma;
421 for (i = 0; i < 64; i++)
426 is_chroma = (i == 1 || i == 2);
427 plane_factor = slice_width_factor + 2;
434 pwidth = avctx->
width;
439 pwidth = avctx->
width >> 1;
443 src = (
const uint16_t*)(pic->
data[i] + yp * linesize +
449 mbs_per_slice, num_cblocks, is_chroma);
451 mbs_per_slice, ctx->
blocks[0],
452 num_cblocks, plane_factor,
454 total_size += sizes[i];
461 unsigned int rice_order, exp_order, switch_bits, switch_val;
465 switch_bits = (codebook & 3) + 1;
466 rice_order = codebook >> 5;
467 exp_order = (codebook >> 2) & 7;
469 switch_val = switch_bits << rice_order;
471 if (val >= switch_val) {
472 val -= switch_val - (1 << exp_order);
475 return exponent * 2 - exp_order + switch_bits + 1;
477 return (val >> rice_order) + rice_order + 1;
485 int codebook = 3, code, dc, prev_dc,
delta, sign, new_sign;
488 prev_dc = (blocks[0] - 0x4000) / scale;
495 for (i = 1; i < blocks_per_slice; i++, blocks += 64) {
496 dc = (blocks[0] - 0x4000) / scale;
498 delta = dc - prev_dc;
500 delta = (delta ^ sign) - sign;
503 codebook = (code + (code & 1)) >> 1;
504 codebook =
FFMIN(codebook, 3);
513 int plane_size_factor,
514 const uint8_t *scan,
const int16_t *qmat)
518 int max_coeffs, abs_level;
521 max_coeffs = blocks_per_slice << 6;
526 for (i = 1; i < 64; i++) {
527 for (idx = scan[i]; idx < max_coeffs; idx += 64) {
528 level = blocks[idx] / qmat[scan[i]];
529 *error +=
FFABS(blocks[idx]) % qmat[scan[i]];
531 abs_level =
FFABS(level);
549 const uint16_t *src,
int linesize,
551 int blocks_per_mb,
int plane_size_factor,
554 int blocks_per_slice;
557 blocks_per_slice = mbs_per_slice * blocks_per_mb;
567 int trellis_node,
int x,
int y,
int mbs_per_slice,
571 int i, q, pq, xp, yp;
573 int slice_width_factor =
av_log2(mbs_per_slice);
578 int error,
bits, bits_limit;
579 int mbs, prev, cur, new_score;
583 int linesize[4], line_add;
589 mbs = x + mbs_per_slice;
592 is_chroma[i] = (i == 1 || i == 2);
593 plane_factor[i] = slice_width_factor + 2;
600 pwidth = avctx->
width;
605 pwidth = avctx->
width >> 1;
609 src = (
const uint16_t*)(pic->
data[i] + yp * linesize[i] +
615 mbs_per_slice, num_cblocks[i], is_chroma[i]);
618 for (q = min_quant; q < max_quant + 2; q++) {
624 for (q = min_quant; q <=
max_quant; q++) {
631 num_cblocks[i], plane_factor[i],
634 if (bits > 65000 * 8) {
638 slice_bits[q] =
bits;
639 slice_score[q] = error;
641 if (slice_bits[max_quant] <= ctx->
bits_per_mb * mbs_per_slice) {
642 slice_bits[max_quant + 1] = slice_bits[
max_quant];
643 slice_score[max_quant + 1] = slice_score[
max_quant] + 1;
646 for (q = max_quant + 1; q < 128; q++) {
653 for (i = 0; i < 64; i++)
660 num_cblocks[i], plane_factor[i],
663 if (bits <= ctx->bits_per_mb * mbs_per_slice)
667 slice_bits[max_quant + 1] =
bits;
668 slice_score[max_quant + 1] = error;
671 td->
nodes[trellis_node + max_quant + 1].
quant = overquant;
674 for (pq = min_quant; pq < max_quant + 2; pq++) {
677 for (q = min_quant; q < max_quant + 2; q++) {
678 cur = trellis_node + q;
680 bits = td->
nodes[prev].
bits + slice_bits[q];
681 error = slice_score[q];
682 if (bits > bits_limit)
701 for (q = min_quant + 1; q < max_quant + 2; q++) {
702 if (td->
nodes[trellis_node + q].
score <= error) {
704 pq = trellis_node + q;
712 int jobnr,
int threadnr)
717 int x, y = jobnr, mb, q = 0;
719 for (x = mb = 0; x < ctx->
mb_width; x += mbs_per_slice, mb++) {
720 while (ctx->
mb_width - x < mbs_per_slice)
736 const AVFrame *pic,
int *got_packet)
739 uint8_t *orig_buf, *buf, *slice_hdr, *slice_sizes, *tmp;
742 int x, y, i, mb, q = 0;
743 int sizes[4] = { 0 };
744 int slice_hdr_size = 2 + 2 * (ctx->
num_planes - 1);
760 orig_buf = pkt->
data;
764 bytestream_put_be32 (&orig_buf,
FRAME_ID);
770 bytestream_put_be16 (&buf, 0);
772 bytestream_put_be16 (&buf, avctx->
width);
773 bytestream_put_be16 (&buf, avctx->
height);
778 bytestream_put_byte (&buf, frame_flags);
780 bytestream_put_byte (&buf, 0);
782 bytestream_put_byte (&buf, avctx->
color_trc);
783 bytestream_put_byte (&buf, avctx->
colorspace);
784 bytestream_put_byte (&buf, 0x40);
785 bytestream_put_byte (&buf, 0);
787 bytestream_put_byte (&buf, 0x03);
789 for (i = 0; i < 64; i++)
790 bytestream_put_byte(&buf, ctx->
quant_mat[i]);
792 for (i = 0; i < 64; i++)
793 bytestream_put_byte(&buf, ctx->
quant_mat[i]);
795 bytestream_put_byte (&buf, 0x00);
797 bytestream_put_be16 (&tmp, buf - orig_buf);
803 picture_size_pos = buf + 1;
804 bytestream_put_byte (&buf, 0x40);
823 for (x = mb = 0; x < ctx->
mb_width; x += mbs_per_slice, mb++) {
827 while (ctx->
mb_width - x < mbs_per_slice)
830 bytestream_put_byte(&buf, slice_hdr_size << 3);
832 buf += slice_hdr_size - 1;
834 encode_slice(avctx, pic, &pb, sizes, x, y, q, mbs_per_slice);
836 bytestream_put_byte(&slice_hdr, q);
837 slice_size = slice_hdr_size + sizes[ctx->
num_planes - 1];
839 bytestream_put_be16(&slice_hdr, sizes[i]);
840 slice_size += sizes[i];
842 bytestream_put_be16(&slice_sizes, slice_size);
843 buf += slice_size - slice_hdr_size;
848 picture_size = buf - picture_size_pos - 6;
850 picture_size = buf - picture_size_pos + 1;
851 bytestream_put_be32(&picture_size_pos, picture_size);
855 frame_size = buf - orig_buf;
856 bytestream_put_be32(&orig_buf, frame_size);
901 if (mps & (mps - 1)) {
903 "there should be an integer power of two MBs per slice\n");
930 if (strlen(ctx->
vendor) != 4) {
951 for (j = 0; j < 64; j++)
975 for (i = min_quant; i < max_quant + 2; i++) {
989 for (j = 0; j < 64; j++) {
1010 "profile %d, %d slices, interlacing: %s, %d bits per MB\n",
1019 #define OFFSET(x) offsetof(ProresContext, x)
1020 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1023 {
"mbs_per_slice",
"macroblocks per slice",
OFFSET(mbs_per_slice),
1029 0, 0,
VE,
"profile" },
1031 0, 0,
VE,
"profile" },
1033 0, 0,
VE,
"profile" },
1035 0, 0,
VE,
"profile" },
1036 {
"vendor",
"vendor ID",
OFFSET(vendor),
1038 {
"bits_per_mb",
"desired bits per macroblock",
OFFSET(bits_per_mb),
1043 0, 0,
VE,
"quant_mat" },
1045 0, 0,
VE,
"quant_mat" },
1047 0, 0,
VE,
"quant_mat" },
1049 0, 0,
VE,
"quant_mat" },
1051 0, 0,
VE,
"quant_mat" },
1053 0, 0,
VE,
"quant_mat" },
#define CODEC_FLAG_INTERLACED_DCT
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
This structure describes decoded (raw) audio or video data.
#define MAX_MBS_PER_SLICE
static void put_sbits(PutBitContext *pb, int n, int32_t value)
AVCodec ff_prores_encoder
static int estimate_acs(int *error, DCTELEM *blocks, int blocks_per_slice, int plane_size_factor, const uint8_t *scan, const int16_t *qmat)
const uint8_t ff_prores_ac_codebook[7]
AVFrame * coded_frame
the picture in the bitstream
static int estimate_vlc(unsigned codebook, int val)
#define FF_MIN_BUFFER_SIZE
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static void get_slice_data(ProresContext *ctx, const uint16_t *src, int linesize, int x, int y, int w, int h, DCTELEM *blocks, uint16_t *emu_buf, int mbs_per_slice, int blocks_per_mb, int is_chroma)
static int find_quant_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
#define AV_PIX_FMT_YUV444P10
static int estimate_slice_plane(ProresContext *ctx, int *error, int plane, const uint16_t *src, int linesize, int mbs_per_slice, int blocks_per_mb, int plane_size_factor, const int16_t *qmat, ProresThreadData *td)
int16_t quants[MAX_STORED_Q][64]
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
static const int prores_mb_limits[NUM_MB_LIMITS]
static void encode_dcs(PutBitContext *pb, DCTELEM *blocks, int blocks_per_slice, int scale)
const uint8_t ff_prores_run_to_cb_index[16]
Lookup tables for adaptive switching between codebooks according with previous run/level value...
const uint8_t ff_prores_lev_to_cb_index[10]
#define MKTAG(a, b, c, d)
static int init(AVCodecParserContext *s)
uint8_t dct_permutation[64]
static const uint8_t frame_size[4]
static int encode_slice(AVCodecContext *avctx, const AVFrame *pic, PutBitContext *pb, int sizes[4], int x, int y, int quant, int mbs_per_slice)
struct ProresContext ProresContext
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
static const int sizes[][2]
static void encode_vlc_codeword(PutBitContext *pb, unsigned codebook, int val)
Write an unsigned rice/exp golomb codeword.
static const uint8_t prores_quant_matrices[][64]
static int find_slice_quant(AVCodecContext *avctx, const AVFrame *pic, int trellis_node, int x, int y, int mbs_per_slice, ProresThreadData *td)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
const struct prores_profile * profile_info
void av_log(void *avcl, int level, const char *fmt,...)
const char * name
Name of the codec implementation.
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pic, int *got_packet)
struct ProresThreadData ProresThreadData
int flags
A combination of AV_PKT_FLAG values.
struct TrellisNode * nodes
static int put_bits_count(PutBitContext *s)
static const AVClass proresenc_class
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
static const struct prores_profile prores_profile_info[4]
int num_chroma_blocks
number of chrominance blocks in a macroblock
int width
picture width / height.
const uint8_t ff_prores_dc_codebook[4]
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
static av_cold int encode_init(AVCodecContext *avctx)
static const AVOption options[]
void(* fdct)(const uint16_t *src, int linesize, DCTELEM *block)
const uint8_t ff_prores_interlaced_scan[64]
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
main external API structure.
static void close(AVCodecParserContext *s)
const uint8_t ff_prores_progressive_scan[64]
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Describe the class of an AVClass context structure.
enum AVColorSpace colorspace
YUV colorspace type.
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
DCTELEM blocks[MAX_PLANES][64 *4 *MAX_MBS_PER_SLICE]
void ff_proresdsp_init(ProresDSPContext *dsp)
#define CODEC_CAP_SLICE_THREADS
int global_quality
Global quality for codecs which cannot change it per frame.
static const uint16_t scale[4]
#define DECLARE_ALIGNED(n, t, v)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
#define AV_PIX_FMT_YUV422P10
static int estimate_dcs(int *error, DCTELEM *blocks, int blocks_per_slice, int scale)
int br_tab[NUM_MB_LIMITS]
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
#define AVERROR_INVALIDDATA
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
static int encode_slice_plane(ProresContext *ctx, PutBitContext *pb, const uint16_t *src, int linesize, int mbs_per_slice, DCTELEM *blocks, int blocks_per_mb, int plane_size_factor, const int16_t *qmat)
int top_field_first
If the content is interlaced, is top field displayed first.
int(* execute2)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count)
The codec may call this to execute several independent things.
void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
int key_frame
1 -> keyframe, 0-> not
static void encode_acs(PutBitContext *pb, DCTELEM *blocks, int blocks_per_slice, int plane_size_factor, const uint8_t *scan, const int16_t *qmat)
const uint8_t * quant_mat
int frame_size_upper_bound
static av_cold int encode_close(AVCodecContext *avctx)
AVPixelFormat
Pixel format.
This structure stores compressed data.
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...