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/imgutils.h" 32#include "avcodec.h" 33#include "get_bits.h" 34#include "mathops.h" 35#include "put_bits.h" 36#include "rangecoder.h" 37#include "threadframe.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_CONTEXT_INPUTS 5 49 50#define AC_GOLOMB_RICE 0 51#define AC_RANGE_DEFAULT_TAB 1 52#define AC_RANGE_CUSTOM_TAB 2 53#define AC_RANGE_DEFAULT_TAB_FORCE -2 54 55typedef struct VlcState { 56 int16_t drift; 57 uint16_t error_sum; 58 int8_t bias; 59 uint8_t count; 60} VlcState; 61 62typedef struct PlaneContext { 63 int16_t quant_table[MAX_CONTEXT_INPUTS][256]; 64 int quant_table_index; 65 int context_count; 66 uint8_t (*state)[CONTEXT_SIZE]; 67 VlcState *vlc_state; 68 uint8_t interlace_bit_state[2]; 69} PlaneContext; 70 71#define MAX_SLICES 1024 72 73typedef struct FFV1Context { 74 AVClass *class; 75 AVCodecContext *avctx; 76 RangeCoder c; 77 GetBitContext gb; 78 PutBitContext pb; 79 uint64_t rc_stat[256][2]; 80 uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2]; 81 int version; 82 int micro_version; 83 int width, height; 84 int chroma_planes; 85 int chroma_h_shift, chroma_v_shift; 86 int transparency; 87 int flags; 88 int picture_number; 89 int key_frame; 90 ThreadFrame picture, last_picture; 91 struct FFV1Context *fsrc; 92 93 AVFrame *cur; 94 int plane_count; 95 int ac; ///< 1=range coder <-> 0=golomb rice 96 int ac_byte_count; ///< number of bytes used for AC coding 97 PlaneContext plane[MAX_PLANES]; 98 int16_t quant_table[MAX_CONTEXT_INPUTS][256]; 99 int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][256]; 100 int context_count[MAX_QUANT_TABLES]; 101 uint8_t state_transition[256]; 102 uint8_t (*initial_states[MAX_QUANT_TABLES])[32]; 103 int run_index; 104 int colorspace; 105 int16_t *sample_buffer; 106 int32_t *sample_buffer32; 107 108 int use32bit; 109 110 int ec; 111 int intra; 112 int slice_damaged; 113 int key_frame_ok; 114 int context_model; 115 116 int bits_per_raw_sample; 117 int packed_at_lsb; 118 119 int gob_count; 120 int quant_table_count; 121 122 struct FFV1Context *slice_context[MAX_SLICES]; 123 int slice_count; 124 int max_slice_count; 125 int num_v_slices; 126 int num_h_slices; 127 int slice_width; 128 int slice_height; 129 int slice_x; 130 int slice_y; 131 int slice_reset_contexts; 132 int slice_coding_mode; 133 int slice_rct_by_coef; 134 int slice_rct_ry_coef; 135} FFV1Context; 136 137int ff_ffv1_common_init(AVCodecContext *avctx); 138int ff_ffv1_init_slice_state(const FFV1Context *f, FFV1Context *fs); 139int ff_ffv1_init_slices_state(FFV1Context *f); 140int ff_ffv1_init_slice_contexts(FFV1Context *f); 141int ff_ffv1_allocate_initial_states(FFV1Context *f); 142void ff_ffv1_clear_slice_state(const FFV1Context *f, FFV1Context *fs); 143int ff_ffv1_close(AVCodecContext *avctx); 144 145static av_always_inline int fold(int diff, int bits) 146{ 147 if (bits == 8) 148 diff = (int8_t)diff; 149 else { 150 diff = sign_extend(diff, bits); 151 } 152 153 return diff; 154} 155 156static inline void update_vlc_state(VlcState *const state, const int v) 157{ 158 int drift = state->drift; 159 int count = state->count; 160 state->error_sum += FFABS(v); 161 drift += v; 162 163 if (count == 128) { // FIXME: variable 164 count >>= 1; 165 drift >>= 1; 166 state->error_sum >>= 1; 167 } 168 count++; 169 170 if (drift <= -count) { 171 state->bias = FFMAX(state->bias - 1, -128); 172 173 drift = FFMAX(drift + count, -count + 1); 174 } else if (drift > 0) { 175 state->bias = FFMIN(state->bias + 1, 127); 176 177 drift = FFMIN(drift - count, 0); 178 } 179 180 state->drift = drift; 181 state->count = count; 182} 183 184#define TYPE int16_t 185#define RENAME(name) name 186#include "ffv1_template.c" 187#undef TYPE 188#undef RENAME 189 190#define TYPE int32_t 191#define RENAME(name) name ## 32 192#include "ffv1_template.c" 193#undef TYPE 194#undef RENAME 195 196#endif /* AVCODEC_FFV1_H */ 197