4 * Copyright (c) 2011 Anatoliy Wasserman
6 * This file is part of FFmpeg.
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.
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.
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
24 * @file libavcodec/proresenc.c
25 * Known FOURCCs: 'apch' (HQ), 'apcn' (SD), 'apcs' (LT), 'acpo' (Proxy)
31 #include "bytestream.h"
34 #define DEFAULT_SLICE_MB_WIDTH 8
36 #define FF_PROFILE_PRORES_PROXY 0
37 #define FF_PROFILE_PRORES_LT 1
38 #define FF_PROFILE_PRORES_STANDARD 2
39 #define FF_PROFILE_PRORES_HQ 3
41 static const AVProfile profiles[] = {
42 { FF_PROFILE_PRORES_PROXY, "apco"},
43 { FF_PROFILE_PRORES_LT, "apcs"},
44 { FF_PROFILE_PRORES_STANDARD, "apcn"},
45 { FF_PROFILE_PRORES_HQ, "apch"},
46 { FF_PROFILE_UNKNOWN }
49 static const int qp_start_table[4] = { 4, 1, 1, 1 };
50 static const int qp_end_table[4] = { 8, 9, 6, 6 };
51 static const int bitrate_table[5] = { 1000, 2100, 3500, 5400 };
53 static const uint8_t progressive_scan[64] = {
54 0, 1, 8, 9, 2, 3, 10, 11,
55 16, 17, 24, 25, 18, 19, 26, 27,
56 4, 5, 12, 20, 13, 6, 7, 14,
57 21, 28, 29, 22, 15, 23, 30, 31,
58 32, 33, 40, 48, 41, 34, 35, 42,
59 49, 56, 57, 50, 43, 36, 37, 44,
60 51, 58, 59, 52, 45, 38, 39, 46,
61 53, 60, 61, 54, 47, 55, 62, 63
64 static const uint8_t QMAT_LUMA[4][64] = {
66 4, 7, 9, 11, 13, 14, 15, 63,
67 7, 7, 11, 12, 14, 15, 63, 63,
68 9, 11, 13, 14, 15, 63, 63, 63,
69 11, 11, 13, 14, 63, 63, 63, 63,
70 11, 13, 14, 63, 63, 63, 63, 63,
71 13, 14, 63, 63, 63, 63, 63, 63,
72 13, 63, 63, 63, 63, 63, 63, 63,
73 63, 63, 63, 63, 63, 63, 63, 63
75 4, 5, 6, 7, 9, 11, 13, 15,
76 5, 5, 7, 8, 11, 13, 15, 17,
77 6, 7, 9, 11, 13, 15, 15, 17,
78 7, 7, 9, 11, 13, 15, 17, 19,
79 7, 9, 11, 13, 14, 16, 19, 23,
80 9, 11, 13, 14, 16, 19, 23, 29,
81 9, 11, 13, 15, 17, 21, 28, 35,
82 11, 13, 16, 17, 21, 28, 35, 41
84 4, 4, 5, 5, 6, 7, 7, 9,
85 4, 4, 5, 6, 7, 7, 9, 9,
86 5, 5, 6, 7, 7, 9, 9, 10,
87 5, 5, 6, 7, 7, 9, 9, 10,
88 5, 6, 7, 7, 8, 9, 10, 12,
89 6, 7, 7, 8, 9, 10, 12, 15,
90 6, 7, 7, 9, 10, 11, 14, 17,
91 7, 7, 9, 10, 11, 14, 17, 21
93 4, 4, 4, 4, 4, 4, 4, 4,
94 4, 4, 4, 4, 4, 4, 4, 4,
95 4, 4, 4, 4, 4, 4, 4, 4,
96 4, 4, 4, 4, 4, 4, 4, 5,
97 4, 4, 4, 4, 4, 4, 5, 5,
98 4, 4, 4, 4, 4, 5, 5, 6,
99 4, 4, 4, 4, 5, 5, 6, 7,
100 4, 4, 4, 4, 5, 6, 7, 7
104 static const uint8_t QMAT_CHROMA[4][64] = {
106 4, 7, 9, 11, 13, 14, 63, 63,
107 7, 7, 11, 12, 14, 63, 63, 63,
108 9, 11, 13, 14, 63, 63, 63, 63,
109 11, 11, 13, 14, 63, 63, 63, 63,
110 11, 13, 14, 63, 63, 63, 63, 63,
111 13, 14, 63, 63, 63, 63, 63, 63,
112 13, 63, 63, 63, 63, 63, 63, 63,
113 63, 63, 63, 63, 63, 63, 63, 63
115 4, 5, 6, 7, 9, 11, 13, 15,
116 5, 5, 7, 8, 11, 13, 15, 17,
117 6, 7, 9, 11, 13, 15, 15, 17,
118 7, 7, 9, 11, 13, 15, 17, 19,
119 7, 9, 11, 13, 14, 16, 19, 23,
120 9, 11, 13, 14, 16, 19, 23, 29,
121 9, 11, 13, 15, 17, 21, 28, 35,
122 11, 13, 16, 17, 21, 28, 35, 41
124 4, 4, 5, 5, 6, 7, 7, 9,
125 4, 4, 5, 6, 7, 7, 9, 9,
126 5, 5, 6, 7, 7, 9, 9, 10,
127 5, 5, 6, 7, 7, 9, 9, 10,
128 5, 6, 7, 7, 8, 9, 10, 12,
129 6, 7, 7, 8, 9, 10, 12, 15,
130 6, 7, 7, 9, 10, 11, 14, 17,
131 7, 7, 9, 10, 11, 14, 17, 21
133 4, 4, 4, 4, 4, 4, 4, 4,
134 4, 4, 4, 4, 4, 4, 4, 4,
135 4, 4, 4, 4, 4, 4, 4, 4,
136 4, 4, 4, 4, 4, 4, 4, 5,
137 4, 4, 4, 4, 4, 4, 5, 5,
138 4, 4, 4, 4, 4, 5, 5, 6,
139 4, 4, 4, 4, 5, 5, 6, 7,
140 4, 4, 4, 4, 5, 6, 7, 7
150 int qmat_luma[16][64];
151 int qmat_chroma[16][64];
154 static void encode_codeword(PutBitContext *pb, int val, int codebook)
156 unsigned int rice_order, exp_order, switch_bits, first_exp, exp, zeros,
159 /* number of bits to switch between rice and exp golomb */
160 switch_bits = codebook & 3;
161 rice_order = codebook >> 5;
162 exp_order = (codebook >> 2) & 7;
164 first_exp = ((switch_bits + 1) << rice_order);
166 if (val >= first_exp) { /* exp golomb */
168 val += (1 << exp_order);
170 zeros = exp - exp_order + switch_bits + 1;
171 put_bits(pb, zeros, 0);
172 put_bits(pb, exp + 1, val);
173 } else if (rice_order) {
174 mask = (1 << rice_order) - 1;
175 put_bits(pb, (val >> rice_order), 0);
177 put_bits(pb, rice_order, val & mask);
179 put_bits(pb, val, 0);
184 #define QSCALE(qmat,ind,val) ((val) / (qmat[ind]))
185 #define TO_GOLOMB(val) ((val << 1) ^ (val >> 31))
186 #define DIFF_SIGN(val, sign) ((val >> 31) ^ sign)
187 #define IS_NEGATIVE(val) (((val >> 31) ^ -1) + 1)
188 #define TO_GOLOMB2(val,sign) (val==0 ? 0 : (val << 1) + sign)
190 static av_always_inline int get_level(int val)
192 int sign = (val >> 31);
193 return (val ^ sign) - sign;
196 #define FIRST_DC_CB 0xB8
198 static const uint8_t dc_codebook[7] = { 0x04, 0x28, 0x28, 0x4D, 0x4D, 0x70, 0x70};
200 static void encode_dc_coeffs(PutBitContext *pb, DCTELEM *in,
201 int blocks_per_slice, int *qmat)
205 int new_dc, delta, diff_sign, new_code;
207 prev_dc = QSCALE(qmat, 0, in[0] - 16384);
208 code = TO_GOLOMB(prev_dc);
209 encode_codeword(pb, code, FIRST_DC_CB);
211 code = 5; sign = 0; idx = 64;
212 for (i = 1; i < blocks_per_slice; i++, idx += 64) {
213 new_dc = QSCALE(qmat, 0, in[idx] - 16384);
214 delta = new_dc - prev_dc;
215 diff_sign = DIFF_SIGN(delta, sign);
216 new_code = TO_GOLOMB2(get_level(delta), diff_sign);
218 encode_codeword(pb, new_code, dc_codebook[FFMIN(code, 6)]);
226 static const uint8_t run_to_cb[16] = { 0x06, 0x06, 0x05, 0x05, 0x04, 0x29,
227 0x29, 0x29, 0x29, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x4C };
228 static const uint8_t lev_to_cb[10] = { 0x04, 0x0A, 0x05, 0x06, 0x04, 0x28,
229 0x28, 0x28, 0x28, 0x4C };
231 static void encode_ac_coeffs(AVCodecContext *avctx, PutBitContext *pb,
232 DCTELEM *in, int blocks_per_slice, int *qmat)
237 int run = 0, level, code, i, j;
238 for (i = 1; i < 64; i++) {
239 int indp = progressive_scan[i];
240 for (j = 0; j < blocks_per_slice; j++) {
241 int val = QSCALE(qmat, indp, in[(j << 6) + indp]);
243 encode_codeword(pb, run, run_to_cb[FFMIN(prev_run, 15)]);
247 level = get_level(val);
250 encode_codeword(pb, code, lev_to_cb[FFMIN(prev_level, 9)]);
254 put_bits(pb, 1, IS_NEGATIVE(val));
262 static void get(uint8_t *pixels, int stride, DCTELEM* block)
264 int16_t *p = (int16_t*)pixels;
268 for (i = 0; i < 8; i++) {
269 for (j = 0; j < 8; j++) {
277 static void fdct_get(uint8_t *pixels, int stride, DCTELEM* block)
279 get(pixels, stride, block);
280 ff_jpeg_fdct_islow_10(block);
283 static int encode_slice_plane(AVCodecContext *avctx, int mb_count,
284 uint8_t *src, int src_stride, uint8_t *buf, unsigned buf_size,
285 int *qmat, int chroma)
287 DECLARE_ALIGNED(16, DCTELEM, blocks)[DEFAULT_SLICE_MB_WIDTH << 8], *block;
288 int i, blocks_per_slice;
292 for (i = 0; i < mb_count; i++) {
293 fdct_get(src, src_stride, block + (0 << 6));
294 fdct_get(src + 8 * src_stride, src_stride, block + ((2 - chroma) << 6));
296 fdct_get(src + 16, src_stride, block + (1 << 6));
297 fdct_get(src + 16 + 8 * src_stride, src_stride, block + (3 << 6));
300 block += (256 >> chroma);
301 src += (32 >> chroma);
304 blocks_per_slice = mb_count << (2 - chroma);
305 init_put_bits(&pb, buf, buf_size << 3);
307 encode_dc_coeffs(&pb, blocks, blocks_per_slice, qmat);
308 encode_ac_coeffs(avctx, &pb, blocks, blocks_per_slice, qmat);
311 return put_bits_ptr(&pb) - pb.buf;
314 static av_always_inline unsigned encode_slice_data(AVCodecContext *avctx,
315 uint8_t *dest_y, uint8_t *dest_u, uint8_t *dest_v, int luma_stride,
316 int chroma_stride, unsigned mb_count, uint8_t *buf, unsigned data_size,
317 unsigned* y_data_size, unsigned* u_data_size, unsigned* v_data_size,
320 ProresContext* ctx = avctx->priv_data;
322 *y_data_size = encode_slice_plane(avctx, mb_count, dest_y, luma_stride,
323 buf, data_size, ctx->qmat_luma[qp - 1], 0);
325 if (!(avctx->flags & CODEC_FLAG_GRAY)) {
326 *u_data_size = encode_slice_plane(avctx, mb_count, dest_u,
327 chroma_stride, buf + *y_data_size, data_size - *y_data_size,
328 ctx->qmat_chroma[qp - 1], 1);
330 *v_data_size = encode_slice_plane(avctx, mb_count, dest_v,
331 chroma_stride, buf + *y_data_size + *u_data_size,
332 data_size - *y_data_size - *u_data_size,
333 ctx->qmat_chroma[qp - 1], 1);
336 return *y_data_size + *u_data_size + *v_data_size;
339 static void subimage_with_fill(uint16_t *src, unsigned x, unsigned y,
340 unsigned stride, unsigned width, unsigned height, uint16_t *dst,
341 unsigned dst_width, unsigned dst_height)
344 int box_width = FFMIN(width - x, dst_width);
345 int box_height = FFMIN(height - y, dst_height);
346 int i, j, src_stride = stride >> 1;
347 uint16_t last_pix, *last_line;
349 src += y * src_stride + x;
350 for (i = 0; i < box_height; ++i) {
351 for (j = 0; j < box_width; ++j) {
354 last_pix = dst[j - 1];
355 for (; j < dst_width; j++)
360 last_line = dst - dst_width;
361 for (; i < dst_height; i++) {
362 for (j = 0; j < dst_width; ++j) {
363 dst[j] = last_line[j];
369 static int encode_slice(AVCodecContext *avctx, AVFrame *pic, int mb_x,
370 int mb_y, unsigned mb_count, uint8_t *buf, unsigned data_size,
373 int luma_stride, chroma_stride;
374 int hdr_size = 6, slice_size;
375 uint8_t *dest_y, *dest_u, *dest_v;
376 unsigned y_data_size = 0, u_data_size = 0, v_data_size = 0;
377 ProresContext* ctx = avctx->priv_data;
378 int tgt_bits = (mb_count * bitrate_table[avctx->profile]) >> 2;
379 int low_bytes = (tgt_bits - (tgt_bits >> 3)) >> 3; // 12% bitrate fluctuation
380 int high_bytes = (tgt_bits + (tgt_bits >> 3)) >> 3;
382 luma_stride = pic->linesize[0];
383 chroma_stride = pic->linesize[1];
385 dest_y = pic->data[0] + (mb_y << 4) * luma_stride + (mb_x << 5);
386 dest_u = pic->data[1] + (mb_y << 4) * chroma_stride + (mb_x << 4);
387 dest_v = pic->data[2] + (mb_y << 4) * chroma_stride + (mb_x << 4);
391 subimage_with_fill((uint16_t *) pic->data[0], mb_x << 4, mb_y << 4,
392 luma_stride, avctx->width, avctx->height,
393 (uint16_t *) ctx->fill_y, mb_count << 4, 16);
394 subimage_with_fill((uint16_t *) pic->data[1], mb_x << 3, mb_y << 4,
395 chroma_stride, avctx->width >> 1, avctx->height,
396 (uint16_t *) ctx->fill_u, mb_count << 3, 16);
397 subimage_with_fill((uint16_t *) pic->data[2], mb_x << 3, mb_y << 4,
398 chroma_stride, avctx->width >> 1, avctx->height,
399 (uint16_t *) ctx->fill_v, mb_count << 3, 16);
401 encode_slice_data(avctx, ctx->fill_y, ctx->fill_u, ctx->fill_v,
402 mb_count << 5, mb_count << 4, mb_count, buf + hdr_size,
403 data_size - hdr_size, &y_data_size, &u_data_size, &v_data_size,
406 slice_size = encode_slice_data(avctx, dest_y, dest_u, dest_v,
407 luma_stride, chroma_stride, mb_count, buf + hdr_size,
408 data_size - hdr_size, &y_data_size, &u_data_size, &v_data_size,
411 if (slice_size > high_bytes && *qp < qp_end_table[avctx->profile]) {
414 slice_size = encode_slice_data(avctx, dest_y, dest_u, dest_v,
415 luma_stride, chroma_stride, mb_count, buf + hdr_size,
416 data_size - hdr_size, &y_data_size, &u_data_size,
418 } while (slice_size > high_bytes && *qp < qp_end_table[avctx->profile]);
419 } else if (slice_size < low_bytes && *qp
420 > qp_start_table[avctx->profile]) {
423 slice_size = encode_slice_data(avctx, dest_y, dest_u, dest_v,
424 luma_stride, chroma_stride, mb_count, buf + hdr_size,
425 data_size - hdr_size, &y_data_size, &u_data_size,
427 } while (slice_size < low_bytes && *qp > qp_start_table[avctx->profile]);
431 buf[0] = hdr_size << 3;
433 AV_WB16(buf + 2, y_data_size);
434 AV_WB16(buf + 4, u_data_size);
436 return hdr_size + y_data_size + u_data_size + v_data_size;
439 static int prores_encode_picture(AVCodecContext *avctx, AVFrame *pic,
440 uint8_t *buf, const int buf_size)
442 int mb_width = (avctx->width + 15) >> 4;
443 int mb_height = (avctx->height + 15) >> 4;
444 int hdr_size, sl_size, i;
445 int mb_y, sl_data_size, qp;
446 int unsafe_bot, unsafe_right;
447 uint8_t *sl_data, *sl_data_sizes;
448 int slice_per_line = 0, rem = mb_width;
450 for (i = av_log2(DEFAULT_SLICE_MB_WIDTH); i >= 0; --i) {
451 slice_per_line += rem >> i;
455 qp = qp_start_table[avctx->profile];
456 hdr_size = 8; sl_data_size = buf_size - hdr_size;
457 sl_data_sizes = buf + hdr_size;
458 sl_data = sl_data_sizes + (slice_per_line * mb_height * 2);
459 for (mb_y = 0; mb_y < mb_height; mb_y++) {
461 int slice_mb_count = DEFAULT_SLICE_MB_WIDTH;
462 while (mb_x < mb_width) {
463 while (mb_width - mb_x < slice_mb_count)
464 slice_mb_count >>= 1;
466 unsafe_bot = (avctx->height & 0xf) && (mb_y == mb_height - 1);
467 unsafe_right = (avctx->width & 0xf) && (mb_x + slice_mb_count == mb_width);
469 sl_size = encode_slice(avctx, pic, mb_x, mb_y, slice_mb_count,
470 sl_data, sl_data_size, unsafe_bot || unsafe_right, &qp);
472 bytestream_put_be16(&sl_data_sizes, sl_size);
474 sl_data_size -= sl_size;
475 mb_x += slice_mb_count;
479 buf[0] = hdr_size << 3;
480 AV_WB32(buf + 1, sl_data - buf);
481 AV_WB16(buf + 5, slice_per_line * mb_height);
482 buf[7] = av_log2(DEFAULT_SLICE_MB_WIDTH) << 4;
484 return sl_data - buf;
487 static int prores_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
488 const AVFrame *pict, int *got_packet)
490 int header_size = 148;
493 int frame_size = FFALIGN(avctx->width, 16) * FFALIGN(avctx->height, 16)*16 + 500 + FF_MIN_BUFFER_SIZE; //FIXME choose tighter limit
496 if ((ret = ff_alloc_packet2(avctx, pkt, frame_size + FF_MIN_BUFFER_SIZE)) < 0)
500 pic_size = prores_encode_picture(avctx, pict, buf + header_size + 8,
501 pkt->size - header_size - 8);
503 bytestream_put_be32(&buf, pic_size + 8 + header_size);
504 bytestream_put_buffer(&buf, "icpf", 4);
506 bytestream_put_be16(&buf, header_size);
507 bytestream_put_be16(&buf, 0);
508 bytestream_put_buffer(&buf, "fmpg", 4);
509 bytestream_put_be16(&buf, avctx->width);
510 bytestream_put_be16(&buf, avctx->height);
511 *buf++ = 0x83; // {10}(422){00}{00}(frame){11}
520 bytestream_put_buffer(&buf, QMAT_LUMA[avctx->profile], 64);
521 bytestream_put_buffer(&buf, QMAT_CHROMA[avctx->profile], 64);
523 pkt->flags |= AV_PKT_FLAG_KEY;
524 pkt->size = pic_size + 8 + header_size;
530 static void scale_mat(const uint8_t* src, int* dst, int scale)
533 for (i = 0; i < 64; i++)
534 dst[i] = src[i] * scale;
537 static av_cold int prores_encode_init(AVCodecContext *avctx)
540 ProresContext* ctx = avctx->priv_data;
542 if (avctx->pix_fmt != PIX_FMT_YUV422P10) {
543 av_log(avctx, AV_LOG_ERROR, "need YUV422P10\n");
546 if (avctx->width & 0x1) {
547 av_log(avctx, AV_LOG_ERROR,
548 "frame width needs to be multiple of 2\n");
552 if ((avctx->height & 0xf) || (avctx->width & 0xf)) {
553 ctx->fill_y = av_malloc(4 * (DEFAULT_SLICE_MB_WIDTH << 8));
555 return AVERROR(ENOMEM);
556 ctx->fill_u = ctx->fill_y + (DEFAULT_SLICE_MB_WIDTH << 9);
557 ctx->fill_v = ctx->fill_u + (DEFAULT_SLICE_MB_WIDTH << 8);
560 if (avctx->profile == FF_PROFILE_UNKNOWN) {
561 avctx->profile = FF_PROFILE_PRORES_STANDARD;
562 av_log(avctx, AV_LOG_INFO,
563 "encoding with ProRes standard (apcn) profile\n");
565 } else if (avctx->profile < FF_PROFILE_PRORES_PROXY
566 || avctx->profile > FF_PROFILE_PRORES_HQ) {
570 "unknown profile %d, use [0 - apco, 1 - apcs, 2 - apcn (default), 3 - apch]\n",
575 avctx->codec_tag = AV_RL32((const uint8_t*)profiles[avctx->profile].name);
577 for (i = 1; i <= 16; i++) {
578 scale_mat(QMAT_LUMA[avctx->profile] , ctx->qmat_luma[i - 1] , i);
579 scale_mat(QMAT_CHROMA[avctx->profile], ctx->qmat_chroma[i - 1], i);
582 avctx->coded_frame = avcodec_alloc_frame();
583 avctx->coded_frame->key_frame = 1;
584 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
589 static av_cold int prores_encode_close(AVCodecContext *avctx)
591 ProresContext* ctx = avctx->priv_data;
592 av_freep(&avctx->coded_frame);
593 av_freep(&ctx->fill_y);
598 AVCodec ff_prores_anatoliy_encoder = {
599 .name = "prores_anatoliy",
600 .type = AVMEDIA_TYPE_VIDEO,
601 .id = CODEC_ID_PRORES,
602 .priv_data_size = sizeof(ProresContext),
603 .init = prores_encode_init,
604 .close = prores_encode_close,
605 .encode2 = prores_encode_frame,
606 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV422P10, PIX_FMT_NONE},
607 .long_name = NULL_IF_CONFIG_SMALL("Apple ProRes"),
611 AVCodec ff_prores_encoder = {
613 .type = AVMEDIA_TYPE_VIDEO,
614 .id = CODEC_ID_PRORES,
615 .priv_data_size = sizeof(ProresContext),
616 .init = prores_encode_init,
617 .close = prores_encode_close,
618 .encode2 = prores_encode_frame,
619 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV422P10, PIX_FMT_NONE},
620 .long_name = NULL_IF_CONFIG_SMALL("Apple ProRes"),