Browse code

Apple ProRes encoder

Signed-off-by: Anatoliy Wasserman <anatoliy.wasserman@yandex.ru>
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>

Anatoliy Wasserman authored on 2011/10/29 05:13:58
Showing 5 changed files
... ...
@@ -69,6 +69,7 @@ easier to use. The changes are:
69 69
 - Speex encoding via libspeex
70 70
 - 4:2:2 H.264 decoding support
71 71
 - Pulseaudio input device
72
+- Prores encoder
72 73
 
73 74
 version 0.8:
74 75
 
... ...
@@ -350,7 +350,7 @@ following image formats are supported:
350 350
     @tab Used in Chinese MP3 players.
351 351
 @item ANSI/ASCII art         @tab     @tab  X
352 352
 @item Apple MJPEG-B          @tab     @tab  X
353
-@item Apple ProRes           @tab     @tab  X
353
+@item Apple ProRes           @tab  X  @tab  X
354 354
 @item Apple QuickDraw        @tab     @tab  X
355 355
     @tab fourcc: qdrw
356 356
 @item Asus v1                @tab  X  @tab  X
... ...
@@ -315,6 +315,7 @@ OBJS-$(CONFIG_PPM_DECODER)             += pnmdec.o pnm.o
315 315
 OBJS-$(CONFIG_PPM_ENCODER)             += pnmenc.o pnm.o
316 316
 OBJS-$(CONFIG_PRORES_DECODER)          += proresdec2.o
317 317
 OBJS-$(CONFIG_PRORES_LGPL_DECODER)     += proresdec_lgpl.o proresdsp.o
318
+OBJS-$(CONFIG_PRORES_ENCODER)          += proresenc.o
318 319
 OBJS-$(CONFIG_PTX_DECODER)             += ptx.o
319 320
 OBJS-$(CONFIG_QCELP_DECODER)           += qcelpdec.o celp_math.o         \
320 321
                                           celp_filters.o acelp_vectors.o \
... ...
@@ -172,7 +172,7 @@ void avcodec_register_all(void)
172 172
     REGISTER_DECODER (PICTOR, pictor);
173 173
     REGISTER_ENCDEC  (PNG, png);
174 174
     REGISTER_ENCDEC  (PPM, ppm);
175
-    REGISTER_DECODER (PRORES, prores);
175
+    REGISTER_ENCDEC  (PRORES, prores);
176 176
     REGISTER_DECODER (PRORES_LGPL, prores_lgpl);
177 177
     REGISTER_DECODER (PTX, ptx);
178 178
     REGISTER_DECODER (QDRAW, qdraw);
