ffv1.h 4.9 KB

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