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