| @@ -0,0 +1,760 @@ |
| 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[10][9] = { |
| 107 | + { 12, 12, 6, 12, 12, 6, 12, 12, 18, }, |
| 108 | + { 14, 14, 7, 14, 14, 7, 18, 18, 24, }, |
| 109 | + { 16, 16, 8, 16, 16, 8, 24, 24, 36, }, |
| 110 | + { 16, 16, 8, 16, 16, 8, 48, 48, 96, }, |
| 111 | + { 24, 24, 16, 24, 24, 16, 64, 64, 128, }, |
| 112 | + { 24, 24, 16, 24, 24, 16, 128, 128, 256, }, |
| 113 | + { 64, 64, 48, 64, 64, 48, 256, 256, 512, }, |
| 114 | + { 64, 64, 48, 64, 64, 48, 512, 512, 768, }, |
| 115 | + { 64, 64, 48, 64, 64, 48,1024,1024,1536, }, |
| 116 | + {160,160,168,172,172,180,2048,2048,3072, }, |
| 117 | +}; |
| 118 | + |
| 119 | +typedef struct Codebook { |
| 120 | + unsigned bits; |
| 121 | + unsigned size; |
| 122 | +} Codebook; |
| 123 | + |
| 124 | +typedef struct Runbook { |
| 125 | + unsigned size; |
| 126 | + unsigned bits; |
| 127 | + unsigned run; |
| 128 | +} Runbook; |
| 129 | + |
| 130 | +typedef struct PlaneEnc { |
| 131 | + unsigned size; |
| 132 | + |
| 133 | + int16_t *dwt_buf; |
| 134 | + int16_t *dwt_tmp; |
| 135 | + |
| 136 | + unsigned quantization[SUBBAND_COUNT]; |
| 137 | + int16_t *subband[SUBBAND_COUNT]; |
| 138 | + int16_t *l_h[8]; |
| 139 | + |
| 140 | + SubBand band[DWT_LEVELS][4]; |
| 141 | +} PlaneEnc; |
| 142 | + |
| 143 | +typedef struct CFHDEncContext { |
| 144 | + PutBitContext pb; |
| 145 | + PutByteContext pby; |
| 146 | + |
| 147 | + unsigned quantization_div; |
| 148 | + int compression; |
| 149 | + int planes; |
| 150 | + int chroma_h_shift; |
| 151 | + int chroma_v_shift; |
| 152 | + PlaneEnc plane[4]; |
| 153 | + |
| 154 | + Runbook rb[321]; |
| 155 | + Codebook cb[513]; |
| 156 | +} CFHDEncContext; |
| 157 | + |
| 158 | +static av_cold int cfhd_encode_init(AVCodecContext *avctx) |
| 159 | +{ |
| 160 | + CFHDEncContext *s = avctx->priv_data; |
| 161 | + const int sign_mask = 256; |
| 162 | + const int twos_complement = -sign_mask; |
| 163 | + const int mag_mask = sign_mask - 1; |
| 164 | + int ret; |
| 165 | + |
| 166 | + s->quantization_div = avctx->pix_fmt == AV_PIX_FMT_YUV422P10 ? 2 : 1; |
| 167 | + |
| 168 | + ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, |
| 169 | + &s->chroma_h_shift, |
| 170 | + &s->chroma_v_shift); |
| 171 | + if (ret < 0) |
| 172 | + return ret; |
| 173 | + |
| 174 | + if (avctx->width & 15) { |
| 175 | + av_log(avctx, AV_LOG_ERROR, "Width must be multiple of 16.\n"); |
| 176 | + return AVERROR_INVALIDDATA; |
| 177 | + } |
| 178 | + |
| 179 | + s->planes = av_pix_fmt_count_planes(avctx->pix_fmt); |
| 180 | + |
| 181 | + if (avctx->compression_level == FF_COMPRESSION_DEFAULT) |
| 182 | + s->compression = 2; |
| 183 | + s->compression = av_clip(avctx->compression_level, 0, 9); |
| 184 | + |
| 185 | + for (int i = 0; i < s->planes; i++) { |
| 186 | + int w8, h8, w4, h4, w2, h2; |
| 187 | + int width = i ? avctx->width >> s->chroma_h_shift : avctx->width; |
| 188 | + int height = i ? FFALIGN(avctx->height >> s->chroma_v_shift, 8) : |
| 189 | + FFALIGN(avctx->height >> s->chroma_v_shift, 8); |
| 190 | + ptrdiff_t stride = FFALIGN(width / 8, 8) * 8; |
| 191 | + |
| 192 | + w8 = FFALIGN(width / 8, 8); |
| 193 | + h8 = height / 8; |
| 194 | + w4 = w8 * 2; |
| 195 | + h4 = h8 * 2; |
| 196 | + w2 = w4 * 2; |
| 197 | + h2 = h4 * 2; |
| 198 | + |
| 199 | + s->plane[i].dwt_buf = |
| 200 | + av_mallocz_array(height * stride, sizeof(*s->plane[i].dwt_buf)); |
| 201 | + s->plane[i].dwt_tmp = |
| 202 | + av_malloc_array(height * stride, sizeof(*s->plane[i].dwt_tmp)); |
| 203 | + if (!s->plane[i].dwt_buf || !s->plane[i].dwt_tmp) |
| 204 | + return AVERROR(ENOMEM); |
| 205 | + |
| 206 | + s->plane[i].subband[0] = s->plane[i].dwt_buf; |
| 207 | + s->plane[i].subband[1] = s->plane[i].dwt_buf + 2 * w8 * h8; |
| 208 | + s->plane[i].subband[2] = s->plane[i].dwt_buf + 1 * w8 * h8; |
| 209 | + s->plane[i].subband[3] = s->plane[i].dwt_buf + 3 * w8 * h8; |
| 210 | + s->plane[i].subband[4] = s->plane[i].dwt_buf + 2 * w4 * h4; |
| 211 | + s->plane[i].subband[5] = s->plane[i].dwt_buf + 1 * w4 * h4; |
| 212 | + s->plane[i].subband[6] = s->plane[i].dwt_buf + 3 * w4 * h4; |
| 213 | + s->plane[i].subband[7] = s->plane[i].dwt_buf + 2 * w2 * h2; |
| 214 | + s->plane[i].subband[8] = s->plane[i].dwt_buf + 1 * w2 * h2; |
| 215 | + s->plane[i].subband[9] = s->plane[i].dwt_buf + 3 * w2 * h2; |
| 216 | + |
| 217 | + for (int j = 0; j < DWT_LEVELS; j++) { |
| 218 | + for (int k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) { |
| 219 | + s->plane[i].band[j][k].width = (width / 8) << j; |
| 220 | + s->plane[i].band[j][k].height = (height / 8) << j; |
| 221 | + s->plane[i].band[j][k].a_width = w8 << j; |
| 222 | + s->plane[i].band[j][k].a_height = h8 << j; |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + /* ll2 and ll1 commented out because they are done in-place */ |
| 227 | + s->plane[i].l_h[0] = s->plane[i].dwt_tmp; |
| 228 | + s->plane[i].l_h[1] = s->plane[i].dwt_tmp + 2 * w8 * h8; |
| 229 | + // s->plane[i].l_h[2] = ll2; |
| 230 | + s->plane[i].l_h[3] = s->plane[i].dwt_tmp; |
| 231 | + s->plane[i].l_h[4] = s->plane[i].dwt_tmp + 2 * w4 * h4; |
| 232 | + // s->plane[i].l_h[5] = ll1; |
| 233 | + s->plane[i].l_h[6] = s->plane[i].dwt_tmp; |
| 234 | + s->plane[i].l_h[7] = s->plane[i].dwt_tmp + 2 * w2 * h2; |
| 235 | + } |
| 236 | + |
| 237 | + for (int i = 0; i < 512; i++) { |
| 238 | + int value = (i & sign_mask) ? twos_complement + (i & mag_mask): i; |
| 239 | + int mag = FFMIN(FFABS(value), 255); |
| 240 | + |
| 241 | + if (mag) { |
| 242 | + s->cb[i].bits = (codebook[mag][1] << 1) | (value > 0 ? 0 : 1); |
| 243 | + s->cb[i].size = codebook[mag][0] + 1; |
| 244 | + } else { |
| 245 | + s->cb[i].bits = codebook[mag][1]; |
| 246 | + s->cb[i].size = codebook[mag][0]; |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + s->cb[512].bits = 0x3114ba3; |
| 251 | + s->cb[512].size = 26; |
| 252 | + |
| 253 | + s->rb[0].run = 0; |
| 254 | + |
| 255 | + for (int i = 1, j = 0; i < 320 && j < 7; j++) { |
| 256 | + int run = runbook[j][2]; |
| 257 | + int end = runbook[j+1][2]; |
| 258 | + |
| 259 | + while (i < end) { |
| 260 | + s->rb[i].run = run; |
| 261 | + s->rb[i].bits = runbook[j][1]; |
| 262 | + s->rb[i++].size = runbook[j][0]; |
| 263 | + } |
| 264 | + } |
| 265 | + |
| 266 | + s->rb[320].bits = runbook[7][1]; |
| 267 | + s->rb[320].size = runbook[7][0]; |
| 268 | + s->rb[320].run = 320; |
| 269 | + |
| 270 | + return 0; |
| 271 | +} |
| 272 | + |
| 273 | +static inline void filter(int16_t *input, ptrdiff_t in_stride, |
| 274 | + int16_t *low, ptrdiff_t low_stride, |
| 275 | + int16_t *high, ptrdiff_t high_stride, |
| 276 | + int len) |
| 277 | +{ |
| 278 | + low[(0>>1) * low_stride] = av_clip_int16(input[0*in_stride] + input[1*in_stride]); |
| 279 | + high[(0>>1) * high_stride] = av_clip_int16((5 * input[0*in_stride] - 11 * input[1*in_stride] + |
| 280 | + 4 * input[2*in_stride] + 4 * input[3*in_stride] - |
| 281 | + 1 * input[4*in_stride] - 1 * input[5*in_stride] + 4) >> 3); |
| 282 | + |
| 283 | + for (int i = 2; i < len - 2; i += 2) { |
| 284 | + low[(i>>1) * low_stride] = av_clip_int16(input[i*in_stride] + input[(i+1)*in_stride]); |
| 285 | + high[(i>>1) * high_stride] = av_clip_int16(((-input[(i-2)*in_stride] - input[(i-1)*in_stride] + |
| 286 | + input[(i+2)*in_stride] + input[(i+3)*in_stride] + 4) >> 3) + |
| 287 | + input[(i+0)*in_stride] - input[(i+1)*in_stride]); |
| 288 | + } |
| 289 | + |
| 290 | + low[((len-2)>>1) * low_stride] = av_clip_int16(input[((len-2)+0)*in_stride] + input[((len-2)+1)*in_stride]); |
| 291 | + high[((len-2)>>1) * high_stride] = av_clip_int16((11* input[((len-2)+0)*in_stride] - 5 * input[((len-2)+1)*in_stride] - |
| 292 | + 4 * input[((len-2)-1)*in_stride] - 4 * input[((len-2)-2)*in_stride] + |
| 293 | + 1 * input[((len-2)-3)*in_stride] + 1 * input[((len-2)-4)*in_stride] + 4) >> 3); |
| 294 | +} |
| 295 | + |
| 296 | +static void horiz_filter(int16_t *input, int16_t *low, int16_t *high, |
| 297 | + int width) |
| 298 | +{ |
| 299 | + filter(input, 1, low, 1, high, 1, width); |
| 300 | +} |
| 301 | + |
| 302 | +static void vert_filter(int16_t *input, ptrdiff_t in_stride, |
| 303 | + int16_t *low, ptrdiff_t low_stride, |
| 304 | + int16_t *high, ptrdiff_t high_stride, int len) |
| 305 | +{ |
| 306 | + filter(input, in_stride, low, low_stride, high, high_stride, len); |
| 307 | +} |
| 308 | + |
| 309 | +static void quantize_band(int16_t *input, int width, int a_width, |
| 310 | + int height, unsigned quantization) |
| 311 | +{ |
| 312 | + const int factor = (1 << 16) / quantization; |
| 313 | + |
| 314 | + for (int i = 0; i < height; i++) { |
| 315 | + for (int j = 0; j < width; j++) |
| 316 | + input[j] = av_clip_intp2((input[j] * factor) / 65536, 8); |
| 317 | + input += a_width; |
| 318 | + } |
| 319 | +} |
| 320 | + |
| 321 | +static int put_runcode(PutBitContext *pb, int count, const Runbook *const rb) |
| 322 | +{ |
| 323 | + while (count > 0) { |
| 324 | + const int index = FFMIN(320, count); |
| 325 | + |
| 326 | + put_bits(pb, rb[index].size, rb[index].bits); |
| 327 | + count -= rb[index].run; |
| 328 | + } |
| 329 | + |
| 330 | + return 0; |
| 331 | +} |
| 332 | + |
| 333 | +static int cfhd_encode_frame(AVCodecContext *avctx, AVPacket *pkt, |
| 334 | + const AVFrame *frame, int *got_packet) |
| 335 | +{ |
| 336 | + CFHDEncContext *s = avctx->priv_data; |
| 337 | + PutByteContext *pby = &s->pby; |
| 338 | + PutBitContext *pb = &s->pb; |
| 339 | + const Codebook *const cb = s->cb; |
| 340 | + const Runbook *const rb = s->rb; |
| 341 | + unsigned pos; |
| 342 | + int ret = 0; |
| 343 | + |
| 344 | + for (int plane = 0; plane < s->planes && !ret; plane++) { |
| 345 | + int width = s->plane[plane].band[2][0].width; |
| 346 | + int a_width = s->plane[plane].band[2][0].a_width; |
| 347 | + int height = s->plane[plane].band[2][0].height; |
| 348 | + int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane; |
| 349 | + int16_t *input = (int16_t *)frame->data[act_plane]; |
| 350 | + int16_t *low = s->plane[plane].l_h[6]; |
| 351 | + int16_t *high = s->plane[plane].l_h[7]; |
| 352 | + const ptrdiff_t in_stride = frame->linesize[act_plane] / 2; |
| 353 | + int low_stride, high_stride; |
| 354 | + |
| 355 | + for (int i = 0; i < height * 2; i++) { |
| 356 | + horiz_filter(input, low, high, width * 2); |
| 357 | + input += in_stride; |
| 358 | + low += a_width; |
| 359 | + high += a_width; |
| 360 | + } |
| 361 | + |
| 362 | + input = s->plane[plane].l_h[7]; |
| 363 | + low = s->plane[plane].subband[7]; |
| 364 | + low_stride = s->plane[plane].band[2][0].a_width; |
| 365 | + high = s->plane[plane].subband[9]; |
| 366 | + high_stride = s->plane[plane].band[2][0].a_width; |
| 367 | + |
| 368 | + for (int i = 0; i < width; i++) { |
| 369 | + vert_filter(input, a_width, low, low_stride, high, high_stride, height * 2); |
| 370 | + input++; |
| 371 | + low++; |
| 372 | + high++; |
| 373 | + } |
| 374 | + |
| 375 | + input = s->plane[plane].l_h[6]; |
| 376 | + low = s->plane[plane].l_h[7]; |
| 377 | + high = s->plane[plane].subband[8]; |
| 378 | + |
| 379 | + for (int i = 0; i < width; i++) { |
| 380 | + vert_filter(input, a_width, low, low_stride, high, high_stride, height * 2); |
| 381 | + input++; |
| 382 | + low++; |
| 383 | + high++; |
| 384 | + } |
| 385 | + |
| 386 | + a_width = s->plane[plane].band[1][0].a_width; |
| 387 | + width = s->plane[plane].band[1][0].width; |
| 388 | + height = s->plane[plane].band[1][0].height; |
| 389 | + input = s->plane[plane].l_h[7]; |
| 390 | + low = s->plane[plane].l_h[3]; |
| 391 | + low_stride = s->plane[plane].band[1][0].a_width; |
| 392 | + high = s->plane[plane].l_h[4]; |
| 393 | + high_stride = s->plane[plane].band[1][0].a_width; |
| 394 | + |
| 395 | + for (int i = 0; i < height * 2; i++) { |
| 396 | + for (int j = 0; j < width * 2; j++) |
| 397 | + input[j] /= 4; |
| 398 | + input += a_width * 2; |
| 399 | + } |
| 400 | + |
| 401 | + input = s->plane[plane].l_h[7]; |
| 402 | + for (int i = 0; i < height * 2; i++) { |
| 403 | + horiz_filter(input, low, high, width * 2); |
| 404 | + input += a_width * 2; |
| 405 | + low += low_stride; |
| 406 | + high += high_stride; |
| 407 | + } |
| 408 | + |
| 409 | + input = s->plane[plane].l_h[4]; |
| 410 | + low = s->plane[plane].subband[4]; |
| 411 | + high = s->plane[plane].subband[6]; |
| 412 | + |
| 413 | + for (int i = 0; i < width; i++) { |
| 414 | + vert_filter(input, a_width, low, low_stride, high, high_stride, height * 2); |
| 415 | + input++; |
| 416 | + low++; |
| 417 | + high++; |
| 418 | + } |
| 419 | + |
| 420 | + input = s->plane[plane].l_h[3]; |
| 421 | + low = s->plane[plane].l_h[4]; |
| 422 | + high = s->plane[plane].subband[5]; |
| 423 | + |
| 424 | + for (int i = 0; i < width; i++) { |
| 425 | + vert_filter(input, a_width, low, low_stride, high, high_stride, height * 2); |
| 426 | + input++; |
| 427 | + low++; |
| 428 | + high++; |
| 429 | + } |
| 430 | + |
| 431 | + a_width = s->plane[plane].band[0][0].a_width; |
| 432 | + width = s->plane[plane].band[0][0].width; |
| 433 | + height = s->plane[plane].band[0][0].height; |
| 434 | + input = s->plane[plane].l_h[4]; |
| 435 | + low = s->plane[plane].l_h[0]; |
| 436 | + low_stride = s->plane[plane].band[0][0].a_width; |
| 437 | + high = s->plane[plane].l_h[1]; |
| 438 | + high_stride = s->plane[plane].band[0][0].a_width; |
| 439 | + |
| 440 | + if (avctx->pix_fmt != AV_PIX_FMT_YUV422P10) { |
| 441 | + for (int i = 0; i < height * 2; i++) { |
| 442 | + for (int j = 0; j < width * 2; j++) |
| 443 | + input[j] /= 4; |
| 444 | + input += a_width * 2; |
| 445 | + } |
| 446 | + } |
| 447 | + |
| 448 | + input = s->plane[plane].l_h[4]; |
| 449 | + for (int i = 0; i < height * 2; i++) { |
| 450 | + horiz_filter(input, low, high, width * 2); |
| 451 | + input += a_width * 2; |
| 452 | + low += low_stride; |
| 453 | + high += high_stride; |
| 454 | + } |
| 455 | + |
| 456 | + low = s->plane[plane].subband[1]; |
| 457 | + high = s->plane[plane].subband[3]; |
| 458 | + input = s->plane[plane].l_h[1]; |
| 459 | + |
| 460 | + for (int i = 0; i < width; i++) { |
| 461 | + vert_filter(input, a_width, low, low_stride, high, high_stride, height * 2); |
| 462 | + input++; |
| 463 | + low++; |
| 464 | + high++; |
| 465 | + } |
| 466 | + |
| 467 | + low = s->plane[plane].subband[0]; |
| 468 | + high = s->plane[plane].subband[2]; |
| 469 | + input = s->plane[plane].l_h[0]; |
| 470 | + |
| 471 | + for (int i = 0; i < width; i++) { |
| 472 | + vert_filter(input, a_width, low, low_stride, high, high_stride, height * 2); |
| 473 | + input++; |
| 474 | + low++; |
| 475 | + high++; |
| 476 | + } |
| 477 | + } |
| 478 | + |
| 479 | + ret = ff_alloc_packet2(avctx, pkt, 60LL + s->planes * (2LL * avctx->width * avctx->height + 1000LL), 0); |
| 480 | + if (ret < 0) |
| 481 | + return ret; |
| 482 | + |
| 483 | + bytestream2_init_writer(pby, pkt->data, pkt->size); |
| 484 | + |
| 485 | + bytestream2_put_be16(pby, SampleType); |
| 486 | + bytestream2_put_be16(pby, 9); |
| 487 | + |
| 488 | + bytestream2_put_be16(pby, SampleIndexTable); |
| 489 | + bytestream2_put_be16(pby, s->planes); |
| 490 | + |
| 491 | + for (int i = 0; i < s->planes; i++) |
| 492 | + bytestream2_put_be32(pby, 0); |
| 493 | + |
| 494 | + bytestream2_put_be16(pby, TransformType); |
| 495 | + bytestream2_put_be16(pby, 0); |
| 496 | + |
| 497 | + bytestream2_put_be16(pby, NumFrames); |
| 498 | + bytestream2_put_be16(pby, 1); |
| 499 | + |
| 500 | + bytestream2_put_be16(pby, ChannelCount); |
| 501 | + bytestream2_put_be16(pby, s->planes); |
| 502 | + |
| 503 | + bytestream2_put_be16(pby, EncodedFormat); |
| 504 | + bytestream2_put_be16(pby, avctx->pix_fmt == AV_PIX_FMT_YUV422P10 ? 1 : 3); |
| 505 | + |
| 506 | + bytestream2_put_be16(pby, WaveletCount); |
| 507 | + bytestream2_put_be16(pby, 3); |
| 508 | + |
| 509 | + bytestream2_put_be16(pby, SubbandCount); |
| 510 | + bytestream2_put_be16(pby, SUBBAND_COUNT); |
| 511 | + |
| 512 | + bytestream2_put_be16(pby, NumSpatial); |
| 513 | + bytestream2_put_be16(pby, 2); |
| 514 | + |
| 515 | + bytestream2_put_be16(pby, FirstWavelet); |
| 516 | + bytestream2_put_be16(pby, 3); |
| 517 | + |
| 518 | + bytestream2_put_be16(pby, ImageWidth); |
| 519 | + bytestream2_put_be16(pby, avctx->width); |
| 520 | + |
| 521 | + bytestream2_put_be16(pby, ImageHeight); |
| 522 | + bytestream2_put_be16(pby, avctx->height); |
| 523 | + |
| 524 | + bytestream2_put_be16(pby, Precision); |
| 525 | + bytestream2_put_be16(pby, avctx->pix_fmt == AV_PIX_FMT_YUV422P10 ? 10 : 12); |
| 526 | + |
| 527 | + bytestream2_put_be16(pby, PrescaleTable); |
| 528 | + bytestream2_put_be16(pby, 0x2800); |
| 529 | + |
| 530 | + bytestream2_put_be16(pby, SampleFlags); |
| 531 | + bytestream2_put_be16(pby, 1); |
| 532 | + |
| 533 | + for (int p = 0; p < s->planes; p++) { |
| 534 | + int width = s->plane[p].band[0][0].width; |
| 535 | + int a_width = s->plane[p].band[0][0].a_width; |
| 536 | + int height = s->plane[p].band[0][0].height; |
| 537 | + int16_t *data = s->plane[p].subband[0]; |
| 538 | + |
| 539 | + if (p) { |
| 540 | + bytestream2_put_be16(pby, SampleType); |
| 541 | + bytestream2_put_be16(pby, 3); |
| 542 | + |
| 543 | + bytestream2_put_be16(pby, ChannelNumber); |
| 544 | + bytestream2_put_be16(pby, p); |
| 545 | + } |
| 546 | + |
| 547 | + bytestream2_put_be16(pby, BitstreamMarker); |
| 548 | + bytestream2_put_be16(pby, 0x1a4a); |
| 549 | + |
| 550 | + pos = bytestream2_tell_p(pby); |
| 551 | + |
| 552 | + bytestream2_put_be16(pby, LowpassSubband); |
| 553 | + bytestream2_put_be16(pby, 0); |
| 554 | + |
| 555 | + bytestream2_put_be16(pby, NumLevels); |
| 556 | + bytestream2_put_be16(pby, 3); |
| 557 | + |
| 558 | + bytestream2_put_be16(pby, LowpassWidth); |
| 559 | + bytestream2_put_be16(pby, width); |
| 560 | + |
| 561 | + bytestream2_put_be16(pby, LowpassHeight); |
| 562 | + bytestream2_put_be16(pby, height); |
| 563 | + |
| 564 | + bytestream2_put_be16(pby, PixelOffset); |
| 565 | + bytestream2_put_be16(pby, 0); |
| 566 | + |
| 567 | + bytestream2_put_be16(pby, LowpassQuantization); |
| 568 | + bytestream2_put_be16(pby, 1); |
| 569 | + |
| 570 | + bytestream2_put_be16(pby, LowpassPrecision); |
| 571 | + bytestream2_put_be16(pby, 16); |
| 572 | + |
| 573 | + bytestream2_put_be16(pby, BitstreamMarker); |
| 574 | + bytestream2_put_be16(pby, 0x0f0f); |
| 575 | + |
| 576 | + for (int i = 0; i < height; i++) { |
| 577 | + for (int j = 0; j < width; j++) |
| 578 | + bytestream2_put_be16(pby, data[j]); |
| 579 | + data += a_width; |
| 580 | + } |
| 581 | + |
| 582 | + bytestream2_put_be16(pby, BitstreamMarker); |
| 583 | + bytestream2_put_be16(pby, 0x1b4b); |
| 584 | + |
| 585 | + for (int l = 0; l < 3; l++) { |
| 586 | + for (int i = 0; i < 3; i++) { |
| 587 | + s->plane[p].quantization[1 + l * 3 + i] = quantization_per_subband[s->compression][l * 3 + i] / s->quantization_div; |
| 588 | + } |
| 589 | + } |
| 590 | + |
| 591 | + for (int l = 0; l < 3; l++) { |
| 592 | + int a_width = s->plane[p].band[l][0].a_width; |
| 593 | + int width = s->plane[p].band[l][0].width; |
| 594 | + int stride = FFALIGN(width, 8); |
| 595 | + int height = s->plane[p].band[l][0].height; |
| 596 | + |
| 597 | + bytestream2_put_be16(pby, BitstreamMarker); |
| 598 | + bytestream2_put_be16(pby, 0x0d0d); |
| 599 | + |
| 600 | + bytestream2_put_be16(pby, WaveletType); |
| 601 | + bytestream2_put_be16(pby, 3 + 2 * (l == 2)); |
| 602 | + |
| 603 | + bytestream2_put_be16(pby, WaveletNumber); |
| 604 | + bytestream2_put_be16(pby, 3 - l); |
| 605 | + |
| 606 | + bytestream2_put_be16(pby, WaveletLevel); |
| 607 | + bytestream2_put_be16(pby, 3 - l); |
| 608 | + |
| 609 | + bytestream2_put_be16(pby, NumBands); |
| 610 | + bytestream2_put_be16(pby, 4); |
| 611 | + |
| 612 | + bytestream2_put_be16(pby, HighpassWidth); |
| 613 | + bytestream2_put_be16(pby, width); |
| 614 | + |
| 615 | + bytestream2_put_be16(pby, HighpassHeight); |
| 616 | + bytestream2_put_be16(pby, height); |
| 617 | + |
| 618 | + bytestream2_put_be16(pby, LowpassBorder); |
| 619 | + bytestream2_put_be16(pby, 0); |
| 620 | + |
| 621 | + bytestream2_put_be16(pby, HighpassBorder); |
| 622 | + bytestream2_put_be16(pby, 0); |
| 623 | + |
| 624 | + bytestream2_put_be16(pby, LowpassScale); |
| 625 | + bytestream2_put_be16(pby, 1); |
| 626 | + |
| 627 | + bytestream2_put_be16(pby, LowpassDivisor); |
| 628 | + bytestream2_put_be16(pby, 1); |
| 629 | + |
| 630 | + for (int i = 0; i < 3; i++) { |
| 631 | + int16_t *data = s->plane[p].subband[1 + l * 3 + i]; |
| 632 | + int count = 0, padd = 0; |
| 633 | + |
| 634 | + bytestream2_put_be16(pby, BitstreamMarker); |
| 635 | + bytestream2_put_be16(pby, 0x0e0e); |
| 636 | + |
| 637 | + bytestream2_put_be16(pby, SubbandNumber); |
| 638 | + bytestream2_put_be16(pby, i + 1); |
| 639 | + |
| 640 | + bytestream2_put_be16(pby, BandCodingFlags); |
| 641 | + bytestream2_put_be16(pby, 2); |
| 642 | + |
| 643 | + bytestream2_put_be16(pby, BandWidth); |
| 644 | + bytestream2_put_be16(pby, width); |
| 645 | + |
| 646 | + bytestream2_put_be16(pby, BandHeight); |
| 647 | + bytestream2_put_be16(pby, height); |
| 648 | + |
| 649 | + bytestream2_put_be16(pby, SubbandBand); |
| 650 | + bytestream2_put_be16(pby, 1 + l * 3 + i); |
| 651 | + |
| 652 | + bytestream2_put_be16(pby, BandEncoding); |
| 653 | + bytestream2_put_be16(pby, 3); |
| 654 | + |
| 655 | + bytestream2_put_be16(pby, Quantization); |
| 656 | + bytestream2_put_be16(pby, s->plane[p].quantization[1 + l * 3 + i]); |
| 657 | + |
| 658 | + bytestream2_put_be16(pby, BandScale); |
| 659 | + bytestream2_put_be16(pby, 1); |
| 660 | + |
| 661 | + bytestream2_put_be16(pby, BandHeader); |
| 662 | + bytestream2_put_be16(pby, 0); |
| 663 | + |
| 664 | + quantize_band(data, width, a_width, height, |
| 665 | + s->plane[p].quantization[1 + l * 3 + i]); |
| 666 | + |
| 667 | + init_put_bits(pb, pkt->data + bytestream2_tell_p(pby), bytestream2_get_bytes_left_p(pby)); |
| 668 | + |
| 669 | + for (int m = 0; m < height; m++) { |
| 670 | + for (int j = 0; j < stride; j++) { |
| 671 | + int16_t index = data[j]; |
| 672 | + |
| 673 | + if (index < 0) |
| 674 | + index += 512; |
| 675 | + if (index == 0) { |
| 676 | + count++; |
| 677 | + continue; |
| 678 | + } else if (count > 0) { |
| 679 | + count = put_runcode(pb, count, rb); |
| 680 | + } |
| 681 | + |
| 682 | + put_bits(pb, cb[index].size, cb[index].bits); |
| 683 | + } |
| 684 | + |
| 685 | + data += a_width; |
| 686 | + } |
| 687 | + |
| 688 | + if (count > 0) { |
| 689 | + count = put_runcode(pb, count, rb); |
| 690 | + } |
| 691 | + |
| 692 | + put_bits(pb, cb[512].size, cb[512].bits); |
| 693 | + |
| 694 | + flush_put_bits(pb); |
| 695 | + bytestream2_skip_p(pby, put_bits_count(pb) >> 3); |
| 696 | + padd = (4 - (bytestream2_tell_p(pby) & 3)) & 3; |
| 697 | + while (padd--) |
| 698 | + bytestream2_put_byte(pby, 0); |
| 699 | + |
| 700 | + bytestream2_put_be16(pby, BandTrailer); |
| 701 | + bytestream2_put_be16(pby, 0); |
| 702 | + } |
| 703 | + |
| 704 | + bytestream2_put_be16(pby, BitstreamMarker); |
| 705 | + bytestream2_put_be16(pby, 0x0c0c); |
| 706 | + } |
| 707 | + |
| 708 | + s->plane[p].size = bytestream2_tell_p(pby) - pos; |
| 709 | + } |
| 710 | + |
| 711 | + bytestream2_put_be16(pby, GroupTrailer); |
| 712 | + bytestream2_put_be16(pby, 0); |
| 713 | + |
| 714 | + av_shrink_packet(pkt, bytestream2_tell_p(pby)); |
| 715 | + |
| 716 | + pkt->flags |= AV_PKT_FLAG_KEY; |
| 717 | + |
| 718 | + bytestream2_seek_p(pby, 8, SEEK_SET); |
| 719 | + for (int i = 0; i < s->planes; i++) |
| 720 | + bytestream2_put_be32(pby, s->plane[i].size); |
| 721 | + |
| 722 | + *got_packet = 1; |
| 723 | + |
| 724 | + return 0; |
| 725 | +} |
| 726 | + |
| 727 | +static av_cold int cfhd_encode_close(AVCodecContext *avctx) |
| 728 | +{ |
| 729 | + CFHDEncContext *s = avctx->priv_data; |
| 730 | + |
| 731 | + for (int i = 0; i < s->planes; i++) { |
| 732 | + av_freep(&s->plane[i].dwt_buf); |
| 733 | + av_freep(&s->plane[i].dwt_tmp); |
| 734 | + |
| 735 | + for (int j = 0; j < SUBBAND_COUNT; j++) |
| 736 | + s->plane[i].subband[j] = NULL; |
| 737 | + |
| 738 | + for (int j = 0; j < 8; j++) |
| 739 | + s->plane[i].l_h[j] = NULL; |
| 740 | + } |
| 741 | + |
| 742 | + return 0; |
| 743 | +} |
| 744 | + |
| 745 | +AVCodec ff_cfhd_encoder = { |
| 746 | + .name = "cfhd", |
| 747 | + .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"), |
| 748 | + .type = AVMEDIA_TYPE_VIDEO, |
| 749 | + .id = AV_CODEC_ID_CFHD, |
| 750 | + .priv_data_size = sizeof(CFHDEncContext), |
| 751 | + .init = cfhd_encode_init, |
| 752 | + .close = cfhd_encode_close, |
| 753 | + .encode2 = cfhd_encode_frame, |
| 754 | + .capabilities = AV_CODEC_CAP_FRAME_THREADS, |
| 755 | + .pix_fmts = (const enum AVPixelFormat[]) { |
| 756 | + AV_PIX_FMT_YUV422P10, |
| 757 | + AV_PIX_FMT_GBRP12, |
| 758 | + AV_PIX_FMT_NONE |
| 759 | + }, |
| 760 | +}; |