179 179
new file mode 100644
... ...
@@ -0,0 +1,610 @@
0
+/*
1
+ * Apple ProRes encoder
2
+ *
3
+ * Copyright (c) 2011 Anatoliy Wasserman
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * FFmpeg is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+/**
23
+ * @file libavcodec/proresenc.c
24
+ * Known FOURCCs: 'apch' (HQ), 'apcn' (SD), 'apcs' (LT), 'acpo' (Proxy)
25
+ */
26
+
27
+#include "avcodec.h"
28
+#include "put_bits.h"
29
+#include "dsputil.h"
30
+
31
+#define DEFAULT_SLICE_MB_WIDTH 8
32
+
33
+#define FF_PROFILE_PRORES_PROXY     0
34
+#define FF_PROFILE_PRORES_LT        1
35
+#define FF_PROFILE_PRORES_STANDARD  2
36
+#define FF_PROFILE_PRORES_HQ        3
37
+
38
+static const AVProfile profiles[] = {
39
+    { FF_PROFILE_PRORES_PROXY,    "apco"},
40
+    { FF_PROFILE_PRORES_LT,       "apcs"},
41
+    { FF_PROFILE_PRORES_STANDARD, "apcn"},
42
+    { FF_PROFILE_PRORES_HQ,       "apch"},
43
+    { FF_PROFILE_UNKNOWN }
44
+};
45
+
46
+static const int qp_start_table[4] = { 4, 1, 1, 1 };
47
+static const int qp_end_table[4]   = { 8, 9, 6, 6 };
48
+static const int bitrate_table[5]  = { 1000, 2100, 3500, 5400 };
49
+
50
+static const uint8_t progressive_scan[64] = {
51
+     0,  1,  8,  9,  2,  3, 10, 11,
52
+    16, 17, 24, 25, 18, 19, 26, 27,
53
+     4,  5, 12, 20, 13,  6,  7, 14,
54
+    21, 28, 29, 22, 15, 23, 30, 31,
55
+    32, 33, 40, 48, 41, 34, 35, 42,
56
+    49, 56, 57, 50, 43, 36, 37, 44,
57
+    51, 58, 59, 52, 45, 38, 39, 46,
58
+    53, 60, 61, 54, 47, 55, 62, 63
59
+};
60
+
61
+static const uint8_t QMAT_LUMA[4][64] = {
62
+    {
63
+         4,  7,  9, 11, 13, 14, 15, 63,
64
+         7,  7, 11, 12, 14, 15, 63, 63,
65
+         9, 11, 13, 14, 15, 63, 63, 63,
66
+        11, 11, 13, 14, 63, 63, 63, 63,
67
+        11, 13, 14, 63, 63, 63, 63, 63,
68
+        13, 14, 63, 63, 63, 63, 63, 63,
69
+        13, 63, 63, 63, 63, 63, 63, 63,
70
+        63, 63, 63, 63, 63, 63, 63, 63
71
+    }, {
72
+         4,  5,  6,  7,  9, 11, 13, 15,
73
+         5,  5,  7,  8, 11, 13, 15, 17,
74
+         6,  7,  9, 11, 13, 15, 15, 17,
75
+         7,  7,  9, 11, 13, 15, 17, 19,
76
+         7,  9, 11, 13, 14, 16, 19, 23,
77
+         9, 11, 13, 14, 16, 19, 23, 29,
78
+         9, 11, 13, 15, 17, 21, 28, 35,
79
+        11, 13, 16, 17, 21, 28, 35, 41
80
+    }, {
81
+         4,  4,  5,  5,  6,  7,  7,  9,
82
+         4,  4,  5,  6,  7,  7,  9,  9,
83
+         5,  5,  6,  7,  7,  9,  9, 10,
84
+         5,  5,  6,  7,  7,  9,  9, 10,
85
+         5,  6,  7,  7,  8,  9, 10, 12,
86
+         6,  7,  7,  8,  9, 10, 12, 15,
87
+         6,  7,  7,  9, 10, 11, 14, 17,
88
+         7,  7,  9, 10, 11, 14, 17, 21
89
+    }, {
90
+         4,  4,  4,  4,  4,  4,  4,  4,
91
+         4,  4,  4,  4,  4,  4,  4,  4,
92
+         4,  4,  4,  4,  4,  4,  4,  4,
93
+         4,  4,  4,  4,  4,  4,  4,  5,
94
+         4,  4,  4,  4,  4,  4,  5,  5,
95
+         4,  4,  4,  4,  4,  5,  5,  6,
96
+         4,  4,  4,  4,  5,  5,  6,  7,
97
+         4,  4,  4,  4,  5,  6,  7,  7
98
+    }
99
+};
100
+
101
+static const uint8_t QMAT_CHROMA[4][64] = {
102
+    {
103
+         4,  7,  9, 11, 13, 14, 63, 63,
104
+         7,  7, 11, 12, 14, 63, 63, 63,
105
+         9, 11, 13, 14, 63, 63, 63, 63,
106
+        11, 11, 13, 14, 63, 63, 63, 63,
107
+        11, 13, 14, 63, 63, 63, 63, 63,
108
+        13, 14, 63, 63, 63, 63, 63, 63,
109
+        13, 63, 63, 63, 63, 63, 63, 63,
110
+        63, 63, 63, 63, 63, 63, 63, 63
111
+    }, {
112
+         4,  5,  6,  7,  9, 11, 13, 15,
113
+         5,  5,  7,  8, 11, 13, 15, 17,
114
+         6,  7,  9, 11, 13, 15, 15, 17,
115
+         7,  7,  9, 11, 13, 15, 17, 19,
116
+         7,  9, 11, 13, 14, 16, 19, 23,
117
+         9, 11, 13, 14, 16, 19, 23, 29,
118
+         9, 11, 13, 15, 17, 21, 28, 35,
119
+        11, 13, 16, 17, 21, 28, 35, 41
120
+    }, {
121
+         4,  4,  5,  5,  6,  7,  7,  9,
122
+         4,  4,  5,  6,  7,  7,  9,  9,
123
+         5,  5,  6,  7,  7,  9,  9, 10,
124
+         5,  5,  6,  7,  7,  9,  9, 10,
125
+         5,  6,  7,  7,  8,  9, 10, 12,
126
+         6,  7,  7,  8,  9, 10, 12, 15,
127
+         6,  7,  7,  9, 10, 11, 14, 17,
128
+         7,  7,  9, 10, 11, 14, 17, 21
129
+    }, {
130
+         4,  4,  4,  4,  4,  4,  4,  4,
131
+         4,  4,  4,  4,  4,  4,  4,  4,
132
+         4,  4,  4,  4,  4,  4,  4,  4,
133
+         4,  4,  4,  4,  4,  4,  4,  5,
134
+         4,  4,  4,  4,  4,  4,  5,  5,
135
+         4,  4,  4,  4,  4,  5,  5,  6,
136
+         4,  4,  4,  4,  5,  5,  6,  7,
137
+         4,  4,  4,  4,  5,  6,  7,  7
138
+    }
139
+};
140
+
141
+
142
+typedef struct {
143
+    uint8_t* fill_y;
144
+    uint8_t* fill_u;
145
+    uint8_t* fill_v;
146
+
147
+    int qmat_luma[16][64];
148
+    int qmat_chroma[16][64];
149
+} ProresContext;
150
+
151
+static void encode_codeword(PutBitContext *pb, int val, int codebook)
152
+{
153
+    unsigned int rice_order, exp_order, switch_bits, first_exp, exp, zeros,
154
+            mask;
155
+
156
+    /* number of bits to switch between rice and exp golomb */
157
+    switch_bits = codebook & 3;
158
+    rice_order  = codebook >> 5;
159
+    exp_order   = (codebook >> 2) & 7;
160
+
161
+    first_exp = ((switch_bits + 1) << rice_order);
162
+
163
+    if (val >= first_exp) { /* exp golomb */
164
+        val -= first_exp;
165
+        val += (1 << exp_order);
166
+        exp = av_log2(val);
167
+        zeros = exp - exp_order + switch_bits + 1;
168
+        put_bits(pb, zeros, 0);
169
+        put_bits(pb, 1, 1);
170
+        put_bits(pb, exp, val);
171
+    } else if (rice_order) {
172
+        mask = (1 << rice_order) - 1;
173
+        put_bits(pb, (val >> rice_order), 0);
174
+        put_bits(pb, 1, 1);
175
+        put_bits(pb, rice_order, val & mask);
176
+    } else {
177
+        put_bits(pb, val, 0);
178
+        put_bits(pb, 1, 1);
179
+    }
180
+}
181
+
182
+#define QSCALE(qmat,ind,val) ((val) / (qmat[ind]))
183
+#define TO_GOLOMB(val) ((val << 1) ^ (val >> 31))
184
+#define DIFF_SIGN(val, sign) ((val >> 31) ^ sign)
185
+#define IS_NEGATIVE(val) (((val >> 31) ^ -1) + 1)
186
+#define TO_GOLOMB2(val,sign) (val==0 ? 0 : (val << 1) + sign)
187
+
188
+static av_always_inline int get_level(int val)
189
+{
190
+    int sign = (val >> 31);
191
+    return (val ^ sign) - sign;
192
+}
193
+
194
+#define FIRST_DC_CB 0xB8
195
+
196
+static const uint8_t dc_codebook[7] = { 0x04, 0x28, 0x28, 0x4D, 0x4D, 0x70, 0x70};
197
+
198
+static void encode_dc_coeffs(PutBitContext *pb, DCTELEM *in,
199
+        int blocks_per_slice, int *qmat)
200
+{
201
+    int prev_dc, code;
202
+    int i, sign, idx;
203
+    int new_dc, delta, diff_sign, new_code;
204
+
205
+    prev_dc = QSCALE(qmat, 0, in[0] - 16384);
206
+    code = TO_GOLOMB(prev_dc);
207
+    encode_codeword(pb, code, FIRST_DC_CB);
208
+
209
+    code = 5; sign = 0; idx = 64;
210
+    for (i = 1; i < blocks_per_slice; i++, idx += 64) {
211
+        new_dc    = QSCALE(qmat, 0, in[idx] - 16384);
212
+        delta     = new_dc - prev_dc;
213
+        diff_sign = DIFF_SIGN(delta, sign);
214
+        new_code  = TO_GOLOMB2(get_level(delta), diff_sign);
215
+
216
+        encode_codeword(pb, new_code, dc_codebook[FFMIN(code, 6)]);
217
+
218
+        code      = new_code;
219
+        sign      = delta >> 31;
220
+        prev_dc   = new_dc;
221
+    }
222
+}
223
+
224
+static const uint8_t run_to_cb[16] = { 0x06, 0x06, 0x05, 0x05, 0x04, 0x29,
225
+        0x29, 0x29, 0x29, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x4C };
226
+static const uint8_t lev_to_cb[10] = { 0x04, 0x0A, 0x05, 0x06, 0x04, 0x28,
227
+        0x28, 0x28, 0x28, 0x4C };
228
+
229
+static void encode_ac_coeffs(AVCodecContext *avctx, PutBitContext *pb,
230
+        DCTELEM *in, int blocks_per_slice, int *qmat)
231
+{
232
+    int prev_run = 4;
233
+    int prev_level = 2;
234
+
235
+    int run = 0, level, code;
236
+    for (int i = 1; i < 64; i++) {
237
+        int indp = progressive_scan[i];
238
+        for (int j = 0; j < blocks_per_slice; j++) {
239
+            int val = QSCALE(qmat, indp, in[(j << 6) + indp]);
240
+            if (val) {
241
+                encode_codeword(pb, run, run_to_cb[FFMIN(prev_run, 15)]);
242
+
243
+                prev_run   = run;
244
+                run        = 0;
245
+                level      = get_level(val);
246
+                code       = level - 1;
247
+
248
+                encode_codeword(pb, code, lev_to_cb[FFMIN(prev_level, 9)]);
249
+
250
+                prev_level = level;
251
+
252
+                put_bits(pb, 1, IS_NEGATIVE(val));
253
+            } else {
254
+                ++run;
255
+            }
256
+        }
257
+    }
258
+}
259
+
260
+static void get(uint8_t *pixels, int stride, DCTELEM* block)
261
+{
262
+    int16_t *p = (int16_t*)pixels;
263
+    int i, j;
264
+
265
+    stride >>= 1;
266
+    for (i = 0; i < 8; i++) {
267
+        for (j = 0; j < 8; j++) {
268
+            block[j] = p[j];
269
+        }
270
+        p += stride;
271
+        block += 8;
272
+    }
273
+}
274
+
275
+static void fdct_get(uint8_t *pixels, int stride, DCTELEM* block)
276
+{
277
+    get(pixels, stride, block);
278
+    ff_jpeg_fdct_islow_10(block);
279
+}
280
+
281
+static int encode_slice_plane(AVCodecContext *avctx, int mb_count,
282
+        uint8_t *src, int src_stride, uint8_t *buf, unsigned buf_size,
283
+        int *qmat, int chroma)
284
+{
285
+    DECLARE_ALIGNED(16, DCTELEM, blocks)[DEFAULT_SLICE_MB_WIDTH << 8], *block;
286
+    int i, blocks_per_slice;
287
+    PutBitContext pb;
288
+
289
+    block = blocks;
290
+    for (i = 0; i < mb_count; i++) {
291
+        fdct_get(src,                  src_stride, block + (0 << 6));
292
+        fdct_get(src + 8 * src_stride, src_stride, block + ((2 - chroma) << 6));
293
+        if (!chroma) {
294
+            fdct_get(src + 16,                  src_stride, block + (1 << 6));
295
+            fdct_get(src + 16 + 8 * src_stride, src_stride, block + (3 << 6));
296
+        }
297
+
298
+        block += (256 >> chroma);
299
+        src   += (32  >> chroma);
300
+    }
301
+
302
+    blocks_per_slice = mb_count << (2 - chroma);
303
+    init_put_bits(&pb, buf, buf_size << 3);
304
+
305
+    encode_dc_coeffs(&pb, blocks, blocks_per_slice, qmat);
306
+    encode_ac_coeffs(avctx, &pb, blocks, blocks_per_slice, qmat);
307
+
308
+    flush_put_bits(&pb);
309
+    return put_bits_ptr(&pb) - pb.buf;
310
+}
311
+
312
+static av_always_inline unsigned encode_slice_data(AVCodecContext *avctx,
313
+        uint8_t *dest_y, uint8_t *dest_u, uint8_t *dest_v, int luma_stride,
314
+        int chroma_stride, unsigned mb_count, uint8_t *buf, unsigned data_size,
315
+        unsigned* y_data_size, unsigned* u_data_size, unsigned* v_data_size,
316
+        int qp)
317
+{
318
+    ProresContext* ctx = (ProresContext*) avctx->priv_data;
319
+
320
+    *y_data_size = encode_slice_plane(avctx, mb_count, dest_y, luma_stride,
321
+            buf, data_size, ctx->qmat_luma[qp - 1], 0);
322
+
323
+    if (!(avctx->flags & CODEC_FLAG_GRAY)) {
324
+        *u_data_size = encode_slice_plane(avctx, mb_count, dest_u,
325
+                chroma_stride, buf + *y_data_size, data_size - *y_data_size,
326
+                ctx->qmat_chroma[qp - 1], 1);
327
+
328
+        *v_data_size = encode_slice_plane(avctx, mb_count, dest_v,
329
+                chroma_stride, buf + *y_data_size + *u_data_size,
330
+                data_size - *y_data_size - *u_data_size,
331
+                ctx->qmat_chroma[qp - 1], 1);
332
+    }
333
+
334
+    return *y_data_size + *u_data_size + *v_data_size;
335
+}
336
+
337
+static void subimage_with_fill(uint16_t *src, unsigned x, unsigned y,
338
+        unsigned stride, unsigned width, unsigned height, uint16_t *dst,
339
+        unsigned dst_width, unsigned dst_height)
340
+{
341
+
342
+    int box_width = FFMIN(width - x, dst_width);
343
+    int box_height = FFMIN(height - y, dst_height);
344
+    int i, j, src_stride = stride >> 1;
345
+    uint16_t last_pix, *last_line;
346
+
347
+    src += y * src_stride + x;
348
+    for (i = 0; i < box_height; ++i) {
349
+        for (j = 0; j < box_width; ++j) {
350
+            dst[j] = src[j];
351
+        }
352
+        last_pix = dst[j - 1];
353
+        for (; j < dst_width; j++)
354
+            dst[j] = last_pix;
355
+        src += src_stride;
356
+        dst += dst_width;
357
+    }
358
+    last_line = dst - dst_width;
359
+    for (; i < dst_height; i++) {
360
+        for (j = 0; j < dst_width; ++j) {
361
+            dst[j] = last_line[j];
362
+        }
363
+        dst += dst_width;
364
+    }
365
+}
366
+
367
+static int encode_slice(AVCodecContext *avctx, AVFrame *pic, int mb_x,
368
+        int mb_y, unsigned mb_count, uint8_t *buf, unsigned data_size,
369
+        int unsafe, int *qp)
370
+{
371
+    int luma_stride, chroma_stride;
372
+    int hdr_size = 6, slice_size;
373
+    uint8_t *dest_y, *dest_u, *dest_v;
374
+    unsigned y_data_size = 0, u_data_size = 0, v_data_size = 0;
375
+    ProresContext* ctx = (ProresContext*)avctx->priv_data;
376
+    int tgt_bits   = (mb_count * bitrate_table[avctx->profile]) >> 2;
377
+    int low_bytes  = (tgt_bits - (tgt_bits >> 3)) >> 3; // 12% bitrate fluctuation
378
+    int high_bytes = (tgt_bits + (tgt_bits >> 3)) >> 3;
379
+
380
+    luma_stride   = pic->linesize[0];
381
+    chroma_stride = pic->linesize[1];
382
+
383
+    dest_y = pic->data[0] + (mb_y << 4) * luma_stride   + (mb_x << 5);
384
+    dest_u = pic->data[1] + (mb_y << 4) * chroma_stride + (mb_x << 4);
385
+    dest_v = pic->data[2] + (mb_y << 4) * chroma_stride + (mb_x << 4);
386
+
387
+    if (unsafe) {
388
+
389
+        subimage_with_fill((uint16_t *) pic->data[0], mb_x << 4, mb_y << 4,
390
+                luma_stride, avctx->width, avctx->height,
391
+                (uint16_t *) ctx->fill_y, mb_count << 4, 16);
392
+        subimage_with_fill((uint16_t *) pic->data[1], mb_x << 3, mb_y << 4,
393
+                chroma_stride, avctx->width >> 1, avctx->height,
394
+                (uint16_t *) ctx->fill_u, mb_count << 3, 16);
395
+        subimage_with_fill((uint16_t *) pic->data[2], mb_x << 3, mb_y << 4,
396
+                chroma_stride, avctx->width >> 1, avctx->height,
397
+                (uint16_t *) ctx->fill_v, mb_count << 3, 16);
398
+
399
+        encode_slice_data(avctx, ctx->fill_y, ctx->fill_u, ctx->fill_v,
400
+                mb_count << 5, mb_count << 4, mb_count, buf + hdr_size,
401
+                data_size - hdr_size, &y_data_size, &u_data_size, &v_data_size,
402
+                *qp);
403
+    } else {
404
+        slice_size = encode_slice_data(avctx, dest_y, dest_u, dest_v,
405
+                luma_stride, chroma_stride, mb_count, buf + hdr_size,
406
+                data_size - hdr_size, &y_data_size, &u_data_size, &v_data_size,
407
+                *qp);
408
+
409
+        if (slice_size > high_bytes && *qp < qp_end_table[avctx->profile]) {
410
+            do {
411
+                *qp += 1;
412
+                slice_size = encode_slice_data(avctx, dest_y, dest_u, dest_v,
413
+                        luma_stride, chroma_stride, mb_count, buf + hdr_size,
414
+                        data_size - hdr_size, &y_data_size, &u_data_size,
415
+                        &v_data_size, *qp);
416
+            } while (slice_size > high_bytes && *qp < qp_end_table[avctx->profile]);
417
+        } else if (slice_size < low_bytes && *qp
418
+                > qp_start_table[avctx->profile]) {
419
+            do {
420
+                *qp -= 1;
421
+                slice_size = encode_slice_data(avctx, dest_y, dest_u, dest_v,
422
+                        luma_stride, chroma_stride, mb_count, buf + hdr_size,
423
+                        data_size - hdr_size, &y_data_size, &u_data_size,
424
+                        &v_data_size, *qp);
425
+            } while (slice_size < low_bytes && *qp > qp_start_table[avctx->profile]);
426
+        }
427
+    }
428
+
429
+    buf[0] = hdr_size << 3;
430
+    buf[1] = *qp;
431
+    AV_WB16(buf + 2, y_data_size);
432
+    AV_WB16(buf + 4, u_data_size);
433
+
434
+    return hdr_size + y_data_size + u_data_size + v_data_size;
435
+}
436
+
437
+static int prores_encode_picture(AVCodecContext *avctx, AVFrame *pic,
438
+        uint8_t *buf, const int buf_size)
439
+{
440
+    int mb_width = (avctx->width + 15) >> 4;
441
+    int mb_height = (avctx->height + 15) >> 4;
442
+    int hdr_size, sl_size, *slice_sizes;
443
+    int sl, mb_y, sl_data_size, qp;
444
+    int unsafe_bot, unsafe_right;
445
+    uint8_t *sl_data;
446
+    int slice_per_line = 0, rem = mb_width;
447
+
448
+    for (int i = av_log2(DEFAULT_SLICE_MB_WIDTH); i >= 0; --i) {
449
+        slice_per_line += rem >> i;
450
+        rem &= (1 << i) - 1;
451
+    }
452
+
453
+    qp = qp_start_table[avctx->profile];
454
+    slice_sizes = av_malloc(slice_per_line * mb_height * sizeof(int));
455
+    sl = 0; hdr_size = 8; sl_data_size = buf_size - hdr_size;
456
+    sl_data = buf + hdr_size + (slice_per_line * mb_height * 2);
457
+    for (mb_y = 0; mb_y < mb_height; mb_y++) {
458
+        int mb_x = 0;
459
+        int slice_mb_count = DEFAULT_SLICE_MB_WIDTH;
460
+        while (mb_x < mb_width) {
461
+            while (mb_width - mb_x < slice_mb_count)
462
+                slice_mb_count >>= 1;
463
+
464
+            unsafe_bot = (avctx->height & 0xf) && (mb_y == mb_height - 1);
465
+            unsafe_right = (avctx->width & 0xf) && (mb_x + slice_mb_count == mb_width);
466
+
467
+            sl_size = encode_slice(avctx, pic, mb_x, mb_y, slice_mb_count,
468
+                    sl_data, sl_data_size, unsafe_bot || unsafe_right, &qp);
469
+
470
+            slice_sizes[sl++]  = sl_size;
471
+            sl_data           += sl_size;
472
+            sl_data_size      -= sl_size;
473
+            mb_x              += slice_mb_count;
474
+        }
475
+    }
476
+
477
+    buf[0] = hdr_size << 3;
478
+    AV_WB32(buf + 1, sl_data - buf);
479
+    AV_WB16(buf + 5, slice_per_line * mb_height);
480
+    buf[7] = av_log2(DEFAULT_SLICE_MB_WIDTH) << 4;
481
+
482
+    for (int i = 0; i < slice_per_line * mb_height; i++)
483
+        AV_WB16(buf + hdr_size + (i << 1), slice_sizes[i]);
484
+
485
+    av_free(slice_sizes);
486
+
487
+    return sl_data - buf;
488
+}
489
+
490
+static int prores_encode_frame(AVCodecContext *avctx, unsigned char *buf,
491
+        int buf_size, void *data)
492
+{
493
+    AVFrame *pic = data;
494
+
495
+    int header_size = 148;
496
+    int pic_size = prores_encode_picture(avctx, pic, buf + header_size + 8,
497
+            buf_size - header_size - 8);
498
+
499
+    AV_WB32(buf, pic_size + 8 + header_size);
500
+    AV_WB8 (buf + 4, 'i');
501
+    AV_WB8 (buf + 5, 'c');
502
+    AV_WB8 (buf + 6, 'p');
503
+    AV_WB8 (buf + 7, 'f');
504
+
505
+    AV_WB16(buf + 8, header_size);
506
+    AV_WB16(buf + 10, 0);
507
+    AV_WB8 (buf + 12, 'f');
508
+    AV_WB8 (buf + 13, 'm');
509
+    AV_WB8 (buf + 14, 'p');
510
+    AV_WB8 (buf + 15, 'g');
511
+    AV_WB16(buf + 16, pic->width);
512
+    AV_WB16(buf + 18, pic->height);
513
+    buf[20] = 0x83; // {10}(422){00}{00}(frame){11}
514
+    buf[21] = 0;
515
+    buf[22] = 2;
516
+    buf[23] = 2;
517
+    buf[24] = 6;
518
+    buf[25] = 32;
519
+    buf[26] = 0;
520
+    buf[27] = 3;
521
+
522
+    memcpy(buf + 28, QMAT_LUMA[avctx->profile], 64);
523
+    memcpy(buf + 92, QMAT_CHROMA[avctx->profile], 64);
524
+
525
+    return pic_size + 8 + header_size;
526
+}
527
+
528
+static void scale_mat(const uint8_t* src, int* dst, int scale)
529
+{
530
+    int i;
531
+    for (i = 0; i < 64; i++)
532
+        dst[i] = src[i] * scale;
533
+}
534
+
535
+static av_cold int prores_encode_init(AVCodecContext *avctx)
536
+{
537
+    int i;
538
+    ProresContext* ctx = (ProresContext*)avctx->priv_data;
539
+
540
+    if (avctx->pix_fmt != PIX_FMT_YUV422P10LE) {
541
+        av_log(avctx, AV_LOG_ERROR, "need YUV422P10\n");
542
+        return -1;
543
+    }
544
+    if (avctx->width & 0x1) {
545
+        av_log(avctx, AV_LOG_ERROR,
546
+                "frame width needs to be multiple of 2\n");
547
+        return -1;
548
+    }
549
+
550
+    memset(ctx, 0, sizeof(ProresContext));
551
+    if ((avctx->height & 0xf) || (avctx->width & 0xf)) {
552
+        ctx->fill_y = av_malloc(DEFAULT_SLICE_MB_WIDTH << 9);
553
+        ctx->fill_u = av_malloc(DEFAULT_SLICE_MB_WIDTH << 8);
554
+        ctx->fill_v = av_malloc(DEFAULT_SLICE_MB_WIDTH << 8);
555
+    }
556
+
557
+    if (avctx->profile == FF_PROFILE_UNKNOWN) {
558
+        avctx->profile = FF_PROFILE_PRORES_STANDARD;
559
+        av_log(avctx, AV_LOG_INFO,
560
+                "encoding with ProRes standard (apcn) profile\n");
561
+
562
+    } else if (avctx->profile < FF_PROFILE_PRORES_PROXY
563
+            || avctx->profile > FF_PROFILE_PRORES_HQ) {
564
+        av_log(
565
+                avctx,
566
+                AV_LOG_ERROR,
567
+                "unknown profile %d, use [0 - apco, 1 - apcs, 2 - apcn (default), 3 - apch]\n",
568
+                avctx->profile);
569
+        return -1;
570
+    }
571
+
572
+    avctx->codec_tag = AV_RL32((const uint8_t*)profiles[avctx->profile].name);
573
+
574
+    for (i = 1; i <= 16; i++) {
575
+        scale_mat(QMAT_LUMA[avctx->profile]  , ctx->qmat_luma[i - 1]  , i);
576
+        scale_mat(QMAT_CHROMA[avctx->profile], ctx->qmat_chroma[i - 1], i);
577
+    }
578
+
579
+    avctx->coded_frame = avcodec_alloc_frame();
580
+    avctx->coded_frame->key_frame = 1;
581
+    avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
582
+
583
+    return 0;
584
+}
585
+
586
+static av_cold int prores_encode_close(AVCodecContext *avctx)
587
+{
588
+    ProresContext* ctx = (ProresContext*)avctx->priv_data;
589
+    av_freep(&avctx->coded_frame);
590
+    av_free(ctx->fill_y);
591
+    av_free(ctx->fill_u);
592
+    av_free(ctx->fill_v);
593
+
594
+    return 0;
595
+}
596
+
597
+AVCodec ff_prores_encoder = {
598
+    .name           = "prores",
599
+    .type           = AVMEDIA_TYPE_VIDEO,
600
+    .id             = CODEC_ID_PRORES,
601
+    .priv_data_size = sizeof(ProresContext),
602
+    .init           = prores_encode_init,
603
+    .close          = prores_encode_close,
604
+    .encode         = prores_encode_frame,
605
+    .pix_fmts       = (const enum PixelFormat[]){PIX_FMT_YUV422P10LE, PIX_FMT_NONE},
606
+    .long_name      = NULL_IF_CONFIG_SMALL("Apple ProRes"),
607
+    .capabilities   = 0,
608
+    .profiles       = profiles
609
+};