| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * FFV1 codec for libavcodec | ||
| 3 | * | ||
| 4 | * Copyright (c) 2003-2012 Michael Niedermayer <michaelni@gmx.at> | ||
| 5 | * | ||
| 6 | * This file is part of FFmpeg. | ||
| 7 | * | ||
| 8 | * FFmpeg is free software; you can redistribute it and/or | ||
| 9 | * modify it under the terms of the GNU Lesser General Public | ||
| 10 | * License as published by the Free Software Foundation; either | ||
| 11 | * version 2.1 of the License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 16 | * Lesser General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public | ||
| 19 | * License along with FFmpeg; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | #ifndef AVCODEC_FFV1_H | ||
| 24 | #define AVCODEC_FFV1_H | ||
| 25 | |||
| 26 | /** | ||
| 27 | * @file | ||
| 28 | * FF Video Codec 1 (a lossless codec) | ||
| 29 | */ | ||
| 30 | |||
| 31 | #include "libavutil/attributes.h" | ||
| 32 | #include "avcodec.h" | ||
| 33 | #include "get_bits.h" | ||
| 34 | #include "mathops.h" | ||
| 35 | #include "progressframe.h" | ||
| 36 | #include "put_bits.h" | ||
| 37 | #include "rangecoder.h" | ||
| 38 | |||
| 39 | #ifdef __INTEL_COMPILER | ||
| 40 | #undef av_flatten | ||
| 41 | #define av_flatten | ||
| 42 | #endif | ||
| 43 | |||
| 44 | #define MAX_PLANES 4 | ||
| 45 | #define CONTEXT_SIZE 32 | ||
| 46 | |||
| 47 | #define MAX_QUANT_TABLES 8 | ||
| 48 | #define MAX_QUANT_TABLE_SIZE 256 | ||
| 49 | #define MAX_QUANT_TABLE_MASK (MAX_QUANT_TABLE_SIZE - 1) | ||
| 50 | #define MAX_CONTEXT_INPUTS 5 | ||
| 51 | |||
| 52 | #define AC_GOLOMB_RICE 0 | ||
| 53 | #define AC_RANGE_DEFAULT_TAB 1 | ||
| 54 | #define AC_RANGE_CUSTOM_TAB 2 | ||
| 55 | #define AC_RANGE_DEFAULT_TAB_FORCE -2 | ||
| 56 | |||
| 57 | typedef struct VlcState { | ||
| 58 | uint32_t error_sum; | ||
| 59 | int16_t drift; | ||
| 60 | int8_t bias; | ||
| 61 | uint8_t count; | ||
| 62 | } VlcState; | ||
| 63 | |||
| 64 | typedef struct PlaneContext { | ||
| 65 | int quant_table_index; | ||
| 66 | int context_count; | ||
| 67 | uint8_t (*state)[CONTEXT_SIZE]; | ||
| 68 | VlcState *vlc_state; | ||
| 69 | } PlaneContext; | ||
| 70 | |||
| 71 | #define MAX_SLICES 1024 | ||
| 72 | |||
| 73 | typedef struct FFV1SliceContext { | ||
| 74 | int16_t *sample_buffer; | ||
| 75 | int32_t *sample_buffer32; | ||
| 76 | |||
| 77 | int slice_width; | ||
| 78 | int slice_height; | ||
| 79 | int slice_x; | ||
| 80 | int slice_y; | ||
| 81 | int sx, sy; | ||
| 82 | |||
| 83 | int run_index; | ||
| 84 | int slice_coding_mode; | ||
| 85 | int slice_rct_by_coef; | ||
| 86 | int slice_rct_ry_coef; | ||
| 87 | int remap; | ||
| 88 | |||
| 89 | // RefStruct reference, array of MAX_PLANES elements | ||
| 90 | PlaneContext *plane; | ||
| 91 | PutBitContext pb; | ||
| 92 | RangeCoder c; | ||
| 93 | |||
| 94 | int ac_byte_count; ///< number of bytes used for AC coding | ||
| 95 | |||
| 96 | union { | ||
| 97 | // decoder-only | ||
| 98 | struct { | ||
| 99 | int slice_reset_contexts; | ||
| 100 | int slice_damaged; | ||
| 101 | }; | ||
| 102 | |||
| 103 | // encoder-only | ||
| 104 | struct { | ||
| 105 | uint64_t rc_stat[256][2]; | ||
| 106 | uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2]; | ||
| 107 | }; | ||
| 108 | }; | ||
| 109 | int remap_count[4]; | ||
| 110 | |||
| 111 | uint32_t *bitmap [4]; //float encode | ||
| 112 | uint16_t *fltmap [4]; //halffloat encode & decode | ||
| 113 | uint32_t *fltmap32[4]; //float decode | ||
| 114 | unsigned int fltmap_size[4]; | ||
| 115 | unsigned int fltmap32_size[4]; | ||
| 116 | struct Unit { | ||
| 117 | uint32_t val; //this is unneeded if you accept a dereference on each access | ||
| 118 | uint32_t ndx; | ||
| 119 | } *unit[4]; | ||
| 120 | } FFV1SliceContext; | ||
| 121 | |||
| 122 | typedef struct FFV1Context { | ||
| 123 | AVClass *class; | ||
| 124 | AVCodecContext *avctx; | ||
| 125 | uint64_t rc_stat[256][2]; | ||
| 126 | uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2]; | ||
| 127 | int version; | ||
| 128 | int micro_version; | ||
| 129 | int combined_version; | ||
| 130 | int width, height; | ||
| 131 | int chroma_planes; | ||
| 132 | int chroma_h_shift, chroma_v_shift; | ||
| 133 | int transparency; | ||
| 134 | int flags; | ||
| 135 | int64_t picture_number; | ||
| 136 | int key_frame; | ||
| 137 | ProgressFrame picture, last_picture; | ||
| 138 | void *hwaccel_picture_private, *hwaccel_last_picture_private; | ||
| 139 | uint32_t crcref; | ||
| 140 | enum AVPixelFormat pix_fmt; | ||
| 141 | enum AVPixelFormat configured_pix_fmt; | ||
| 142 | int configured_width, configured_height; | ||
| 143 | int configured_ac; | ||
| 144 | |||
| 145 | const AVFrame *cur_enc_frame; | ||
| 146 | int plane_count; | ||
| 147 | int ac; ///< 1=range coder <-> 0=golomb rice | ||
| 148 | int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][MAX_QUANT_TABLE_SIZE]; | ||
| 149 | int context_count[MAX_QUANT_TABLES]; | ||
| 150 | uint8_t state_transition[256]; | ||
| 151 | uint8_t (*initial_states[MAX_QUANT_TABLES])[32]; | ||
| 152 | int colorspace; | ||
| 153 | int flt; | ||
| 154 | int remap_mode; | ||
| 155 | int remap_optimizer; | ||
| 156 | int maxsize_warned; | ||
| 157 | |||
| 158 | int use32bit; | ||
| 159 | |||
| 160 | int ec; | ||
| 161 | int intra; | ||
| 162 | int key_frame_ok; | ||
| 163 | int context_model; | ||
| 164 | int qtable; | ||
| 165 | |||
| 166 | int bits_per_raw_sample; | ||
| 167 | int packed_at_lsb; | ||
| 168 | |||
| 169 | int gob_count; | ||
| 170 | int quant_table_count; | ||
| 171 | |||
| 172 | int slice_count; | ||
| 173 | int max_slice_count; | ||
| 174 | int num_v_slices; | ||
| 175 | int num_h_slices; | ||
| 176 | |||
| 177 | FFV1SliceContext *slices; | ||
| 178 | /* RefStruct object, per-slice damage flags shared between frame threads. | ||
| 179 | * | ||
| 180 | * After a frame thread marks some slice as finished with | ||
| 181 | * ff_progress_frame_report(), the corresponding array element must not be | ||
| 182 | * accessed by this thread anymore, as from then on it is owned by the next | ||
| 183 | * thread. | ||
| 184 | */ | ||
| 185 | uint8_t *slice_damaged; | ||
| 186 | /* Frame damage flag, used to delay announcing progress, since ER is | ||
| 187 | * applied after all the slices are decoded. | ||
| 188 | * NOT shared between frame threads. | ||
| 189 | */ | ||
| 190 | uint8_t frame_damaged; | ||
| 191 | } FFV1Context; | ||
| 192 | |||
| 193 | int ff_ffv1_common_init(AVCodecContext *avctx, FFV1Context *s); | ||
| 194 | int ff_ffv1_init_slice_state(const FFV1Context *f, FFV1SliceContext *sc); | ||
| 195 | int ff_ffv1_init_slices_state(FFV1Context *f); | ||
| 196 | int ff_ffv1_init_slice_contexts(FFV1Context *f); | ||
| 197 | PlaneContext *ff_ffv1_planes_alloc(void); | ||
| 198 | int ff_ffv1_allocate_initial_states(FFV1Context *f); | ||
| 199 | void ff_ffv1_clear_slice_state(const FFV1Context *f, FFV1SliceContext *sc); | ||
| 200 | void ff_ffv1_close(FFV1Context *s); | ||
| 201 | int ff_need_new_slices(int width, int num_h_slices, int chroma_shift); | ||
| 202 | int ff_ffv1_parse_header(FFV1Context *f, RangeCoder *c, uint8_t *state); | ||
| 203 | int ff_ffv1_read_extra_header(FFV1Context *f); | ||
| 204 | int ff_ffv1_read_quant_tables(RangeCoder *c, | ||
| 205 | int16_t quant_table[MAX_CONTEXT_INPUTS][256]); | ||
| 206 | void ff_ffv1_compute_bits_per_plane(const FFV1Context *f, FFV1SliceContext *sc, int bits[4], int *offset, int mask[4], int bits_per_raw_sample); | ||
| 207 | int ff_ffv1_get_symbol(RangeCoder *c, uint8_t *state, int is_signed); | ||
| 208 | |||
| 209 | /** | ||
| 210 | * This is intended for both width and height | ||
| 211 | */ | ||
| 212 | int ff_slice_coord(const FFV1Context *f, int width, int sx, int num_h_slices, int chroma_shift); | ||
| 213 | |||
| 214 | 733670082 | static av_always_inline int fold(int diff, int bits) | |
| 215 | { | ||
| 216 |
2/2✓ Branch 0 taken 408253256 times.
✓ Branch 1 taken 325416826 times.
|
733670082 | if (bits == 8) |
| 217 | 408253256 | diff = (int8_t)diff; | |
| 218 | else { | ||
| 219 | 325416826 | diff = sign_extend(diff, bits); | |
| 220 | } | ||
| 221 | |||
| 222 | 733670082 | return diff; | |
| 223 | } | ||
| 224 | |||
| 225 | 327319982 | static inline void update_vlc_state(VlcState *const state, const int v) | |
| 226 | { | ||
| 227 | 327319982 | int drift = state->drift; | |
| 228 | 327319982 | int count = state->count; | |
| 229 | 327319982 | state->error_sum += FFABS(v); | |
| 230 | 327319982 | drift += v; | |
| 231 | |||
| 232 |
2/2✓ Branch 0 taken 4485770 times.
✓ Branch 1 taken 322834212 times.
|
327319982 | if (count == 128) { // FIXME: variable |
| 233 | 4485770 | count >>= 1; | |
| 234 | 4485770 | drift >>= 1; | |
| 235 | 4485770 | state->error_sum >>= 1; | |
| 236 | } | ||
| 237 | 327319982 | count++; | |
| 238 | |||
| 239 |
2/2✓ Branch 0 taken 11300080 times.
✓ Branch 1 taken 316019902 times.
|
327319982 | if (drift <= -count) { |
| 240 |
2/2✓ Branch 0 taken 11294414 times.
✓ Branch 1 taken 5666 times.
|
11300080 | state->bias = FFMAX(state->bias - 1, -128); |
| 241 | |||
| 242 | 11300080 | drift = FFMAX(drift + count, -count + 1); | |
| 243 |
2/2✓ Branch 0 taken 10297046 times.
✓ Branch 1 taken 305722856 times.
|
316019902 | } else if (drift > 0) { |
| 244 |
2/2✓ Branch 0 taken 10272954 times.
✓ Branch 1 taken 24092 times.
|
10297046 | state->bias = FFMIN(state->bias + 1, 127); |
| 245 | |||
| 246 | 10297046 | drift = FFMIN(drift - count, 0); | |
| 247 | } | ||
| 248 | |||
| 249 | 327319982 | state->drift = drift; | |
| 250 | 327319982 | state->count = count; | |
| 251 | 327319982 | } | |
| 252 | |||
| 253 | |||
| 254 | 180482384 | static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state, | |
| 255 | int is_signed) | ||
| 256 | { | ||
| 257 |
2/2✓ Branch 1 taken 70640466 times.
✓ Branch 2 taken 109841918 times.
|
180482384 | if (get_rac(c, state + 0)) |
| 258 | 70640466 | return 0; | |
| 259 | else { | ||
| 260 | int e; | ||
| 261 | unsigned a; | ||
| 262 | 109841918 | e = 0; | |
| 263 |
4/4✓ Branch 0 taken 666366859 times.
✓ Branch 1 taken 45110687 times.
✓ Branch 3 taken 601635628 times.
✓ Branch 4 taken 109841918 times.
|
711477546 | while (get_rac(c, state + 1 + FFMIN(e, 9))) { // 1..10 |
| 264 | 601635628 | e++; | |
| 265 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 601635628 times.
|
601635628 | if (e > 31) |
| 266 | ✗ | return AVERROR_INVALIDDATA; | |
| 267 | } | ||
| 268 | |||
| 269 | 109841918 | a = 1; | |
| 270 |
2/2✓ Branch 0 taken 601635628 times.
✓ Branch 1 taken 109841918 times.
|
711477546 | for (int i = e - 1; i >= 0; i--) |
| 271 |
2/2✓ Branch 0 taken 581487119 times.
✓ Branch 1 taken 20148509 times.
|
601635628 | a += a + get_rac(c, state + 22 + FFMIN(i, 9)); // 22..31 |
| 272 | |||
| 273 |
6/6✓ Branch 0 taken 109812332 times.
✓ Branch 1 taken 29586 times.
✓ Branch 2 taken 98610141 times.
✓ Branch 3 taken 11202191 times.
✓ Branch 5 taken 60180373 times.
✓ Branch 6 taken 49631959 times.
|
109841918 | e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21 |
| 274 | 109841918 | return (a ^ e) - e; | |
| 275 | } | ||
| 276 | } | ||
| 277 | |||
| 278 | #endif /* AVCODEC_FFV1_H */ | ||
| 279 |