| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 <stddef.h> | ||
| 27 | |||
| 28 | #include "libavutil/mem.h" | ||
| 29 | #include "libavutil/opt.h" | ||
| 30 | #include "libavutil/pixdesc.h" | ||
| 31 | |||
| 32 | #include "avcodec.h" | ||
| 33 | #include "bytestream.h" | ||
| 34 | #include "cfhd.h" | ||
| 35 | #include "cfhdencdsp.h" | ||
| 36 | #include "codec_internal.h" | ||
| 37 | #include "encode.h" | ||
| 38 | #include "put_bits.h" | ||
| 39 | |||
| 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 | |||
| 229 | PutBitContext pb; | ||
| 230 | PutByteContext pby; | ||
| 231 | |||
| 232 | int quality; | ||
| 233 | int planes; | ||
| 234 | int chroma_h_shift; | ||
| 235 | int chroma_v_shift; | ||
| 236 | PlaneEnc plane[4]; | ||
| 237 | |||
| 238 | uint16_t lut[1024]; | ||
| 239 | Runbook rb[321]; | ||
| 240 | Codebook cb[513]; | ||
| 241 | int16_t *alpha; | ||
| 242 | |||
| 243 | CFHDEncDSPContext dsp; | ||
| 244 | } CFHDEncContext; | ||
| 245 | |||
| 246 | ✗ | static av_cold int cfhd_encode_init(AVCodecContext *avctx) | |
| 247 | { | ||
| 248 | ✗ | CFHDEncContext *s = avctx->priv_data; | |
| 249 | ✗ | const int sign_mask = 256; | |
| 250 | ✗ | const int twos_complement = -sign_mask; | |
| 251 | ✗ | const int mag_mask = sign_mask - 1; | |
| 252 | ✗ | int ret, last = 0; | |
| 253 | |||
| 254 | ✗ | ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, | |
| 255 | &s->chroma_h_shift, | ||
| 256 | &s->chroma_v_shift); | ||
| 257 | ✗ | if (ret < 0) | |
| 258 | ✗ | return ret; | |
| 259 | |||
| 260 | ✗ | if (avctx->height < 32) { | |
| 261 | ✗ | av_log(avctx, AV_LOG_ERROR, "Height must be >= 32.\n"); | |
| 262 | ✗ | return AVERROR_INVALIDDATA; | |
| 263 | } | ||
| 264 | |||
| 265 | ✗ | if (avctx->width & 15) { | |
| 266 | ✗ | av_log(avctx, AV_LOG_ERROR, "Width must be multiple of 16.\n"); | |
| 267 | ✗ | return AVERROR_INVALIDDATA; | |
| 268 | } | ||
| 269 | |||
| 270 | ✗ | s->planes = av_pix_fmt_count_planes(avctx->pix_fmt); | |
| 271 | |||
| 272 | ✗ | for (int i = 0; i < s->planes; i++) { | |
| 273 | int w8, h8, w4, h4, w2, h2; | ||
| 274 | ✗ | const int a_height = FFALIGN(avctx->height, 8); | |
| 275 | ✗ | int width = i ? AV_CEIL_RSHIFT(avctx->width, s->chroma_h_shift) : avctx->width; | |
| 276 | ✗ | int height = i ? a_height >> s->chroma_v_shift: a_height; | |
| 277 | |||
| 278 | ✗ | w8 = width / 8 + 64; | |
| 279 | ✗ | h8 = height / 8; | |
| 280 | ✗ | w4 = w8 * 2; | |
| 281 | ✗ | h4 = h8 * 2; | |
| 282 | ✗ | w2 = w4 * 2; | |
| 283 | ✗ | h2 = h4 * 2; | |
| 284 | |||
| 285 | ✗ | s->plane[i].dwt_buf = | |
| 286 | ✗ | av_calloc(h8 * 8 * w8 * 8, sizeof(*s->plane[i].dwt_buf)); | |
| 287 | ✗ | s->plane[i].dwt_tmp = | |
| 288 | ✗ | av_calloc(h8 * 8 * w8 * 8, sizeof(*s->plane[i].dwt_tmp)); | |
| 289 | ✗ | if (!s->plane[i].dwt_buf || !s->plane[i].dwt_tmp) | |
| 290 | ✗ | return AVERROR(ENOMEM); | |
| 291 | |||
| 292 | ✗ | s->plane[i].subband[0] = s->plane[i].dwt_buf; | |
| 293 | ✗ | s->plane[i].subband[1] = s->plane[i].dwt_buf + 2 * w8 * h8; | |
| 294 | ✗ | s->plane[i].subband[2] = s->plane[i].dwt_buf + 1 * w8 * h8; | |
| 295 | ✗ | s->plane[i].subband[3] = s->plane[i].dwt_buf + 3 * w8 * h8; | |
| 296 | ✗ | s->plane[i].subband[4] = s->plane[i].dwt_buf + 2 * w4 * h4; | |
| 297 | ✗ | s->plane[i].subband[5] = s->plane[i].dwt_buf + 1 * w4 * h4; | |
| 298 | ✗ | s->plane[i].subband[6] = s->plane[i].dwt_buf + 3 * w4 * h4; | |
| 299 | ✗ | s->plane[i].subband[7] = s->plane[i].dwt_buf + 2 * w2 * h2; | |
| 300 | ✗ | s->plane[i].subband[8] = s->plane[i].dwt_buf + 1 * w2 * h2; | |
| 301 | ✗ | s->plane[i].subband[9] = s->plane[i].dwt_buf + 3 * w2 * h2; | |
| 302 | |||
| 303 | ✗ | for (int j = 0; j < DWT_LEVELS; j++) { | |
| 304 | ✗ | for (int k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) { | |
| 305 | ✗ | s->plane[i].band[j][k].width = (width / 8) << j; | |
| 306 | ✗ | s->plane[i].band[j][k].height = height >> (DWT_LEVELS - j); | |
| 307 | ✗ | s->plane[i].band[j][k].a_width = w8 << j; | |
| 308 | ✗ | s->plane[i].band[j][k].a_height = h8 << j; | |
| 309 | } | ||
| 310 | } | ||
| 311 | |||
| 312 | /* ll2 and ll1 commented out because they are done in-place */ | ||
| 313 | ✗ | s->plane[i].l_h[0] = s->plane[i].dwt_tmp; | |
| 314 | ✗ | s->plane[i].l_h[1] = s->plane[i].dwt_tmp + 2 * w8 * h8; | |
| 315 | // s->plane[i].l_h[2] = ll2; | ||
| 316 | ✗ | s->plane[i].l_h[3] = s->plane[i].dwt_tmp; | |
| 317 | ✗ | s->plane[i].l_h[4] = s->plane[i].dwt_tmp + 2 * w4 * h4; | |
| 318 | // s->plane[i].l_h[5] = ll1; | ||
| 319 | ✗ | s->plane[i].l_h[6] = s->plane[i].dwt_tmp; | |
| 320 | ✗ | s->plane[i].l_h[7] = s->plane[i].dwt_tmp + 2 * w2 * h2; | |
| 321 | } | ||
| 322 | |||
| 323 | ✗ | for (int i = 0; i < 512; i++) { | |
| 324 | ✗ | int value = (i & sign_mask) ? twos_complement + (i & mag_mask): i; | |
| 325 | ✗ | int mag = FFMIN(FFABS(value), 255); | |
| 326 | |||
| 327 | ✗ | if (mag) { | |
| 328 | ✗ | s->cb[i].bits = (codebook[mag][1] << 1) | (value > 0 ? 0 : 1); | |
| 329 | ✗ | s->cb[i].size = codebook[mag][0] + 1; | |
| 330 | } else { | ||
| 331 | ✗ | s->cb[i].bits = codebook[mag][1]; | |
| 332 | ✗ | s->cb[i].size = codebook[mag][0]; | |
| 333 | } | ||
| 334 | } | ||
| 335 | |||
| 336 | ✗ | s->cb[512].bits = 0x3114ba3; | |
| 337 | ✗ | s->cb[512].size = 26; | |
| 338 | |||
| 339 | ✗ | s->rb[0].run = 0; | |
| 340 | |||
| 341 | ✗ | for (int i = 1, j = 0; i < 320 && j < 17; j++) { | |
| 342 | ✗ | int run = runbook[j][2]; | |
| 343 | ✗ | int end = runbook[j+1][2]; | |
| 344 | |||
| 345 | ✗ | while (i < end) { | |
| 346 | ✗ | s->rb[i].run = run; | |
| 347 | ✗ | s->rb[i].bits = runbook[j][1]; | |
| 348 | ✗ | s->rb[i++].size = runbook[j][0]; | |
| 349 | } | ||
| 350 | } | ||
| 351 | |||
| 352 | ✗ | s->rb[320].bits = runbook[17][1]; | |
| 353 | ✗ | s->rb[320].size = runbook[17][0]; | |
| 354 | ✗ | s->rb[320].run = 320; | |
| 355 | |||
| 356 | ✗ | for (int i = 0; i < 256; i++) { | |
| 357 | ✗ | int idx = i + ((768LL * i * i * i) / (256 * 256 * 256)); | |
| 358 | |||
| 359 | ✗ | s->lut[idx] = i; | |
| 360 | } | ||
| 361 | ✗ | for (int i = 0; i < 1024; i++) { | |
| 362 | ✗ | if (s->lut[i]) | |
| 363 | ✗ | last = s->lut[i]; | |
| 364 | else | ||
| 365 | ✗ | s->lut[i] = last; | |
| 366 | } | ||
| 367 | |||
| 368 | ✗ | ff_cfhdencdsp_init(&s->dsp); | |
| 369 | |||
| 370 | ✗ | if (s->planes != 4) | |
| 371 | ✗ | return 0; | |
| 372 | |||
| 373 | ✗ | s->alpha = av_calloc(avctx->width * avctx->height, sizeof(*s->alpha)); | |
| 374 | ✗ | if (!s->alpha) | |
| 375 | ✗ | return AVERROR(ENOMEM); | |
| 376 | |||
| 377 | ✗ | return 0; | |
| 378 | } | ||
| 379 | |||
| 380 | ✗ | static void quantize_band(int16_t *input, int width, int a_width, | |
| 381 | int height, unsigned quantization) | ||
| 382 | { | ||
| 383 | ✗ | const int16_t factor = (uint32_t)(1U << 15) / quantization; | |
| 384 | |||
| 385 | ✗ | for (int i = 0; i < height; i++) { | |
| 386 | ✗ | for (int j = 0; j < width; j++) | |
| 387 | ✗ | input[j] = av_clip_intp2(((input[j] * factor + 16384 * FFSIGN(input[j])) / 32768), 10); | |
| 388 | ✗ | input += a_width; | |
| 389 | } | ||
| 390 | ✗ | } | |
| 391 | |||
| 392 | ✗ | static int put_runcode(PutBitContext *pb, int count, const Runbook *const rb) | |
| 393 | { | ||
| 394 | ✗ | while (count > 0) { | |
| 395 | ✗ | const int index = FFMIN(320, count); | |
| 396 | |||
| 397 | ✗ | put_bits(pb, rb[index].size, rb[index].bits); | |
| 398 | ✗ | count -= rb[index].run; | |
| 399 | } | ||
| 400 | |||
| 401 | ✗ | return 0; | |
| 402 | } | ||
| 403 | |||
| 404 | ✗ | static void process_alpha(const int16_t *src, int width, int height, ptrdiff_t stride, int16_t *dst) | |
| 405 | { | ||
| 406 | ✗ | for (int i = 0; i < height; i++) { | |
| 407 | ✗ | for (int j = 0; j < width; j++) { | |
| 408 | ✗ | int alpha = src[j]; | |
| 409 | |||
| 410 | ✗ | if (alpha > 0 && alpha < 4080) { | |
| 411 | ✗ | alpha *= 223; | |
| 412 | ✗ | alpha += 128; | |
| 413 | ✗ | alpha >>= 8; | |
| 414 | ✗ | alpha += 256; | |
| 415 | } | ||
| 416 | |||
| 417 | ✗ | dst[j] = av_clip_uintp2(alpha, 12); | |
| 418 | } | ||
| 419 | |||
| 420 | ✗ | src += stride; | |
| 421 | ✗ | dst += width; | |
| 422 | } | ||
| 423 | ✗ | } | |
| 424 | |||
| 425 | ✗ | static int cfhd_encode_frame(AVCodecContext *avctx, AVPacket *pkt, | |
| 426 | const AVFrame *frame, int *got_packet) | ||
| 427 | { | ||
| 428 | ✗ | CFHDEncContext *s = avctx->priv_data; | |
| 429 | ✗ | CFHDEncDSPContext *dsp = &s->dsp; | |
| 430 | ✗ | PutByteContext *pby = &s->pby; | |
| 431 | ✗ | PutBitContext *pb = &s->pb; | |
| 432 | ✗ | const Codebook *const cb = s->cb; | |
| 433 | ✗ | const Runbook *const rb = s->rb; | |
| 434 | ✗ | const uint16_t *lut = s->lut; | |
| 435 | unsigned pos; | ||
| 436 | int ret; | ||
| 437 | |||
| 438 | ✗ | for (int plane = 0; plane < s->planes; plane++) { | |
| 439 | ✗ | const int h_shift = plane ? s->chroma_h_shift : 0; | |
| 440 | ✗ | int width = s->plane[plane].band[2][0].width; | |
| 441 | ✗ | int a_width = s->plane[plane].band[2][0].a_width; | |
| 442 | ✗ | int height = s->plane[plane].band[2][0].height; | |
| 443 | ✗ | int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane; | |
| 444 | ✗ | const int16_t *input = (int16_t *)frame->data[act_plane]; | |
| 445 | int16_t *buf; | ||
| 446 | ✗ | int16_t *low = s->plane[plane].l_h[6]; | |
| 447 | ✗ | int16_t *high = s->plane[plane].l_h[7]; | |
| 448 | ✗ | ptrdiff_t in_stride = frame->linesize[act_plane] / 2; | |
| 449 | int low_stride, high_stride; | ||
| 450 | |||
| 451 | ✗ | if (plane == 3) { | |
| 452 | ✗ | process_alpha(input, avctx->width, avctx->height, | |
| 453 | in_stride, s->alpha); | ||
| 454 | ✗ | input = s->alpha; | |
| 455 | ✗ | in_stride = avctx->width; | |
| 456 | } | ||
| 457 | |||
| 458 | ✗ | dsp->horiz_filter(input, low, high, | |
| 459 | in_stride, a_width, a_width, | ||
| 460 | ✗ | avctx->width >> h_shift, avctx->height); | |
| 461 | |||
| 462 | ✗ | input = s->plane[plane].l_h[7]; | |
| 463 | ✗ | low = s->plane[plane].subband[7]; | |
| 464 | ✗ | low_stride = s->plane[plane].band[2][0].a_width; | |
| 465 | ✗ | high = s->plane[plane].subband[9]; | |
| 466 | ✗ | high_stride = s->plane[plane].band[2][0].a_width; | |
| 467 | |||
| 468 | ✗ | dsp->vert_filter(input, low, high, | |
| 469 | a_width, low_stride, high_stride, | ||
| 470 | width, height * 2); | ||
| 471 | |||
| 472 | ✗ | input = s->plane[plane].l_h[6]; | |
| 473 | ✗ | low = s->plane[plane].l_h[7]; | |
| 474 | ✗ | high = s->plane[plane].subband[8]; | |
| 475 | |||
| 476 | ✗ | dsp->vert_filter(input, low, high, | |
| 477 | a_width, low_stride, high_stride, | ||
| 478 | width, height * 2); | ||
| 479 | |||
| 480 | ✗ | a_width = s->plane[plane].band[1][0].a_width; | |
| 481 | ✗ | width = s->plane[plane].band[1][0].width; | |
| 482 | ✗ | height = s->plane[plane].band[1][0].height; | |
| 483 | ✗ | input = s->plane[plane].l_h[7]; | |
| 484 | ✗ | low = s->plane[plane].l_h[3]; | |
| 485 | ✗ | low_stride = s->plane[plane].band[1][0].a_width; | |
| 486 | ✗ | high = s->plane[plane].l_h[4]; | |
| 487 | ✗ | high_stride = s->plane[plane].band[1][0].a_width; | |
| 488 | |||
| 489 | ✗ | buf = s->plane[plane].l_h[7]; | |
| 490 | ✗ | for (int i = 0; i < height * 2; i++) { | |
| 491 | ✗ | for (int j = 0; j < width * 2; j++) | |
| 492 | ✗ | buf[j] /= 4; | |
| 493 | ✗ | buf += a_width * 2; | |
| 494 | } | ||
| 495 | |||
| 496 | ✗ | dsp->horiz_filter(input, low, high, | |
| 497 | ✗ | a_width * 2, low_stride, high_stride, | |
| 498 | width * 2, height * 2); | ||
| 499 | |||
| 500 | ✗ | input = s->plane[plane].l_h[4]; | |
| 501 | ✗ | low = s->plane[plane].subband[4]; | |
| 502 | ✗ | high = s->plane[plane].subband[6]; | |
| 503 | |||
| 504 | ✗ | dsp->vert_filter(input, low, high, | |
| 505 | a_width, low_stride, high_stride, | ||
| 506 | width, height * 2); | ||
| 507 | |||
| 508 | ✗ | input = s->plane[plane].l_h[3]; | |
| 509 | ✗ | low = s->plane[plane].l_h[4]; | |
| 510 | ✗ | high = s->plane[plane].subband[5]; | |
| 511 | |||
| 512 | ✗ | dsp->vert_filter(input, low, high, | |
| 513 | a_width, low_stride, high_stride, | ||
| 514 | width, height * 2); | ||
| 515 | |||
| 516 | ✗ | a_width = s->plane[plane].band[0][0].a_width; | |
| 517 | ✗ | width = s->plane[plane].band[0][0].width; | |
| 518 | ✗ | height = s->plane[plane].band[0][0].height; | |
| 519 | ✗ | input = s->plane[plane].l_h[4]; | |
| 520 | ✗ | low = s->plane[plane].l_h[0]; | |
| 521 | ✗ | low_stride = s->plane[plane].band[0][0].a_width; | |
| 522 | ✗ | high = s->plane[plane].l_h[1]; | |
| 523 | ✗ | high_stride = s->plane[plane].band[0][0].a_width; | |
| 524 | |||
| 525 | ✗ | if (avctx->pix_fmt != AV_PIX_FMT_YUV422P10) { | |
| 526 | ✗ | int16_t *buf = s->plane[plane].l_h[4]; | |
| 527 | ✗ | for (int i = 0; i < height * 2; i++) { | |
| 528 | ✗ | for (int j = 0; j < width * 2; j++) | |
| 529 | ✗ | buf[j] /= 4; | |
| 530 | ✗ | buf += a_width * 2; | |
| 531 | } | ||
| 532 | } | ||
| 533 | |||
| 534 | ✗ | dsp->horiz_filter(input, low, high, | |
| 535 | ✗ | a_width * 2, low_stride, high_stride, | |
| 536 | width * 2, height * 2); | ||
| 537 | |||
| 538 | ✗ | low = s->plane[plane].subband[1]; | |
| 539 | ✗ | high = s->plane[plane].subband[3]; | |
| 540 | ✗ | input = s->plane[plane].l_h[1]; | |
| 541 | |||
| 542 | ✗ | dsp->vert_filter(input, low, high, | |
| 543 | a_width, low_stride, high_stride, | ||
| 544 | width, height * 2); | ||
| 545 | |||
| 546 | ✗ | low = s->plane[plane].subband[0]; | |
| 547 | ✗ | high = s->plane[plane].subband[2]; | |
| 548 | ✗ | input = s->plane[plane].l_h[0]; | |
| 549 | |||
| 550 | ✗ | dsp->vert_filter(input, low, high, | |
| 551 | a_width, low_stride, high_stride, | ||
| 552 | width, height * 2); | ||
| 553 | } | ||
| 554 | |||
| 555 | ✗ | ret = ff_alloc_packet(avctx, pkt, 256LL + s->planes * (4LL * avctx->width * (avctx->height + 15) + 2048LL)); | |
| 556 | ✗ | if (ret < 0) | |
| 557 | ✗ | return ret; | |
| 558 | |||
| 559 | ✗ | bytestream2_init_writer(pby, pkt->data, pkt->size); | |
| 560 | |||
| 561 | ✗ | bytestream2_put_be16(pby, SampleType); | |
| 562 | ✗ | bytestream2_put_be16(pby, 9); | |
| 563 | |||
| 564 | ✗ | bytestream2_put_be16(pby, SampleIndexTable); | |
| 565 | ✗ | bytestream2_put_be16(pby, s->planes); | |
| 566 | |||
| 567 | ✗ | for (int i = 0; i < s->planes; i++) | |
| 568 | ✗ | bytestream2_put_be32(pby, 0); | |
| 569 | |||
| 570 | ✗ | bytestream2_put_be16(pby, TransformType); | |
| 571 | ✗ | bytestream2_put_be16(pby, 0); | |
| 572 | |||
| 573 | ✗ | bytestream2_put_be16(pby, NumFrames); | |
| 574 | ✗ | bytestream2_put_be16(pby, 1); | |
| 575 | |||
| 576 | ✗ | bytestream2_put_be16(pby, ChannelCount); | |
| 577 | ✗ | bytestream2_put_be16(pby, s->planes); | |
| 578 | |||
| 579 | ✗ | bytestream2_put_be16(pby, EncodedFormat); | |
| 580 | ✗ | bytestream2_put_be16(pby, avctx->pix_fmt == AV_PIX_FMT_YUV422P10 ? 1 : 3 + (s->planes == 4)); | |
| 581 | |||
| 582 | ✗ | bytestream2_put_be16(pby, WaveletCount); | |
| 583 | ✗ | bytestream2_put_be16(pby, 3); | |
| 584 | |||
| 585 | ✗ | bytestream2_put_be16(pby, SubbandCount); | |
| 586 | ✗ | bytestream2_put_be16(pby, SUBBAND_COUNT); | |
| 587 | |||
| 588 | ✗ | bytestream2_put_be16(pby, NumSpatial); | |
| 589 | ✗ | bytestream2_put_be16(pby, 2); | |
| 590 | |||
| 591 | ✗ | bytestream2_put_be16(pby, FirstWavelet); | |
| 592 | ✗ | bytestream2_put_be16(pby, 3); | |
| 593 | |||
| 594 | ✗ | bytestream2_put_be16(pby, ImageWidth); | |
| 595 | ✗ | bytestream2_put_be16(pby, avctx->width); | |
| 596 | |||
| 597 | ✗ | bytestream2_put_be16(pby, ImageHeight); | |
| 598 | ✗ | bytestream2_put_be16(pby, FFALIGN(avctx->height, 8)); | |
| 599 | |||
| 600 | ✗ | bytestream2_put_be16(pby, -DisplayHeight); | |
| 601 | ✗ | bytestream2_put_be16(pby, avctx->height); | |
| 602 | |||
| 603 | ✗ | bytestream2_put_be16(pby, -FrameNumber); | |
| 604 | ✗ | bytestream2_put_be16(pby, frame->pts & 0xFFFF); | |
| 605 | |||
| 606 | ✗ | bytestream2_put_be16(pby, Precision); | |
| 607 | ✗ | bytestream2_put_be16(pby, avctx->pix_fmt == AV_PIX_FMT_YUV422P10 ? 10 : 12); | |
| 608 | |||
| 609 | ✗ | bytestream2_put_be16(pby, PrescaleTable); | |
| 610 | ✗ | bytestream2_put_be16(pby, avctx->pix_fmt == AV_PIX_FMT_YUV422P10 ? 0x2000 : 0x2800); | |
| 611 | |||
| 612 | ✗ | bytestream2_put_be16(pby, SampleFlags); | |
| 613 | ✗ | bytestream2_put_be16(pby, 1); | |
| 614 | |||
| 615 | ✗ | for (int p = 0; p < s->planes; p++) { | |
| 616 | ✗ | int width = s->plane[p].band[0][0].width; | |
| 617 | ✗ | int a_width = s->plane[p].band[0][0].a_width; | |
| 618 | ✗ | int height = s->plane[p].band[0][0].height; | |
| 619 | ✗ | int16_t *data = s->plane[p].subband[0]; | |
| 620 | |||
| 621 | ✗ | if (p) { | |
| 622 | ✗ | bytestream2_put_be16(pby, SampleType); | |
| 623 | ✗ | bytestream2_put_be16(pby, 3); | |
| 624 | |||
| 625 | ✗ | bytestream2_put_be16(pby, ChannelNumber); | |
| 626 | ✗ | bytestream2_put_be16(pby, p); | |
| 627 | } | ||
| 628 | |||
| 629 | ✗ | bytestream2_put_be16(pby, BitstreamMarker); | |
| 630 | ✗ | bytestream2_put_be16(pby, 0x1a4a); | |
| 631 | |||
| 632 | ✗ | pos = bytestream2_tell_p(pby); | |
| 633 | |||
| 634 | ✗ | bytestream2_put_be16(pby, LowpassSubband); | |
| 635 | ✗ | bytestream2_put_be16(pby, 0); | |
| 636 | |||
| 637 | ✗ | bytestream2_put_be16(pby, NumLevels); | |
| 638 | ✗ | bytestream2_put_be16(pby, 3); | |
| 639 | |||
| 640 | ✗ | bytestream2_put_be16(pby, LowpassWidth); | |
| 641 | ✗ | bytestream2_put_be16(pby, width); | |
| 642 | |||
| 643 | ✗ | bytestream2_put_be16(pby, LowpassHeight); | |
| 644 | ✗ | bytestream2_put_be16(pby, height); | |
| 645 | |||
| 646 | ✗ | bytestream2_put_be16(pby, PixelOffset); | |
| 647 | ✗ | bytestream2_put_be16(pby, 0); | |
| 648 | |||
| 649 | ✗ | bytestream2_put_be16(pby, LowpassQuantization); | |
| 650 | ✗ | bytestream2_put_be16(pby, 1); | |
| 651 | |||
| 652 | ✗ | bytestream2_put_be16(pby, LowpassPrecision); | |
| 653 | ✗ | bytestream2_put_be16(pby, 16); | |
| 654 | |||
| 655 | ✗ | bytestream2_put_be16(pby, BitstreamMarker); | |
| 656 | ✗ | bytestream2_put_be16(pby, 0x0f0f); | |
| 657 | |||
| 658 | ✗ | for (int i = 0; i < height; i++) { | |
| 659 | ✗ | for (int j = 0; j < width; j++) | |
| 660 | ✗ | bytestream2_put_be16(pby, data[j]); | |
| 661 | ✗ | data += a_width; | |
| 662 | } | ||
| 663 | |||
| 664 | ✗ | bytestream2_put_be16(pby, BitstreamMarker); | |
| 665 | ✗ | bytestream2_put_be16(pby, 0x1b4b); | |
| 666 | |||
| 667 | ✗ | for (int l = 0; l < 3; l++) { | |
| 668 | ✗ | for (int i = 0; i < 3; i++) { | |
| 669 | ✗ | 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]; | |
| 670 | } | ||
| 671 | } | ||
| 672 | |||
| 673 | ✗ | for (int l = 0; l < 3; l++) { | |
| 674 | ✗ | int a_width = s->plane[p].band[l][0].a_width; | |
| 675 | ✗ | int width = s->plane[p].band[l][0].width; | |
| 676 | ✗ | int stride = FFALIGN(width, 8); | |
| 677 | ✗ | int height = s->plane[p].band[l][0].height; | |
| 678 | |||
| 679 | ✗ | bytestream2_put_be16(pby, BitstreamMarker); | |
| 680 | ✗ | bytestream2_put_be16(pby, 0x0d0d); | |
| 681 | |||
| 682 | ✗ | bytestream2_put_be16(pby, WaveletType); | |
| 683 | ✗ | bytestream2_put_be16(pby, 3 + 2 * (l == 2)); | |
| 684 | |||
| 685 | ✗ | bytestream2_put_be16(pby, WaveletNumber); | |
| 686 | ✗ | bytestream2_put_be16(pby, 3 - l); | |
| 687 | |||
| 688 | ✗ | bytestream2_put_be16(pby, WaveletLevel); | |
| 689 | ✗ | bytestream2_put_be16(pby, 3 - l); | |
| 690 | |||
| 691 | ✗ | bytestream2_put_be16(pby, NumBands); | |
| 692 | ✗ | bytestream2_put_be16(pby, 4); | |
| 693 | |||
| 694 | ✗ | bytestream2_put_be16(pby, HighpassWidth); | |
| 695 | ✗ | bytestream2_put_be16(pby, width); | |
| 696 | |||
| 697 | ✗ | bytestream2_put_be16(pby, HighpassHeight); | |
| 698 | ✗ | bytestream2_put_be16(pby, height); | |
| 699 | |||
| 700 | ✗ | bytestream2_put_be16(pby, LowpassBorder); | |
| 701 | ✗ | bytestream2_put_be16(pby, 0); | |
| 702 | |||
| 703 | ✗ | bytestream2_put_be16(pby, HighpassBorder); | |
| 704 | ✗ | bytestream2_put_be16(pby, 0); | |
| 705 | |||
| 706 | ✗ | bytestream2_put_be16(pby, LowpassScale); | |
| 707 | ✗ | bytestream2_put_be16(pby, 1); | |
| 708 | |||
| 709 | ✗ | bytestream2_put_be16(pby, LowpassDivisor); | |
| 710 | ✗ | bytestream2_put_be16(pby, 1); | |
| 711 | |||
| 712 | ✗ | for (int i = 0; i < 3; i++) { | |
| 713 | ✗ | int16_t *data = s->plane[p].subband[1 + l * 3 + i]; | |
| 714 | ✗ | int count = 0, padd = 0; | |
| 715 | |||
| 716 | ✗ | bytestream2_put_be16(pby, BitstreamMarker); | |
| 717 | ✗ | bytestream2_put_be16(pby, 0x0e0e); | |
| 718 | |||
| 719 | ✗ | bytestream2_put_be16(pby, SubbandNumber); | |
| 720 | ✗ | bytestream2_put_be16(pby, i + 1); | |
| 721 | |||
| 722 | ✗ | bytestream2_put_be16(pby, BandCodingFlags); | |
| 723 | ✗ | bytestream2_put_be16(pby, 1); | |
| 724 | |||
| 725 | ✗ | bytestream2_put_be16(pby, BandWidth); | |
| 726 | ✗ | bytestream2_put_be16(pby, width); | |
| 727 | |||
| 728 | ✗ | bytestream2_put_be16(pby, BandHeight); | |
| 729 | ✗ | bytestream2_put_be16(pby, height); | |
| 730 | |||
| 731 | ✗ | bytestream2_put_be16(pby, SubbandBand); | |
| 732 | ✗ | bytestream2_put_be16(pby, 1 + l * 3 + i); | |
| 733 | |||
| 734 | ✗ | bytestream2_put_be16(pby, BandEncoding); | |
| 735 | ✗ | bytestream2_put_be16(pby, 3); | |
| 736 | |||
| 737 | ✗ | bytestream2_put_be16(pby, Quantization); | |
| 738 | ✗ | bytestream2_put_be16(pby, s->plane[p].quantization[1 + l * 3 + i]); | |
| 739 | |||
| 740 | ✗ | bytestream2_put_be16(pby, BandScale); | |
| 741 | ✗ | bytestream2_put_be16(pby, 1); | |
| 742 | |||
| 743 | ✗ | bytestream2_put_be16(pby, BandHeader); | |
| 744 | ✗ | bytestream2_put_be16(pby, 0); | |
| 745 | |||
| 746 | ✗ | quantize_band(data, width, a_width, height, | |
| 747 | ✗ | s->plane[p].quantization[1 + l * 3 + i]); | |
| 748 | |||
| 749 | ✗ | init_put_bits(pb, pkt->data + bytestream2_tell_p(pby), bytestream2_get_bytes_left_p(pby)); | |
| 750 | |||
| 751 | ✗ | for (int m = 0; m < height; m++) { | |
| 752 | ✗ | for (int j = 0; j < stride; j++) { | |
| 753 | ✗ | int16_t index = j >= width ? 0 : FFSIGN(data[j]) * lut[FFABS(data[j])]; | |
| 754 | |||
| 755 | ✗ | if (index < 0) | |
| 756 | ✗ | index += 512; | |
| 757 | ✗ | if (index == 0) { | |
| 758 | ✗ | count++; | |
| 759 | ✗ | continue; | |
| 760 | ✗ | } else if (count > 0) { | |
| 761 | ✗ | count = put_runcode(pb, count, rb); | |
| 762 | } | ||
| 763 | ✗ | put_bits(pb, cb[index].size, cb[index].bits); | |
| 764 | } | ||
| 765 | |||
| 766 | ✗ | data += a_width; | |
| 767 | } | ||
| 768 | |||
| 769 | ✗ | if (count > 0) { | |
| 770 | ✗ | count = put_runcode(pb, count, rb); | |
| 771 | } | ||
| 772 | |||
| 773 | ✗ | put_bits(pb, cb[512].size, cb[512].bits); | |
| 774 | |||
| 775 | ✗ | flush_put_bits(pb); | |
| 776 | ✗ | bytestream2_skip_p(pby, put_bytes_output(pb)); | |
| 777 | ✗ | padd = (4 - (bytestream2_tell_p(pby) & 3)) & 3; | |
| 778 | ✗ | while (padd--) | |
| 779 | ✗ | bytestream2_put_byte(pby, 0); | |
| 780 | |||
| 781 | ✗ | bytestream2_put_be16(pby, BandTrailer); | |
| 782 | ✗ | bytestream2_put_be16(pby, 0); | |
| 783 | } | ||
| 784 | |||
| 785 | ✗ | bytestream2_put_be16(pby, BitstreamMarker); | |
| 786 | ✗ | bytestream2_put_be16(pby, 0x0c0c); | |
| 787 | } | ||
| 788 | |||
| 789 | ✗ | s->plane[p].size = bytestream2_tell_p(pby) - pos; | |
| 790 | } | ||
| 791 | |||
| 792 | ✗ | bytestream2_put_be16(pby, GroupTrailer); | |
| 793 | ✗ | bytestream2_put_be16(pby, 0); | |
| 794 | |||
| 795 | ✗ | av_shrink_packet(pkt, bytestream2_tell_p(pby)); | |
| 796 | |||
| 797 | ✗ | pkt->flags |= AV_PKT_FLAG_KEY; | |
| 798 | |||
| 799 | ✗ | bytestream2_seek_p(pby, 8, SEEK_SET); | |
| 800 | ✗ | for (int i = 0; i < s->planes; i++) | |
| 801 | ✗ | bytestream2_put_be32(pby, s->plane[i].size); | |
| 802 | |||
| 803 | ✗ | *got_packet = 1; | |
| 804 | |||
| 805 | ✗ | return 0; | |
| 806 | } | ||
| 807 | |||
| 808 | ✗ | static av_cold int cfhd_encode_close(AVCodecContext *avctx) | |
| 809 | { | ||
| 810 | ✗ | CFHDEncContext *s = avctx->priv_data; | |
| 811 | |||
| 812 | ✗ | for (int i = 0; i < s->planes; i++) { | |
| 813 | ✗ | av_freep(&s->plane[i].dwt_buf); | |
| 814 | ✗ | av_freep(&s->plane[i].dwt_tmp); | |
| 815 | |||
| 816 | ✗ | for (int j = 0; j < SUBBAND_COUNT; j++) | |
| 817 | ✗ | s->plane[i].subband[j] = NULL; | |
| 818 | |||
| 819 | ✗ | for (int j = 0; j < 8; j++) | |
| 820 | ✗ | s->plane[i].l_h[j] = NULL; | |
| 821 | } | ||
| 822 | |||
| 823 | ✗ | av_freep(&s->alpha); | |
| 824 | |||
| 825 | ✗ | return 0; | |
| 826 | } | ||
| 827 | |||
| 828 | #define OFFSET(x) offsetof(CFHDEncContext, x) | ||
| 829 | #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM | ||
| 830 | static const AVOption options[] = { | ||
| 831 | { "quality", "set quality", OFFSET(quality), AV_OPT_TYPE_INT, {.i64= 0}, 0, 12, VE, .unit = "q" }, | ||
| 832 | { "film3+", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 0}, 0, 0, VE, .unit = "q" }, | ||
| 833 | { "film3", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 1}, 0, 0, VE, .unit = "q" }, | ||
| 834 | { "film2+", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 2}, 0, 0, VE, .unit = "q" }, | ||
| 835 | { "film2", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 3}, 0, 0, VE, .unit = "q" }, | ||
| 836 | { "film1.5", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 4}, 0, 0, VE, .unit = "q" }, | ||
| 837 | { "film1+", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 5}, 0, 0, VE, .unit = "q" }, | ||
| 838 | { "film1", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 6}, 0, 0, VE, .unit = "q" }, | ||
| 839 | { "high+", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 7}, 0, 0, VE, .unit = "q" }, | ||
| 840 | { "high", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 8}, 0, 0, VE, .unit = "q" }, | ||
| 841 | { "medium+", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 9}, 0, 0, VE, .unit = "q" }, | ||
| 842 | { "medium", NULL, 0, AV_OPT_TYPE_CONST, {.i64=10}, 0, 0, VE, .unit = "q" }, | ||
| 843 | { "low+", NULL, 0, AV_OPT_TYPE_CONST, {.i64=11}, 0, 0, VE, .unit = "q" }, | ||
| 844 | { "low", NULL, 0, AV_OPT_TYPE_CONST, {.i64=12}, 0, 0, VE, .unit = "q" }, | ||
| 845 | { NULL}, | ||
| 846 | }; | ||
| 847 | |||
| 848 | static const AVClass cfhd_class = { | ||
| 849 | .class_name = "cfhd", | ||
| 850 | .item_name = av_default_item_name, | ||
| 851 | .option = options, | ||
| 852 | .version = LIBAVUTIL_VERSION_INT, | ||
| 853 | }; | ||
| 854 | |||
| 855 | const FFCodec ff_cfhd_encoder = { | ||
| 856 | .p.name = "cfhd", | ||
| 857 | CODEC_LONG_NAME("GoPro CineForm HD"), | ||
| 858 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 859 | .p.id = AV_CODEC_ID_CFHD, | ||
| 860 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS | | ||
| 861 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
| 862 | .priv_data_size = sizeof(CFHDEncContext), | ||
| 863 | .p.priv_class = &cfhd_class, | ||
| 864 | .init = cfhd_encode_init, | ||
| 865 | .close = cfhd_encode_close, | ||
| 866 | FF_CODEC_ENCODE_CB(cfhd_encode_frame), | ||
| 867 | CODEC_PIXFMTS(AV_PIX_FMT_YUV422P10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12), | ||
| 868 | .color_ranges = AVCOL_RANGE_MPEG, | ||
| 869 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
| 870 | }; | ||
| 871 |