snow.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
  3. * Copyright (C) 2006 Robert Edele <yartrebo@earthlink.net>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #ifndef AVCODEC_SNOW_H
  22. #define AVCODEC_SNOW_H
  23. #include "libavutil/motion_vector.h"
  24. #include "avcodec.h"
  25. #include "hpeldsp.h"
  26. #include "snow_dwt.h"
  27. #include "rangecoder.h"
  28. #include "mathops.h"
  29. #include "h264qpel.h"
  30. #include "videodsp.h"
  31. #define SNOW_MAX_PLANES 4
  32. #define MID_STATE 128
  33. #define MAX_PLANES 4
  34. #define QSHIFT 5
  35. #define QROOT (1<<QSHIFT)
  36. #define LOSSLESS_QLOG -128
  37. #define FRAC_BITS 4
  38. #define MAX_REF_FRAMES 8
  39. #define LOG2_OBMC_MAX 8
  40. #define OBMC_MAX (1<<(LOG2_OBMC_MAX))
  41. typedef struct BlockNode{
  42. int16_t mx; ///< Motion vector component X, see mv_scale
  43. int16_t my; ///< Motion vector component Y, see mv_scale
  44. uint8_t ref; ///< Reference frame index
  45. uint8_t color[3]; ///< Color for intra
  46. uint8_t type; ///< Bitfield of BLOCK_*
  47. //#define TYPE_SPLIT 1
  48. #define BLOCK_INTRA 1 ///< Intra block, inter otherwise
  49. #define BLOCK_OPT 2 ///< Block needs no checks in this round of iterative motion estiation
  50. //#define TYPE_NOCOLOR 4
  51. uint8_t level; //FIXME merge into type?
  52. }BlockNode;
  53. static const BlockNode null_block= { //FIXME add border maybe
  54. .color= {128,128,128},
  55. .mx= 0,
  56. .my= 0,
  57. .ref= 0,
  58. .type= 0,
  59. .level= 0,
  60. };
  61. #define LOG2_MB_SIZE 4
  62. #define MB_SIZE (1<<LOG2_MB_SIZE)
  63. #define ENCODER_EXTRA_BITS 4
  64. #define HTAPS_MAX 8
  65. typedef struct x_and_coeff{
  66. int16_t x;
  67. uint16_t coeff;
  68. } x_and_coeff;
  69. typedef struct SubBand{
  70. int level;
  71. int stride;
  72. int width;
  73. int height;
  74. int qlog; ///< log(qscale)/log[2^(1/6)]
  75. DWTELEM *buf;
  76. IDWTELEM *ibuf;
  77. int buf_x_offset;
  78. int buf_y_offset;
  79. int stride_line; ///< Stride measured in lines, not pixels.
  80. x_and_coeff * x_coeff;
  81. struct SubBand *parent;
  82. uint8_t state[/*7*2*/ 7 + 512][32];
  83. }SubBand;
  84. typedef struct Plane{
  85. int width;
  86. int height;
  87. SubBand band[MAX_DECOMPOSITIONS][4];
  88. int htaps;
  89. int8_t hcoeff[HTAPS_MAX/2];
  90. int diag_mc;
  91. int fast_mc;
  92. int last_htaps;
  93. int8_t last_hcoeff[HTAPS_MAX/2];
  94. int last_diag_mc;
  95. }Plane;
  96. typedef struct SnowContext{
  97. AVClass *class;
  98. AVCodecContext *avctx;
  99. RangeCoder c;
  100. HpelDSPContext hdsp;
  101. VideoDSPContext vdsp;
  102. H264QpelContext h264qpel;
  103. SnowDWTContext dwt;
  104. AVFrame *input_picture; ///< new_picture with the internal linesizes
  105. AVFrame *current_picture;
  106. AVFrame *last_picture[MAX_REF_FRAMES];
  107. AVFrame *mconly_picture;
  108. // uint8_t q_context[16];
  109. uint8_t header_state[32];
  110. uint8_t block_state[128 + 32*128];
  111. int keyframe;
  112. int always_reset;
  113. int version;
  114. int spatial_decomposition_type;
  115. int last_spatial_decomposition_type;
  116. int temporal_decomposition_type;
  117. int spatial_decomposition_count;
  118. int last_spatial_decomposition_count;
  119. int temporal_decomposition_count;
  120. int max_ref_frames;
  121. int ref_frames;
  122. int16_t (*ref_mvs[MAX_REF_FRAMES])[2];
  123. uint32_t *ref_scores[MAX_REF_FRAMES];
  124. DWTELEM *spatial_dwt_buffer;
  125. DWTELEM *temp_dwt_buffer;
  126. IDWTELEM *spatial_idwt_buffer;
  127. IDWTELEM *temp_idwt_buffer;
  128. int *run_buffer;
  129. int colorspace_type;
  130. int chroma_h_shift;
  131. int chroma_v_shift;
  132. int spatial_scalability;
  133. int qlog;
  134. int last_qlog;
  135. int mv_scale;
  136. int last_mv_scale;
  137. int qbias;
  138. int last_qbias;
  139. #define QBIAS_SHIFT 3
  140. int b_width;
  141. int b_height;
  142. int block_max_depth;
  143. int last_block_max_depth;
  144. int nb_planes;
  145. Plane plane[MAX_PLANES];
  146. BlockNode *block;
  147. slice_buffer sb;
  148. uint8_t *scratchbuf;
  149. uint8_t *emu_edge_buffer;
  150. AVMotionVector *avmv;
  151. unsigned avmv_size;
  152. int avmv_index;
  153. }SnowContext;
  154. /* Tables */
  155. extern const uint8_t * const ff_obmc_tab[4];
  156. extern const uint8_t ff_qexp[QROOT];
  157. extern int ff_scale_mv_ref[MAX_REF_FRAMES][MAX_REF_FRAMES];
  158. /* common code */
  159. int ff_snow_common_init(AVCodecContext *avctx);
  160. int ff_snow_common_init_after_header(AVCodecContext *avctx);
  161. void ff_snow_common_end(SnowContext *s);
  162. void ff_snow_reset_contexts(SnowContext *s);
  163. int ff_snow_alloc_blocks(SnowContext *s);
  164. int ff_snow_frames_prepare(SnowContext *s);
  165. void ff_snow_pred_block(SnowContext *s, uint8_t *dst, uint8_t *tmp, ptrdiff_t stride,
  166. int sx, int sy, int b_w, int b_h, const BlockNode *block,
  167. int plane_index, int w, int h);
  168. /* common inline functions */
  169. //XXX doublecheck all of them should stay inlined
  170. static inline void pred_mv(SnowContext *s, int *mx, int *my, int ref,
  171. const BlockNode *left, const BlockNode *top, const BlockNode *tr){
  172. if(s->ref_frames == 1){
  173. *mx = mid_pred(left->mx, top->mx, tr->mx);
  174. *my = mid_pred(left->my, top->my, tr->my);
  175. }else{
  176. const int *scale = ff_scale_mv_ref[ref];
  177. *mx = mid_pred((left->mx * scale[left->ref] + 128) >>8,
  178. (top ->mx * scale[top ->ref] + 128) >>8,
  179. (tr ->mx * scale[tr ->ref] + 128) >>8);
  180. *my = mid_pred((left->my * scale[left->ref] + 128) >>8,
  181. (top ->my * scale[top ->ref] + 128) >>8,
  182. (tr ->my * scale[tr ->ref] + 128) >>8);
  183. }
  184. }
  185. static av_always_inline int same_block(BlockNode *a, BlockNode *b){
  186. if((a->type&BLOCK_INTRA) && (b->type&BLOCK_INTRA)){
  187. return !((a->color[0] - b->color[0]) | (a->color[1] - b->color[1]) | (a->color[2] - b->color[2]));
  188. }else{
  189. return !((a->mx - b->mx) | (a->my - b->my) | (a->ref - b->ref) | ((a->type ^ b->type)&BLOCK_INTRA));
  190. }
  191. }
  192. //FIXME name cleanup (b_w, block_w, b_width stuff)
  193. //XXX should we really inline it?
  194. static av_always_inline void add_yblock(SnowContext *s, int sliced, slice_buffer *sb, IDWTELEM *dst, uint8_t *dst8, const uint8_t *obmc, int src_x, int src_y, int b_w, int b_h, int w, int h, int dst_stride, int src_stride, int obmc_stride, int b_x, int b_y, int add, int offset_dst, int plane_index){
  195. const int b_width = s->b_width << s->block_max_depth;
  196. const int b_height= s->b_height << s->block_max_depth;
  197. const int b_stride= b_width;
  198. BlockNode *lt= &s->block[b_x + b_y*b_stride];
  199. BlockNode *rt= lt+1;
  200. BlockNode *lb= lt+b_stride;
  201. BlockNode *rb= lb+1;
  202. uint8_t *block[4];
  203. // When src_stride is large enough, it is possible to interleave the blocks.
  204. // Otherwise the blocks are written sequentially in the tmp buffer.
  205. int tmp_step= src_stride >= 7*MB_SIZE ? MB_SIZE : MB_SIZE*src_stride;
  206. uint8_t *tmp = s->scratchbuf;
  207. uint8_t *ptmp;
  208. int x,y;
  209. if(b_x<0){
  210. lt= rt;
  211. lb= rb;
  212. }else if(b_x + 1 >= b_width){
  213. rt= lt;
  214. rb= lb;
  215. }
  216. if(b_y<0){
  217. lt= lb;
  218. rt= rb;
  219. }else if(b_y + 1 >= b_height){
  220. lb= lt;
  221. rb= rt;
  222. }
  223. if(src_x<0){ //FIXME merge with prev & always round internal width up to *16
  224. obmc -= src_x;
  225. b_w += src_x;
  226. if(!sliced && !offset_dst)
  227. dst -= src_x;
  228. src_x=0;
  229. }
  230. if(src_x + b_w > w){
  231. b_w = w - src_x;
  232. }
  233. if(src_y<0){
  234. obmc -= src_y*obmc_stride;
  235. b_h += src_y;
  236. if(!sliced && !offset_dst)
  237. dst -= src_y*dst_stride;
  238. src_y=0;
  239. }
  240. if(src_y + b_h> h){
  241. b_h = h - src_y;
  242. }
  243. if(b_w<=0 || b_h<=0) return;
  244. if(!sliced && offset_dst)
  245. dst += src_x + src_y*dst_stride;
  246. dst8+= src_x + src_y*src_stride;
  247. // src += src_x + src_y*src_stride;
  248. ptmp= tmp + 3*tmp_step;
  249. block[0]= ptmp;
  250. ptmp+=tmp_step;
  251. ff_snow_pred_block(s, block[0], tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h);
  252. if(same_block(lt, rt)){
  253. block[1]= block[0];
  254. }else{
  255. block[1]= ptmp;
  256. ptmp+=tmp_step;
  257. ff_snow_pred_block(s, block[1], tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h);
  258. }
  259. if(same_block(lt, lb)){
  260. block[2]= block[0];
  261. }else if(same_block(rt, lb)){
  262. block[2]= block[1];
  263. }else{
  264. block[2]= ptmp;
  265. ptmp+=tmp_step;
  266. ff_snow_pred_block(s, block[2], tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h);
  267. }
  268. if(same_block(lt, rb) ){
  269. block[3]= block[0];
  270. }else if(same_block(rt, rb)){
  271. block[3]= block[1];
  272. }else if(same_block(lb, rb)){
  273. block[3]= block[2];
  274. }else{
  275. block[3]= ptmp;
  276. ff_snow_pred_block(s, block[3], tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h);
  277. }
  278. if(sliced){
  279. s->dwt.inner_add_yblock(obmc, obmc_stride, block, b_w, b_h, src_x,src_y, src_stride, sb, add, dst8);
  280. }else{
  281. for(y=0; y<b_h; y++){
  282. //FIXME ugly misuse of obmc_stride
  283. const uint8_t *obmc1= obmc + y*obmc_stride;
  284. const uint8_t *obmc2= obmc1+ (obmc_stride>>1);
  285. const uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
  286. const uint8_t *obmc4= obmc3+ (obmc_stride>>1);
  287. for(x=0; x<b_w; x++){
  288. int v= obmc1[x] * block[3][x + y*src_stride]
  289. +obmc2[x] * block[2][x + y*src_stride]
  290. +obmc3[x] * block[1][x + y*src_stride]
  291. +obmc4[x] * block[0][x + y*src_stride];
  292. v <<= 8 - LOG2_OBMC_MAX;
  293. if(FRAC_BITS != 8){
  294. v >>= 8 - FRAC_BITS;
  295. }
  296. if(add){
  297. v += dst[x + y*dst_stride];
  298. v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
  299. if(v&(~255)) v= ~(v>>31);
  300. dst8[x + y*src_stride] = v;
  301. }else{
  302. dst[x + y*dst_stride] -= v;
  303. }
  304. }
  305. }
  306. }
  307. }
  308. static av_always_inline void predict_slice(SnowContext *s, IDWTELEM *buf, int plane_index, int add, int mb_y){
  309. Plane *p= &s->plane[plane_index];
  310. const int mb_w= s->b_width << s->block_max_depth;
  311. const int mb_h= s->b_height << s->block_max_depth;
  312. int x, y, mb_x;
  313. int block_size = MB_SIZE >> s->block_max_depth;
  314. int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
  315. int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
  316. const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];
  317. const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
  318. int ref_stride= s->current_picture->linesize[plane_index];
  319. uint8_t *dst8= s->current_picture->data[plane_index];
  320. int w= p->width;
  321. int h= p->height;
  322. av_assert2(s->chroma_h_shift == s->chroma_v_shift); // obmc params assume squares
  323. if(s->keyframe || (s->avctx->debug&512)){
  324. if(mb_y==mb_h)
  325. return;
  326. if(add){
  327. for(y=block_h*mb_y; y<FFMIN(h,block_h*(mb_y+1)); y++){
  328. for(x=0; x<w; x++){
  329. int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  330. v >>= FRAC_BITS;
  331. if(v&(~255)) v= ~(v>>31);
  332. dst8[x + y*ref_stride]= v;
  333. }
  334. }
  335. }else{
  336. for(y=block_h*mb_y; y<FFMIN(h,block_h*(mb_y+1)); y++){
  337. for(x=0; x<w; x++){
  338. buf[x + y*w]-= 128<<FRAC_BITS;
  339. }
  340. }
  341. }
  342. return;
  343. }
  344. for(mb_x=0; mb_x<=mb_w; mb_x++){
  345. add_yblock(s, 0, NULL, buf, dst8, obmc,
  346. block_w*mb_x - block_w/2,
  347. block_h*mb_y - block_h/2,
  348. block_w, block_h,
  349. w, h,
  350. w, ref_stride, obmc_stride,
  351. mb_x - 1, mb_y - 1,
  352. add, 1, plane_index);
  353. }
  354. }
  355. static av_always_inline void predict_plane(SnowContext *s, IDWTELEM *buf, int plane_index, int add){
  356. const int mb_h= s->b_height << s->block_max_depth;
  357. int mb_y;
  358. for(mb_y=0; mb_y<=mb_h; mb_y++)
  359. predict_slice(s, buf, plane_index, add, mb_y);
  360. }
  361. static inline void set_blocks(SnowContext *s, int level, int x, int y, int l, int cb, int cr, int mx, int my, int ref, int type){
  362. const int w= s->b_width << s->block_max_depth;
  363. const int rem_depth= s->block_max_depth - level;
  364. const int index= (x + y*w) << rem_depth;
  365. const int block_w= 1<<rem_depth;
  366. const int block_h= 1<<rem_depth; //FIXME "w!=h"
  367. BlockNode block;
  368. int i,j;
  369. block.color[0]= l;
  370. block.color[1]= cb;
  371. block.color[2]= cr;
  372. block.mx= mx;
  373. block.my= my;
  374. block.ref= ref;
  375. block.type= type;
  376. block.level= level;
  377. for(j=0; j<block_h; j++){
  378. for(i=0; i<block_w; i++){
  379. s->block[index + i + j*w]= block;
  380. }
  381. }
  382. }
  383. extern const int8_t ff_quant3bA[256];
  384. #define QEXPSHIFT (7-FRAC_BITS+8) //FIXME try to change this to 0
  385. #endif /* AVCODEC_SNOW_H */