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.

268 lines
7.7KB

  1. /*
  2. * VP8 compatible video decoder
  3. *
  4. * Copyright (C) 2010 David Conrad
  5. * Copyright (C) 2010 Ronald S. Bultje
  6. * Copyright (C) 2010 Jason Garrett-Glaser
  7. * Copyright (C) 2012 Daniel Kang
  8. *
  9. * This file is part of Libav.
  10. *
  11. * Libav is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Libav is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Libav; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #ifndef AVCODEC_VP8_H
  26. #define AVCODEC_VP8_H
  27. #include "vp56.h"
  28. #include "vp56data.h"
  29. #include "vp8dsp.h"
  30. #include "h264pred.h"
  31. #define VP8_MAX_QUANT 127
  32. enum dct_token {
  33. DCT_0,
  34. DCT_1,
  35. DCT_2,
  36. DCT_3,
  37. DCT_4,
  38. DCT_CAT1,
  39. DCT_CAT2,
  40. DCT_CAT3,
  41. DCT_CAT4,
  42. DCT_CAT5,
  43. DCT_CAT6,
  44. DCT_EOB,
  45. NUM_DCT_TOKENS
  46. };
  47. // used to signal 4x4 intra pred in luma MBs
  48. #define MODE_I4x4 4
  49. enum inter_mvmode {
  50. VP8_MVMODE_ZERO = MODE_I4x4 + 1,
  51. VP8_MVMODE_MV,
  52. VP8_MVMODE_SPLIT
  53. };
  54. enum inter_splitmvmode {
  55. VP8_SPLITMVMODE_16x8 = 0, ///< 2 16x8 blocks (vertical)
  56. VP8_SPLITMVMODE_8x16, ///< 2 8x16 blocks (horizontal)
  57. VP8_SPLITMVMODE_8x8, ///< 2x2 blocks of 8x8px each
  58. VP8_SPLITMVMODE_4x4, ///< 4x4 blocks of 4x4px each
  59. VP8_SPLITMVMODE_NONE, ///< (only used in prediction) no split MVs
  60. };
  61. typedef struct {
  62. uint8_t filter_level;
  63. uint8_t inner_limit;
  64. uint8_t inner_filter;
  65. } VP8FilterStrength;
  66. typedef struct {
  67. uint8_t skip;
  68. // todo: make it possible to check for at least (i4x4 or split_mv)
  69. // in one op. are others needed?
  70. uint8_t mode;
  71. uint8_t ref_frame;
  72. uint8_t partitioning;
  73. uint8_t chroma_pred_mode;
  74. uint8_t segment;
  75. uint8_t intra4x4_pred_mode_mb[16];
  76. uint8_t intra4x4_pred_mode_top[4];
  77. VP56mv mv;
  78. VP56mv bmv[16];
  79. } VP8Macroblock;
  80. typedef struct {
  81. pthread_mutex_t lock;
  82. pthread_cond_t cond;
  83. int thread_nr;
  84. int thread_mb_pos; // (mb_y << 16) | (mb_x & 0xFFFF)
  85. int wait_mb_pos; // What the current thread is waiting on.
  86. uint8_t *edge_emu_buffer;
  87. /**
  88. * For coeff decode, we need to know whether the above block had non-zero
  89. * coefficients. This means for each macroblock, we need data for 4 luma
  90. * blocks, 2 u blocks, 2 v blocks, and the luma dc block, for a total of 9
  91. * per macroblock. We keep the last row in top_nnz.
  92. */
  93. DECLARE_ALIGNED(8, uint8_t, left_nnz)[9];
  94. /**
  95. * This is the index plus one of the last non-zero coeff
  96. * for each of the blocks in the current macroblock.
  97. * So, 0 -> no coeffs
  98. * 1 -> dc-only (special transform)
  99. * 2+-> full transform
  100. */
  101. DECLARE_ALIGNED(16, uint8_t, non_zero_count_cache)[6][4];
  102. DECLARE_ALIGNED(16, DCTELEM, block)[6][4][16];
  103. DECLARE_ALIGNED(16, DCTELEM, block_dc)[16];
  104. VP8FilterStrength *filter_strength;
  105. } VP8ThreadData;
  106. #define MAX_THREADS 8
  107. typedef struct {
  108. VP8ThreadData *thread_data;
  109. AVCodecContext *avctx;
  110. AVFrame *framep[4];
  111. AVFrame *next_framep[4];
  112. AVFrame *curframe;
  113. AVFrame *prev_frame;
  114. uint16_t mb_width; /* number of horizontal MB */
  115. uint16_t mb_height; /* number of vertical MB */
  116. int linesize;
  117. int uvlinesize;
  118. uint8_t keyframe;
  119. uint8_t deblock_filter;
  120. uint8_t mbskip_enabled;
  121. uint8_t profile;
  122. VP56mv mv_min;
  123. VP56mv mv_max;
  124. int8_t sign_bias[4]; ///< one state [0, 1] per ref frame type
  125. int ref_count[3];
  126. /**
  127. * Base parameters for segmentation, i.e. per-macroblock parameters.
  128. * These must be kept unchanged even if segmentation is not used for
  129. * a frame, since the values persist between interframes.
  130. */
  131. struct {
  132. uint8_t enabled;
  133. uint8_t absolute_vals;
  134. uint8_t update_map;
  135. int8_t base_quant[4];
  136. int8_t filter_level[4]; ///< base loop filter level
  137. } segmentation;
  138. struct {
  139. uint8_t simple;
  140. uint8_t level;
  141. uint8_t sharpness;
  142. } filter;
  143. VP8Macroblock *macroblocks;
  144. uint8_t *intra4x4_pred_mode_top;
  145. uint8_t intra4x4_pred_mode_left[4];
  146. /**
  147. * Macroblocks can have one of 4 different quants in a frame when
  148. * segmentation is enabled.
  149. * If segmentation is disabled, only the first segment's values are used.
  150. */
  151. struct {
  152. // [0] - DC qmul [1] - AC qmul
  153. int16_t luma_qmul[2];
  154. int16_t luma_dc_qmul[2]; ///< luma dc-only block quant
  155. int16_t chroma_qmul[2];
  156. } qmat[4];
  157. struct {
  158. uint8_t enabled; ///< whether each mb can have a different strength based on mode/ref
  159. /**
  160. * filter strength adjustment for the following macroblock modes:
  161. * [0-3] - i16x16 (always zero)
  162. * [4] - i4x4
  163. * [5] - zero mv
  164. * [6] - inter modes except for zero or split mv
  165. * [7] - split mv
  166. * i16x16 modes never have any adjustment
  167. */
  168. int8_t mode[VP8_MVMODE_SPLIT+1];
  169. /**
  170. * filter strength adjustment for macroblocks that reference:
  171. * [0] - intra / VP56_FRAME_CURRENT
  172. * [1] - VP56_FRAME_PREVIOUS
  173. * [2] - VP56_FRAME_GOLDEN
  174. * [3] - altref / VP56_FRAME_GOLDEN2
  175. */
  176. int8_t ref[4];
  177. } lf_delta;
  178. uint8_t (*top_border)[16+8+8];
  179. uint8_t (*top_nnz)[9];
  180. VP56RangeCoder c; ///< header context, includes mb modes and motion vectors
  181. /**
  182. * These are all of the updatable probabilities for binary decisions.
  183. * They are only implictly reset on keyframes, making it quite likely
  184. * for an interframe to desync if a prior frame's header was corrupt
  185. * or missing outright!
  186. */
  187. struct {
  188. uint8_t segmentid[3];
  189. uint8_t mbskip;
  190. uint8_t intra;
  191. uint8_t last;
  192. uint8_t golden;
  193. uint8_t pred16x16[4];
  194. uint8_t pred8x8c[3];
  195. uint8_t token[4][16][3][NUM_DCT_TOKENS-1];
  196. uint8_t mvc[2][19];
  197. } prob[2];
  198. VP8Macroblock *macroblocks_base;
  199. int invisible;
  200. int update_last; ///< update VP56_FRAME_PREVIOUS with the current one
  201. int update_golden; ///< VP56_FRAME_NONE if not updated, or which frame to copy if so
  202. int update_altref;
  203. /**
  204. * If this flag is not set, all the probability updates
  205. * are discarded after this frame is decoded.
  206. */
  207. int update_probabilities;
  208. /**
  209. * All coefficients are contained in separate arith coding contexts.
  210. * There can be 1, 2, 4, or 8 of these after the header context.
  211. */
  212. int num_coeff_partitions;
  213. VP56RangeCoder coeff_partition[8];
  214. DSPContext dsp;
  215. VP8DSPContext vp8dsp;
  216. H264PredContext hpc;
  217. vp8_mc_func put_pixels_tab[3][3][3];
  218. AVFrame frames[5];
  219. /**
  220. * A list of segmentation_map buffers that are to be free()'ed in
  221. * the next decoding iteration. We can't free() them right away
  222. * because the map may still be used by subsequent decoding threads.
  223. * Unused if frame threading is off.
  224. */
  225. uint8_t *segmentation_maps[5];
  226. int num_maps_to_be_freed;
  227. int maps_are_invalid;
  228. int num_jobs;
  229. /**
  230. * This describes the macroblock memory layout.
  231. * 0 -> Only width+height*2+1 macroblocks allocated (frame/single thread).
  232. * 1 -> Macroblocks for entire frame alloced (sliced thread).
  233. */
  234. int mb_layout;
  235. } VP8Context;
  236. #endif /* AVCODEC_VP8_H */