FFmpeg
cfhdenc.c
Go to the documentation of this file.
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[18][3] = {
98  {1, 0x0000, 1}, {2, 0x0000, 2}, {3, 0x0000, 3}, {4, 0x0000, 4},
99  {5, 0x0000, 5}, {6, 0x0000, 6}, {7, 0x0000, 7}, {8, 0x0000, 8},
100  {9, 0x0000, 9}, {10, 0x0000, 10}, {11, 0x0000, 11},
101  {7, 0x0069, 12}, {8, 0x00D1, 20}, {9, 0x018A, 32},
102  {10, 0x0343, 60}, {11, 0x0685, 100}, {13, 0x18BF, 180}, {13, 0x1BA5, 320},
103 };
104 
105 /*
106  * Derived by inspecting various quality encodes
107  * and adding some more from scratch.
108  */
109 static const uint16_t quantization_per_subband[2][3][13][9] = {
110  {{
111  { 16, 16, 8, 4, 4, 2, 6, 6, 9, }, // film3+
112  { 16, 16, 8, 4, 4, 2, 6, 6, 9, }, // film3
113  { 16, 16, 8, 4, 4, 2, 7, 7, 10, }, // film2+
114  { 16, 16, 8, 4, 4, 2, 8, 8, 12, }, // film2
115  { 16, 16, 8, 4, 4, 2, 16, 16, 26, }, // film1++
116  { 24, 24, 12, 6, 6, 3, 24, 24, 36, }, // film1+
117  { 24, 24, 12, 6, 6, 3, 24, 24, 36, }, // film1
118  { 32, 32, 24, 8, 8, 6, 32, 32, 48, }, // high+
119  { 32, 32, 24, 8, 8, 6, 32, 32, 48, }, // high
120  { 48, 48, 32, 12, 12, 8, 64, 64, 96, }, // medium+
121  { 48, 48, 32, 12, 12, 8, 64, 64, 96, }, // medium
122  { 64, 64, 48, 16, 16, 12, 96, 96, 144, }, // low+
123  { 64, 64, 48, 16, 16, 12, 128, 128, 192, }, // low
124  },
125  {
126  { 16, 16, 8, 4, 4, 2, 6, 6, 9, }, // film3+
127  { 16, 16, 8, 4, 4, 2, 6, 6, 12, }, // film3
128  { 16, 16, 8, 4, 4, 2, 7, 7, 14, }, // film2+
129  { 16, 16, 8, 4, 4, 2, 8, 8, 16, }, // film2
130  { 16, 16, 8, 4, 4, 2, 16, 16, 26, }, // film1++
131  { 24, 24, 12, 6, 6, 3, 24, 24, 36, }, // film1+
132  { 24, 24, 12, 6, 6, 3, 24, 24, 48, }, // film1
133  { 32, 32, 24, 8, 8, 6, 32, 32, 48, }, // high+
134  { 48, 48, 32, 12, 12, 8, 32, 32, 64, }, // high
135  { 48, 48, 32, 12, 12, 8, 64, 64, 96, }, // medium+
136  { 48, 48, 32, 12, 12, 8, 64, 64, 128, }, // medium
137  { 64, 64, 48, 16, 16, 12, 96, 96, 160, }, // low+
138  { 64, 64, 48, 16, 16, 12, 128, 128, 192, }, // low
139  },
140  {
141  { 16, 16, 8, 4, 4, 2, 6, 6, 9, }, // film3+
142  { 16, 16, 8, 4, 4, 2, 6, 6, 12, }, // film3
143  { 16, 16, 8, 4, 4, 2, 7, 7, 14, }, // film2+
144  { 16, 16, 8, 4, 4, 2, 8, 8, 16, }, // film2
145  { 16, 16, 8, 4, 4, 2, 16, 16, 26, }, // film1++
146  { 24, 24, 12, 6, 6, 3, 24, 24, 36, }, // film1+
147  { 24, 24, 12, 6, 6, 3, 24, 24, 48, }, // film1
148  { 32, 32, 24, 8, 8, 6, 32, 32, 48, }, // high+
149  { 48, 48, 32, 12, 12, 8, 32, 32, 64, }, // high
150  { 48, 48, 32, 12, 12, 8, 64, 64, 96, }, // medium+
151  { 48, 48, 32, 12, 12, 8, 64, 64, 128, }, // medium
152  { 64, 64, 48, 16, 16, 12, 96, 96, 160, }, // low+
153  { 64, 64, 48, 16, 16, 12, 128, 128, 192, }, // low
154  }},
155  {{
156  { 16, 16, 8, 16, 16, 8, 24, 24, 36, }, // film3+
157  { 16, 16, 8, 16, 16, 8, 24, 24, 36, }, // film3
158  { 16, 16, 8, 16, 16, 8, 32, 32, 48, }, // film2+
159  { 16, 16, 8, 16, 16, 8, 32, 32, 48, }, // film2
160  { 16, 16, 8, 20, 20, 10, 80, 80, 128, }, // film1++
161  { 24, 24, 12, 24, 24, 12, 96, 96, 144, }, // film1+
162  { 24, 24, 12, 24, 24, 12, 96, 96, 144, }, // film1
163  { 32, 32, 24, 32, 32, 24, 128, 128, 192, }, // high+
164  { 32, 32, 24, 32, 32, 24, 128, 128, 192, }, // high
165  { 48, 48, 32, 48, 48, 32, 256, 256, 384, }, // medium+
166  { 48, 48, 32, 48, 48, 32, 256, 256, 384, }, // medium
167  { 56, 56, 40, 56, 56, 40, 512, 512, 768, }, // low+
168  { 64, 64, 48, 64, 64, 48, 512, 512, 768, }, // low
169  },
170  {
171  { 16, 16, 8, 16, 16, 8, 24, 24, 36, }, // film3+
172  { 16, 16, 8, 16, 16, 8, 48, 48, 72, }, // film3
173  { 16, 16, 8, 16, 16, 8, 48, 48, 72, }, // film2+
174  { 16, 16, 8, 16, 16, 8, 64, 64, 96, }, // film2
175  { 16, 16, 8, 20, 20, 10, 80, 80, 128, }, // film1++
176  { 24, 24, 12, 24, 24, 12, 96, 96, 144, }, // film1+
177  { 24, 24, 12, 24, 24, 12, 192, 192, 288, }, // film1
178  { 32, 32, 24, 32, 32, 24, 128, 128, 192, }, // high+
179  { 32, 32, 24, 32, 32, 24, 256, 256, 384, }, // high
180  { 48, 48, 32, 48, 48, 32, 256, 256, 384, }, // medium+
181  { 48, 48, 32, 48, 48, 32, 512, 512, 768, }, // medium
182  { 56, 56, 40, 56, 56, 40, 512, 512, 768, }, // low+
183  { 64, 64, 48, 64, 64, 48,1024,1024,1536, }, // low
184  },
185  {
186  { 16, 16, 8, 16, 16, 8, 24, 24, 36, }, // film3+
187  { 16, 16, 8, 16, 16, 8, 48, 48, 72, }, // film3
188  { 16, 16, 8, 16, 16, 8, 48, 48, 72, }, // film2+
189  { 16, 16, 8, 16, 16, 8, 64, 64, 96, }, // film2
190  { 16, 16, 10, 20, 20, 10, 80, 80, 128, }, // film1++
191  { 24, 24, 12, 24, 24, 12, 96, 96, 144, }, // film1+
192  { 24, 24, 12, 24, 24, 12, 192, 192, 288, }, // film1
193  { 32, 32, 24, 32, 32, 24, 128, 128, 192, }, // high+
194  { 32, 32, 24, 32, 32, 24, 256, 256, 384, }, // high
195  { 48, 48, 32, 48, 48, 32, 256, 256, 384, }, // medium+
196  { 48, 48, 32, 48, 48, 32, 512, 512, 768, }, // medium
197  { 56, 56, 40, 56, 56, 40, 512, 512, 768, }, // low+
198  { 64, 64, 48, 64, 64, 48,1024,1024,1536, }, // low
199  }},
200 };
201 
202 typedef struct Codebook {
203  unsigned bits;
204  unsigned size;
205 } Codebook;
206 
207 typedef struct Runbook {
208  unsigned size;
209  unsigned bits;
210  unsigned run;
211 } Runbook;
212 
213 typedef struct PlaneEnc {
214  unsigned size;
215 
216  int16_t *dwt_buf;
217  int16_t *dwt_tmp;
218 
219  unsigned quantization[SUBBAND_COUNT];
220  int16_t *subband[SUBBAND_COUNT];
221  int16_t *l_h[8];
222 
223  SubBand band[DWT_LEVELS][4];
224 } PlaneEnc;
225 
226 typedef struct CFHDEncContext {
227  const AVClass *class;
228 
231 
232  int quality;
233  int planes;
236  PlaneEnc plane[4];
237 
238  uint16_t lut[1024];
239  Runbook rb[321];
240  Codebook cb[513];
241  int16_t *alpha;
243 
245 {
246  CFHDEncContext *s = avctx->priv_data;
247  const int sign_mask = 256;
248  const int twos_complement = -sign_mask;
249  const int mag_mask = sign_mask - 1;
250  int ret, last = 0;
251 
253  &s->chroma_h_shift,
254  &s->chroma_v_shift);
255  if (ret < 0)
256  return ret;
257 
258  if (avctx->width & 15) {
259  av_log(avctx, AV_LOG_ERROR, "Width must be multiple of 16.\n");
260  return AVERROR_INVALIDDATA;
261  }
262 
264 
265  for (int i = 0; i < s->planes; i++) {
266  int w8, h8, w4, h4, w2, h2;
267  int width = i ? avctx->width >> s->chroma_h_shift : avctx->width;
268  int height = i ? FFALIGN(avctx->height >> s->chroma_v_shift, 8) :
269  FFALIGN(avctx->height >> s->chroma_v_shift, 8);
270  ptrdiff_t stride = FFALIGN(width / 8, 8) * 8;
271 
272  w8 = FFALIGN(width / 8, 8);
273  h8 = height / 8;
274  w4 = w8 * 2;
275  h4 = h8 * 2;
276  w2 = w4 * 2;
277  h2 = h4 * 2;
278 
279  s->plane[i].dwt_buf =
280  av_mallocz_array(height * stride, sizeof(*s->plane[i].dwt_buf));
281  s->plane[i].dwt_tmp =
282  av_malloc_array(height * stride, sizeof(*s->plane[i].dwt_tmp));
283  if (!s->plane[i].dwt_buf || !s->plane[i].dwt_tmp)
284  return AVERROR(ENOMEM);
285 
286  s->plane[i].subband[0] = s->plane[i].dwt_buf;
287  s->plane[i].subband[1] = s->plane[i].dwt_buf + 2 * w8 * h8;
288  s->plane[i].subband[2] = s->plane[i].dwt_buf + 1 * w8 * h8;
289  s->plane[i].subband[3] = s->plane[i].dwt_buf + 3 * w8 * h8;
290  s->plane[i].subband[4] = s->plane[i].dwt_buf + 2 * w4 * h4;
291  s->plane[i].subband[5] = s->plane[i].dwt_buf + 1 * w4 * h4;
292  s->plane[i].subband[6] = s->plane[i].dwt_buf + 3 * w4 * h4;
293  s->plane[i].subband[7] = s->plane[i].dwt_buf + 2 * w2 * h2;
294  s->plane[i].subband[8] = s->plane[i].dwt_buf + 1 * w2 * h2;
295  s->plane[i].subband[9] = s->plane[i].dwt_buf + 3 * w2 * h2;
296 
297  for (int j = 0; j < DWT_LEVELS; j++) {
298  for (int k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
299  s->plane[i].band[j][k].width = (width / 8) << j;
300  s->plane[i].band[j][k].height = (height / 8) << j;
301  s->plane[i].band[j][k].a_width = w8 << j;
302  s->plane[i].band[j][k].a_height = h8 << j;
303  }
304  }
305 
306  /* ll2 and ll1 commented out because they are done in-place */
307  s->plane[i].l_h[0] = s->plane[i].dwt_tmp;
308  s->plane[i].l_h[1] = s->plane[i].dwt_tmp + 2 * w8 * h8;
309  // s->plane[i].l_h[2] = ll2;
310  s->plane[i].l_h[3] = s->plane[i].dwt_tmp;
311  s->plane[i].l_h[4] = s->plane[i].dwt_tmp + 2 * w4 * h4;
312  // s->plane[i].l_h[5] = ll1;
313  s->plane[i].l_h[6] = s->plane[i].dwt_tmp;
314  s->plane[i].l_h[7] = s->plane[i].dwt_tmp + 2 * w2 * h2;
315  }
316 
317  for (int i = 0; i < 512; i++) {
318  int value = (i & sign_mask) ? twos_complement + (i & mag_mask): i;
319  int mag = FFMIN(FFABS(value), 255);
320 
321  if (mag) {
322  s->cb[i].bits = (codebook[mag][1] << 1) | (value > 0 ? 0 : 1);
323  s->cb[i].size = codebook[mag][0] + 1;
324  } else {
325  s->cb[i].bits = codebook[mag][1];
326  s->cb[i].size = codebook[mag][0];
327  }
328  }
329 
330  s->cb[512].bits = 0x3114ba3;
331  s->cb[512].size = 26;
332 
333  s->rb[0].run = 0;
334 
335  for (int i = 1, j = 0; i < 320 && j < 17; j++) {
336  int run = runbook[j][2];
337  int end = runbook[j+1][2];
338 
339  while (i < end) {
340  s->rb[i].run = run;
341  s->rb[i].bits = runbook[j][1];
342  s->rb[i++].size = runbook[j][0];
343  }
344  }
345 
346  s->rb[320].bits = runbook[17][1];
347  s->rb[320].size = runbook[17][0];
348  s->rb[320].run = 320;
349 
350  for (int i = 0; i < 256; i++) {
351  int idx = i + ((768LL * i * i * i) / (256 * 256 * 256));
352 
353  s->lut[idx] = i;
354  }
355  for (int i = 0; i < 1024; i++) {
356  if (s->lut[i])
357  last = s->lut[i];
358  else
359  s->lut[i] = last;
360  }
361 
362  if (s->planes != 4)
363  return 0;
364 
365  s->alpha = av_calloc(avctx->width * avctx->height, sizeof(*s->alpha));
366  if (!s->alpha)
367  return AVERROR(ENOMEM);
368 
369  return 0;
370 }
371 
372 static av_always_inline void filter(int16_t *input, ptrdiff_t in_stride,
373  int16_t *low, ptrdiff_t low_stride,
374  int16_t *high, ptrdiff_t high_stride,
375  int len)
376 {
377  low[(0>>1) * low_stride] = av_clip_int16(input[0*in_stride] + input[1*in_stride]);
378  high[(0>>1) * high_stride] = av_clip_int16((5 * input[0*in_stride] - 11 * input[1*in_stride] +
379  4 * input[2*in_stride] + 4 * input[3*in_stride] -
380  1 * input[4*in_stride] - 1 * input[5*in_stride] + 4) >> 3);
381 
382  for (int i = 2; i < len - 2; i += 2) {
383  low[(i>>1) * low_stride] = av_clip_int16(input[i*in_stride] + input[(i+1)*in_stride]);
384  high[(i>>1) * high_stride] = av_clip_int16(((-input[(i-2)*in_stride] - input[(i-1)*in_stride] +
385  input[(i+2)*in_stride] + input[(i+3)*in_stride] + 4) >> 3) +
386  input[(i+0)*in_stride] - input[(i+1)*in_stride]);
387  }
388 
389  low[((len-2)>>1) * low_stride] = av_clip_int16(input[((len-2)+0)*in_stride] + input[((len-2)+1)*in_stride]);
390  high[((len-2)>>1) * high_stride] = av_clip_int16((11* input[((len-2)+0)*in_stride] - 5 * input[((len-2)+1)*in_stride] -
391  4 * input[((len-2)-1)*in_stride] - 4 * input[((len-2)-2)*in_stride] +
392  1 * input[((len-2)-3)*in_stride] + 1 * input[((len-2)-4)*in_stride] + 4) >> 3);
393 }
394 
395 static void horiz_filter(int16_t *input, int16_t *low, int16_t *high,
396  int width)
397 {
398  filter(input, 1, low, 1, high, 1, width);
399 }
400 
401 static void vert_filter(int16_t *input, ptrdiff_t in_stride,
402  int16_t *low, ptrdiff_t low_stride,
403  int16_t *high, ptrdiff_t high_stride, int len)
404 {
405  filter(input, in_stride, low, low_stride, high, high_stride, len);
406 }
407 
408 static void quantize_band(int16_t *input, int width, int a_width,
409  int height, unsigned quantization)
410 {
411  const int16_t factor = (uint32_t)(1U << 15) / quantization;
412 
413  for (int i = 0; i < height; i++) {
414  for (int j = 0; j < width; j++)
415  input[j] = av_clip_intp2(((input[j] * factor + 16384 * FFSIGN(input[j])) / 32768), 10);
416  input += a_width;
417  }
418 }
419 
420 static int put_runcode(PutBitContext *pb, int count, const Runbook *const rb)
421 {
422  while (count > 0) {
423  const int index = FFMIN(320, count);
424 
425  put_bits(pb, rb[index].size, rb[index].bits);
426  count -= rb[index].run;
427  }
428 
429  return 0;
430 }
431 
432 static void process_alpha(const int16_t *src, int width, int height, ptrdiff_t stride, int16_t *dst)
433 {
434  for (int i = 0; i < height; i++) {
435  for (int j = 0; j < width; j++) {
436  int alpha = src[j];
437 
438  if (alpha > 0 && alpha < 4080) {
439  alpha *= 223;
440  alpha += 128;
441  alpha >>= 8;
442  alpha += 256;
443  }
444 
445  dst[j] = av_clip_uintp2(alpha, 12);
446  }
447 
448  src += stride;
449  dst += width;
450  }
451 }
452 
454  const AVFrame *frame, int *got_packet)
455 {
456  CFHDEncContext *s = avctx->priv_data;
457  PutByteContext *pby = &s->pby;
458  PutBitContext *pb = &s->pb;
459  const Codebook *const cb = s->cb;
460  const Runbook *const rb = s->rb;
461  const uint16_t *lut = s->lut;
462  unsigned pos;
463  int ret;
464 
465  for (int plane = 0; plane < s->planes; plane++) {
466  int width = s->plane[plane].band[2][0].width;
467  int a_width = s->plane[plane].band[2][0].a_width;
468  int height = s->plane[plane].band[2][0].height;
469  int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
470  int16_t *input = (int16_t *)frame->data[act_plane];
471  int16_t *low = s->plane[plane].l_h[6];
472  int16_t *high = s->plane[plane].l_h[7];
473  ptrdiff_t in_stride = frame->linesize[act_plane] / 2;
474  int low_stride, high_stride;
475 
476  if (plane == 3) {
477  process_alpha(input, avctx->width, avctx->height,
478  in_stride, s->alpha);
479  input = s->alpha;
480  in_stride = avctx->width;
481  }
482 
483  for (int i = 0; i < height * 2; i++) {
484  horiz_filter(input, low, high, width * 2);
485  input += in_stride;
486  low += a_width;
487  high += a_width;
488  }
489 
490  input = s->plane[plane].l_h[7];
491  low = s->plane[plane].subband[7];
492  low_stride = s->plane[plane].band[2][0].a_width;
493  high = s->plane[plane].subband[9];
494  high_stride = s->plane[plane].band[2][0].a_width;
495 
496  for (int i = 0; i < width; i++) {
497  vert_filter(input, a_width, low, low_stride, high, high_stride, height * 2);
498  input++;
499  low++;
500  high++;
501  }
502 
503  input = s->plane[plane].l_h[6];
504  low = s->plane[plane].l_h[7];
505  high = s->plane[plane].subband[8];
506 
507  for (int i = 0; i < width; i++) {
508  vert_filter(input, a_width, low, low_stride, high, high_stride, height * 2);
509  input++;
510  low++;
511  high++;
512  }
513 
514  a_width = s->plane[plane].band[1][0].a_width;
515  width = s->plane[plane].band[1][0].width;
516  height = s->plane[plane].band[1][0].height;
517  input = s->plane[plane].l_h[7];
518  low = s->plane[plane].l_h[3];
519  low_stride = s->plane[plane].band[1][0].a_width;
520  high = s->plane[plane].l_h[4];
521  high_stride = s->plane[plane].band[1][0].a_width;
522 
523  for (int i = 0; i < height * 2; i++) {
524  for (int j = 0; j < width * 2; j++)
525  input[j] /= 4;
526  input += a_width * 2;
527  }
528 
529  input = s->plane[plane].l_h[7];
530  for (int i = 0; i < height * 2; i++) {
531  horiz_filter(input, low, high, width * 2);
532  input += a_width * 2;
533  low += low_stride;
534  high += high_stride;
535  }
536 
537  input = s->plane[plane].l_h[4];
538  low = s->plane[plane].subband[4];
539  high = s->plane[plane].subband[6];
540 
541  for (int i = 0; i < width; i++) {
542  vert_filter(input, a_width, low, low_stride, high, high_stride, height * 2);
543  input++;
544  low++;
545  high++;
546  }
547 
548  input = s->plane[plane].l_h[3];
549  low = s->plane[plane].l_h[4];
550  high = s->plane[plane].subband[5];
551 
552  for (int i = 0; i < width; i++) {
553  vert_filter(input, a_width, low, low_stride, high, high_stride, height * 2);
554  input++;
555  low++;
556  high++;
557  }
558 
559  a_width = s->plane[plane].band[0][0].a_width;
560  width = s->plane[plane].band[0][0].width;
561  height = s->plane[plane].band[0][0].height;
562  input = s->plane[plane].l_h[4];
563  low = s->plane[plane].l_h[0];
564  low_stride = s->plane[plane].band[0][0].a_width;
565  high = s->plane[plane].l_h[1];
566  high_stride = s->plane[plane].band[0][0].a_width;
567 
568  if (avctx->pix_fmt != AV_PIX_FMT_YUV422P10) {
569  for (int i = 0; i < height * 2; i++) {
570  for (int j = 0; j < width * 2; j++)
571  input[j] /= 4;
572  input += a_width * 2;
573  }
574  }
575 
576  input = s->plane[plane].l_h[4];
577  for (int i = 0; i < height * 2; i++) {
578  horiz_filter(input, low, high, width * 2);
579  input += a_width * 2;
580  low += low_stride;
581  high += high_stride;
582  }
583 
584  low = s->plane[plane].subband[1];
585  high = s->plane[plane].subband[3];
586  input = s->plane[plane].l_h[1];
587 
588  for (int i = 0; i < width; i++) {
589  vert_filter(input, a_width, low, low_stride, high, high_stride, height * 2);
590  input++;
591  low++;
592  high++;
593  }
594 
595  low = s->plane[plane].subband[0];
596  high = s->plane[plane].subband[2];
597  input = s->plane[plane].l_h[0];
598 
599  for (int i = 0; i < width; i++) {
600  vert_filter(input, a_width, low, low_stride, high, high_stride, height * 2);
601  input++;
602  low++;
603  high++;
604  }
605  }
606 
607  ret = ff_alloc_packet2(avctx, pkt, 64LL + s->planes * (2LL * avctx->width * avctx->height + 1000LL), 0);
608  if (ret < 0)
609  return ret;
610 
611  bytestream2_init_writer(pby, pkt->data, pkt->size);
612 
613  bytestream2_put_be16(pby, SampleType);
614  bytestream2_put_be16(pby, 9);
615 
616  bytestream2_put_be16(pby, SampleIndexTable);
617  bytestream2_put_be16(pby, s->planes);
618 
619  for (int i = 0; i < s->planes; i++)
620  bytestream2_put_be32(pby, 0);
621 
622  bytestream2_put_be16(pby, TransformType);
623  bytestream2_put_be16(pby, 0);
624 
625  bytestream2_put_be16(pby, NumFrames);
626  bytestream2_put_be16(pby, 1);
627 
628  bytestream2_put_be16(pby, ChannelCount);
629  bytestream2_put_be16(pby, s->planes);
630 
631  bytestream2_put_be16(pby, EncodedFormat);
632  bytestream2_put_be16(pby, avctx->pix_fmt == AV_PIX_FMT_YUV422P10 ? 1 : 3 + (s->planes == 4));
633 
634  bytestream2_put_be16(pby, WaveletCount);
635  bytestream2_put_be16(pby, 3);
636 
637  bytestream2_put_be16(pby, SubbandCount);
638  bytestream2_put_be16(pby, SUBBAND_COUNT);
639 
640  bytestream2_put_be16(pby, NumSpatial);
641  bytestream2_put_be16(pby, 2);
642 
643  bytestream2_put_be16(pby, FirstWavelet);
644  bytestream2_put_be16(pby, 3);
645 
646  bytestream2_put_be16(pby, ImageWidth);
647  bytestream2_put_be16(pby, avctx->width);
648 
649  bytestream2_put_be16(pby, ImageHeight);
650  bytestream2_put_be16(pby, avctx->height);
651 
652  bytestream2_put_be16(pby, -FrameNumber);
653  bytestream2_put_be16(pby, avctx->frame_number);
654 
655  bytestream2_put_be16(pby, Precision);
656  bytestream2_put_be16(pby, avctx->pix_fmt == AV_PIX_FMT_YUV422P10 ? 10 : 12);
657 
658  bytestream2_put_be16(pby, PrescaleTable);
659  bytestream2_put_be16(pby, avctx->pix_fmt == AV_PIX_FMT_YUV422P10 ? 0x2000 : 0x2800);
660 
661  bytestream2_put_be16(pby, SampleFlags);
662  bytestream2_put_be16(pby, 1);
663 
664  for (int p = 0; p < s->planes; p++) {
665  int width = s->plane[p].band[0][0].width;
666  int a_width = s->plane[p].band[0][0].a_width;
667  int height = s->plane[p].band[0][0].height;
668  int16_t *data = s->plane[p].subband[0];
669 
670  if (p) {
671  bytestream2_put_be16(pby, SampleType);
672  bytestream2_put_be16(pby, 3);
673 
674  bytestream2_put_be16(pby, ChannelNumber);
675  bytestream2_put_be16(pby, p);
676  }
677 
678  bytestream2_put_be16(pby, BitstreamMarker);
679  bytestream2_put_be16(pby, 0x1a4a);
680 
681  pos = bytestream2_tell_p(pby);
682 
683  bytestream2_put_be16(pby, LowpassSubband);
684  bytestream2_put_be16(pby, 0);
685 
686  bytestream2_put_be16(pby, NumLevels);
687  bytestream2_put_be16(pby, 3);
688 
689  bytestream2_put_be16(pby, LowpassWidth);
690  bytestream2_put_be16(pby, width);
691 
692  bytestream2_put_be16(pby, LowpassHeight);
693  bytestream2_put_be16(pby, height);
694 
695  bytestream2_put_be16(pby, PixelOffset);
696  bytestream2_put_be16(pby, 0);
697 
698  bytestream2_put_be16(pby, LowpassQuantization);
699  bytestream2_put_be16(pby, 1);
700 
701  bytestream2_put_be16(pby, LowpassPrecision);
702  bytestream2_put_be16(pby, 16);
703 
704  bytestream2_put_be16(pby, BitstreamMarker);
705  bytestream2_put_be16(pby, 0x0f0f);
706 
707  for (int i = 0; i < height; i++) {
708  for (int j = 0; j < width; j++)
709  bytestream2_put_be16(pby, data[j]);
710  data += a_width;
711  }
712 
713  bytestream2_put_be16(pby, BitstreamMarker);
714  bytestream2_put_be16(pby, 0x1b4b);
715 
716  for (int l = 0; l < 3; l++) {
717  for (int i = 0; i < 3; i++) {
718  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];
719  }
720  }
721 
722  for (int l = 0; l < 3; l++) {
723  int a_width = s->plane[p].band[l][0].a_width;
724  int width = s->plane[p].band[l][0].width;
725  int stride = FFALIGN(width, 8);
726  int height = s->plane[p].band[l][0].height;
727 
728  bytestream2_put_be16(pby, BitstreamMarker);
729  bytestream2_put_be16(pby, 0x0d0d);
730 
731  bytestream2_put_be16(pby, WaveletType);
732  bytestream2_put_be16(pby, 3 + 2 * (l == 2));
733 
734  bytestream2_put_be16(pby, WaveletNumber);
735  bytestream2_put_be16(pby, 3 - l);
736 
737  bytestream2_put_be16(pby, WaveletLevel);
738  bytestream2_put_be16(pby, 3 - l);
739 
740  bytestream2_put_be16(pby, NumBands);
741  bytestream2_put_be16(pby, 4);
742 
743  bytestream2_put_be16(pby, HighpassWidth);
744  bytestream2_put_be16(pby, width);
745 
746  bytestream2_put_be16(pby, HighpassHeight);
747  bytestream2_put_be16(pby, height);
748 
749  bytestream2_put_be16(pby, LowpassBorder);
750  bytestream2_put_be16(pby, 0);
751 
752  bytestream2_put_be16(pby, HighpassBorder);
753  bytestream2_put_be16(pby, 0);
754 
755  bytestream2_put_be16(pby, LowpassScale);
756  bytestream2_put_be16(pby, 1);
757 
758  bytestream2_put_be16(pby, LowpassDivisor);
759  bytestream2_put_be16(pby, 1);
760 
761  for (int i = 0; i < 3; i++) {
762  int16_t *data = s->plane[p].subband[1 + l * 3 + i];
763  int count = 0, padd = 0;
764 
765  bytestream2_put_be16(pby, BitstreamMarker);
766  bytestream2_put_be16(pby, 0x0e0e);
767 
768  bytestream2_put_be16(pby, SubbandNumber);
769  bytestream2_put_be16(pby, i + 1);
770 
771  bytestream2_put_be16(pby, BandCodingFlags);
772  bytestream2_put_be16(pby, 1);
773 
774  bytestream2_put_be16(pby, BandWidth);
775  bytestream2_put_be16(pby, width);
776 
777  bytestream2_put_be16(pby, BandHeight);
778  bytestream2_put_be16(pby, height);
779 
780  bytestream2_put_be16(pby, SubbandBand);
781  bytestream2_put_be16(pby, 1 + l * 3 + i);
782 
783  bytestream2_put_be16(pby, BandEncoding);
784  bytestream2_put_be16(pby, 3);
785 
786  bytestream2_put_be16(pby, Quantization);
787  bytestream2_put_be16(pby, s->plane[p].quantization[1 + l * 3 + i]);
788 
789  bytestream2_put_be16(pby, BandScale);
790  bytestream2_put_be16(pby, 1);
791 
792  bytestream2_put_be16(pby, BandHeader);
793  bytestream2_put_be16(pby, 0);
794 
795  quantize_band(data, width, a_width, height,
796  s->plane[p].quantization[1 + l * 3 + i]);
797 
799 
800  for (int m = 0; m < height; m++) {
801  for (int j = 0; j < stride; j++) {
802  int16_t index = FFSIGN(data[j]) * lut[FFABS(data[j])];
803 
804  if (index < 0)
805  index += 512;
806  if (index == 0) {
807  count++;
808  continue;
809  } else if (count > 0) {
810  count = put_runcode(pb, count, rb);
811  }
812 
813  put_bits(pb, cb[index].size, cb[index].bits);
814  }
815 
816  data += a_width;
817  }
818 
819  if (count > 0) {
820  count = put_runcode(pb, count, rb);
821  }
822 
823  put_bits(pb, cb[512].size, cb[512].bits);
824 
825  flush_put_bits(pb);
826  bytestream2_skip_p(pby, put_bits_count(pb) >> 3);
827  padd = (4 - (bytestream2_tell_p(pby) & 3)) & 3;
828  while (padd--)
829  bytestream2_put_byte(pby, 0);
830 
831  bytestream2_put_be16(pby, BandTrailer);
832  bytestream2_put_be16(pby, 0);
833  }
834 
835  bytestream2_put_be16(pby, BitstreamMarker);
836  bytestream2_put_be16(pby, 0x0c0c);
837  }
838 
839  s->plane[p].size = bytestream2_tell_p(pby) - pos;
840  }
841 
842  bytestream2_put_be16(pby, GroupTrailer);
843  bytestream2_put_be16(pby, 0);
844 
846 
847  pkt->flags |= AV_PKT_FLAG_KEY;
848 
849  bytestream2_seek_p(pby, 8, SEEK_SET);
850  for (int i = 0; i < s->planes; i++)
851  bytestream2_put_be32(pby, s->plane[i].size);
852 
853  *got_packet = 1;
854 
855  return 0;
856 }
857 
859 {
860  CFHDEncContext *s = avctx->priv_data;
861 
862  for (int i = 0; i < s->planes; i++) {
863  av_freep(&s->plane[i].dwt_buf);
864  av_freep(&s->plane[i].dwt_tmp);
865 
866  for (int j = 0; j < SUBBAND_COUNT; j++)
867  s->plane[i].subband[j] = NULL;
868 
869  for (int j = 0; j < 8; j++)
870  s->plane[i].l_h[j] = NULL;
871  }
872 
873  av_freep(&s->alpha);
874 
875  return 0;
876 }
877 
878 #define OFFSET(x) offsetof(CFHDEncContext, x)
879 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
880 static const AVOption options[] = {
881  { "quality", "set quality", OFFSET(quality), AV_OPT_TYPE_INT, {.i64= 0}, 0, 12, VE, "q" },
882  { "film3+", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 0}, 0, 0, VE, "q" },
883  { "film3", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 1}, 0, 0, VE, "q" },
884  { "film2+", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 2}, 0, 0, VE, "q" },
885  { "film2", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 3}, 0, 0, VE, "q" },
886  { "film1.5", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 4}, 0, 0, VE, "q" },
887  { "film1+", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 5}, 0, 0, VE, "q" },
888  { "film1", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 6}, 0, 0, VE, "q" },
889  { "high+", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 7}, 0, 0, VE, "q" },
890  { "high", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 8}, 0, 0, VE, "q" },
891  { "medium+", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 9}, 0, 0, VE, "q" },
892  { "medium", NULL, 0, AV_OPT_TYPE_CONST, {.i64=10}, 0, 0, VE, "q" },
893  { "low+", NULL, 0, AV_OPT_TYPE_CONST, {.i64=11}, 0, 0, VE, "q" },
894  { "low", NULL, 0, AV_OPT_TYPE_CONST, {.i64=12}, 0, 0, VE, "q" },
895  { NULL},
896 };
897 
898 static const AVClass cfhd_class = {
899  .class_name = "cfhd",
900  .item_name = av_default_item_name,
901  .option = options,
902  .version = LIBAVUTIL_VERSION_INT,
903 };
904 
906  .name = "cfhd",
907  .long_name = NULL_IF_CONFIG_SMALL("GoPro CineForm HD"),
908  .type = AVMEDIA_TYPE_VIDEO,
909  .id = AV_CODEC_ID_CFHD,
910  .priv_data_size = sizeof(CFHDEncContext),
911  .priv_class = &cfhd_class,
913  .close = cfhd_encode_close,
914  .encode2 = cfhd_encode_frame,
915  .capabilities = AV_CODEC_CAP_FRAME_THREADS,
916  .pix_fmts = (const enum AVPixelFormat[]) {
921  },
922  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
923 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
#define NULL
Definition: coverity.c:32
uint16_t lut[1024]
Definition: cfhdenc.c:238
static const AVClass cfhd_class
Definition: cfhdenc.c:898
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static const unsigned codebook[256][2]
Definition: cfhdenc.c:41
This structure describes decoded (raw) audio or video data.
Definition: frame.h:314
Definition: cfhd.h:43
AVOption.
Definition: opt.h:248
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
TransformType
Definition: webp.c:110
static void horiz_filter(int16_t *input, int16_t *low, int16_t *high, int width)
Definition: cfhdenc.c:395
misc image utilities
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:218
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2613
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:103
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define VE
Definition: cfhdenc.c:879
int a_height
Definition: cfhd.h:115
Definition: cfhd.h:72
static const uint16_t quantization_per_subband[2][3][13][9]
Definition: cfhdenc.c:109
int size
Definition: packet.h:364
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition: bytestream.h:147
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:741
unsigned size
Definition: cfhdenc.c:208
uint8_t run
Definition: svq3.c:204
static AVPacket pkt
PutByteContext pby
Definition: cfhdenc.c:230
AVCodec.
Definition: codec.h:190
Definition: cfhd.h:83
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:245
int width
Definition: cfhd.h:114
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:33
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:215
SubBand band[DWT_LEVELS][4]
Definition: cfhdenc.c:223
#define av_cold
Definition: attributes.h:88
AVOptions.
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:92
Multithreading support functions.
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:108
#define height
uint8_t * data
Definition: packet.h:363
PutBitContext pb
Definition: cfhdenc.c:229
ptrdiff_t size
Definition: opengl_enc.c:100
static av_cold int cfhd_encode_init(AVCodecContext *avctx)
Definition: cfhdenc.c:244
#define FFALIGN(x, a)
Definition: macros.h:48
#define SUBBAND_COUNT
Definition: cfhd.h:99
#define av_log(a,...)
Definition: cfhd.h:111
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:401
static av_cold int cfhd_encode_close(AVCodecContext *avctx)
Definition: cfhdenc.c:858
#define U(x)
Definition: vp56_arith.h:37
#define src
Definition: vp8dsp.c:254
unsigned quantization[SUBBAND_COUNT]
Definition: cfhdenc.c:219
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
unsigned size
Definition: cfhdenc.c:214
unsigned bits
Definition: cfhdenc.c:203
static const AVOption options[]
Definition: cfhdenc.c:880
static av_always_inline int bytestream2_get_bytes_left_p(PutByteContext *p)
Definition: bytestream.h:163
AVCodec ff_cfhd_encoder
Definition: cfhdenc.c:905
int16_t * l_h[8]
Definition: cfhdenc.c:221
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: pixdesc.c:2601
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:153
unsigned int pos
Definition: spdifenc.c:410
Codebook cb[513]
Definition: cfhdenc.c:240
#define AV_PIX_FMT_GBRAP12
Definition: pixfmt.h:420
#define DWT_LEVELS
Definition: cfhd.h:108
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
Definition: codec.h:197
GLsizei count
Definition: opengl_enc.c:108
static av_always_inline int bytestream2_tell_p(PutByteContext *p)
Definition: bytestream.h:197
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:106
static int cfhd_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: cfhdenc.c:453
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:369
int a_width
Definition: cfhd.h:113
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:83
int16_t SampleType
Definition: ac3enc.h:70
static av_always_inline void bytestream2_skip_p(PutByteContext *p, unsigned int size)
Definition: bytestream.h:180
#define FFMIN(a, b)
Definition: common.h:96
#define width
#define FFSIGN(a)
Definition: common.h:73
int width
picture width / height.
Definition: avcodec.h:704
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about quality
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define s(width, name)
Definition: cbs_vp9.c:257
int16_t * alpha
Definition: cfhdenc.c:241
int16_t * dwt_tmp
Definition: cfhdenc.c:217
#define FF_ARRAY_ELEMS(a)
if(ret)
static av_always_inline int bytestream2_seek_p(PutByteContext *p, int offset, int whence)
Definition: bytestream.h:236
unsigned size
Definition: cfhdenc.c:204
unsigned run
Definition: cfhdenc.c:210
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:345
static const int16_t alpha[]
Definition: ilbcdata.h:55
main external API structure.
Definition: avcodec.h:531
static av_always_inline void filter(int16_t *input, ptrdiff_t in_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len)
Definition: cfhdenc.c:372
Runbook rb[321]
Definition: cfhdenc.c:239
Describe the class of an AVClass context structure.
Definition: log.h:67
int index
Definition: gxfenc.c:89
int chroma_h_shift
Definition: cfhdenc.c:234
static const int factor[16]
Definition: vf_pp7.c:75
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
Definition: cfhd.h:55
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:300
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:416
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:400
static const uint16_t runbook[18][3]
Definition: cfhdenc.c:97
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:328
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:104
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:117
static void vert_filter(int16_t *input, ptrdiff_t in_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len)
Definition: cfhdenc.c:401
PlaneEnc plane[4]
Definition: cfhdenc.c:236
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:64
void * priv_data
Definition: avcodec.h:558
static void quantize_band(int16_t *input, int width, int a_width, int height, unsigned quantization)
Definition: cfhdenc.c:408
Definition: cfhd.h:77
unsigned bits
Definition: cfhdenc.c:209
int len
int chroma_v_shift
Definition: cfhdenc.c:235
#define OFFSET(x)
Definition: cfhdenc.c:878
int height
Definition: cfhd.h:116
static int put_runcode(PutBitContext *pb, int count, const Runbook *const rb)
Definition: cfhdenc.c:420
Definition: cfhd.h:64
int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:1222
int16_t * dwt_buf
Definition: cfhdenc.c:216
#define av_freep(p)
#define av_always_inline
Definition: attributes.h:45
#define av_malloc_array(a, b)
#define stride
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
This structure stores compressed data.
Definition: packet.h:340
static void process_alpha(const int16_t *src, int width, int height, ptrdiff_t stride, int16_t *dst)
Definition: cfhdenc.c:432
for(j=16;j >0;--j)
int16_t * subband[SUBBAND_COUNT]
Definition: cfhdenc.c:220
int i
Definition: input.c:407
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:190
bitstream writer API