You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
5.1KB

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