xref: /third_party/ffmpeg/libavcodec/cfhdenc.c (revision cabdff1a)
1/*
2 * Copyright (c) 2020 Paul B Mahol
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/**
22 * @file
23 * Cineform HD video encoder
24 */
25
26#include <stdlib.h>
27#include <string.h>
28
29#include "libavutil/imgutils.h"
30#include "libavutil/opt.h"
31
32#include "avcodec.h"
33#include "bytestream.h"
34#include "cfhd.h"
35#include "cfhdencdsp.h"
36#include "codec_internal.h"
37#include "encode.h"
38#include "put_bits.h"
39#include "thread.h"
40
41/* Derived from existing tables from decoder */
42static const unsigned codebook[256][2] = {
43    { 1, 0x00000000 }, { 2, 0x00000002 }, { 3, 0x00000007 }, { 5, 0x00000019 }, { 6, 0x00000030 },
44    { 6, 0x00000036 }, { 7, 0x00000063 }, { 7, 0x0000006B }, { 7, 0x0000006F }, { 8, 0x000000D4 },
45    { 8, 0x000000DC }, { 9, 0x00000189 }, { 9, 0x000001A0 }, { 9, 0x000001AB }, {10, 0x00000310 },
46    {10, 0x00000316 }, {10, 0x00000354 }, {10, 0x00000375 }, {10, 0x00000377 }, {11, 0x00000623 },
47    {11, 0x00000684 }, {11, 0x000006AB }, {11, 0x000006EC }, {12, 0x00000C44 }, {12, 0x00000C5C },
48    {12, 0x00000C5E }, {12, 0x00000D55 }, {12, 0x00000DD1 }, {12, 0x00000DD3 }, {12, 0x00000DDB },
49    {13, 0x0000188B }, {13, 0x000018BB }, {13, 0x00001AA8 }, {13, 0x00001BA0 }, {13, 0x00001BA4 },
50    {13, 0x00001BB5 }, {14, 0x00003115 }, {14, 0x00003175 }, {14, 0x0000317D }, {14, 0x00003553 },
51    {14, 0x00003768 }, {15, 0x00006228 }, {15, 0x000062E8 }, {15, 0x000062F8 }, {15, 0x00006AA4 },
52    {15, 0x00006E85 }, {15, 0x00006E87 }, {15, 0x00006ED3 }, {16, 0x0000C453 }, {16, 0x0000C5D3 },
53    {16, 0x0000C5F3 }, {16, 0x0000DD08 }, {16, 0x0000DD0C }, {16, 0x0000DDA4 }, {17, 0x000188A4 },
54    {17, 0x00018BA5 }, {17, 0x00018BE5 }, {17, 0x0001AA95 }, {17, 0x0001AA97 }, {17, 0x0001BA13 },
55    {17, 0x0001BB4A }, {17, 0x0001BB4B }, {18, 0x00031748 }, {18, 0x000317C8 }, {18, 0x00035528 },
56    {18, 0x0003552C }, {18, 0x00037424 }, {18, 0x00037434 }, {18, 0x00037436 }, {19, 0x00062294 },
57    {19, 0x00062E92 }, {19, 0x00062F92 }, {19, 0x0006AA52 }, {19, 0x0006AA5A }, {19, 0x0006E84A },
58    {19, 0x0006E86A }, {19, 0x0006E86E }, {20, 0x000C452A }, {20, 0x000C5D27 }, {20, 0x000C5F26 },
59    {20, 0x000D54A6 }, {20, 0x000D54B6 }, {20, 0x000DD096 }, {20, 0x000DD0D6 }, {20, 0x000DD0DE },
60    {21, 0x00188A56 }, {21, 0x0018BA4D }, {21, 0x0018BE4E }, {21, 0x0018BE4F }, {21, 0x001AA96E },
61    {21, 0x001BA12E }, {21, 0x001BA12F }, {21, 0x001BA1AF }, {21, 0x001BA1BF }, {22, 0x00317498 },
62    {22, 0x0035529C }, {22, 0x0035529D }, {22, 0x003552DE }, {22, 0x003552DF }, {22, 0x0037435D },
63    {22, 0x0037437D }, {23, 0x0062295D }, {23, 0x0062E933 }, {23, 0x006AA53D }, {23, 0x006AA53E },
64    {23, 0x006AA53F }, {23, 0x006E86B9 }, {23, 0x006E86F8 }, {24, 0x00C452B8 }, {24, 0x00C5D265 },
65    {24, 0x00D54A78 }, {24, 0x00D54A79 }, {24, 0x00DD0D70 }, {24, 0x00DD0D71 }, {24, 0x00DD0DF2 },
66    {24, 0x00DD0DF3 }, {26, 0x03114BA2 }, {25, 0x0188A5B1 }, {25, 0x0188A58B }, {25, 0x0188A595 },
67    {25, 0x0188A5D6 }, {25, 0x0188A5D7 }, {25, 0x0188A5A8 }, {25, 0x0188A5AE }, {25, 0x0188A5AF },
68    {25, 0x0188A5C4 }, {25, 0x0188A5C5 }, {25, 0x0188A587 }, {25, 0x0188A584 }, {25, 0x0188A585 },
69    {25, 0x0188A5C6 }, {25, 0x0188A5C7 }, {25, 0x0188A5CC }, {25, 0x0188A5CD }, {25, 0x0188A581 },
70    {25, 0x0188A582 }, {25, 0x0188A583 }, {25, 0x0188A5CE }, {25, 0x0188A5CF }, {25, 0x0188A5C2 },
71    {25, 0x0188A5C3 }, {25, 0x0188A5C1 }, {25, 0x0188A5B4 }, {25, 0x0188A5B5 }, {25, 0x0188A5E6 },
72    {25, 0x0188A5E7 }, {25, 0x0188A5E4 }, {25, 0x0188A5E5 }, {25, 0x0188A5AB }, {25, 0x0188A5E0 },
73    {25, 0x0188A5E1 }, {25, 0x0188A5E2 }, {25, 0x0188A5E3 }, {25, 0x0188A5B6 }, {25, 0x0188A5B7 },
74    {25, 0x0188A5FD }, {25, 0x0188A57E }, {25, 0x0188A57F }, {25, 0x0188A5EC }, {25, 0x0188A5ED },
75    {25, 0x0188A5FE }, {25, 0x0188A5FF }, {25, 0x0188A57D }, {25, 0x0188A59C }, {25, 0x0188A59D },
76    {25, 0x0188A5E8 }, {25, 0x0188A5E9 }, {25, 0x0188A5EA }, {25, 0x0188A5EB }, {25, 0x0188A5EF },
77    {25, 0x0188A57A }, {25, 0x0188A57B }, {25, 0x0188A578 }, {25, 0x0188A579 }, {25, 0x0188A5BA },
78    {25, 0x0188A5BB }, {25, 0x0188A5B8 }, {25, 0x0188A5B9 }, {25, 0x0188A588 }, {25, 0x0188A589 },
79    {25, 0x018BA4C8 }, {25, 0x018BA4C9 }, {25, 0x0188A5FA }, {25, 0x0188A5FB }, {25, 0x0188A5BC },
80    {25, 0x0188A5BD }, {25, 0x0188A598 }, {25, 0x0188A599 }, {25, 0x0188A5F4 }, {25, 0x0188A5F5 },
81    {25, 0x0188A59B }, {25, 0x0188A5DE }, {25, 0x0188A5DF }, {25, 0x0188A596 }, {25, 0x0188A597 },
82    {25, 0x0188A5F8 }, {25, 0x0188A5F9 }, {25, 0x0188A5F1 }, {25, 0x0188A58E }, {25, 0x0188A58F },
83    {25, 0x0188A5DC }, {25, 0x0188A5DD }, {25, 0x0188A5F2 }, {25, 0x0188A5F3 }, {25, 0x0188A58C },
84    {25, 0x0188A58D }, {25, 0x0188A5A4 }, {25, 0x0188A5F0 }, {25, 0x0188A5A5 }, {25, 0x0188A5A6 },
85    {25, 0x0188A5A7 }, {25, 0x0188A59A }, {25, 0x0188A5A2 }, {25, 0x0188A5A3 }, {25, 0x0188A58A },
86    {25, 0x0188A5B0 }, {25, 0x0188A5A0 }, {25, 0x0188A5A1 }, {25, 0x0188A5DA }, {25, 0x0188A5DB },
87    {25, 0x0188A59E }, {25, 0x0188A59F }, {25, 0x0188A5D8 }, {25, 0x0188A5EE }, {25, 0x0188A5D9 },
88    {25, 0x0188A5F6 }, {25, 0x0188A5F7 }, {25, 0x0188A57C }, {25, 0x0188A5C8 }, {25, 0x0188A5C9 },
89    {25, 0x0188A594 }, {25, 0x0188A5FC }, {25, 0x0188A5CA }, {25, 0x0188A5CB }, {25, 0x0188A5B2 },
90    {25, 0x0188A5AA }, {25, 0x0188A5B3 }, {25, 0x0188A572 }, {25, 0x0188A573 }, {25, 0x0188A5C0 },
91    {25, 0x0188A5BE }, {25, 0x0188A5BF }, {25, 0x0188A592 }, {25, 0x0188A580 }, {25, 0x0188A593 },
92    {25, 0x0188A590 }, {25, 0x0188A591 }, {25, 0x0188A586 }, {25, 0x0188A5A9 }, {25, 0x0188A5D2 },
93    {25, 0x0188A5D3 }, {25, 0x0188A5D4 }, {25, 0x0188A5D5 }, {25, 0x0188A5AC }, {25, 0x0188A5AD },
94    {25, 0x0188A5D0 },
95};
96
97/* Derived by extracting runcodes from existing tables from decoder */
98static const uint16_t runbook[18][3] = {
99    {1,  0x0000,   1}, {2,  0x0000,   2}, {3,  0x0000,   3}, {4,  0x0000,   4},
100    {5,  0x0000,   5}, {6,  0x0000,   6}, {7,  0x0000,   7}, {8,  0x0000,   8},
101    {9,  0x0000,   9}, {10, 0x0000,  10}, {11, 0x0000,  11},
102    {7,  0x0069,  12}, {8,  0x00D1,  20}, {9,  0x018A,  32},
103    {10, 0x0343,  60}, {11, 0x0685, 100}, {13, 0x18BF, 180}, {13, 0x1BA5, 320},
104};
105
106/*
107 * Derived by inspecting various quality encodes
108 * and adding some more from scratch.
109 */
110static const uint16_t quantization_per_subband[2][3][13][9] = {
111    {{
112        { 16, 16,  8,  4,  4,  2,   6,   6,   9, }, // film3+
113        { 16, 16,  8,  4,  4,  2,   6,   6,   9, }, // film3
114        { 16, 16,  8,  4,  4,  2,   7,   7,  10, }, // film2+
115        { 16, 16,  8,  4,  4,  2,   8,   8,  12, }, // film2
116        { 16, 16,  8,  4,  4,  2,  16,  16,  26, }, // film1++
117        { 24, 24, 12,  6,  6,  3,  24,  24,  36, }, // film1+
118        { 24, 24, 12,  6,  6,  3,  24,  24,  36, }, // film1
119        { 32, 32, 24,  8,  8,  6,  32,  32,  48, }, // high+
120        { 32, 32, 24,  8,  8,  6,  32,  32,  48, }, // high
121        { 48, 48, 32, 12, 12,  8,  64,  64,  96, }, // medium+
122        { 48, 48, 32, 12, 12,  8,  64,  64,  96, }, // medium
123        { 64, 64, 48, 16, 16, 12,  96,  96, 144, }, // low+
124        { 64, 64, 48, 16, 16, 12, 128, 128, 192, }, // low
125    },
126    {
127        { 16, 16,  8,  4,  4,  2,   6,   6,   9, }, // film3+
128        { 16, 16,  8,  4,  4,  2,   6,   6,  12, }, // film3
129        { 16, 16,  8,  4,  4,  2,   7,   7,  14, }, // film2+
130        { 16, 16,  8,  4,  4,  2,   8,   8,  16, }, // film2
131        { 16, 16,  8,  4,  4,  2,  16,  16,  26, }, // film1++
132        { 24, 24, 12,  6,  6,  3,  24,  24,  36, }, // film1+
133        { 24, 24, 12,  6,  6,  3,  24,  24,  48, }, // film1
134        { 32, 32, 24,  8,  8,  6,  32,  32,  48, }, // high+
135        { 48, 48, 32, 12, 12,  8,  32,  32,  64, }, // high
136        { 48, 48, 32, 12, 12,  8,  64,  64,  96, }, // medium+
137        { 48, 48, 32, 12, 12,  8,  64,  64, 128, }, // medium
138        { 64, 64, 48, 16, 16, 12,  96,  96, 160, }, // low+
139        { 64, 64, 48, 16, 16, 12, 128, 128, 192, }, // low
140    },
141    {
142        { 16, 16,  8,  4,  4,  2,   6,   6,   9, }, // film3+
143        { 16, 16,  8,  4,  4,  2,   6,   6,  12, }, // film3
144        { 16, 16,  8,  4,  4,  2,   7,   7,  14, }, // film2+
145        { 16, 16,  8,  4,  4,  2,   8,   8,  16, }, // film2
146        { 16, 16,  8,  4,  4,  2,  16,  16,  26, }, // film1++
147        { 24, 24, 12,  6,  6,  3,  24,  24,  36, }, // film1+
148        { 24, 24, 12,  6,  6,  3,  24,  24,  48, }, // film1
149        { 32, 32, 24,  8,  8,  6,  32,  32,  48, }, // high+
150        { 48, 48, 32, 12, 12,  8,  32,  32,  64, }, // high
151        { 48, 48, 32, 12, 12,  8,  64,  64,  96, }, // medium+
152        { 48, 48, 32, 12, 12,  8,  64,  64, 128, }, // medium
153        { 64, 64, 48, 16, 16, 12,  96,  96, 160, }, // low+
154        { 64, 64, 48, 16, 16, 12, 128, 128, 192, }, // low
155    }},
156    {{
157        { 16, 16,  8, 16, 16,  8,  24,  24,  36, }, // film3+
158        { 16, 16,  8, 16, 16,  8,  24,  24,  36, }, // film3
159        { 16, 16,  8, 16, 16,  8,  32,  32,  48, }, // film2+
160        { 16, 16,  8, 16, 16,  8,  32,  32,  48, }, // film2
161        { 16, 16,  8, 20, 20, 10,  80,  80, 128, }, // film1++
162        { 24, 24, 12, 24, 24, 12,  96,  96, 144, }, // film1+
163        { 24, 24, 12, 24, 24, 12,  96,  96, 144, }, // film1
164        { 32, 32, 24, 32, 32, 24, 128, 128, 192, }, // high+
165        { 32, 32, 24, 32, 32, 24, 128, 128, 192, }, // high
166        { 48, 48, 32, 48, 48, 32, 256, 256, 384, }, // medium+
167        { 48, 48, 32, 48, 48, 32, 256, 256, 384, }, // medium
168        { 56, 56, 40, 56, 56, 40, 512, 512, 768, }, // low+
169        { 64, 64, 48, 64, 64, 48, 512, 512, 768, }, // low
170    },
171    {
172        { 16, 16,  8, 16, 16,  8,  24,  24,  36, }, // film3+
173        { 16, 16,  8, 16, 16,  8,  48,  48,  72, }, // film3
174        { 16, 16,  8, 16, 16,  8,  48,  48,  72, }, // film2+
175        { 16, 16,  8, 16, 16,  8,  64,  64,  96, }, // film2
176        { 16, 16,  8, 20, 20, 10,  80,  80, 128, }, // film1++
177        { 24, 24, 12, 24, 24, 12,  96,  96, 144, }, // film1+
178        { 24, 24, 12, 24, 24, 12, 192, 192, 288, }, // film1
179        { 32, 32, 24, 32, 32, 24, 128, 128, 192, }, // high+
180        { 32, 32, 24, 32, 32, 24, 256, 256, 384, }, // high
181        { 48, 48, 32, 48, 48, 32, 256, 256, 384, }, // medium+
182        { 48, 48, 32, 48, 48, 32, 512, 512, 768, }, // medium
183        { 56, 56, 40, 56, 56, 40, 512, 512, 768, }, // low+
184        { 64, 64, 48, 64, 64, 48,1024,1024,1536, }, // low
185    },
186    {
187        { 16, 16,  8, 16, 16,  8,  24,  24,  36, }, // film3+
188        { 16, 16,  8, 16, 16,  8,  48,  48,  72, }, // film3
189        { 16, 16,  8, 16, 16,  8,  48,  48,  72, }, // film2+
190        { 16, 16,  8, 16, 16,  8,  64,  64,  96, }, // film2
191        { 16, 16, 10, 20, 20, 10,  80,  80, 128, }, // film1++
192        { 24, 24, 12, 24, 24, 12,  96,  96, 144, }, // film1+
193        { 24, 24, 12, 24, 24, 12, 192, 192, 288, }, // film1
194        { 32, 32, 24, 32, 32, 24, 128, 128, 192, }, // high+
195        { 32, 32, 24, 32, 32, 24, 256, 256, 384, }, // high
196        { 48, 48, 32, 48, 48, 32, 256, 256, 384, }, // medium+
197        { 48, 48, 32, 48, 48, 32, 512, 512, 768, }, // medium
198        { 56, 56, 40, 56, 56, 40, 512, 512, 768, }, // low+
199        { 64, 64, 48, 64, 64, 48,1024,1024,1536, }, // low
200    }},
201};
202
203typedef struct Codebook {
204    unsigned bits;
205    unsigned size;
206} Codebook;
207
208typedef struct Runbook {
209    unsigned size;
210    unsigned bits;
211    unsigned run;
212} Runbook;
213
214typedef struct PlaneEnc {
215    unsigned size;
216
217    int16_t *dwt_buf;
218    int16_t *dwt_tmp;
219
220    unsigned quantization[SUBBAND_COUNT];
221    int16_t *subband[SUBBAND_COUNT];
222    int16_t *l_h[8];
223
224    SubBand band[DWT_LEVELS][4];
225} PlaneEnc;
226
227typedef struct CFHDEncContext {
228    const AVClass *class;
229
230    PutBitContext       pb;
231    PutByteContext      pby;
232
233    int quality;
234    int planes;
235    int chroma_h_shift;
236    int chroma_v_shift;
237    PlaneEnc plane[4];
238
239    uint16_t lut[1024];
240    Runbook  rb[321];
241    Codebook cb[513];
242    int16_t *alpha;
243
244    CFHDEncDSPContext dsp;
245} CFHDEncContext;
246
247static av_cold int cfhd_encode_init(AVCodecContext *avctx)
248{
249    CFHDEncContext *s = avctx->priv_data;
250    const int sign_mask = 256;
251    const int twos_complement = -sign_mask;
252    const int mag_mask = sign_mask - 1;
253    int ret, last = 0;
254
255    ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt,
256                                           &s->chroma_h_shift,
257                                           &s->chroma_v_shift);
258    if (ret < 0)
259        return ret;
260
261    if (avctx->width & 15) {
262        av_log(avctx, AV_LOG_ERROR, "Width must be multiple of 16.\n");
263        return AVERROR_INVALIDDATA;
264    }
265
266    s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
267
268    for (int i = 0; i < s->planes; i++) {
269        int w8, h8, w4, h4, w2, h2;
270        int width  = i ? avctx->width >> s->chroma_h_shift : avctx->width;
271        int height = i ? FFALIGN(avctx->height >> s->chroma_v_shift, 8) :
272                         FFALIGN(avctx->height >> s->chroma_v_shift, 8);
273        ptrdiff_t stride = (FFALIGN(width / 8, 8) + 64) * 8;
274
275        w8 = FFALIGN(width / 8, 8) + 64;
276        h8 = FFALIGN(height, 8) / 8;
277        w4 = w8 * 2;
278        h4 = h8 * 2;
279        w2 = w4 * 2;
280        h2 = h4 * 2;
281
282        s->plane[i].dwt_buf =
283            av_calloc(height * stride, sizeof(*s->plane[i].dwt_buf));
284        s->plane[i].dwt_tmp =
285            av_malloc_array(height * stride, sizeof(*s->plane[i].dwt_tmp));
286        if (!s->plane[i].dwt_buf || !s->plane[i].dwt_tmp)
287            return AVERROR(ENOMEM);
288
289        s->plane[i].subband[0] = s->plane[i].dwt_buf;
290        s->plane[i].subband[1] = s->plane[i].dwt_buf + 2 * w8 * h8;
291        s->plane[i].subband[2] = s->plane[i].dwt_buf + 1 * w8 * h8;
292        s->plane[i].subband[3] = s->plane[i].dwt_buf + 3 * w8 * h8;
293        s->plane[i].subband[4] = s->plane[i].dwt_buf + 2 * w4 * h4;
294        s->plane[i].subband[5] = s->plane[i].dwt_buf + 1 * w4 * h4;
295        s->plane[i].subband[6] = s->plane[i].dwt_buf + 3 * w4 * h4;
296        s->plane[i].subband[7] = s->plane[i].dwt_buf + 2 * w2 * h2;
297        s->plane[i].subband[8] = s->plane[i].dwt_buf + 1 * w2 * h2;
298        s->plane[i].subband[9] = s->plane[i].dwt_buf + 3 * w2 * h2;
299
300        for (int j = 0; j < DWT_LEVELS; j++) {
301            for (int k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
302                s->plane[i].band[j][k].width  = (width / 8) << j;
303                s->plane[i].band[j][k].height = (height / 8) << j;
304                s->plane[i].band[j][k].a_width  = w8 << j;
305                s->plane[i].band[j][k].a_height = h8 << j;
306            }
307        }
308
309        /* ll2 and ll1 commented out because they are done in-place */
310        s->plane[i].l_h[0] = s->plane[i].dwt_tmp;
311        s->plane[i].l_h[1] = s->plane[i].dwt_tmp + 2 * w8 * h8;
312        // s->plane[i].l_h[2] = ll2;
313        s->plane[i].l_h[3] = s->plane[i].dwt_tmp;
314        s->plane[i].l_h[4] = s->plane[i].dwt_tmp + 2 * w4 * h4;
315        // s->plane[i].l_h[5] = ll1;
316        s->plane[i].l_h[6] = s->plane[i].dwt_tmp;
317        s->plane[i].l_h[7] = s->plane[i].dwt_tmp + 2 * w2 * h2;
318    }
319
320    for (int i = 0; i < 512; i++) {
321        int value = (i & sign_mask) ? twos_complement + (i & mag_mask): i;
322        int mag = FFMIN(FFABS(value), 255);
323
324        if (mag) {
325            s->cb[i].bits = (codebook[mag][1] << 1) | (value > 0 ? 0 : 1);
326            s->cb[i].size = codebook[mag][0] + 1;
327        } else {
328            s->cb[i].bits = codebook[mag][1];
329            s->cb[i].size = codebook[mag][0];
330        }
331    }
332
333    s->cb[512].bits = 0x3114ba3;
334    s->cb[512].size = 26;
335
336    s->rb[0].run = 0;
337
338    for (int i = 1, j = 0; i < 320 && j < 17; j++) {
339        int run = runbook[j][2];
340        int end = runbook[j+1][2];
341
342        while (i < end) {
343            s->rb[i].run = run;
344            s->rb[i].bits = runbook[j][1];
345            s->rb[i++].size = runbook[j][0];
346        }
347    }
348
349    s->rb[320].bits = runbook[17][1];
350    s->rb[320].size = runbook[17][0];
351    s->rb[320].run = 320;
352
353    for (int i = 0; i < 256; i++) {
354        int idx = i + ((768LL * i * i * i) / (256 * 256 * 256));
355
356        s->lut[idx] = i;
357    }
358    for (int i = 0; i < 1024; i++) {
359        if (s->lut[i])
360            last = s->lut[i];
361        else
362            s->lut[i] = last;
363    }
364
365    ff_cfhdencdsp_init(&s->dsp);
366
367    if (s->planes != 4)
368        return 0;
369
370    s->alpha = av_calloc(avctx->width * avctx->height, sizeof(*s->alpha));
371    if (!s->alpha)
372        return AVERROR(ENOMEM);
373
374    return 0;
375}
376
377static void quantize_band(int16_t *input, int width, int a_width,
378                          int height, unsigned quantization)
379{
380    const int16_t factor = (uint32_t)(1U << 15) / quantization;
381
382    for (int i = 0; i < height; i++) {
383        for (int j = 0; j < width; j++)
384            input[j] = av_clip_intp2(((input[j] * factor + 16384 * FFSIGN(input[j])) / 32768), 10);
385        input += a_width;
386    }
387}
388
389static int put_runcode(PutBitContext *pb, int count, const Runbook *const rb)
390{
391    while (count > 0) {
392        const int index = FFMIN(320, count);
393
394        put_bits(pb, rb[index].size, rb[index].bits);
395        count -= rb[index].run;
396    }
397
398    return 0;
399}
400
401static void process_alpha(const int16_t *src, int width, int height, ptrdiff_t stride, int16_t *dst)
402{
403    for (int i = 0; i < height; i++) {
404        for (int j = 0; j < width; j++) {
405            int alpha = src[j];
406
407            if (alpha > 0 && alpha < 4080) {
408                alpha *= 223;
409                alpha += 128;
410                alpha >>= 8;
411                alpha += 256;
412            }
413
414            dst[j] = av_clip_uintp2(alpha, 12);
415        }
416
417        src += stride;
418        dst += width;
419    }
420}
421
422static int cfhd_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
423                             const AVFrame *frame, int *got_packet)
424{
425    CFHDEncContext *s = avctx->priv_data;
426    CFHDEncDSPContext *dsp = &s->dsp;
427    PutByteContext *pby = &s->pby;
428    PutBitContext *pb = &s->pb;
429    const Codebook *const cb = s->cb;
430    const Runbook *const rb = s->rb;
431    const uint16_t *lut = s->lut;
432    unsigned pos;
433    int ret;
434
435    for (int plane = 0; plane < s->planes; plane++) {
436        int width = s->plane[plane].band[2][0].width;
437        int a_width = s->plane[plane].band[2][0].a_width;
438        int height = s->plane[plane].band[2][0].height;
439        int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
440        int16_t *input = (int16_t *)frame->data[act_plane];
441        int16_t *low = s->plane[plane].l_h[6];
442        int16_t *high = s->plane[plane].l_h[7];
443        ptrdiff_t in_stride = frame->linesize[act_plane] / 2;
444        int low_stride, high_stride;
445
446        if (plane == 3) {
447            process_alpha(input, avctx->width, avctx->height,
448                          in_stride, s->alpha);
449            input = s->alpha;
450            in_stride = avctx->width;
451        }
452
453        dsp->horiz_filter(input, low, high,
454                          in_stride, a_width, a_width,
455                          width * 2, height * 2);
456
457        input = s->plane[plane].l_h[7];
458        low = s->plane[plane].subband[7];
459        low_stride = s->plane[plane].band[2][0].a_width;
460        high = s->plane[plane].subband[9];
461        high_stride = s->plane[plane].band[2][0].a_width;
462
463        dsp->vert_filter(input, low, high,
464                         a_width, low_stride, high_stride,
465                         width, height * 2);
466
467        input = s->plane[plane].l_h[6];
468        low = s->plane[plane].l_h[7];
469        high = s->plane[plane].subband[8];
470
471        dsp->vert_filter(input, low, high,
472                         a_width, low_stride, high_stride,
473                         width, height * 2);
474
475        a_width = s->plane[plane].band[1][0].a_width;
476        width = s->plane[plane].band[1][0].width;
477        height = s->plane[plane].band[1][0].height;
478        input = s->plane[plane].l_h[7];
479        low = s->plane[plane].l_h[3];
480        low_stride = s->plane[plane].band[1][0].a_width;
481        high = s->plane[plane].l_h[4];
482        high_stride = s->plane[plane].band[1][0].a_width;
483
484        for (int i = 0; i < height * 2; i++) {
485            for (int j = 0; j < width * 2; j++)
486                input[j] /= 4;
487            input += a_width * 2;
488        }
489
490        input = s->plane[plane].l_h[7];
491        dsp->horiz_filter(input, low, high,
492                          a_width * 2, low_stride, high_stride,
493                          width * 2, height * 2);
494
495        input = s->plane[plane].l_h[4];
496        low = s->plane[plane].subband[4];
497        high = s->plane[plane].subband[6];
498
499        dsp->vert_filter(input, low, high,
500                         a_width, low_stride, high_stride,
501                         width, height * 2);
502
503        input = s->plane[plane].l_h[3];
504        low = s->plane[plane].l_h[4];
505        high = s->plane[plane].subband[5];
506
507        dsp->vert_filter(input, low, high,
508                         a_width, low_stride, high_stride,
509                         width, height * 2);
510
511        a_width = s->plane[plane].band[0][0].a_width;
512        width = s->plane[plane].band[0][0].width;
513        height = s->plane[plane].band[0][0].height;
514        input = s->plane[plane].l_h[4];
515        low = s->plane[plane].l_h[0];
516        low_stride = s->plane[plane].band[0][0].a_width;
517        high = s->plane[plane].l_h[1];
518        high_stride = s->plane[plane].band[0][0].a_width;
519
520        if (avctx->pix_fmt != AV_PIX_FMT_YUV422P10) {
521            for (int i = 0; i < height * 2; i++) {
522                for (int j = 0; j < width * 2; j++)
523                    input[j] /= 4;
524                input += a_width * 2;
525            }
526        }
527
528        input = s->plane[plane].l_h[4];
529        dsp->horiz_filter(input, low, high,
530                          a_width * 2, low_stride, high_stride,
531                          width * 2, height * 2);
532
533        low = s->plane[plane].subband[1];
534        high = s->plane[plane].subband[3];
535        input = s->plane[plane].l_h[1];
536
537        dsp->vert_filter(input, low, high,
538                         a_width, low_stride, high_stride,
539                         width, height * 2);
540
541        low = s->plane[plane].subband[0];
542        high = s->plane[plane].subband[2];
543        input = s->plane[plane].l_h[0];
544
545        dsp->vert_filter(input, low, high,
546                         a_width, low_stride, high_stride,
547                         width, height * 2);
548    }
549
550    ret = ff_alloc_packet(avctx, pkt, 64LL + s->planes * (2LL * avctx->width * avctx->height + 1000LL));
551    if (ret < 0)
552        return ret;
553
554    bytestream2_init_writer(pby, pkt->data, pkt->size);
555
556    bytestream2_put_be16(pby, SampleType);
557    bytestream2_put_be16(pby, 9);
558
559    bytestream2_put_be16(pby, SampleIndexTable);
560    bytestream2_put_be16(pby, s->planes);
561
562    for (int i = 0; i < s->planes; i++)
563        bytestream2_put_be32(pby, 0);
564
565    bytestream2_put_be16(pby, TransformType);
566    bytestream2_put_be16(pby, 0);
567
568    bytestream2_put_be16(pby, NumFrames);
569    bytestream2_put_be16(pby, 1);
570
571    bytestream2_put_be16(pby, ChannelCount);
572    bytestream2_put_be16(pby, s->planes);
573
574    bytestream2_put_be16(pby, EncodedFormat);
575    bytestream2_put_be16(pby, avctx->pix_fmt == AV_PIX_FMT_YUV422P10 ? 1 : 3 + (s->planes == 4));
576
577    bytestream2_put_be16(pby, WaveletCount);
578    bytestream2_put_be16(pby, 3);
579
580    bytestream2_put_be16(pby, SubbandCount);
581    bytestream2_put_be16(pby, SUBBAND_COUNT);
582
583    bytestream2_put_be16(pby, NumSpatial);
584    bytestream2_put_be16(pby, 2);
585
586    bytestream2_put_be16(pby, FirstWavelet);
587    bytestream2_put_be16(pby, 3);
588
589    bytestream2_put_be16(pby, ImageWidth);
590    bytestream2_put_be16(pby, avctx->width);
591
592    bytestream2_put_be16(pby, ImageHeight);
593    bytestream2_put_be16(pby, avctx->height);
594
595    bytestream2_put_be16(pby, -FrameNumber);
596    bytestream2_put_be16(pby, frame->pts & 0xFFFF);
597
598    bytestream2_put_be16(pby, Precision);
599    bytestream2_put_be16(pby, avctx->pix_fmt == AV_PIX_FMT_YUV422P10 ? 10 : 12);
600
601    bytestream2_put_be16(pby, PrescaleTable);
602    bytestream2_put_be16(pby, avctx->pix_fmt == AV_PIX_FMT_YUV422P10 ? 0x2000 : 0x2800);
603
604    bytestream2_put_be16(pby, SampleFlags);
605    bytestream2_put_be16(pby, 1);
606
607    for (int p = 0; p < s->planes; p++) {
608        int width = s->plane[p].band[0][0].width;
609        int a_width = s->plane[p].band[0][0].a_width;
610        int height = s->plane[p].band[0][0].height;
611        int16_t *data = s->plane[p].subband[0];
612
613        if (p) {
614            bytestream2_put_be16(pby, SampleType);
615            bytestream2_put_be16(pby, 3);
616
617            bytestream2_put_be16(pby, ChannelNumber);
618            bytestream2_put_be16(pby, p);
619        }
620
621        bytestream2_put_be16(pby, BitstreamMarker);
622        bytestream2_put_be16(pby, 0x1a4a);
623
624        pos = bytestream2_tell_p(pby);
625
626        bytestream2_put_be16(pby, LowpassSubband);
627        bytestream2_put_be16(pby, 0);
628
629        bytestream2_put_be16(pby, NumLevels);
630        bytestream2_put_be16(pby, 3);
631
632        bytestream2_put_be16(pby, LowpassWidth);
633        bytestream2_put_be16(pby, width);
634
635        bytestream2_put_be16(pby, LowpassHeight);
636        bytestream2_put_be16(pby, height);
637
638        bytestream2_put_be16(pby, PixelOffset);
639        bytestream2_put_be16(pby, 0);
640
641        bytestream2_put_be16(pby, LowpassQuantization);
642        bytestream2_put_be16(pby, 1);
643
644        bytestream2_put_be16(pby, LowpassPrecision);
645        bytestream2_put_be16(pby, 16);
646
647        bytestream2_put_be16(pby, BitstreamMarker);
648        bytestream2_put_be16(pby, 0x0f0f);
649
650        for (int i = 0; i < height; i++) {
651            for (int j = 0; j < width; j++)
652                bytestream2_put_be16(pby, data[j]);
653            data += a_width;
654        }
655
656        bytestream2_put_be16(pby, BitstreamMarker);
657        bytestream2_put_be16(pby, 0x1b4b);
658
659        for (int l = 0; l < 3; l++) {
660            for (int i = 0; i < 3; i++) {
661                s->plane[p].quantization[1 + l * 3 + i] = quantization_per_subband[avctx->pix_fmt != AV_PIX_FMT_YUV422P10][p >= 3 ? 0 : p][s->quality][l * 3 + i];
662            }
663        }
664
665        for (int l = 0; l < 3; l++) {
666            int a_width = s->plane[p].band[l][0].a_width;
667            int width = s->plane[p].band[l][0].width;
668            int stride = FFALIGN(width, 8);
669            int height = s->plane[p].band[l][0].height;
670
671            bytestream2_put_be16(pby, BitstreamMarker);
672            bytestream2_put_be16(pby, 0x0d0d);
673
674            bytestream2_put_be16(pby, WaveletType);
675            bytestream2_put_be16(pby, 3 + 2 * (l == 2));
676
677            bytestream2_put_be16(pby, WaveletNumber);
678            bytestream2_put_be16(pby, 3 - l);
679
680            bytestream2_put_be16(pby, WaveletLevel);
681            bytestream2_put_be16(pby, 3 - l);
682
683            bytestream2_put_be16(pby, NumBands);
684            bytestream2_put_be16(pby, 4);
685
686            bytestream2_put_be16(pby, HighpassWidth);
687            bytestream2_put_be16(pby, width);
688
689            bytestream2_put_be16(pby, HighpassHeight);
690            bytestream2_put_be16(pby, height);
691
692            bytestream2_put_be16(pby, LowpassBorder);
693            bytestream2_put_be16(pby, 0);
694
695            bytestream2_put_be16(pby, HighpassBorder);
696            bytestream2_put_be16(pby, 0);
697
698            bytestream2_put_be16(pby, LowpassScale);
699            bytestream2_put_be16(pby, 1);
700
701            bytestream2_put_be16(pby, LowpassDivisor);
702            bytestream2_put_be16(pby, 1);
703
704            for (int i = 0; i < 3; i++) {
705                int16_t *data = s->plane[p].subband[1 + l * 3 + i];
706                int count = 0, padd = 0;
707
708                bytestream2_put_be16(pby, BitstreamMarker);
709                bytestream2_put_be16(pby, 0x0e0e);
710
711                bytestream2_put_be16(pby, SubbandNumber);
712                bytestream2_put_be16(pby, i + 1);
713
714                bytestream2_put_be16(pby, BandCodingFlags);
715                bytestream2_put_be16(pby, 1);
716
717                bytestream2_put_be16(pby, BandWidth);
718                bytestream2_put_be16(pby, width);
719
720                bytestream2_put_be16(pby, BandHeight);
721                bytestream2_put_be16(pby, height);
722
723                bytestream2_put_be16(pby, SubbandBand);
724                bytestream2_put_be16(pby, 1 + l * 3 + i);
725
726                bytestream2_put_be16(pby, BandEncoding);
727                bytestream2_put_be16(pby, 3);
728
729                bytestream2_put_be16(pby, Quantization);
730                bytestream2_put_be16(pby, s->plane[p].quantization[1 + l * 3 + i]);
731
732                bytestream2_put_be16(pby, BandScale);
733                bytestream2_put_be16(pby, 1);
734
735                bytestream2_put_be16(pby, BandHeader);
736                bytestream2_put_be16(pby, 0);
737
738                quantize_band(data, width, a_width, height,
739                              s->plane[p].quantization[1 + l * 3 + i]);
740
741                init_put_bits(pb, pkt->data + bytestream2_tell_p(pby), bytestream2_get_bytes_left_p(pby));
742
743                for (int m = 0; m < height; m++) {
744                    for (int j = 0; j < stride; j++) {
745                        int16_t index = j >= width ? 0 : FFSIGN(data[j]) * lut[FFABS(data[j])];
746
747                        if (index < 0)
748                            index += 512;
749                        if (index == 0) {
750                            count++;
751                            continue;
752                        } else if (count > 0) {
753                            count = put_runcode(pb, count, rb);
754                        }
755
756                        put_bits(pb, cb[index].size, cb[index].bits);
757                    }
758
759                    data += a_width;
760                }
761
762                if (count > 0) {
763                    count = put_runcode(pb, count, rb);
764                }
765
766                put_bits(pb, cb[512].size, cb[512].bits);
767
768                flush_put_bits(pb);
769                bytestream2_skip_p(pby, put_bytes_output(pb));
770                padd = (4 - (bytestream2_tell_p(pby) & 3)) & 3;
771                while (padd--)
772                    bytestream2_put_byte(pby, 0);
773
774                bytestream2_put_be16(pby, BandTrailer);
775                bytestream2_put_be16(pby, 0);
776            }
777
778            bytestream2_put_be16(pby, BitstreamMarker);
779            bytestream2_put_be16(pby, 0x0c0c);
780        }
781
782        s->plane[p].size = bytestream2_tell_p(pby) - pos;
783    }
784
785    bytestream2_put_be16(pby, GroupTrailer);
786    bytestream2_put_be16(pby, 0);
787
788    av_shrink_packet(pkt, bytestream2_tell_p(pby));
789
790    pkt->flags |= AV_PKT_FLAG_KEY;
791
792    bytestream2_seek_p(pby, 8, SEEK_SET);
793    for (int i = 0; i < s->planes; i++)
794        bytestream2_put_be32(pby, s->plane[i].size);
795
796    *got_packet = 1;
797
798    return 0;
799}
800
801static av_cold int cfhd_encode_close(AVCodecContext *avctx)
802{
803    CFHDEncContext *s = avctx->priv_data;
804
805    for (int i = 0; i < s->planes; i++) {
806        av_freep(&s->plane[i].dwt_buf);
807        av_freep(&s->plane[i].dwt_tmp);
808
809        for (int j = 0; j < SUBBAND_COUNT; j++)
810            s->plane[i].subband[j] = NULL;
811
812        for (int j = 0; j < 8; j++)
813            s->plane[i].l_h[j] = NULL;
814    }
815
816    av_freep(&s->alpha);
817
818    return 0;
819}
820
821#define OFFSET(x) offsetof(CFHDEncContext, x)
822#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
823static const AVOption options[] = {
824    { "quality", "set quality", OFFSET(quality), AV_OPT_TYPE_INT,   {.i64= 0}, 0, 12, VE, "q" },
825    { "film3+",   NULL,         0,               AV_OPT_TYPE_CONST, {.i64= 0}, 0,  0, VE, "q" },
826    { "film3",    NULL,         0,               AV_OPT_TYPE_CONST, {.i64= 1}, 0,  0, VE, "q" },
827    { "film2+",   NULL,         0,               AV_OPT_TYPE_CONST, {.i64= 2}, 0,  0, VE, "q" },
828    { "film2",    NULL,         0,               AV_OPT_TYPE_CONST, {.i64= 3}, 0,  0, VE, "q" },
829    { "film1.5",  NULL,         0,               AV_OPT_TYPE_CONST, {.i64= 4}, 0,  0, VE, "q" },
830    { "film1+",   NULL,         0,               AV_OPT_TYPE_CONST, {.i64= 5}, 0,  0, VE, "q" },
831    { "film1",    NULL,         0,               AV_OPT_TYPE_CONST, {.i64= 6}, 0,  0, VE, "q" },
832    { "high+",    NULL,         0,               AV_OPT_TYPE_CONST, {.i64= 7}, 0,  0, VE, "q" },
833    { "high",     NULL,         0,               AV_OPT_TYPE_CONST, {.i64= 8}, 0,  0, VE, "q" },
834    { "medium+",  NULL,         0,               AV_OPT_TYPE_CONST, {.i64= 9}, 0,  0, VE, "q" },
835    { "medium",   NULL,         0,               AV_OPT_TYPE_CONST, {.i64=10}, 0,  0, VE, "q" },
836    { "low+",     NULL,         0,               AV_OPT_TYPE_CONST, {.i64=11}, 0,  0, VE, "q" },
837    { "low",      NULL,         0,               AV_OPT_TYPE_CONST, {.i64=12}, 0,  0, VE, "q" },
838    { NULL},
839};
840
841static const AVClass cfhd_class = {
842    .class_name = "cfhd",
843    .item_name  = av_default_item_name,
844    .option     = options,
845    .version    = LIBAVUTIL_VERSION_INT,
846};
847
848const FFCodec ff_cfhd_encoder = {
849    .p.name           = "cfhd",
850    .p.long_name      = NULL_IF_CONFIG_SMALL("GoPro CineForm HD"),
851    .p.type           = AVMEDIA_TYPE_VIDEO,
852    .p.id             = AV_CODEC_ID_CFHD,
853    .priv_data_size   = sizeof(CFHDEncContext),
854    .p.priv_class     = &cfhd_class,
855    .init             = cfhd_encode_init,
856    .close            = cfhd_encode_close,
857    FF_CODEC_ENCODE_CB(cfhd_encode_frame),
858    .p.capabilities   = AV_CODEC_CAP_FRAME_THREADS,
859    .p.pix_fmts       = (const enum AVPixelFormat[]) {
860                          AV_PIX_FMT_YUV422P10,
861                          AV_PIX_FMT_GBRP12,
862                          AV_PIX_FMT_GBRAP12,
863                          AV_PIX_FMT_NONE
864                        },
865    .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
866};
867