]> git.sesse.net Git - ffmpeg/blob - libavcodec/proresenc_anatoliy.c
lavc/qsvenc: assert uninitialized pict_type
[ffmpeg] / libavcodec / proresenc_anatoliy.c
1 /*
2  * Apple ProRes encoder
3  *
4  * Copyright (c) 2011 Anatoliy Wasserman
5  * Copyright (c) 2012 Konstantin Shishkov
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 /**
25  * @file
26  * Apple ProRes encoder (Anatoliy Wasserman version)
27  * Known FOURCCs: 'ap4h' (444), 'apch' (HQ), 'apcn' (422), 'apcs' (LT), 'acpo' (Proxy)
28  */
29
30 #include "avcodec.h"
31 #include "dct.h"
32 #include "internal.h"
33 #include "profiles.h"
34 #include "proresdata.h"
35 #include "put_bits.h"
36 #include "bytestream.h"
37 #include "fdctdsp.h"
38
39 #define DEFAULT_SLICE_MB_WIDTH 8
40
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_PRORES_4444,     "ap4h"},
47     { FF_PROFILE_UNKNOWN }
48 };
49
50 static const int qp_start_table[5] = {  8, 3, 2, 1, 1};
51 static const int qp_end_table[5]   = { 13, 9, 6, 6, 5};
52 static const int bitrate_table[5]  = { 1000, 2100, 3500, 5400, 7000};
53
54 static const uint8_t QMAT_LUMA[5][64] = {
55     {
56          4,  7,  9, 11, 13, 14, 15, 63,
57          7,  7, 11, 12, 14, 15, 63, 63,
58          9, 11, 13, 14, 15, 63, 63, 63,
59         11, 11, 13, 14, 63, 63, 63, 63,
60         11, 13, 14, 63, 63, 63, 63, 63,
61         13, 14, 63, 63, 63, 63, 63, 63,
62         13, 63, 63, 63, 63, 63, 63, 63,
63         63, 63, 63, 63, 63, 63, 63, 63
64     }, {
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
73     }, {
74          4,  4,  5,  5,  6,  7,  7,  9,
75          4,  4,  5,  6,  7,  7,  9,  9,
76          5,  5,  6,  7,  7,  9,  9, 10,
77          5,  5,  6,  7,  7,  9,  9, 10,
78          5,  6,  7,  7,  8,  9, 10, 12,
79          6,  7,  7,  8,  9, 10, 12, 15,
80          6,  7,  7,  9, 10, 11, 14, 17,
81          7,  7,  9, 10, 11, 14, 17, 21
82     }, {
83          4,  4,  4,  4,  4,  4,  4,  4,
84          4,  4,  4,  4,  4,  4,  4,  4,
85          4,  4,  4,  4,  4,  4,  4,  4,
86          4,  4,  4,  4,  4,  4,  4,  5,
87          4,  4,  4,  4,  4,  4,  5,  5,
88          4,  4,  4,  4,  4,  5,  5,  6,
89          4,  4,  4,  4,  5,  5,  6,  7,
90          4,  4,  4,  4,  5,  6,  7,  7
91     }, { /* 444 */
92         4,  4,  4,  4,  4,  4,  4,  4,
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,  5,
96         4,  4,  4,  4,  4,  4,  5,  5,
97         4,  4,  4,  4,  4,  5,  5,  6,
98         4,  4,  4,  4,  5,  5,  6,  7,
99         4,  4,  4,  4,  5,  6,  7,  7
100     }
101 };
102
103 static const uint8_t QMAT_CHROMA[5][64] = {
104     {
105          4,  7,  9, 11, 13, 14, 63, 63,
106          7,  7, 11, 12, 14, 63, 63, 63,
107          9, 11, 13, 14, 63, 63, 63, 63,
108         11, 11, 13, 14, 63, 63, 63, 63,
109         11, 13, 14, 63, 63, 63, 63, 63,
110         13, 14, 63, 63, 63, 63, 63, 63,
111         13, 63, 63, 63, 63, 63, 63, 63,
112         63, 63, 63, 63, 63, 63, 63, 63
113     }, {
114          4,  5,  6,  7,  9, 11, 13, 15,
115          5,  5,  7,  8, 11, 13, 15, 17,
116          6,  7,  9, 11, 13, 15, 15, 17,
117          7,  7,  9, 11, 13, 15, 17, 19,
118          7,  9, 11, 13, 14, 16, 19, 23,
119          9, 11, 13, 14, 16, 19, 23, 29,
120          9, 11, 13, 15, 17, 21, 28, 35,
121         11, 13, 16, 17, 21, 28, 35, 41
122     }, {
123          4,  4,  5,  5,  6,  7,  7,  9,
124          4,  4,  5,  6,  7,  7,  9,  9,
125          5,  5,  6,  7,  7,  9,  9, 10,
126          5,  5,  6,  7,  7,  9,  9, 10,
127          5,  6,  7,  7,  8,  9, 10, 12,
128          6,  7,  7,  8,  9, 10, 12, 15,
129          6,  7,  7,  9, 10, 11, 14, 17,
130          7,  7,  9, 10, 11, 14, 17, 21
131     }, {
132          4,  4,  4,  4,  4,  4,  4,  4,
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,  5,
136          4,  4,  4,  4,  4,  4,  5,  5,
137          4,  4,  4,  4,  4,  5,  5,  6,
138          4,  4,  4,  4,  5,  5,  6,  7,
139          4,  4,  4,  4,  5,  6,  7,  7
140     }, { /* 444 */
141         4,  4,  4,  4,  4,  4,  4,  4,
142         4,  4,  4,  4,  4,  4,  4,  4,
143         4,  4,  4,  4,  4,  4,  4,  4,
144         4,  4,  4,  4,  4,  4,  4,  5,
145         4,  4,  4,  4,  4,  4,  5,  5,
146         4,  4,  4,  4,  4,  5,  5,  6,
147         4,  4,  4,  4,  5,  5,  6,  7,
148         4,  4,  4,  4,  5,  6,  7,  7
149     }
150 };
151
152
153 typedef struct {
154     FDCTDSPContext fdsp;
155     uint8_t* fill_y;
156     uint8_t* fill_u;
157     uint8_t* fill_v;
158     uint8_t* fill_a;
159
160     int qmat_luma[16][64];
161     int qmat_chroma[16][64];
162
163     int is_422;
164     int need_alpha;
165 } ProresContext;
166
167 static void encode_codeword(PutBitContext *pb, int val, int codebook)
168 {
169     unsigned int rice_order, exp_order, switch_bits, first_exp, exp, zeros;
170
171     /* number of bits to switch between rice and exp golomb */
172     switch_bits = codebook & 3;
173     rice_order  = codebook >> 5;
174     exp_order   = (codebook >> 2) & 7;
175
176     first_exp = ((switch_bits + 1) << rice_order);
177
178     if (val >= first_exp) { /* exp golomb */
179         val -= first_exp;
180         val += (1 << exp_order);
181         exp = av_log2(val);
182         zeros = exp - exp_order + switch_bits + 1;
183         put_bits(pb, zeros, 0);
184         put_bits(pb, exp + 1, val);
185     } else if (rice_order) {
186         put_bits(pb, (val >> rice_order), 0);
187         put_bits(pb, 1, 1);
188         put_sbits(pb, rice_order, val);
189     } else {
190         put_bits(pb, val, 0);
191         put_bits(pb, 1, 1);
192     }
193 }
194
195 #define QSCALE(qmat,ind,val) ((val) / ((qmat)[ind]))
196 #define TO_GOLOMB(val) (((val) << 1) ^ ((val) >> 31))
197 #define DIFF_SIGN(val, sign) (((val) >> 31) ^ (sign))
198 #define IS_NEGATIVE(val) ((((val) >> 31) ^ -1) + 1)
199 #define TO_GOLOMB2(val,sign) ((val)==0 ? 0 : ((val) << 1) + (sign))
200
201 static av_always_inline int get_level(int val)
202 {
203     int sign = (val >> 31);
204     return (val ^ sign) - sign;
205 }
206
207 #define FIRST_DC_CB 0xB8
208
209 static const uint8_t dc_codebook[7] = { 0x04, 0x28, 0x28, 0x4D, 0x4D, 0x70, 0x70};
210
211 static void encode_dc_coeffs(PutBitContext *pb, int16_t *in,
212         int blocks_per_slice, int *qmat)
213 {
214     int prev_dc, code;
215     int i, sign, idx;
216     int new_dc, delta, diff_sign, new_code;
217
218     prev_dc = QSCALE(qmat, 0, in[0] - 16384);
219     code = TO_GOLOMB(prev_dc);
220     encode_codeword(pb, code, FIRST_DC_CB);
221
222     code = 5; sign = 0; idx = 64;
223     for (i = 1; i < blocks_per_slice; i++, idx += 64) {
224         new_dc    = QSCALE(qmat, 0, in[idx] - 16384);
225         delta     = new_dc - prev_dc;
226         diff_sign = DIFF_SIGN(delta, sign);
227         new_code  = TO_GOLOMB2(get_level(delta), diff_sign);
228
229         encode_codeword(pb, new_code, dc_codebook[FFMIN(code, 6)]);
230
231         code      = new_code;
232         sign      = delta >> 31;
233         prev_dc   = new_dc;
234     }
235 }
236
237 static const uint8_t run_to_cb[16] = { 0x06, 0x06, 0x05, 0x05, 0x04, 0x29,
238         0x29, 0x29, 0x29, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x4C };
239 static const uint8_t lev_to_cb[10] = { 0x04, 0x0A, 0x05, 0x06, 0x04, 0x28,
240         0x28, 0x28, 0x28, 0x4C };
241
242 static void encode_ac_coeffs(PutBitContext *pb,
243         int16_t *in, int blocks_per_slice, int *qmat)
244 {
245     int prev_run = 4;
246     int prev_level = 2;
247
248     int run = 0, level, code, i, j;
249     for (i = 1; i < 64; i++) {
250         int indp = ff_prores_progressive_scan[i];
251         for (j = 0; j < blocks_per_slice; j++) {
252             int val = QSCALE(qmat, indp, in[(j << 6) + indp]);
253             if (val) {
254                 encode_codeword(pb, run, run_to_cb[FFMIN(prev_run, 15)]);
255
256                 prev_run   = run;
257                 run        = 0;
258                 level      = get_level(val);
259                 code       = level - 1;
260
261                 encode_codeword(pb, code, lev_to_cb[FFMIN(prev_level, 9)]);
262
263                 prev_level = level;
264
265                 put_bits(pb, 1, IS_NEGATIVE(val));
266             } else {
267                 ++run;
268             }
269         }
270     }
271 }
272
273 static void get(uint8_t *pixels, int stride, int16_t* block)
274 {
275     int i;
276
277     for (i = 0; i < 8; i++) {
278         AV_WN64(block, AV_RN64(pixels));
279         AV_WN64(block+4, AV_RN64(pixels+8));
280         pixels += stride;
281         block += 8;
282     }
283 }
284
285 static void fdct_get(FDCTDSPContext *fdsp, uint8_t *pixels, int stride, int16_t* block)
286 {
287     get(pixels, stride, block);
288     fdsp->fdct(block);
289 }
290
291 static void calc_plane_dct(FDCTDSPContext *fdsp, uint8_t *src, int16_t * blocks, int src_stride, int mb_count, int chroma, int is_422)
292 {
293     int16_t *block;
294     int i;
295
296     block = blocks;
297
298     if (!chroma) { /* Luma plane */
299         for (i = 0; i < mb_count; i++) {
300             fdct_get(fdsp, src,                       src_stride, block + (0 << 6));
301             fdct_get(fdsp, src + 16,                  src_stride, block + (1 << 6));
302             fdct_get(fdsp, src +      8 * src_stride, src_stride, block + (2 << 6));
303             fdct_get(fdsp, src + 16 + 8 * src_stride, src_stride, block + (3 << 6));
304
305             block += 256;
306             src   += 32;
307         }
308     } else if (chroma && is_422){ /* chroma plane 422 */
309         for (i = 0; i < mb_count; i++) {
310             fdct_get(fdsp, src,                  src_stride, block + (0 << 6));
311             fdct_get(fdsp, src + 8 * src_stride, src_stride, block + (1 << 6));
312             block += (256 >> 1);
313             src   += (32  >> 1);
314         }
315     } else { /* chroma plane 444 */
316         for (i = 0; i < mb_count; i++) {
317             fdct_get(fdsp, src,                       src_stride, block + (0 << 6));
318             fdct_get(fdsp, src +      8 * src_stride, src_stride, block + (1 << 6));
319             fdct_get(fdsp, src + 16,                  src_stride, block + (2 << 6));
320             fdct_get(fdsp, src + 16 + 8 * src_stride, src_stride, block + (3 << 6));
321
322             block += 256;
323             src   += 32;
324         }
325     }
326 }
327
328 static int encode_slice_plane(int16_t *blocks, int mb_count, uint8_t *buf, unsigned buf_size, int *qmat, int sub_sample_chroma)
329 {
330     int blocks_per_slice;
331     PutBitContext pb;
332
333     blocks_per_slice = mb_count << (2 - sub_sample_chroma);
334     init_put_bits(&pb, buf, buf_size);
335
336     encode_dc_coeffs(&pb, blocks, blocks_per_slice, qmat);
337     encode_ac_coeffs(&pb, blocks, blocks_per_slice, qmat);
338
339     flush_put_bits(&pb);
340     return put_bits_ptr(&pb) - pb.buf;
341 }
342
343 static av_always_inline unsigned encode_slice_data(AVCodecContext *avctx,
344                                                    int16_t * blocks_y, int16_t * blocks_u, int16_t * blocks_v,
345                                                    unsigned mb_count, uint8_t *buf, unsigned data_size,
346                                                    unsigned* y_data_size, unsigned* u_data_size, unsigned* v_data_size,
347                                                    int qp)
348 {
349     ProresContext* ctx = avctx->priv_data;
350
351     *y_data_size = encode_slice_plane(blocks_y, mb_count,
352                                       buf, data_size, ctx->qmat_luma[qp - 1], 0);
353
354     if (!(avctx->flags & AV_CODEC_FLAG_GRAY)) {
355         *u_data_size = encode_slice_plane(blocks_u, mb_count, buf + *y_data_size, data_size - *y_data_size,
356                                           ctx->qmat_chroma[qp - 1], ctx->is_422);
357
358         *v_data_size = encode_slice_plane(blocks_v, mb_count, buf + *y_data_size + *u_data_size,
359                                           data_size - *y_data_size - *u_data_size,
360                                           ctx->qmat_chroma[qp - 1], ctx->is_422);
361     }
362
363     return *y_data_size + *u_data_size + *v_data_size;
364 }
365
366 static void put_alpha_diff(PutBitContext *pb, int cur, int prev)
367 {
368     const int abits = 16;
369     const int dbits = 7;
370     const int dsize = 1 << dbits - 1;
371     int diff = cur - prev;
372
373     diff = av_mod_uintp2(diff, abits);
374     if (diff >= (1 << abits) - dsize)
375         diff -= 1 << abits;
376     if (diff < -dsize || diff > dsize || !diff) {
377         put_bits(pb, 1, 1);
378         put_bits(pb, abits, diff);
379     } else {
380         put_bits(pb, 1, 0);
381         put_bits(pb, dbits - 1, FFABS(diff) - 1);
382         put_bits(pb, 1, diff < 0);
383     }
384 }
385
386 static inline void put_alpha_run(PutBitContext *pb, int run)
387 {
388     if (run) {
389         put_bits(pb, 1, 0);
390         if (run < 0x10)
391             put_bits(pb, 4, run);
392         else
393             put_bits(pb, 15, run);
394     } else {
395         put_bits(pb, 1, 1);
396     }
397 }
398
399 static av_always_inline int encode_alpha_slice_data(AVCodecContext *avctx, int8_t * src_a,
400                                                    unsigned mb_count, uint8_t *buf, unsigned data_size, unsigned* a_data_size)
401 {
402     const int abits = 16;
403     const int mask  = (1 << abits) - 1;
404     const int num_coeffs = mb_count * 256;
405     int prev = mask, cur;
406     int idx = 0;
407     int run = 0;
408     int16_t * blocks = (int16_t *)src_a;
409     PutBitContext pb;
410     init_put_bits(&pb, buf, data_size);
411
412     cur = blocks[idx++];
413     put_alpha_diff(&pb, cur, prev);
414     prev = cur;
415     do {
416         cur = blocks[idx++];
417         if (cur != prev) {
418             put_alpha_run (&pb, run);
419             put_alpha_diff(&pb, cur, prev);
420             prev = cur;
421             run  = 0;
422         } else {
423             run++;
424         }
425     } while (idx < num_coeffs);
426     if (run)
427         put_alpha_run(&pb, run);
428     flush_put_bits(&pb);
429     *a_data_size = put_bits_count(&pb) >> 3;
430
431     if (put_bits_left(&pb) < 0) {
432         av_log(avctx, AV_LOG_ERROR,
433                "Underestimated required buffer size.\n");
434         return AVERROR_BUG;
435     } else {
436         return 0;
437     }
438 }
439
440 static void subimage_with_fill(uint16_t *src, unsigned x, unsigned y,
441         unsigned stride, unsigned width, unsigned height, uint16_t *dst,
442         unsigned dst_width, unsigned dst_height)
443 {
444
445     int box_width = FFMIN(width - x, dst_width);
446     int box_height = FFMIN(height - y, dst_height);
447     int i, j, src_stride = stride >> 1;
448     uint16_t last_pix, *last_line;
449
450     src += y * src_stride + x;
451     for (i = 0; i < box_height; ++i) {
452         for (j = 0; j < box_width; ++j) {
453             dst[j] = src[j];
454         }
455         last_pix = dst[j - 1];
456         for (; j < dst_width; j++)
457             dst[j] = last_pix;
458         src += src_stride;
459         dst += dst_width;
460     }
461     last_line = dst - dst_width;
462     for (; i < dst_height; i++) {
463         for (j = 0; j < dst_width; ++j) {
464             dst[j] = last_line[j];
465         }
466         dst += dst_width;
467     }
468 }
469
470 /* reorganize alpha data and convert 10b -> 16b */
471 static void subimage_alpha_with_fill(uint16_t *src, unsigned x, unsigned y,
472                                unsigned stride, unsigned width, unsigned height, uint16_t *dst,
473                                unsigned dst_width, unsigned dst_height)
474 {
475     int box_width = FFMIN(width - x, dst_width);
476     int box_height = FFMIN(height - y, dst_height);
477     int i, j, src_stride = stride >> 1;
478     uint16_t last_pix, *last_line;
479
480     src += y * src_stride + x;
481     for (i = 0; i < box_height; ++i) {
482         for (j = 0; j < box_width; ++j) {
483             dst[j] = src[j] << 6; /* 10b to 16b */
484         }
485         last_pix = dst[j - 1] << 6; /* 10b to 16b */
486         for (; j < dst_width; j++)
487             dst[j] = last_pix;
488         src += src_stride;
489         dst += dst_width;
490     }
491     last_line = dst - dst_width;
492     for (; i < dst_height; i++) {
493         for (j = 0; j < dst_width; ++j) {
494             dst[j] = last_line[j];
495         }
496         dst += dst_width;
497     }
498 }
499
500 static int encode_slice(AVCodecContext *avctx, const AVFrame *pic, int mb_x,
501         int mb_y, unsigned mb_count, uint8_t *buf, unsigned data_size,
502         int unsafe, int *qp)
503 {
504     int luma_stride, chroma_stride, alpha_stride = 0;
505     ProresContext* ctx = avctx->priv_data;
506     int hdr_size = 6 + (ctx->need_alpha * 2); /* v data size is write when there is alpha */
507     int ret = 0, slice_size;
508     uint8_t *dest_y, *dest_u, *dest_v;
509     unsigned y_data_size = 0, u_data_size = 0, v_data_size = 0, a_data_size = 0;
510     FDCTDSPContext *fdsp = &ctx->fdsp;
511     int tgt_bits   = (mb_count * bitrate_table[avctx->profile]) >> 2;
512     int low_bytes  = (tgt_bits - (tgt_bits >> 3)) >> 3; // 12% bitrate fluctuation
513     int high_bytes = (tgt_bits + (tgt_bits >> 3)) >> 3;
514
515     LOCAL_ALIGNED(16, int16_t, blocks_y, [DEFAULT_SLICE_MB_WIDTH << 8]);
516     LOCAL_ALIGNED(16, int16_t, blocks_u, [DEFAULT_SLICE_MB_WIDTH << 8]);
517     LOCAL_ALIGNED(16, int16_t, blocks_v, [DEFAULT_SLICE_MB_WIDTH << 8]);
518
519     luma_stride   = pic->linesize[0];
520     chroma_stride = pic->linesize[1];
521
522     if (ctx->need_alpha)
523         alpha_stride = pic->linesize[3];
524
525     dest_y = pic->data[0] + (mb_y << 4) * luma_stride   + (mb_x << 5);
526     dest_u = pic->data[1] + (mb_y << 4) * chroma_stride + (mb_x << (5 - ctx->is_422));
527     dest_v = pic->data[2] + (mb_y << 4) * chroma_stride + (mb_x << (5 - ctx->is_422));
528
529     if (unsafe) {
530         subimage_with_fill((uint16_t *) pic->data[0], mb_x << 4, mb_y << 4,
531                 luma_stride, avctx->width, avctx->height,
532                 (uint16_t *) ctx->fill_y, mb_count << 4, 16);
533         subimage_with_fill((uint16_t *) pic->data[1], mb_x << (4 - ctx->is_422), mb_y << 4,
534                            chroma_stride, avctx->width >> ctx->is_422, avctx->height,
535                            (uint16_t *) ctx->fill_u, mb_count << (4 - ctx->is_422), 16);
536         subimage_with_fill((uint16_t *) pic->data[2], mb_x << (4 - ctx->is_422), mb_y << 4,
537                            chroma_stride, avctx->width >> ctx->is_422, avctx->height,
538                            (uint16_t *) ctx->fill_v, mb_count << (4 - ctx->is_422), 16);
539
540         calc_plane_dct(fdsp, ctx->fill_y, blocks_y, mb_count <<  5,                mb_count, 0, 0);
541         calc_plane_dct(fdsp, ctx->fill_u, blocks_u, mb_count << (5 - ctx->is_422), mb_count, 1, ctx->is_422);
542         calc_plane_dct(fdsp, ctx->fill_v, blocks_v, mb_count << (5 - ctx->is_422), mb_count, 1, ctx->is_422);
543
544         slice_size = encode_slice_data(avctx, blocks_y, blocks_u, blocks_v,
545                           mb_count, buf + hdr_size, data_size - hdr_size,
546                           &y_data_size, &u_data_size, &v_data_size,
547                           *qp);
548     } else {
549         calc_plane_dct(fdsp, dest_y, blocks_y, luma_stride, mb_count, 0, 0);
550         calc_plane_dct(fdsp, dest_u, blocks_u, chroma_stride, mb_count, 1, ctx->is_422);
551         calc_plane_dct(fdsp, dest_v, blocks_v, chroma_stride, mb_count, 1, ctx->is_422);
552
553         slice_size = encode_slice_data(avctx, blocks_y, blocks_u, blocks_v,
554                           mb_count, buf + hdr_size, data_size - hdr_size,
555                           &y_data_size, &u_data_size, &v_data_size,
556                           *qp);
557
558         if (slice_size > high_bytes && *qp < qp_end_table[avctx->profile]) {
559             do {
560                 *qp += 1;
561                 slice_size = encode_slice_data(avctx, blocks_y, blocks_u, blocks_v,
562                                                mb_count, buf + hdr_size, data_size - hdr_size,
563                                                &y_data_size, &u_data_size, &v_data_size,
564                                                *qp);
565             } while (slice_size > high_bytes && *qp < qp_end_table[avctx->profile]);
566         } else if (slice_size < low_bytes && *qp
567                 > qp_start_table[avctx->profile]) {
568             do {
569                 *qp -= 1;
570                 slice_size = encode_slice_data(avctx, blocks_y, blocks_u, blocks_v,
571                                                mb_count, buf + hdr_size, data_size - hdr_size,
572                                                &y_data_size, &u_data_size, &v_data_size,
573                                                *qp);
574             } while (slice_size < low_bytes && *qp > qp_start_table[avctx->profile]);
575         }
576     }
577
578     buf[0] = hdr_size << 3;
579     buf[1] = *qp;
580     AV_WB16(buf + 2, y_data_size);
581     AV_WB16(buf + 4, u_data_size);
582
583     if (ctx->need_alpha) {
584         AV_WB16(buf + 6, v_data_size); /* write v data size only if there is alpha */
585
586         subimage_alpha_with_fill((uint16_t *) pic->data[3], mb_x << 4, mb_y << 4,
587                            alpha_stride, avctx->width, avctx->height,
588                            (uint16_t *) ctx->fill_a, mb_count << 4, 16);
589         ret = encode_alpha_slice_data(avctx, ctx->fill_a, mb_count,
590                                       buf + hdr_size + slice_size,
591                                       data_size - hdr_size - slice_size, &a_data_size);
592     }
593
594     if (ret != 0) {
595         return ret;
596     }
597     return hdr_size + y_data_size + u_data_size + v_data_size + a_data_size;
598 }
599
600 static int prores_encode_picture(AVCodecContext *avctx, const AVFrame *pic,
601         uint8_t *buf, const int buf_size)
602 {
603     int mb_width = (avctx->width + 15) >> 4;
604     int mb_height = (avctx->height + 15) >> 4;
605     int hdr_size, sl_size, i;
606     int mb_y, sl_data_size, qp;
607     int unsafe_bot, unsafe_right;
608     uint8_t *sl_data, *sl_data_sizes;
609     int slice_per_line = 0, rem = mb_width;
610
611     for (i = av_log2(DEFAULT_SLICE_MB_WIDTH); i >= 0; --i) {
612         slice_per_line += rem >> i;
613         rem &= (1 << i) - 1;
614     }
615
616     qp = qp_start_table[avctx->profile];
617     hdr_size = 8; sl_data_size = buf_size - hdr_size;
618     sl_data_sizes = buf + hdr_size;
619     sl_data = sl_data_sizes + (slice_per_line * mb_height * 2);
620     for (mb_y = 0; mb_y < mb_height; mb_y++) {
621         int mb_x = 0;
622         int slice_mb_count = DEFAULT_SLICE_MB_WIDTH;
623         while (mb_x < mb_width) {
624             while (mb_width - mb_x < slice_mb_count)
625                 slice_mb_count >>= 1;
626
627             unsafe_bot = (avctx->height & 0xf) && (mb_y == mb_height - 1);
628             unsafe_right = (avctx->width & 0xf) && (mb_x + slice_mb_count == mb_width);
629
630             sl_size = encode_slice(avctx, pic, mb_x, mb_y, slice_mb_count,
631                     sl_data, sl_data_size, unsafe_bot || unsafe_right, &qp);
632             if (sl_size < 0){
633                 return sl_size;
634             }
635
636             bytestream_put_be16(&sl_data_sizes, sl_size);
637             sl_data           += sl_size;
638             sl_data_size      -= sl_size;
639             mb_x              += slice_mb_count;
640         }
641     }
642
643     buf[0] = hdr_size << 3;
644     AV_WB32(buf + 1, sl_data - buf);
645     AV_WB16(buf + 5, slice_per_line * mb_height);
646     buf[7] = av_log2(DEFAULT_SLICE_MB_WIDTH) << 4;
647
648     return sl_data - buf;
649 }
650
651 static int prores_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
652                                const AVFrame *pict, int *got_packet)
653 {
654     int header_size = 148;
655     uint8_t *buf;
656     int pic_size, ret;
657     int frame_size = FFALIGN(avctx->width, 16) * FFALIGN(avctx->height, 16)*16 + 500 + AV_INPUT_BUFFER_MIN_SIZE; //FIXME choose tighter limit
658
659
660     if ((ret = ff_alloc_packet2(avctx, pkt, frame_size + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
661         return ret;
662
663     buf = pkt->data;
664     pic_size = prores_encode_picture(avctx, pict, buf + header_size + 8,
665             pkt->size - header_size - 8);
666     if (pic_size < 0) {
667         return pic_size;
668     }
669
670     bytestream_put_be32(&buf, pic_size + 8 + header_size);
671     bytestream_put_buffer(&buf, "icpf", 4);
672
673     bytestream_put_be16(&buf, header_size);
674     bytestream_put_be16(&buf, 0); /* version */
675     bytestream_put_buffer(&buf, "fmpg", 4);
676     bytestream_put_be16(&buf, avctx->width);
677     bytestream_put_be16(&buf, avctx->height);
678     if (avctx->profile == FF_PROFILE_PRORES_4444) {
679         *buf++ = 0xC2; // 444, not interlaced
680     } else {
681         *buf++ = 0x82; // 422, not interlaced
682     }
683     *buf++ = 0; /* reserved */
684     *buf++ = pict->color_primaries;
685     *buf++ = pict->color_trc;
686     *buf++ = pict->colorspace;
687     if (avctx->profile >= FF_PROFILE_PRORES_4444) {
688         if (avctx->pix_fmt == AV_PIX_FMT_YUV444P10) {
689             *buf++ = 0xA0;/* src b64a and no alpha */
690         } else {
691             *buf++ = 0xA2;/* src b64a and 16b alpha */
692         }
693     } else {
694         *buf++ = 32;/* src v210 and no alpha */
695     }
696     *buf++ = 0; /* reserved */
697     *buf++ = 3; /* luma and chroma matrix present */
698
699     bytestream_put_buffer(&buf, QMAT_LUMA[avctx->profile],   64);
700     bytestream_put_buffer(&buf, QMAT_CHROMA[avctx->profile], 64);
701
702     pkt->flags |= AV_PKT_FLAG_KEY;
703     pkt->size = pic_size + 8 + header_size;
704     *got_packet = 1;
705
706     return 0;
707 }
708
709 static void scale_mat(const uint8_t* src, int* dst, int scale)
710 {
711     int i;
712     for (i = 0; i < 64; i++)
713         dst[i] = src[i] * scale;
714 }
715
716 static av_cold int prores_encode_init(AVCodecContext *avctx)
717 {
718     int i;
719     ProresContext* ctx = avctx->priv_data;
720
721     avctx->bits_per_raw_sample = 10;
722     ctx->need_alpha = 0;
723
724     if (avctx->width & 0x1) {
725         av_log(avctx, AV_LOG_ERROR,
726                 "frame width needs to be multiple of 2\n");
727         return AVERROR(EINVAL);
728     }
729
730     if (avctx->width > 65534 || avctx->height > 65535) {
731         av_log(avctx, AV_LOG_ERROR,
732                 "The maximum dimensions are 65534x65535\n");
733         return AVERROR(EINVAL);
734     }
735
736     if (avctx->profile == FF_PROFILE_UNKNOWN) {
737         if (avctx->pix_fmt == AV_PIX_FMT_YUV422P10) {
738             avctx->profile = FF_PROFILE_PRORES_STANDARD;
739             av_log(avctx, AV_LOG_INFO,
740                 "encoding with ProRes standard (apcn) profile\n");
741         } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P10) {
742             avctx->profile = FF_PROFILE_PRORES_4444;
743             av_log(avctx, AV_LOG_INFO,
744                    "encoding with ProRes 4444 (ap4h) profile\n");
745         } else if (avctx->pix_fmt == AV_PIX_FMT_YUVA444P10) {
746             avctx->profile = FF_PROFILE_PRORES_4444;
747             av_log(avctx, AV_LOG_INFO,
748                    "encoding with ProRes 4444+ (ap4h) profile\n");
749         } else {
750             av_log(avctx, AV_LOG_ERROR, "Unknown pixel format\n");
751             return AVERROR(EINVAL);
752         }
753     } else if (avctx->profile < FF_PROFILE_PRORES_PROXY
754             || avctx->profile > FF_PROFILE_PRORES_4444) {
755         av_log(
756                 avctx,
757                 AV_LOG_ERROR,
758                 "unknown profile %d, use [0 - apco, 1 - apcs, 2 - apcn (default), 3 - apch, 4 - ap4h]\n",
759                 avctx->profile);
760         return AVERROR(EINVAL);
761     } else if ((avctx->pix_fmt == AV_PIX_FMT_YUV422P10) && (avctx->profile > FF_PROFILE_PRORES_HQ)){
762         av_log(avctx, AV_LOG_ERROR,
763                "encoding with ProRes 444 (ap4h) profile, need YUV444P10 input\n");
764         return AVERROR(EINVAL);
765     }  else if ((avctx->pix_fmt == AV_PIX_FMT_YUV444P10 || avctx->pix_fmt == AV_PIX_FMT_YUVA444P10)
766                 && (avctx->profile < FF_PROFILE_PRORES_4444)){
767         av_log(avctx, AV_LOG_ERROR,
768                "encoding with ProRes Proxy/LT/422/422 HQ (apco, apcs, apcn, ap4h) profile, need YUV422P10 input\n");
769         return AVERROR(EINVAL);
770     }
771
772     if (avctx->profile < FF_PROFILE_PRORES_4444) { /* 422 versions */
773         ctx->is_422 = 1;
774         if ((avctx->height & 0xf) || (avctx->width & 0xf)) {
775             ctx->fill_y = av_malloc(4 * (DEFAULT_SLICE_MB_WIDTH << 8));
776             if (!ctx->fill_y)
777                 return AVERROR(ENOMEM);
778             ctx->fill_u = ctx->fill_y + (DEFAULT_SLICE_MB_WIDTH << 9);
779             ctx->fill_v = ctx->fill_u + (DEFAULT_SLICE_MB_WIDTH << 8);
780         }
781     } else { /* 444 */
782         ctx->is_422 = 0;
783         if ((avctx->height & 0xf) || (avctx->width & 0xf)) {
784             ctx->fill_y = av_malloc(3 * (DEFAULT_SLICE_MB_WIDTH << 9));
785             if (!ctx->fill_y)
786                 return AVERROR(ENOMEM);
787             ctx->fill_u = ctx->fill_y + (DEFAULT_SLICE_MB_WIDTH << 9);
788             ctx->fill_v = ctx->fill_u + (DEFAULT_SLICE_MB_WIDTH << 9);
789         }
790         if (avctx->pix_fmt == AV_PIX_FMT_YUVA444P10) {
791             ctx->need_alpha = 1;
792             ctx->fill_a = av_malloc(DEFAULT_SLICE_MB_WIDTH << 9); /* 8 blocks x 16px x 16px x sizeof (uint16) */
793             if (!ctx->fill_a)
794                 return AVERROR(ENOMEM);
795         }
796     }
797
798     ff_fdctdsp_init(&ctx->fdsp, avctx);
799
800     avctx->codec_tag = AV_RL32((const uint8_t*)profiles[avctx->profile].name);
801
802     for (i = 1; i <= 16; i++) {
803         scale_mat(QMAT_LUMA[avctx->profile]  , ctx->qmat_luma[i - 1]  , i);
804         scale_mat(QMAT_CHROMA[avctx->profile], ctx->qmat_chroma[i - 1], i);
805     }
806
807     return 0;
808 }
809
810 static av_cold int prores_encode_close(AVCodecContext *avctx)
811 {
812     ProresContext* ctx = avctx->priv_data;
813     av_freep(&ctx->fill_y);
814     av_freep(&ctx->fill_a);
815
816     return 0;
817 }
818
819 AVCodec ff_prores_aw_encoder = {
820     .name           = "prores_aw",
821     .long_name      = NULL_IF_CONFIG_SMALL("Apple ProRes"),
822     .type           = AVMEDIA_TYPE_VIDEO,
823     .id             = AV_CODEC_ID_PRORES,
824     .priv_data_size = sizeof(ProresContext),
825     .init           = prores_encode_init,
826     .close          = prores_encode_close,
827     .encode2        = prores_encode_frame,
828     .pix_fmts       = (const enum AVPixelFormat[]){AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_NONE},
829     .capabilities   = AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_INTRA_ONLY,
830     .profiles       = NULL_IF_CONFIG_SMALL(ff_prores_profiles),
831 };
832
833 AVCodec ff_prores_encoder = {
834     .name           = "prores",
835     .long_name      = NULL_IF_CONFIG_SMALL("Apple ProRes"),
836     .type           = AVMEDIA_TYPE_VIDEO,
837     .id             = AV_CODEC_ID_PRORES,
838     .priv_data_size = sizeof(ProresContext),
839     .init           = prores_encode_init,
840     .close          = prores_encode_close,
841     .encode2        = prores_encode_frame,
842     .pix_fmts       = (const enum AVPixelFormat[]){AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_NONE},
843     .capabilities   = AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_INTRA_ONLY,
844     .profiles       = NULL_IF_CONFIG_SMALL(ff_prores_profiles),
845 };