Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 235.09 KB | None | 0 0
  1. From 0d90ea4ec79409199d6d8b82ac02043dce919ad5 Mon Sep 17 00:00:00 2001
  2. From: Stanislav Dolganov <dolganov@qst.hk>
  3. Date: Thu, 18 Aug 2016 14:07:35 +0300
  4. Subject: [PATCH 1/4] factoring obmc out of snow
  5.  
  6. ---
  7. libavcodec/Makefile | 8 +-
  8. libavcodec/obmc.c | 61 ++
  9. libavcodec/obmc.h | 45 ++
  10. libavcodec/obme.c | 1135 +++++++++++++++++++++++++++++++++++++
  11. libavcodec/obme.h | 58 ++
  12. libavcodec/obmemc.c | 651 +++++++++++++++++++++
  13. libavcodec/obmemc.h | 522 +++++++++++++++++
  14. libavcodec/obmemc_data.h | 132 +++++
  15. libavcodec/snow.c | 571 +------------------
  16. libavcodec/snow.h | 356 +-----------
  17. libavcodec/snowdata.h | 132 -----
  18. libavcodec/snowdec.c | 234 +++-----
  19. libavcodec/snowenc.c | 1409 ++++++++--------------------------------------
  20. 13 files changed, 2939 insertions(+), 2375 deletions(-)
  21. create mode 100644 libavcodec/obmc.c
  22. create mode 100644 libavcodec/obmc.h
  23. create mode 100644 libavcodec/obme.c
  24. create mode 100644 libavcodec/obme.h
  25. create mode 100644 libavcodec/obmemc.c
  26. create mode 100644 libavcodec/obmemc.h
  27. create mode 100644 libavcodec/obmemc_data.h
  28. delete mode 100644 libavcodec/snowdata.h
  29.  
  30. diff --git a/libavcodec/Makefile b/libavcodec/Makefile
  31. index b375720..dbbf9a1 100644
  32. --- a/libavcodec/Makefile
  33. +++ b/libavcodec/Makefile
  34. @@ -511,9 +511,11 @@ OBJS-$(CONFIG_SMACKAUD_DECODER) += smacker.o
  35. OBJS-$(CONFIG_SMACKER_DECODER) += smacker.o
  36. OBJS-$(CONFIG_SMC_DECODER) += smc.o
  37. OBJS-$(CONFIG_SMVJPEG_DECODER) += smvjpegdec.o
  38. -OBJS-$(CONFIG_SNOW_DECODER) += snowdec.o snow.o snow_dwt.o
  39. -OBJS-$(CONFIG_SNOW_ENCODER) += snowenc.o snow.o snow_dwt.o \
  40. - h263.o ituh263enc.o
  41. +OBJS-$(CONFIG_SNOW_DECODER) += snowdec.o snow.o snow_dwt.o\
  42. + obmemc.o obmc.o
  43. +OBJS-$(CONFIG_SNOW_ENCODER) += snowenc.o snow.o snow_dwt.o\
  44. + h263.o ituh263enc.o\
  45. + obmemc.o obme.o
  46. OBJS-$(CONFIG_SOL_DPCM_DECODER) += dpcm.o
  47. OBJS-$(CONFIG_SONIC_DECODER) += sonic.o
  48. OBJS-$(CONFIG_SONIC_ENCODER) += sonic.o
  49. diff --git a/libavcodec/obmc.c b/libavcodec/obmc.c
  50. new file mode 100644
  51. index 0000000..fccad24
  52. --- /dev/null
  53. +++ b/libavcodec/obmc.c
  54. @@ -0,0 +1,61 @@
  55. +/*
  56. + * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
  57. + *
  58. + * This file is part of FFmpeg.
  59. + *
  60. + * FFmpeg is free software; you can redistribute it and/or
  61. + * modify it under the terms of the GNU Lesser General Public
  62. + * License as published by the Free Software Foundation; either
  63. + * version 2.1 of the License, or (at your option) any later version.
  64. + *
  65. + * FFmpeg is distributed in the hope that it will be useful,
  66. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  67. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  68. + * Lesser General Public License for more details.
  69. + *
  70. + * You should have received a copy of the GNU Lesser General Public
  71. + * License along with FFmpeg; if not, write to the Free Software
  72. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  73. + */
  74. +
  75. + #include "obmc.h"
  76. +
  77. +int ff_obmc_decode_init(OBMCContext *f) {
  78. + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(f->avctx->pix_fmt);
  79. + if (!desc)
  80. + return AVERROR_INVALIDDATA;
  81. + int i;
  82. + f->nb_planes = 0;
  83. + for (i = 0; i < desc->nb_components; i++)
  84. + f->nb_planes = FFMAX(f->nb_planes, desc->comp[i].plane + 1);
  85. +
  86. + avcodec_get_chroma_sub_sample(f->avctx->pix_fmt, &f->chroma_h_shift, &f->chroma_v_shift);
  87. +
  88. + return 0;
  89. +}
  90. +
  91. +int ff_obmc_predecode_frame(OBMCContext *f) {
  92. + int plane_index, ret;
  93. + for(plane_index=0; plane_index < f->nb_planes; plane_index++){
  94. + PlaneObmc *pc= &f->plane[plane_index];
  95. + pc->fast_mc= pc->diag_mc && pc->htaps==6 && pc->hcoeff[0]==40
  96. + && pc->hcoeff[1]==-10
  97. + && pc->hcoeff[2]==2;
  98. + }
  99. +
  100. + if ((ret = ff_obmc_alloc_blocks(f)) < 0)
  101. + return ret;
  102. +
  103. + if ((ret = ff_obmc_frame_start(f)) < 0)
  104. + return ret;
  105. +
  106. + f->current_picture->pict_type = f->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  107. +
  108. + av_assert0(!f->avmv);
  109. + if (f->avctx->flags2 & AV_CODEC_FLAG2_EXPORT_MVS) {
  110. + f->avmv = av_malloc_array(f->b_width * f->b_height, sizeof(AVMotionVector) << (f->block_max_depth*2));
  111. + }
  112. + f->avmv_index = 0;
  113. +
  114. + return 0;
  115. +}
  116. diff --git a/libavcodec/obmc.h b/libavcodec/obmc.h
  117. new file mode 100644
  118. index 0000000..1e218b1
  119. --- /dev/null
  120. +++ b/libavcodec/obmc.h
  121. @@ -0,0 +1,45 @@
  122. +/*
  123. + * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
  124. + *
  125. + * This file is part of FFmpeg.
  126. + *
  127. + * FFmpeg is free software; you can redistribute it and/or
  128. + * modify it under the terms of the GNU Lesser General Public
  129. + * License as published by the Free Software Foundation; either
  130. + * version 2.1 of the License, or (at your option) any later version.
  131. + *
  132. + * FFmpeg is distributed in the hope that it will be useful,
  133. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  134. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  135. + * Lesser General Public License for more details.
  136. + *
  137. + * You should have received a copy of the GNU Lesser General Public
  138. + * License along with FFmpeg; if not, write to the Free Software
  139. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  140. + */
  141. +
  142. + /**
  143. + * @file obmc.h
  144. + * @brief Overlapped block motion compensation functions
  145. + */
  146. +
  147. +#ifndef AVCODEC_OBMC_H
  148. +#define AVCODEC_OBMC_H
  149. +
  150. +#include "obmemc.h"
  151. +
  152. + /**
  153. + * Inits OBMC context parameters needed for decoding process
  154. + *
  155. + * @param[in,out] f OBMC context to init
  156. + */
  157. +int ff_obmc_decode_init(OBMCContext *f);
  158. +
  159. + /**
  160. + * Prepares OBMC context for block decoding for each frame
  161. + *
  162. + * @param[in,out] f OBMC context to prepare
  163. + */
  164. +int ff_obmc_predecode_frame(OBMCContext *f);
  165. +
  166. +#endif /* AVCODEC_OBMC_H */
  167. diff --git a/libavcodec/obme.c b/libavcodec/obme.c
  168. new file mode 100644
  169. index 0000000..f442b26
  170. --- /dev/null
  171. +++ b/libavcodec/obme.c
  172. @@ -0,0 +1,1135 @@
  173. +/*
  174. + * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
  175. + *
  176. + * This file is part of FFmpeg.
  177. + *
  178. + * FFmpeg is free software; you can redistribute it and/or
  179. + * modify it under the terms of the GNU Lesser General Public
  180. + * License as published by the Free Software Foundation; either
  181. + * version 2.1 of the License, or (at your option) any later version.
  182. + *
  183. + * FFmpeg is distributed in the hope that it will be useful,
  184. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  185. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  186. + * Lesser General Public License for more details.
  187. + *
  188. + * You should have received a copy of the GNU Lesser General Public
  189. + * License along with FFmpeg; if not, write to the Free Software
  190. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  191. + */
  192. +
  193. +#include "obme.h"
  194. +#include "h263.h"
  195. +
  196. +int ff_obmc_encode_init(OBMCContext *s, AVCodecContext *avctx)
  197. +{
  198. + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  199. + if (!desc)
  200. + return AVERROR_INVALIDDATA;
  201. + int plane_index, ret;
  202. + int i;
  203. +
  204. + #if FF_API_MOTION_EST
  205. + FF_DISABLE_DEPRECATION_WARNINGS
  206. + if (avctx->me_method == ME_ITER)
  207. + s->motion_est = FF_ME_ITER;
  208. + FF_ENABLE_DEPRECATION_WARNINGS
  209. + #endif
  210. +
  211. + s->mv_scale = (avctx->flags & AV_CODEC_FLAG_QPEL) ? 2 : 4;
  212. + s->block_max_depth= (avctx->flags & AV_CODEC_FLAG_4MV ) ? 1 : 0;
  213. +
  214. + avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  215. +
  216. + for(plane_index=0; plane_index<3; plane_index++){
  217. + s->plane[plane_index].diag_mc= 1;
  218. + s->plane[plane_index].htaps= 6;
  219. + s->plane[plane_index].hcoeff[0]= 40;
  220. + s->plane[plane_index].hcoeff[1]= -10;
  221. + s->plane[plane_index].hcoeff[2]= 2;
  222. + s->plane[plane_index].fast_mc= 1;
  223. + }
  224. +
  225. + ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
  226. +
  227. + if ((ret = ff_obmc_alloc_blocks(s)) < 0)
  228. + return ret;
  229. +
  230. + s->m.avctx = avctx;
  231. + s->m.bit_rate= avctx->bit_rate;
  232. +
  233. + s->m.me.temp =
  234. + s->m.me.scratchpad= av_mallocz_array((avctx->width+64), 2*16*2*sizeof(uint8_t));
  235. + s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  236. + s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  237. + s->m.sc.obmc_scratchpad= av_mallocz(MB_SIZE*MB_SIZE*12*sizeof(uint32_t));
  238. + if (!s->m.me.scratchpad || !s->m.me.map || !s->m.me.score_map || !s->m.sc.obmc_scratchpad)
  239. + return AVERROR(ENOMEM);
  240. +
  241. + ff_h263_encode_init(&s->m); //mv_penalty
  242. +
  243. + s->max_ref_frames = av_clip(avctx->refs, 1, MAX_REF_FRAMES);
  244. +
  245. + s->nb_planes = 0;
  246. + for (i = 0; i < desc->nb_components; i++)
  247. + s->nb_planes = FFMAX(s->nb_planes, desc->comp[i].plane + 1);
  248. +
  249. + ff_set_cmp(&s->mecc, s->mecc.me_cmp, s->avctx->me_cmp);
  250. + ff_set_cmp(&s->mecc, s->mecc.me_sub_cmp, s->avctx->me_sub_cmp);
  251. +
  252. + s->input_picture = av_frame_alloc();
  253. + if (!s->input_picture)
  254. + return AVERROR(ENOMEM);
  255. +
  256. + if ((ret = ff_obmc_get_buffer(s, s->input_picture)) < 0)
  257. + return ret;
  258. +
  259. + if(s->motion_est == FF_ME_ITER){
  260. + int size= s->b_width * s->b_height << 2*s->block_max_depth;
  261. + for(i=0; i<s->max_ref_frames; i++){
  262. + s->ref_mvs[i]= av_mallocz_array(size, sizeof(int16_t[2]));
  263. + s->ref_scores[i]= av_mallocz_array(size, sizeof(uint32_t));
  264. + if (!s->ref_mvs[i] || !s->ref_scores[i])
  265. + return AVERROR(ENOMEM);
  266. + }
  267. + }
  268. +
  269. + return 0;
  270. +}
  271. +
  272. +//near copy & paste from dsputil, FIXME
  273. +static int pix_sum(uint8_t * pix, int line_size, int w, int h)
  274. +{
  275. + int s, i, j;
  276. +
  277. + s = 0;
  278. + for (i = 0; i < h; i++) {
  279. + for (j = 0; j < w; j++) {
  280. + s += pix[0];
  281. + pix ++;
  282. + }
  283. + pix += line_size - w;
  284. + }
  285. + return s;
  286. +}
  287. +
  288. +//near copy & paste from dsputil, FIXME
  289. +static int pix_norm1(uint8_t * pix, int line_size, int w)
  290. +{
  291. + int s, i, j;
  292. + uint32_t *sq = ff_square_tab + 256;
  293. +
  294. + s = 0;
  295. + for (i = 0; i < w; i++) {
  296. + for (j = 0; j < w; j ++) {
  297. + s += sq[pix[0]];
  298. + pix ++;
  299. + }
  300. + pix += line_size - w;
  301. + }
  302. + return s;
  303. +}
  304. +
  305. +static inline int get_penalty_factor(int lambda, int lambda2, int type){
  306. + switch(type&0xFF){
  307. + default:
  308. + case FF_CMP_SAD:
  309. + return lambda>>FF_LAMBDA_SHIFT;
  310. + case FF_CMP_DCT:
  311. + return (3*lambda)>>(FF_LAMBDA_SHIFT+1);
  312. + case FF_CMP_W53:
  313. + return (4*lambda)>>(FF_LAMBDA_SHIFT);
  314. + case FF_CMP_W97:
  315. + return (2*lambda)>>(FF_LAMBDA_SHIFT);
  316. + case FF_CMP_SATD:
  317. + case FF_CMP_DCT264:
  318. + return (2*lambda)>>FF_LAMBDA_SHIFT;
  319. + case FF_CMP_RD:
  320. + case FF_CMP_PSNR:
  321. + case FF_CMP_SSE:
  322. + case FF_CMP_NSSE:
  323. + return lambda2>>FF_LAMBDA_SHIFT;
  324. + case FF_CMP_BIT:
  325. + return 1;
  326. + }
  327. +}
  328. +
  329. +//FIXME copy&paste
  330. +#define P_LEFT P[1]
  331. +#define P_TOP P[2]
  332. +#define P_TOPRIGHT P[3]
  333. +#define P_MEDIAN P[4]
  334. +#define P_MV1 P[9]
  335. +#define FLAG_QPEL 1 //must be 1
  336. +
  337. +static int encode_q_branch(OBMCContext *s, int level, int x, int y)
  338. +{
  339. + ObmcCoderContext *const rc = &s->obmc_coder;
  340. + ObmcCoderContext pc, ic;
  341. + int score, score2, iscore, block_s, sum;;
  342. + const int w= s->b_width << s->block_max_depth;
  343. + const int h= s->b_height << s->block_max_depth;
  344. + const int rem_depth= s->block_max_depth - level;
  345. + const int index= (x + y*w) << rem_depth;
  346. + const int block_w= 1<<(LOG2_MB_SIZE - level);
  347. + int trx= (x+1)<<rem_depth;
  348. + int try= (y+1)<<rem_depth;
  349. + const BlockNode *left = x ? &s->block[index-1] : &null_block;
  350. + const BlockNode *top = y ? &s->block[index-w] : &null_block;
  351. + const BlockNode *right = trx<w ? &s->block[index+1] : &null_block;
  352. + const BlockNode *bottom= try<h ? &s->block[index+w] : &null_block;
  353. + const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  354. + const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  355. + int pl = left->color[0];
  356. + int pcb= left->color[1];
  357. + int pcr= left->color[2];
  358. + int pmx, pmy;
  359. + int mx=0, my=0;
  360. + int l,cr,cb;
  361. + const int stride= s->current_picture->linesize[0];
  362. + const int uvstride= s->current_picture->linesize[1];
  363. + uint8_t *current_data[3]= { s->input_picture->data[0] + (x + y* stride)*block_w,
  364. + s->input_picture->data[1] + ((x*block_w)>>s->chroma_h_shift) + ((y*uvstride*block_w)>>s->chroma_v_shift),
  365. + s->input_picture->data[2] + ((x*block_w)>>s->chroma_h_shift) + ((y*uvstride*block_w)>>s->chroma_v_shift)};
  366. + int P[10][2];
  367. + int16_t last_mv[3][2];
  368. + int qpel= !!(s->avctx->flags & AV_CODEC_FLAG_QPEL); //unused
  369. + const int shift= 1+qpel;
  370. + MotionEstContext *c= &s->m.me;
  371. + int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
  372. + int mx_context= av_log2(2*FFABS(left->mx - top->mx));
  373. + int my_context= av_log2(2*FFABS(left->my - top->my));
  374. + int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  375. + int ref, best_ref, ref_score, ref_mx, ref_my;
  376. +
  377. + if(s->keyframe){
  378. + set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
  379. + return 0;
  380. + }
  381. +
  382. +// clip predictors / edge ?
  383. +
  384. + P_LEFT[0]= left->mx;
  385. + P_LEFT[1]= left->my;
  386. + P_TOP [0]= top->mx;
  387. + P_TOP [1]= top->my;
  388. + P_TOPRIGHT[0]= tr->mx;
  389. + P_TOPRIGHT[1]= tr->my;
  390. +
  391. + last_mv[0][0]= s->block[index].mx;
  392. + last_mv[0][1]= s->block[index].my;
  393. + last_mv[1][0]= right->mx;
  394. + last_mv[1][1]= right->my;
  395. + last_mv[2][0]= bottom->mx;
  396. + last_mv[2][1]= bottom->my;
  397. +
  398. + s->m.mb_stride=2;
  399. + s->m.mb_x=
  400. + s->m.mb_y= 0;
  401. + c->skip= 0;
  402. +
  403. + av_assert1(c-> stride == stride);
  404. + av_assert1(c->uvstride == uvstride);
  405. +
  406. + c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  407. + c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  408. + c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  409. + c->current_mv_penalty= c->mv_penalty[s->m.f_code=1] + MAX_DMV;
  410. +
  411. + c->xmin = - x*block_w - 16+3;
  412. + c->ymin = - y*block_w - 16+3;
  413. + c->xmax = - (x+1)*block_w + (w<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-3;
  414. + c->ymax = - (y+1)*block_w + (h<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-3;
  415. +
  416. + if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  417. + if(P_LEFT[1] > (c->ymax<<shift)) P_LEFT[1] = (c->ymax<<shift);
  418. + if(P_TOP[0] > (c->xmax<<shift)) P_TOP[0] = (c->xmax<<shift);
  419. + if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
  420. + if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  421. + if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift); //due to pmx no clip
  422. + if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  423. +
  424. + P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  425. + P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  426. +
  427. + if (!y) {
  428. + c->pred_x= P_LEFT[0];
  429. + c->pred_y= P_LEFT[1];
  430. + } else {
  431. + c->pred_x = P_MEDIAN[0];
  432. + c->pred_y = P_MEDIAN[1];
  433. + }
  434. +
  435. + score= INT_MAX;
  436. + best_ref= 0;
  437. + for(ref=0; ref<s->ref_frames; ref++){
  438. + init_ref(c, current_data, s->last_pictures[ref]->data, NULL, block_w*x, block_w*y, 0);
  439. +
  440. + ref_score= ff_epzs_motion_search(&s->m, &ref_mx, &ref_my, P, 0, /*ref_index*/ 0, last_mv,
  441. + (1<<16)>>shift, level-LOG2_MB_SIZE+4, block_w);
  442. +
  443. + av_assert2(ref_mx >= c->xmin);
  444. + av_assert2(ref_mx <= c->xmax);
  445. + av_assert2(ref_my >= c->ymin);
  446. + av_assert2(ref_my <= c->ymax);
  447. +
  448. + ref_score= c->sub_motion_search(&s->m, &ref_mx, &ref_my, ref_score, 0, 0, level-LOG2_MB_SIZE+4, block_w);
  449. + ref_score= ff_get_mb_score(&s->m, ref_mx, ref_my, 0, 0, level-LOG2_MB_SIZE+4, block_w, 0);
  450. + ref_score+= 2*av_log2(2*ref)*c->penalty_factor;
  451. + if(s->ref_mvs[ref]){
  452. + s->ref_mvs[ref][index][0]= ref_mx;
  453. + s->ref_mvs[ref][index][1]= ref_my;
  454. + s->ref_scores[ref][index]= ref_score;
  455. + }
  456. + if(score > ref_score){
  457. + score= ref_score;
  458. + best_ref= ref;
  459. + mx= ref_mx;
  460. + my= ref_my;
  461. + }
  462. + }
  463. + //FIXME if mb_cmp != SSE then intra cannot be compared currently and mb_penalty vs. lambda2
  464. +
  465. + // subpel search
  466. + rc->init_frame_coder(s->avctx, &pc);
  467. +
  468. + if(level!=s->block_max_depth)
  469. + pc.put_level_break(&pc, 4 + s_context, 1);
  470. + pc.put_block_type(&pc, 1 + left->type + top->type, 0);
  471. + if(s->ref_frames > 1)
  472. + pc.put_best_ref(&pc, 128 + 1024 + 32*ref_context, best_ref);
  473. + pred_mv(s, &pmx, &pmy, best_ref, left, top, tr);
  474. + pc.put_block_mv(&pc,
  475. + 128 + 32*(mx_context + 16*!!best_ref), 128 + 32*(my_context + 16*!!best_ref),
  476. + mx - pmx, my - pmy
  477. + );
  478. + score += (s->lambda2*pc.get_bits(&pc))>>FF_LAMBDA_SHIFT;
  479. +
  480. + block_s= block_w*block_w;
  481. + sum = pix_sum(current_data[0], stride, block_w, block_w);
  482. + l= (sum + block_s/2)/block_s;
  483. + iscore = pix_norm1(current_data[0], stride, block_w) - 2*l*sum + l*l*block_s;
  484. +
  485. + if (s->nb_planes > 2) {
  486. + block_s= block_w*block_w>>(s->chroma_h_shift + s->chroma_v_shift);
  487. + sum = pix_sum(current_data[1], uvstride, block_w>>s->chroma_h_shift, block_w>>s->chroma_v_shift);
  488. + cb= (sum + block_s/2)/block_s;
  489. + // iscore += pix_norm1(&current_mb[1][0], uvstride, block_w>>1) - 2*cb*sum + cb*cb*block_s;
  490. + sum = pix_sum(current_data[2], uvstride, block_w>>s->chroma_h_shift, block_w>>s->chroma_v_shift);
  491. + cr= (sum + block_s/2)/block_s;
  492. + // iscore += pix_norm1(&current_mb[2][0], uvstride, block_w>>1) - 2*cr*sum + cr*cr*block_s;
  493. + }else
  494. + cb = cr = 0;
  495. +
  496. + rc->init_frame_coder(s->avctx, &ic);
  497. +
  498. + if(level!=s->block_max_depth)
  499. + ic.put_level_break(&ic, 4 + s_context, 1);
  500. + ic.put_block_type(&ic, 1 + left->type + top->type, 1);
  501. + ic.put_block_color(&ic, 32, 64, 96, l-pl, cb-pcb, cr-pcr);
  502. + iscore += (s->lambda2*ic.get_bits(&ic))>>FF_LAMBDA_SHIFT;
  503. +
  504. + av_assert1(iscore < 255*255*256 + s->lambda2*10);
  505. + av_assert1(iscore >= 0);
  506. + av_assert1(l>=0 && l<=255);
  507. + av_assert1(pl>=0 && pl<=255);
  508. +
  509. + if(level==0){
  510. + int varc= iscore >> 8;
  511. + int vard= score >> 8;
  512. + if (vard <= 64 || vard < varc)
  513. + c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
  514. + else
  515. + c->scene_change_score+= s->m.qscale;
  516. + }
  517. +
  518. + if(level!=s->block_max_depth){
  519. + rc->put_level_break(rc, 4 + s_context, 0);
  520. + score2 = encode_q_branch(s, level+1, 2*x+0, 2*y+0);
  521. + score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+0);
  522. + score2+= encode_q_branch(s, level+1, 2*x+0, 2*y+1);
  523. + score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+1);
  524. + score2+= s->lambda2>>FF_LAMBDA_SHIFT; //FIXME exact split overhead
  525. +
  526. + if(score2 < score && score2 < iscore) {
  527. + rc->free(&ic); rc->free(&pc);
  528. + return score2;
  529. + }
  530. + }
  531. +
  532. + if(iscore < score){
  533. + pred_mv(s, &pmx, &pmy, 0, left, top, tr);
  534. + rc->copy_coder(&ic);
  535. + set_blocks(s, level, x, y, l, cb, cr, pmx, pmy, 0, BLOCK_INTRA);
  536. + rc->free(&ic); rc->free(&pc);
  537. + return iscore;
  538. + }else{
  539. + rc->copy_coder(&pc);
  540. + set_blocks(s, level, x, y, pl, pcb, pcr, mx, my, best_ref, 0);
  541. + rc->free(&ic); rc->free(&pc);
  542. + return score;
  543. + }
  544. +}
  545. +
  546. +static void encode_q_branch2(OBMCContext *s, int level, int x, int y)
  547. +{
  548. + ObmcCoderContext *const rc = &s->obmc_coder;
  549. + const int w= s->b_width << s->block_max_depth;
  550. + const int rem_depth= s->block_max_depth - level;
  551. + const int index= (x + y*w) << rem_depth;
  552. + int trx= (x+1)<<rem_depth;
  553. + BlockNode *b= &s->block[index];
  554. + const BlockNode *left = x ? &s->block[index-1] : &null_block;
  555. + const BlockNode *top = y ? &s->block[index-w] : &null_block;
  556. + const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  557. + const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  558. + int pl = left->color[0];
  559. + int pcb= left->color[1];
  560. + int pcr= left->color[2];
  561. + int pmx, pmy;
  562. + int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
  563. + int mx_context= av_log2(2*FFABS(left->mx - top->mx)) + 16*!!b->ref;
  564. + int my_context= av_log2(2*FFABS(left->my - top->my)) + 16*!!b->ref;
  565. + int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  566. +
  567. + if(s->keyframe){
  568. + set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
  569. + return;
  570. + }
  571. +
  572. + if(level!=s->block_max_depth){
  573. + if(same_block(b,b+1) && same_block(b,b+w) && same_block(b,b+w+1)){
  574. + rc->put_level_break(rc, 4 + s_context, 1);
  575. + }else{
  576. + rc->put_level_break(rc, 4 + s_context, 0);
  577. + encode_q_branch2(s, level+1, 2*x+0, 2*y+0);
  578. + encode_q_branch2(s, level+1, 2*x+1, 2*y+0);
  579. + encode_q_branch2(s, level+1, 2*x+0, 2*y+1);
  580. + encode_q_branch2(s, level+1, 2*x+1, 2*y+1);
  581. + return;
  582. + }
  583. + }
  584. + if(b->type & BLOCK_INTRA){
  585. + pred_mv(s, &pmx, &pmy, 0, left, top, tr);
  586. + rc->put_block_type(rc, 1 + (left->type&1) + (top->type&1), 1);
  587. + rc->put_block_color(
  588. + rc,
  589. + 32, 64, 96,
  590. + b->color[0]-pl, b->color[1]-pcb, b->color[2]-pcr
  591. + );
  592. + set_blocks(s, level, x, y, b->color[0], b->color[1], b->color[2], pmx, pmy, 0, BLOCK_INTRA);
  593. + }else{
  594. + pred_mv(s, &pmx, &pmy, b->ref, left, top, tr);
  595. + rc->put_block_type(rc, 1 + (left->type&1) + (top->type&1), 0);
  596. + if(s->ref_frames > 1)
  597. + rc->put_best_ref(rc, 128 + 1024 + 32*ref_context, b->ref);
  598. + rc->put_block_mv(rc,
  599. + 128 + 32*mx_context, 128 + 32*my_context,
  600. + b->mx - pmx, b->my - pmy
  601. + );
  602. + set_blocks(s, level, x, y, pl, pcb, pcr, b->mx, b->my, b->ref, 0);
  603. + }
  604. +}
  605. +
  606. +static int get_dc(OBMCContext *s, int mb_x, int mb_y, int plane_index){
  607. + int i, x2, y2;
  608. + PlaneObmc *p= &s->plane[plane_index];
  609. + const int block_size = MB_SIZE >> s->block_max_depth;
  610. + const int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
  611. + const int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
  612. + const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];
  613. + const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
  614. + const int ref_stride= s->current_picture->linesize[plane_index];
  615. + uint8_t *src= s-> input_picture->data[plane_index];
  616. + IDWTELEM *dst= (IDWTELEM*)s->m.sc.obmc_scratchpad + plane_index*block_size*block_size*4; //FIXME change to unsigned
  617. + const int b_stride = s->b_width << s->block_max_depth;
  618. + const int w= p->width;
  619. + const int h= p->height;
  620. + int index= mb_x + mb_y*b_stride;
  621. + BlockNode *b= &s->block[index];
  622. + BlockNode backup= *b;
  623. + int ab=0;
  624. + int aa=0;
  625. +
  626. + av_assert2(s->chroma_h_shift == s->chroma_v_shift); //obmc stuff above
  627. +
  628. + b->type|= BLOCK_INTRA;
  629. + b->color[plane_index]= 0;
  630. + memset(dst, 0, obmc_stride*obmc_stride*sizeof(IDWTELEM));
  631. +
  632. + for(i=0; i<4; i++){
  633. + int mb_x2= mb_x + (i &1) - 1;
  634. + int mb_y2= mb_y + (i>>1) - 1;
  635. + int x= block_w*mb_x2 + block_w/2;
  636. + int y= block_h*mb_y2 + block_h/2;
  637. +
  638. + add_yblock(s, 0, NULL, dst + (i&1)*block_w + (i>>1)*obmc_stride*block_h, NULL, obmc,
  639. + x, y, block_w, block_h, w, h, obmc_stride, ref_stride, obmc_stride, mb_x2, mb_y2, 0, 0, plane_index);
  640. +
  641. + for(y2= FFMAX(y, 0); y2<FFMIN(h, y+block_h); y2++){
  642. + for(x2= FFMAX(x, 0); x2<FFMIN(w, x+block_w); x2++){
  643. + int index= x2-(block_w*mb_x - block_w/2) + (y2-(block_h*mb_y - block_h/2))*obmc_stride;
  644. + int obmc_v= obmc[index];
  645. + int d;
  646. + if(y<0) obmc_v += obmc[index + block_h*obmc_stride];
  647. + if(x<0) obmc_v += obmc[index + block_w];
  648. + if(y+block_h>h) obmc_v += obmc[index - block_h*obmc_stride];
  649. + if(x+block_w>w) obmc_v += obmc[index - block_w];
  650. + //FIXME precalculate this or simplify it somehow else
  651. +
  652. + d = -dst[index] + (1<<(FRAC_BITS-1));
  653. + dst[index] = d;
  654. + ab += (src[x2 + y2*ref_stride] - (d>>FRAC_BITS)) * obmc_v;
  655. + aa += obmc_v * obmc_v; //FIXME precalculate this
  656. + }
  657. + }
  658. + }
  659. + *b= backup;
  660. +
  661. + return av_clip_uint8( ROUNDED_DIV(ab<<LOG2_OBMC_MAX, aa) ); //FIXME we should not need clipping
  662. +}
  663. +
  664. +static inline int get_block_bits(OBMCContext *s, int x, int y, int w){
  665. + const int b_stride = s->b_width << s->block_max_depth;
  666. + const int b_height = s->b_height<< s->block_max_depth;
  667. + int index= x + y*b_stride;
  668. + const BlockNode *b = &s->block[index];
  669. + const BlockNode *left = x ? &s->block[index-1] : &null_block;
  670. + const BlockNode *top = y ? &s->block[index-b_stride] : &null_block;
  671. + const BlockNode *tl = y && x ? &s->block[index-b_stride-1] : left;
  672. + const BlockNode *tr = y && x+w<b_stride ? &s->block[index-b_stride+w] : tl;
  673. + int dmx, dmy;
  674. +// int mx_context= av_log2(2*FFABS(left->mx - top->mx));
  675. +// int my_context= av_log2(2*FFABS(left->my - top->my));
  676. +
  677. + if(x<0 || x>=b_stride || y>=b_height)
  678. + return 0;
  679. +/*
  680. +1 0 0
  681. +01X 1-2 1
  682. +001XX 3-6 2-3
  683. +0001XXX 7-14 4-7
  684. +00001XXXX 15-30 8-15
  685. +*/
  686. +//FIXME try accurate rate
  687. +//FIXME intra and inter predictors if surrounding blocks are not the same type
  688. + if(b->type & BLOCK_INTRA){
  689. + return 3+2*( av_log2(2*FFABS(left->color[0] - b->color[0]))
  690. + + av_log2(2*FFABS(left->color[1] - b->color[1]))
  691. + + av_log2(2*FFABS(left->color[2] - b->color[2])));
  692. + }else{
  693. + pred_mv(s, &dmx, &dmy, b->ref, left, top, tr);
  694. + dmx-= b->mx;
  695. + dmy-= b->my;
  696. + return 2*(1 + av_log2(2*FFABS(dmx)) //FIXME kill the 2* can be merged in lambda
  697. + + av_log2(2*FFABS(dmy))
  698. + + av_log2(2*b->ref));
  699. + }
  700. +}
  701. +
  702. +static int get_block_rd(OBMCContext *s, int mb_x, int mb_y, int plane_index, uint8_t (*obmc_edged)[MB_SIZE * 2]){
  703. + PlaneObmc *p= &s->plane[plane_index];
  704. + const int block_size = MB_SIZE >> s->block_max_depth;
  705. + const int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
  706. + const int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
  707. + const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
  708. + const int ref_stride= s->current_picture->linesize[plane_index];
  709. + uint8_t *dst= s->current_picture->data[plane_index];
  710. + uint8_t *src= s-> input_picture->data[plane_index];
  711. + IDWTELEM *pred= (IDWTELEM*)s->m.sc.obmc_scratchpad + plane_index*block_size*block_size*4;
  712. + uint8_t *cur = s->scratchbuf;
  713. + uint8_t *tmp = s->emu_edge_buffer;
  714. + const int b_stride = s->b_width << s->block_max_depth;
  715. + const int b_height = s->b_height<< s->block_max_depth;
  716. + const int w= p->width;
  717. + const int h= p->height;
  718. + int distortion;
  719. + int rate= 0;
  720. + const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
  721. + int sx= block_w*mb_x - block_w/2;
  722. + int sy= block_h*mb_y - block_h/2;
  723. + int x0= FFMAX(0,-sx);
  724. + int y0= FFMAX(0,-sy);
  725. + int x1= FFMIN(block_w*2, w-sx);
  726. + int y1= FFMIN(block_h*2, h-sy);
  727. + int i,x,y;
  728. +
  729. + av_assert2(s->chroma_h_shift == s->chroma_v_shift); //obmc and square assumtions below chckinhg only block_w
  730. +
  731. + ff_obmc_pred_block(s, cur, tmp, ref_stride, sx, sy, block_w*2, block_h*2, &s->block[mb_x + mb_y*b_stride], plane_index, w, h);
  732. +
  733. + for(y=y0; y<y1; y++){
  734. + const uint8_t *obmc1= obmc_edged[y];
  735. + const IDWTELEM *pred1 = pred + y*obmc_stride;
  736. + uint8_t *cur1 = cur + y*ref_stride;
  737. + uint8_t *dst1 = dst + sx + (sy+y)*ref_stride;
  738. + for(x=x0; x<x1; x++){
  739. +#if FRAC_BITS >= LOG2_OBMC_MAX
  740. + int v = (cur1[x] * obmc1[x]) << (FRAC_BITS - LOG2_OBMC_MAX);
  741. +#else
  742. + int v = (cur1[x] * obmc1[x] + (1<<(LOG2_OBMC_MAX - FRAC_BITS-1))) >> (LOG2_OBMC_MAX - FRAC_BITS);
  743. +#endif
  744. + v = (v + pred1[x]) >> FRAC_BITS;
  745. + if(v&(~255)) v= ~(v>>31);
  746. + dst1[x] = v;
  747. + }
  748. + }
  749. +
  750. + /* copy the regions where obmc[] = (uint8_t)256 */
  751. + if(LOG2_OBMC_MAX == 8
  752. + && (mb_x == 0 || mb_x == b_stride-1)
  753. + && (mb_y == 0 || mb_y == b_height-1)){
  754. + if(mb_x == 0)
  755. + x1 = block_w;
  756. + else
  757. + x0 = block_w;
  758. + if(mb_y == 0)
  759. + y1 = block_h;
  760. + else
  761. + y0 = block_h;
  762. + for(y=y0; y<y1; y++)
  763. + memcpy(dst + sx+x0 + (sy+y)*ref_stride, cur + x0 + y*ref_stride, x1-x0);
  764. + }
  765. +
  766. + if(block_w==16){
  767. + /* FIXME rearrange dsputil to fit 32x32 cmp functions */
  768. + /* FIXME check alignment of the cmp wavelet vs the encoding wavelet */
  769. + /* FIXME cmps overlap but do not cover the wavelet's whole support.
  770. + * So improving the score of one block is not strictly guaranteed
  771. + * to improve the score of the whole frame, thus iterative motion
  772. + * estimation does not always converge. */
  773. + if(s->avctx->me_cmp == FF_CMP_W97)
  774. + distortion = ff_w97_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
  775. + else if(s->avctx->me_cmp == FF_CMP_W53)
  776. + distortion = ff_w53_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
  777. + else{
  778. + distortion = 0;
  779. + for(i=0; i<4; i++){
  780. + int off = sx+16*(i&1) + (sy+16*(i>>1))*ref_stride;
  781. + distortion += s->mecc.me_cmp[0](&s->m, src + off, dst + off, ref_stride, 16);
  782. + }
  783. + }
  784. + }else{
  785. + av_assert2(block_w==8);
  786. + distortion = s->mecc.me_cmp[0](&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, block_w*2);
  787. + }
  788. +
  789. + if(plane_index==0){
  790. + for(i=0; i<4; i++){
  791. +/* ..RRr
  792. + * .RXx.
  793. + * rxx..
  794. + */
  795. + rate += get_block_bits(s, mb_x + (i&1) - (i>>1), mb_y + (i>>1), 1);
  796. + }
  797. + if(mb_x == b_stride-2)
  798. + rate += get_block_bits(s, mb_x + 1, mb_y + 1, 1);
  799. + }
  800. + return distortion + rate*penalty_factor;
  801. +}
  802. +
  803. +static int get_4block_rd(OBMCContext *s, int mb_x, int mb_y, int plane_index){
  804. + int i, y2;
  805. + PlaneObmc *p= &s->plane[plane_index];
  806. + const int block_size = MB_SIZE >> s->block_max_depth;
  807. + const int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
  808. + const int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
  809. + const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];
  810. + const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
  811. + const int ref_stride= s->current_picture->linesize[plane_index];
  812. + uint8_t *dst= s->current_picture->data[plane_index];
  813. + uint8_t *src= s-> input_picture->data[plane_index];
  814. + //FIXME zero_dst is const but add_yblock changes dst if add is 0 (this is never the case for dst=zero_dst
  815. + // const has only been removed from zero_dst to suppress a warning
  816. + static IDWTELEM zero_dst[4096]; //FIXME
  817. + const int b_stride = s->b_width << s->block_max_depth;
  818. + const int w= p->width;
  819. + const int h= p->height;
  820. + int distortion= 0;
  821. + int rate= 0;
  822. + const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
  823. +
  824. + av_assert2(s->chroma_h_shift == s->chroma_v_shift); //obmc and square assumtions below
  825. +
  826. + for(i=0; i<9; i++){
  827. + int mb_x2= mb_x + (i%3) - 1;
  828. + int mb_y2= mb_y + (i/3) - 1;
  829. + int x= block_w*mb_x2 + block_w/2;
  830. + int y= block_h*mb_y2 + block_h/2;
  831. +
  832. + add_yblock(s, 0, NULL, zero_dst, dst, obmc,
  833. + x, y, block_w, block_h, w, h, /*dst_stride*/0, ref_stride, obmc_stride, mb_x2, mb_y2, 1, 1, plane_index);
  834. +
  835. + //FIXME find a cleaner/simpler way to skip the outside stuff
  836. + for(y2= y; y2<0; y2++)
  837. + memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
  838. + for(y2= h; y2<y+block_h; y2++)
  839. + memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
  840. + if(x<0){
  841. + for(y2= y; y2<y+block_h; y2++)
  842. + memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, -x);
  843. + }
  844. + if(x+block_w > w){
  845. + for(y2= y; y2<y+block_h; y2++)
  846. + memcpy(dst + w + y2*ref_stride, src + w + y2*ref_stride, x+block_w - w);
  847. + }
  848. +
  849. + av_assert1(block_w== 8 || block_w==16);
  850. + distortion += s->mecc.me_cmp[block_w==8](&s->m, src + x + y*ref_stride, dst + x + y*ref_stride, ref_stride, block_h);
  851. + }
  852. +
  853. + if(plane_index==0){
  854. + BlockNode *b= &s->block[mb_x+mb_y*b_stride];
  855. + int merged= same_block(b,b+1) && same_block(b,b+b_stride) && same_block(b,b+b_stride+1);
  856. +
  857. +/* ..RRRr
  858. + * .RXXx.
  859. + * .RXXx.
  860. + * rxxx.
  861. + */
  862. + if(merged)
  863. + rate = get_block_bits(s, mb_x, mb_y, 2);
  864. + for(i=merged?4:0; i<9; i++){
  865. + static const int dxy[9][2] = {{0,0},{1,0},{0,1},{1,1},{2,0},{2,1},{-1,2},{0,2},{1,2}};
  866. + rate += get_block_bits(s, mb_x + dxy[i][0], mb_y + dxy[i][1], 1);
  867. + }
  868. + }
  869. + return distortion + rate*penalty_factor;
  870. +}
  871. +
  872. +static av_always_inline int check_block(OBMCContext *s, int mb_x, int mb_y, int p[3], int intra, uint8_t (*obmc_edged)[MB_SIZE * 2], int *best_rd){
  873. + const int b_stride= s->b_width << s->block_max_depth;
  874. + BlockNode *block= &s->block[mb_x + mb_y * b_stride];
  875. + BlockNode backup= *block;
  876. + unsigned value;
  877. + int rd, index;
  878. +
  879. + av_assert2(mb_x>=0 && mb_y>=0);
  880. + av_assert2(mb_x<b_stride);
  881. +
  882. + if(intra){
  883. + block->color[0] = p[0];
  884. + block->color[1] = p[1];
  885. + block->color[2] = p[2];
  886. + block->type |= BLOCK_INTRA;
  887. + }else{
  888. + index= (p[0] + 31*p[1]) & (ME_CACHE_SIZE-1);
  889. + value= s->me_cache_generation + (p[0]>>10) + (p[1]<<6) + (block->ref<<12);
  890. + if(s->me_cache[index] == value)
  891. + return 0;
  892. + s->me_cache[index]= value;
  893. +
  894. + block->mx= p[0];
  895. + block->my= p[1];
  896. + block->type &= ~BLOCK_INTRA;
  897. + }
  898. +
  899. + rd= get_block_rd(s, mb_x, mb_y, 0, obmc_edged) + s->intra_penalty * !!intra;
  900. +
  901. +//FIXME chroma
  902. + if(rd < *best_rd){
  903. + *best_rd= rd;
  904. + return 1;
  905. + }else{
  906. + *block= backup;
  907. + return 0;
  908. + }
  909. +}
  910. +
  911. +/* special case for int[2] args we discard afterwards,
  912. + * fixes compilation problem with gcc 2.95 */
  913. +static av_always_inline int check_block_inter(OBMCContext *s, int mb_x, int mb_y, int p0, int p1, uint8_t (*obmc_edged)[MB_SIZE * 2], int *best_rd){
  914. + int p[2] = {p0, p1};
  915. + return check_block(s, mb_x, mb_y, p, 0, obmc_edged, best_rd);
  916. +}
  917. +
  918. +static av_always_inline int check_4block_inter(OBMCContext *s, int mb_x, int mb_y, int p0, int p1, int ref, int *best_rd){
  919. + const int b_stride= s->b_width << s->block_max_depth;
  920. + BlockNode *block= &s->block[mb_x + mb_y * b_stride];
  921. + BlockNode backup[4];
  922. + unsigned value;
  923. + int rd, index;
  924. +
  925. + /* We don't initialize backup[] during variable declaration, because
  926. + * that fails to compile on MSVC: "cannot convert from 'BlockNode' to
  927. + * 'int16_t'". */
  928. + backup[0] = block[0];
  929. + backup[1] = block[1];
  930. + backup[2] = block[b_stride];
  931. + backup[3] = block[b_stride + 1];
  932. +
  933. + av_assert2(mb_x>=0 && mb_y>=0);
  934. + av_assert2(mb_x<b_stride);
  935. + av_assert2(((mb_x|mb_y)&1) == 0);
  936. +
  937. + index= (p0 + 31*p1) & (ME_CACHE_SIZE-1);
  938. + value= s->me_cache_generation + (p0>>10) + (p1<<6) + (block->ref<<12);
  939. + if(s->me_cache[index] == value)
  940. + return 0;
  941. + s->me_cache[index]= value;
  942. +
  943. + block->mx= p0;
  944. + block->my= p1;
  945. + block->ref= ref;
  946. + block->type &= ~BLOCK_INTRA;
  947. + block[1]= block[b_stride]= block[b_stride+1]= *block;
  948. +
  949. + rd= get_4block_rd(s, mb_x, mb_y, 0);
  950. +
  951. +//FIXME chroma
  952. + if(rd < *best_rd){
  953. + *best_rd= rd;
  954. + return 1;
  955. + }else{
  956. + block[0]= backup[0];
  957. + block[1]= backup[1];
  958. + block[b_stride]= backup[2];
  959. + block[b_stride+1]= backup[3];
  960. + return 0;
  961. + }
  962. +}
  963. +
  964. +static void iterative_me(OBMCContext *s){
  965. + int pass, mb_x, mb_y;
  966. + const int b_width = s->b_width << s->block_max_depth;
  967. + const int b_height= s->b_height << s->block_max_depth;
  968. + const int b_stride= b_width;
  969. + int color[3];
  970. +
  971. + {
  972. + ObmcCoderContext r;
  973. + s->obmc_coder.init_frame_coder(s->avctx, &r);
  974. + for(mb_y= 0; mb_y<s->b_height; mb_y++)
  975. + for(mb_x= 0; mb_x<s->b_width; mb_x++)
  976. + encode_q_branch(s, 0, mb_x, mb_y);
  977. + s->obmc_coder.reset_coder(&r);
  978. + s->obmc_coder.free(&r);
  979. + }
  980. +
  981. + for(pass=0; pass<25; pass++){
  982. + int change= 0;
  983. +
  984. + for(mb_y= 0; mb_y<b_height; mb_y++){
  985. + for(mb_x= 0; mb_x<b_width; mb_x++){
  986. + int dia_change, i, j, ref;
  987. + int best_rd= INT_MAX, ref_rd;
  988. + BlockNode backup, ref_b;
  989. + const int index= mb_x + mb_y * b_stride;
  990. + BlockNode *block= &s->block[index];
  991. + BlockNode *tb = mb_y ? &s->block[index-b_stride ] : NULL;
  992. + BlockNode *lb = mb_x ? &s->block[index -1] : NULL;
  993. + BlockNode *rb = mb_x+1<b_width ? &s->block[index +1] : NULL;
  994. + BlockNode *bb = mb_y+1<b_height ? &s->block[index+b_stride ] : NULL;
  995. + BlockNode *tlb= mb_x && mb_y ? &s->block[index-b_stride-1] : NULL;
  996. + BlockNode *trb= mb_x+1<b_width && mb_y ? &s->block[index-b_stride+1] : NULL;
  997. + BlockNode *blb= mb_x && mb_y+1<b_height ? &s->block[index+b_stride-1] : NULL;
  998. + BlockNode *brb= mb_x+1<b_width && mb_y+1<b_height ? &s->block[index+b_stride+1] : NULL;
  999. + const int b_w= (MB_SIZE >> s->block_max_depth);
  1000. + uint8_t obmc_edged[MB_SIZE * 2][MB_SIZE * 2];
  1001. +
  1002. + if(pass && (block->type & BLOCK_OPT))
  1003. + continue;
  1004. + block->type |= BLOCK_OPT;
  1005. +
  1006. + backup= *block;
  1007. +
  1008. + if(!s->me_cache_generation)
  1009. + memset(s->me_cache, 0, sizeof(s->me_cache));
  1010. + s->me_cache_generation += 1<<22;
  1011. +
  1012. + //FIXME precalculate
  1013. + {
  1014. + int x, y;
  1015. + for (y = 0; y < b_w * 2; y++)
  1016. + memcpy(obmc_edged[y], ff_obmc_tab[s->block_max_depth] + y * b_w * 2, b_w * 2);
  1017. + if(mb_x==0)
  1018. + for(y=0; y<b_w*2; y++)
  1019. + memset(obmc_edged[y], obmc_edged[y][0] + obmc_edged[y][b_w-1], b_w);
  1020. + if(mb_x==b_stride-1)
  1021. + for(y=0; y<b_w*2; y++)
  1022. + memset(obmc_edged[y]+b_w, obmc_edged[y][b_w] + obmc_edged[y][b_w*2-1], b_w);
  1023. + if(mb_y==0){
  1024. + for(x=0; x<b_w*2; x++)
  1025. + obmc_edged[0][x] += obmc_edged[b_w-1][x];
  1026. + for(y=1; y<b_w; y++)
  1027. + memcpy(obmc_edged[y], obmc_edged[0], b_w*2);
  1028. + }
  1029. + if(mb_y==b_height-1){
  1030. + for(x=0; x<b_w*2; x++)
  1031. + obmc_edged[b_w*2-1][x] += obmc_edged[b_w][x];
  1032. + for(y=b_w; y<b_w*2-1; y++)
  1033. + memcpy(obmc_edged[y], obmc_edged[b_w*2-1], b_w*2);
  1034. + }
  1035. + }
  1036. +
  1037. + //skip stuff outside the picture
  1038. + if(mb_x==0 || mb_y==0 || mb_x==b_width-1 || mb_y==b_height-1){
  1039. + uint8_t *src= s-> input_picture->data[0];
  1040. + uint8_t *dst= s->current_picture->data[0];
  1041. + const int stride= s->current_picture->linesize[0];
  1042. + const int block_w= MB_SIZE >> s->block_max_depth;
  1043. + const int block_h= MB_SIZE >> s->block_max_depth;
  1044. + const int sx= block_w*mb_x - block_w/2;
  1045. + const int sy= block_h*mb_y - block_h/2;
  1046. + const int w= s->plane[0].width;
  1047. + const int h= s->plane[0].height;
  1048. + int y;
  1049. +
  1050. + for(y=sy; y<0; y++)
  1051. + memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
  1052. + for(y=h; y<sy+block_h*2; y++)
  1053. + memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
  1054. + if(sx<0){
  1055. + for(y=sy; y<sy+block_h*2; y++)
  1056. + memcpy(dst + sx + y*stride, src + sx + y*stride, -sx);
  1057. + }
  1058. + if(sx+block_w*2 > w){
  1059. + for(y=sy; y<sy+block_h*2; y++)
  1060. + memcpy(dst + w + y*stride, src + w + y*stride, sx+block_w*2 - w);
  1061. + }
  1062. + }
  1063. +
  1064. + // intra(black) = neighbors' contribution to the current block
  1065. + for(i=0; i < s->nb_planes; i++)
  1066. + color[i]= get_dc(s, mb_x, mb_y, i);
  1067. +
  1068. + // get previous score (cannot be cached due to OBMC)
  1069. + if(pass > 0 && (block->type&BLOCK_INTRA)){
  1070. + int color0[3]= {block->color[0], block->color[1], block->color[2]};
  1071. + check_block(s, mb_x, mb_y, color0, 1, obmc_edged, &best_rd);
  1072. + }else
  1073. + check_block_inter(s, mb_x, mb_y, block->mx, block->my, obmc_edged, &best_rd);
  1074. +
  1075. + ref_b= *block;
  1076. + ref_rd= best_rd;
  1077. + for(ref=0; ref < s->ref_frames; ref++){
  1078. + int16_t (*mvr)[2]= &s->ref_mvs[ref][index];
  1079. + if(s->ref_scores[ref][index] > s->ref_scores[ref_b.ref][index]*3/2) //FIXME tune threshold
  1080. + continue;
  1081. + block->ref= ref;
  1082. + best_rd= INT_MAX;
  1083. +
  1084. + check_block_inter(s, mb_x, mb_y, mvr[0][0], mvr[0][1], obmc_edged, &best_rd);
  1085. + check_block_inter(s, mb_x, mb_y, 0, 0, obmc_edged, &best_rd);
  1086. + if(tb)
  1087. + check_block_inter(s, mb_x, mb_y, mvr[-b_stride][0], mvr[-b_stride][1], obmc_edged, &best_rd);
  1088. + if(lb)
  1089. + check_block_inter(s, mb_x, mb_y, mvr[-1][0], mvr[-1][1], obmc_edged, &best_rd);
  1090. + if(rb)
  1091. + check_block_inter(s, mb_x, mb_y, mvr[1][0], mvr[1][1], obmc_edged, &best_rd);
  1092. + if(bb)
  1093. + check_block_inter(s, mb_x, mb_y, mvr[b_stride][0], mvr[b_stride][1], obmc_edged, &best_rd);
  1094. +
  1095. + /* fullpel ME */
  1096. + //FIXME avoid subpel interpolation / round to nearest integer
  1097. + do{
  1098. + int newx = block->mx;
  1099. + int newy = block->my;
  1100. + int dia_size = s->iterative_dia_size ? s->iterative_dia_size : FFMAX(s->avctx->dia_size, 1);
  1101. + dia_change=0;
  1102. + for(i=0; i < dia_size; i++){
  1103. + for(j=0; j<i; j++){
  1104. + dia_change |= check_block_inter(s, mb_x, mb_y, newx+4*(i-j), newy+(4*j), obmc_edged, &best_rd);
  1105. + dia_change |= check_block_inter(s, mb_x, mb_y, newx-4*(i-j), newy-(4*j), obmc_edged, &best_rd);
  1106. + dia_change |= check_block_inter(s, mb_x, mb_y, newx-(4*j), newy+4*(i-j), obmc_edged, &best_rd);
  1107. + dia_change |= check_block_inter(s, mb_x, mb_y, newx+(4*j), newy-4*(i-j), obmc_edged, &best_rd);
  1108. + }
  1109. + }
  1110. + }while(dia_change);
  1111. + /* subpel ME */
  1112. + do{
  1113. + static const int square[8][2]= {{+1, 0},{-1, 0},{ 0,+1},{ 0,-1},{+1,+1},{-1,-1},{+1,-1},{-1,+1},};
  1114. + dia_change=0;
  1115. + for(i=0; i<8; i++)
  1116. + dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+square[i][0], block->my+square[i][1], obmc_edged, &best_rd);
  1117. + }while(dia_change);
  1118. + //FIXME or try the standard 2 pass qpel or similar
  1119. +
  1120. + mvr[0][0]= block->mx;
  1121. + mvr[0][1]= block->my;
  1122. + if(ref_rd > best_rd){
  1123. + ref_rd= best_rd;
  1124. + ref_b= *block;
  1125. + }
  1126. + }
  1127. + best_rd= ref_rd;
  1128. + *block= ref_b;
  1129. + check_block(s, mb_x, mb_y, color, 1, obmc_edged, &best_rd);
  1130. + //FIXME RD style color selection
  1131. + if(!same_block(block, &backup)){
  1132. + if(tb ) tb ->type &= ~BLOCK_OPT;
  1133. + if(lb ) lb ->type &= ~BLOCK_OPT;
  1134. + if(rb ) rb ->type &= ~BLOCK_OPT;
  1135. + if(bb ) bb ->type &= ~BLOCK_OPT;
  1136. + if(tlb) tlb->type &= ~BLOCK_OPT;
  1137. + if(trb) trb->type &= ~BLOCK_OPT;
  1138. + if(blb) blb->type &= ~BLOCK_OPT;
  1139. + if(brb) brb->type &= ~BLOCK_OPT;
  1140. + change ++;
  1141. + }
  1142. + }
  1143. + }
  1144. + av_log(s->avctx, AV_LOG_DEBUG, "pass:%d changed:%d\n", pass, change);
  1145. + if(!change)
  1146. + break;
  1147. + }
  1148. +
  1149. + if(s->block_max_depth == 1){
  1150. + int change= 0;
  1151. + for(mb_y= 0; mb_y<b_height; mb_y+=2){
  1152. + for(mb_x= 0; mb_x<b_width; mb_x+=2){
  1153. + int i;
  1154. + int best_rd, init_rd;
  1155. + const int index= mb_x + mb_y * b_stride;
  1156. + BlockNode *b[4];
  1157. +
  1158. + b[0]= &s->block[index];
  1159. + b[1]= b[0]+1;
  1160. + b[2]= b[0]+b_stride;
  1161. + b[3]= b[2]+1;
  1162. + if(same_block(b[0], b[1]) &&
  1163. + same_block(b[0], b[2]) &&
  1164. + same_block(b[0], b[3]))
  1165. + continue;
  1166. +
  1167. + if(!s->me_cache_generation)
  1168. + memset(s->me_cache, 0, sizeof(s->me_cache));
  1169. + s->me_cache_generation += 1<<22;
  1170. +
  1171. + init_rd= best_rd= get_4block_rd(s, mb_x, mb_y, 0);
  1172. +
  1173. + //FIXME more multiref search?
  1174. + check_4block_inter(s, mb_x, mb_y,
  1175. + (b[0]->mx + b[1]->mx + b[2]->mx + b[3]->mx + 2) >> 2,
  1176. + (b[0]->my + b[1]->my + b[2]->my + b[3]->my + 2) >> 2, 0, &best_rd);
  1177. +
  1178. + for(i=0; i<4; i++)
  1179. + if(!(b[i]->type&BLOCK_INTRA))
  1180. + check_4block_inter(s, mb_x, mb_y, b[i]->mx, b[i]->my, b[i]->ref, &best_rd);
  1181. +
  1182. + if(init_rd != best_rd)
  1183. + change++;
  1184. + }
  1185. + }
  1186. + av_log(s->avctx, AV_LOG_DEBUG, "pass:4mv changed:%d\n", change*4);
  1187. + }
  1188. +}
  1189. +
  1190. +static void encode_blocks(OBMCContext *s, int search){
  1191. + int x, y;
  1192. + int w= s->b_width;
  1193. + int h= s->b_height;
  1194. + ObmcCoderContext *const c = &s->obmc_coder;
  1195. +
  1196. + if(s->motion_est == FF_ME_ITER && !s->keyframe && search)
  1197. + iterative_me(s);
  1198. +
  1199. + for(y=0; y<h; y++){
  1200. + if(c->available_bytes(c) < w*MB_SIZE*MB_SIZE*3){ //FIXME nicer limit
  1201. + av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  1202. + return;
  1203. + }
  1204. + for(x=0; x<w; x++){
  1205. + if(s->motion_est == FF_ME_ITER || !search)
  1206. + encode_q_branch2(s, 0, x, y);
  1207. + else
  1208. + encode_q_branch (s, 0, x, y);
  1209. + }
  1210. + }
  1211. +}
  1212. +
  1213. +int ff_obmc_pre_encode_frame(OBMCContext *s, AVCodecContext *avctx, const AVFrame *pict)
  1214. +{
  1215. + const int width= s->avctx->width;
  1216. + const int height= s->avctx->height;
  1217. +
  1218. + int ret;
  1219. + if (s->current_picture->data[0]
  1220. + #if FF_API_EMU_EDGE
  1221. + && !(s->avctx->flags&CODEC_FLAG_EMU_EDGE)
  1222. + #endif
  1223. + ) {
  1224. + int w = s->avctx->width;
  1225. + int h = s->avctx->height;
  1226. +
  1227. + s->mpvencdsp.draw_edges(s->current_picture->data[0],
  1228. + s->current_picture->linesize[0], w , h ,
  1229. + EDGE_WIDTH , EDGE_WIDTH , EDGE_TOP | EDGE_BOTTOM);
  1230. + if (s->current_picture->data[2]) {
  1231. + s->mpvencdsp.draw_edges(s->current_picture->data[1],
  1232. + s->current_picture->linesize[1], w>>s->chroma_h_shift, h>>s->chroma_v_shift,
  1233. + EDGE_WIDTH>>s->chroma_h_shift, EDGE_WIDTH>>s->chroma_v_shift, EDGE_TOP | EDGE_BOTTOM);
  1234. + s->mpvencdsp.draw_edges(s->current_picture->data[2],
  1235. + s->current_picture->linesize[2], w>>s->chroma_h_shift, h>>s->chroma_v_shift,
  1236. + EDGE_WIDTH>>s->chroma_h_shift, EDGE_WIDTH>>s->chroma_v_shift, EDGE_TOP | EDGE_BOTTOM);
  1237. + }
  1238. + }
  1239. +
  1240. + if ((ret = ff_obmc_frame_start(s)) < 0)
  1241. + return ret;
  1242. +
  1243. +#if FF_API_CODED_FRAME
  1244. +FF_DISABLE_DEPRECATION_WARNINGS
  1245. + av_frame_unref(avctx->coded_frame);
  1246. + ret = av_frame_ref(avctx->coded_frame, s->current_picture);
  1247. + if (ret < 0)
  1248. + return ret;
  1249. +FF_ENABLE_DEPRECATION_WARNINGS
  1250. +#endif
  1251. +
  1252. + s->m.current_picture_ptr= &s->m.current_picture;
  1253. + s->m.current_picture.f = s->current_picture;
  1254. + s->m.current_picture.f->pts = pict->pts;
  1255. + if(s->m.pict_type == AV_PICTURE_TYPE_P){
  1256. + int block_width = (width +15)>>4;
  1257. + int block_height= (height+15)>>4;
  1258. + int stride= s->current_picture->linesize[0];
  1259. +
  1260. + av_assert0(s->current_picture->data[0]);
  1261. + av_assert0(s->last_pictures[0]->data[0]);
  1262. +
  1263. + s->m.avctx= s->avctx;
  1264. + s->m.last_picture.f = s->last_pictures[0];
  1265. + s->m.new_picture.f = s->input_picture;
  1266. + s->m.last_picture_ptr= &s->m.last_picture;
  1267. + s->m.linesize = stride;
  1268. + s->m.uvlinesize= s->current_picture->linesize[1];
  1269. + s->m.width = width;
  1270. + s->m.height= height;
  1271. + s->m.mb_width = block_width;
  1272. + s->m.mb_height= block_height;
  1273. + s->m.mb_stride= s->m.mb_width+1;
  1274. + s->m.b8_stride= 2*s->m.mb_width+1;
  1275. + s->m.f_code=1;
  1276. + /* s->m.pict_type = pic->pict_type; // DELETED */
  1277. +#if FF_API_MOTION_EST
  1278. +FF_DISABLE_DEPRECATION_WARNINGS
  1279. + s->m.me_method= s->avctx->me_method;
  1280. +FF_ENABLE_DEPRECATION_WARNINGS
  1281. +#endif
  1282. + s->m.motion_est= s->motion_est;
  1283. + s->m.me.scene_change_score=0;
  1284. + s->m.me.dia_size = avctx->dia_size;
  1285. + s->m.quarter_sample= (s->avctx->flags & AV_CODEC_FLAG_QPEL)!=0;
  1286. + s->m.out_format= FMT_H263;
  1287. + s->m.unrestricted_mv= 1;
  1288. +
  1289. + s->m.lambda = s->lambda;
  1290. + s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
  1291. + s->lambda2= s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
  1292. +
  1293. + s->m.mecc= s->mecc; //move
  1294. + s->m.qdsp= s->qdsp; //move
  1295. + s->m.hdsp = s->hdsp;
  1296. + ff_init_me(&s->m);
  1297. + s->hdsp = s->m.hdsp;
  1298. + s->mecc= s->m.mecc;
  1299. + }
  1300. +
  1301. + return 0;
  1302. +}
  1303. +
  1304. +void ff_obmc_encode_blocks(OBMCContext *s, int search)
  1305. +{
  1306. + encode_blocks(s, search);
  1307. +}
  1308. diff --git a/libavcodec/obme.h b/libavcodec/obme.h
  1309. new file mode 100644
  1310. index 0000000..08b4760
  1311. --- /dev/null
  1312. +++ b/libavcodec/obme.h
  1313. @@ -0,0 +1,58 @@
  1314. +/*
  1315. + * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
  1316. + *
  1317. + * This file is part of FFmpeg.
  1318. + *
  1319. + * FFmpeg is free software; you can redistribute it and/or
  1320. + * modify it under the terms of the GNU Lesser General Public
  1321. + * License as published by the Free Software Foundation; either
  1322. + * version 2.1 of the License, or (at your option) any later version.
  1323. + *
  1324. + * FFmpeg is distributed in the hope that it will be useful,
  1325. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1326. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1327. + * Lesser General Public License for more details.
  1328. + *
  1329. + * You should have received a copy of the GNU Lesser General Public
  1330. + * License along with FFmpeg; if not, write to the Free Software
  1331. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  1332. + */
  1333. +
  1334. + /**
  1335. + * @file obme.h
  1336. + * @brief Overlapped block motion estimation functions
  1337. + */
  1338. +
  1339. +#ifndef AVCODEC_OBME_H
  1340. +#define AVCODEC_OBME_H
  1341. +
  1342. +#include "obmemc.h"
  1343. +
  1344. +#define FF_ME_ITER 50
  1345. +
  1346. + /**
  1347. + * Inits OBMC context parameters needed for encoding process
  1348. + *
  1349. + * @param[in,out] s OBMC context to init
  1350. + * @param[in] avctx Codec context to retrieve some initial values
  1351. + */
  1352. +int ff_obmc_encode_init(OBMCContext *s, AVCodecContext *avctx);
  1353. +
  1354. + /**
  1355. + * Prepares OBMC context for block encoding for each frame
  1356. + *
  1357. + * @param[in,out] f OBMC context to prepare
  1358. + * @param[in] avctx Codec context to retrieve some required values
  1359. + * @param[in] pict Frame to encode
  1360. + */
  1361. +int ff_obmc_pre_encode_frame(OBMCContext *f, AVCodecContext *avctx, const AVFrame *pict);
  1362. +
  1363. + /**
  1364. + * Starts encoding blocks
  1365. + *
  1366. + * @param[in,out] s OBMC context
  1367. + * @param[in] search Defines if reference blocks should be searched for
  1368. + */
  1369. +void ff_obmc_encode_blocks(OBMCContext *s, int search);
  1370. +
  1371. +#endif /* AVCODEC_OBME_H */
  1372. diff --git a/libavcodec/obmemc.c b/libavcodec/obmemc.c
  1373. new file mode 100644
  1374. index 0000000..314b1b3
  1375. --- /dev/null
  1376. +++ b/libavcodec/obmemc.c
  1377. @@ -0,0 +1,651 @@
  1378. +/*
  1379. + * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
  1380. + *
  1381. + * This file is part of FFmpeg.
  1382. + *
  1383. + * FFmpeg is free software; you can redistribute it and/or
  1384. + * modify it under the terms of the GNU Lesser General Public
  1385. + * License as published by the Free Software Foundation; either
  1386. + * version 2.1 of the License, or (at your option) any later version.
  1387. + *
  1388. + * FFmpeg is distributed in the hope that it will be useful,
  1389. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1390. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1391. + * Lesser General Public License for more details.
  1392. + *
  1393. + * You should have received a copy of the GNU Lesser General Public
  1394. + * License along with FFmpeg; if not, write to the Free Software
  1395. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  1396. + */
  1397. +
  1398. +#include "obmemc.h"
  1399. +#include "obmemc_data.h"
  1400. +
  1401. +int ff_obmc_get_buffer(OBMCContext *s, AVFrame *frame)
  1402. +{
  1403. + int ret, i;
  1404. + int edges_needed = av_codec_is_encoder(s->avctx->codec);
  1405. +
  1406. + frame->width = s->avctx->width ;
  1407. + frame->height = s->avctx->height;
  1408. + if (edges_needed) {
  1409. + frame->width += 2 * EDGE_WIDTH;
  1410. + frame->height += 2 * EDGE_WIDTH;
  1411. + }
  1412. + if ((ret = ff_get_buffer(s->avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
  1413. + return ret;
  1414. + if (edges_needed) {
  1415. + for (i = 0; frame->data[i]; i++) {
  1416. + int offset = (EDGE_WIDTH >> (i ? s->chroma_v_shift : 0)) *
  1417. + frame->linesize[i] +
  1418. + (EDGE_WIDTH >> (i ? s->chroma_h_shift : 0));
  1419. + frame->data[i] += offset;
  1420. + }
  1421. + frame->width = s->avctx->width;
  1422. + frame->height = s->avctx->height;
  1423. + }
  1424. +
  1425. + return 0;
  1426. +}
  1427. +
  1428. +int ff_obmc_alloc_blocks(OBMCContext *s)
  1429. +{
  1430. + int w= AV_CEIL_RSHIFT(s->avctx->width, LOG2_MB_SIZE);
  1431. + int h= AV_CEIL_RSHIFT(s->avctx->height, LOG2_MB_SIZE);
  1432. +
  1433. + s->b_width = w;
  1434. + s->b_height= h;
  1435. +
  1436. + av_free(s->block);
  1437. + s->block= av_mallocz_array(w * h, sizeof(BlockNode) << (s->block_max_depth*2));
  1438. + if (!s->block)
  1439. + return AVERROR(ENOMEM);
  1440. +
  1441. + return 0;
  1442. +}
  1443. +
  1444. +static av_cold void init_qexp(void)
  1445. +{
  1446. + int i;
  1447. + double v=128;
  1448. +
  1449. + for(i=0; i<QROOT; i++)
  1450. + {
  1451. + ff_qexp[i]= lrintf(v);
  1452. + v *= pow(2, 1.0 / QROOT);
  1453. + }
  1454. +}
  1455. +
  1456. +static void mc_block(PlaneObmc *p, uint8_t *dst, const uint8_t *src, int stride, int b_w, int b_h, int dx, int dy){
  1457. + static const uint8_t weight[64]={
  1458. + 8,7,6,5,4,3,2,1,
  1459. + 7,7,0,0,0,0,0,1,
  1460. + 6,0,6,0,0,0,2,0,
  1461. + 5,0,0,5,0,3,0,0,
  1462. + 4,0,0,0,4,0,0,0,
  1463. + 3,0,0,5,0,3,0,0,
  1464. + 2,0,6,0,0,0,2,0,
  1465. + 1,7,0,0,0,0,0,1,
  1466. + };
  1467. +
  1468. + static const uint8_t brane[256]={
  1469. + 0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x11,0x12,0x12,0x12,0x12,0x12,0x12,0x12,
  1470. + 0x04,0x05,0xcc,0xcc,0xcc,0xcc,0xcc,0x41,0x15,0x16,0xcc,0xcc,0xcc,0xcc,0xcc,0x52,
  1471. + 0x04,0xcc,0x05,0xcc,0xcc,0xcc,0x41,0xcc,0x15,0xcc,0x16,0xcc,0xcc,0xcc,0x52,0xcc,
  1472. + 0x04,0xcc,0xcc,0x05,0xcc,0x41,0xcc,0xcc,0x15,0xcc,0xcc,0x16,0xcc,0x52,0xcc,0xcc,
  1473. + 0x04,0xcc,0xcc,0xcc,0x41,0xcc,0xcc,0xcc,0x15,0xcc,0xcc,0xcc,0x16,0xcc,0xcc,0xcc,
  1474. + 0x04,0xcc,0xcc,0x41,0xcc,0x05,0xcc,0xcc,0x15,0xcc,0xcc,0x52,0xcc,0x16,0xcc,0xcc,
  1475. + 0x04,0xcc,0x41,0xcc,0xcc,0xcc,0x05,0xcc,0x15,0xcc,0x52,0xcc,0xcc,0xcc,0x16,0xcc,
  1476. + 0x04,0x41,0xcc,0xcc,0xcc,0xcc,0xcc,0x05,0x15,0x52,0xcc,0xcc,0xcc,0xcc,0xcc,0x16,
  1477. + 0x44,0x45,0x45,0x45,0x45,0x45,0x45,0x45,0x55,0x56,0x56,0x56,0x56,0x56,0x56,0x56,
  1478. + 0x48,0x49,0xcc,0xcc,0xcc,0xcc,0xcc,0x85,0x59,0x5A,0xcc,0xcc,0xcc,0xcc,0xcc,0x96,
  1479. + 0x48,0xcc,0x49,0xcc,0xcc,0xcc,0x85,0xcc,0x59,0xcc,0x5A,0xcc,0xcc,0xcc,0x96,0xcc,
  1480. + 0x48,0xcc,0xcc,0x49,0xcc,0x85,0xcc,0xcc,0x59,0xcc,0xcc,0x5A,0xcc,0x96,0xcc,0xcc,
  1481. + 0x48,0xcc,0xcc,0xcc,0x49,0xcc,0xcc,0xcc,0x59,0xcc,0xcc,0xcc,0x96,0xcc,0xcc,0xcc,
  1482. + 0x48,0xcc,0xcc,0x85,0xcc,0x49,0xcc,0xcc,0x59,0xcc,0xcc,0x96,0xcc,0x5A,0xcc,0xcc,
  1483. + 0x48,0xcc,0x85,0xcc,0xcc,0xcc,0x49,0xcc,0x59,0xcc,0x96,0xcc,0xcc,0xcc,0x5A,0xcc,
  1484. + 0x48,0x85,0xcc,0xcc,0xcc,0xcc,0xcc,0x49,0x59,0x96,0xcc,0xcc,0xcc,0xcc,0xcc,0x5A,
  1485. + };
  1486. +
  1487. + static const uint8_t needs[16]={
  1488. + 0,1,0,0,
  1489. + 2,4,2,0,
  1490. + 0,1,0,0,
  1491. + 15
  1492. + };
  1493. +
  1494. + int x, y, b, r, l;
  1495. + int16_t tmpIt [64*(32+HTAPS_MAX)];
  1496. + uint8_t tmp2t[3][64*(32+HTAPS_MAX)];
  1497. + int16_t *tmpI= tmpIt;
  1498. + uint8_t *tmp2= tmp2t[0];
  1499. + const uint8_t *hpel[11];
  1500. + av_assert2(dx<16 && dy<16);
  1501. + r= brane[dx + 16*dy]&15;
  1502. + l= brane[dx + 16*dy]>>4;
  1503. +
  1504. + b= needs[l] | needs[r];
  1505. + if(p && !p->diag_mc)
  1506. + b= 15;
  1507. +
  1508. + if(b&5){
  1509. + for(y=0; y < b_h+HTAPS_MAX-1; y++){
  1510. + for(x=0; x < b_w; x++){
  1511. + int a_1=src[x + HTAPS_MAX/2-4];
  1512. + int a0= src[x + HTAPS_MAX/2-3];
  1513. + int a1= src[x + HTAPS_MAX/2-2];
  1514. + int a2= src[x + HTAPS_MAX/2-1];
  1515. + int a3= src[x + HTAPS_MAX/2+0];
  1516. + int a4= src[x + HTAPS_MAX/2+1];
  1517. + int a5= src[x + HTAPS_MAX/2+2];
  1518. + int a6= src[x + HTAPS_MAX/2+3];
  1519. + int am=0;
  1520. + if(!p || p->fast_mc){
  1521. + am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
  1522. + tmpI[x]= am;
  1523. + am= (am+16)>>5;
  1524. + }else{
  1525. + am= p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6);
  1526. + tmpI[x]= am;
  1527. + am= (am+32)>>6;
  1528. + }
  1529. +
  1530. + if(am&(~255)) am= ~(am>>31);
  1531. + tmp2[x]= am;
  1532. + }
  1533. + tmpI+= 64;
  1534. + tmp2+= 64;
  1535. + src += stride;
  1536. + }
  1537. + src -= stride*y;
  1538. + }
  1539. + src += HTAPS_MAX/2 - 1;
  1540. + tmp2= tmp2t[1];
  1541. +
  1542. + if(b&2){
  1543. + for(y=0; y < b_h; y++){
  1544. + for(x=0; x < b_w+1; x++){
  1545. + int a_1=src[x + (HTAPS_MAX/2-4)*stride];
  1546. + int a0= src[x + (HTAPS_MAX/2-3)*stride];
  1547. + int a1= src[x + (HTAPS_MAX/2-2)*stride];
  1548. + int a2= src[x + (HTAPS_MAX/2-1)*stride];
  1549. + int a3= src[x + (HTAPS_MAX/2+0)*stride];
  1550. + int a4= src[x + (HTAPS_MAX/2+1)*stride];
  1551. + int a5= src[x + (HTAPS_MAX/2+2)*stride];
  1552. + int a6= src[x + (HTAPS_MAX/2+3)*stride];
  1553. + int am=0;
  1554. + if(!p || p->fast_mc)
  1555. + am= (20*(a2+a3) - 5*(a1+a4) + (a0+a5) + 16)>>5;
  1556. + else
  1557. + am= (p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6) + 32)>>6;
  1558. +
  1559. + if(am&(~255)) am= ~(am>>31);
  1560. + tmp2[x]= am;
  1561. + }
  1562. + src += stride;
  1563. + tmp2+= 64;
  1564. + }
  1565. + src -= stride*y;
  1566. + }
  1567. + src += stride*(HTAPS_MAX/2 - 1);
  1568. + tmp2= tmp2t[2];
  1569. + tmpI= tmpIt;
  1570. + if(b&4){
  1571. + for(y=0; y < b_h; y++){
  1572. + for(x=0; x < b_w; x++){
  1573. + int a_1=tmpI[x + (HTAPS_MAX/2-4)*64];
  1574. + int a0= tmpI[x + (HTAPS_MAX/2-3)*64];
  1575. + int a1= tmpI[x + (HTAPS_MAX/2-2)*64];
  1576. + int a2= tmpI[x + (HTAPS_MAX/2-1)*64];
  1577. + int a3= tmpI[x + (HTAPS_MAX/2+0)*64];
  1578. + int a4= tmpI[x + (HTAPS_MAX/2+1)*64];
  1579. + int a5= tmpI[x + (HTAPS_MAX/2+2)*64];
  1580. + int a6= tmpI[x + (HTAPS_MAX/2+3)*64];
  1581. + int am=0;
  1582. + if(!p || p->fast_mc)
  1583. + am= (20*(a2+a3) - 5*(a1+a4) + (a0+a5) + 512)>>10;
  1584. + else
  1585. + am= (p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6) + 2048)>>12;
  1586. + if(am&(~255)) am= ~(am>>31);
  1587. + tmp2[x]= am;
  1588. + }
  1589. + tmpI+= 64;
  1590. + tmp2+= 64;
  1591. + }
  1592. + }
  1593. +
  1594. + hpel[ 0]= src;
  1595. + hpel[ 1]= tmp2t[0] + 64*(HTAPS_MAX/2-1);
  1596. + hpel[ 2]= src + 1;
  1597. +
  1598. + hpel[ 4]= tmp2t[1];
  1599. + hpel[ 5]= tmp2t[2];
  1600. + hpel[ 6]= tmp2t[1] + 1;
  1601. +
  1602. + hpel[ 8]= src + stride;
  1603. + hpel[ 9]= hpel[1] + 64;
  1604. + hpel[10]= hpel[8] + 1;
  1605. +
  1606. +#define MC_STRIDE(x) (needs[x] ? 64 : stride)
  1607. +
  1608. + if(b==15){
  1609. + int dxy = dx / 8 + dy / 8 * 4;
  1610. + const uint8_t *src1 = hpel[dxy ];
  1611. + const uint8_t *src2 = hpel[dxy + 1];
  1612. + const uint8_t *src3 = hpel[dxy + 4];
  1613. + const uint8_t *src4 = hpel[dxy + 5];
  1614. + int stride1 = MC_STRIDE(dxy);
  1615. + int stride2 = MC_STRIDE(dxy + 1);
  1616. + int stride3 = MC_STRIDE(dxy + 4);
  1617. + int stride4 = MC_STRIDE(dxy + 5);
  1618. + dx&=7;
  1619. + dy&=7;
  1620. + for(y=0; y < b_h; y++){
  1621. + for(x=0; x < b_w; x++){
  1622. + dst[x]= ((8-dx)*(8-dy)*src1[x] + dx*(8-dy)*src2[x]+
  1623. + (8-dx)* dy *src3[x] + dx* dy *src4[x]+32)>>6;
  1624. + }
  1625. + src1+=stride1;
  1626. + src2+=stride2;
  1627. + src3+=stride3;
  1628. + src4+=stride4;
  1629. + dst +=stride;
  1630. + }
  1631. + }else{
  1632. + const uint8_t *src1= hpel[l];
  1633. + const uint8_t *src2= hpel[r];
  1634. + int stride1 = MC_STRIDE(l);
  1635. + int stride2 = MC_STRIDE(r);
  1636. + int a= weight[((dx&7) + (8*(dy&7)))];
  1637. + int b= 8-a;
  1638. + for(y=0; y < b_h; y++){
  1639. + for(x=0; x < b_w; x++){
  1640. + dst[x]= (a*src1[x] + b*src2[x] + 4)>>3;
  1641. + }
  1642. + src1+=stride1;
  1643. + src2+=stride2;
  1644. + dst +=stride;
  1645. + }
  1646. + }
  1647. +}
  1648. +
  1649. +void ff_obmc_pred_block(OBMCContext *s, uint8_t *dst, uint8_t *tmp, ptrdiff_t stride,
  1650. + int sx, int sy, int b_w, int b_h, const BlockNode *block,
  1651. + int plane_index, int w, int h)
  1652. +{
  1653. + if(block->type & BLOCK_INTRA){
  1654. + int x, y;
  1655. + const unsigned color = block->color[plane_index];
  1656. + const unsigned color4 = color*0x01010101;
  1657. + if(b_w==32){
  1658. + for(y=0; y < b_h; y++){
  1659. + *(uint32_t*)&dst[0 + y*stride]= color4;
  1660. + *(uint32_t*)&dst[4 + y*stride]= color4;
  1661. + *(uint32_t*)&dst[8 + y*stride]= color4;
  1662. + *(uint32_t*)&dst[12+ y*stride]= color4;
  1663. + *(uint32_t*)&dst[16+ y*stride]= color4;
  1664. + *(uint32_t*)&dst[20+ y*stride]= color4;
  1665. + *(uint32_t*)&dst[24+ y*stride]= color4;
  1666. + *(uint32_t*)&dst[28+ y*stride]= color4;
  1667. + }
  1668. + }else if(b_w==16){
  1669. + for(y=0; y < b_h; y++){
  1670. + *(uint32_t*)&dst[0 + y*stride]= color4;
  1671. + *(uint32_t*)&dst[4 + y*stride]= color4;
  1672. + *(uint32_t*)&dst[8 + y*stride]= color4;
  1673. + *(uint32_t*)&dst[12+ y*stride]= color4;
  1674. + }
  1675. + }else if(b_w==8){
  1676. + for(y=0; y < b_h; y++){
  1677. + *(uint32_t*)&dst[0 + y*stride]= color4;
  1678. + *(uint32_t*)&dst[4 + y*stride]= color4;
  1679. + }
  1680. + }else if(b_w==4){
  1681. + for(y=0; y < b_h; y++){
  1682. + *(uint32_t*)&dst[0 + y*stride]= color4;
  1683. + }
  1684. + }else{
  1685. + for(y=0; y < b_h; y++){
  1686. + for(x=0; x < b_w; x++){
  1687. + dst[x + y*stride]= color;
  1688. + }
  1689. + }
  1690. + }
  1691. + }else{
  1692. + uint8_t *src= s->last_pictures[block->ref]->data[plane_index];
  1693. + const int scale= plane_index ? (2*s->mv_scale)>>s->chroma_h_shift : 2*s->mv_scale;
  1694. + int mx= block->mx*scale;
  1695. + int my= block->my*scale;
  1696. + const int dx= mx&15;
  1697. + const int dy= my&15;
  1698. + const int tab_index= 3 - (b_w>>2) + (b_w>>4);
  1699. + sx += (mx>>4) - (HTAPS_MAX/2-1);
  1700. + sy += (my>>4) - (HTAPS_MAX/2-1);
  1701. + src += sx + sy*stride;
  1702. + if( (unsigned)sx >= FFMAX(w - b_w - (HTAPS_MAX-2), 0)
  1703. + || (unsigned)sy >= FFMAX(h - b_h - (HTAPS_MAX-2), 0)){
  1704. + s->vdsp.emulated_edge_mc(tmp + MB_SIZE, src,
  1705. + stride, stride,
  1706. + b_w+HTAPS_MAX-1, b_h+HTAPS_MAX-1,
  1707. + sx, sy, w, h);
  1708. + src= tmp + MB_SIZE;
  1709. + }
  1710. +
  1711. + av_assert2(s->chroma_h_shift == s->chroma_v_shift); // only one mv_scale
  1712. +
  1713. + av_assert2((tab_index>=0 && tab_index<4) || b_w==32);
  1714. + if( (dx&3) || (dy&3)
  1715. + || !(b_w == b_h || 2*b_w == b_h || b_w == 2*b_h)
  1716. + || (b_w&(b_w-1))
  1717. + || b_w == 1
  1718. + || b_h == 1
  1719. + || !s->plane[plane_index].fast_mc )
  1720. + mc_block(&s->plane[plane_index], dst, src, stride, b_w, b_h, dx, dy);
  1721. + else if(b_w==32){
  1722. + int y;
  1723. + for(y=0; y<b_h; y+=16){
  1724. + s->h264qpel.put_h264_qpel_pixels_tab[0][dy+(dx>>2)](dst + y*stride, src + 3 + (y+3)*stride,stride);
  1725. + s->h264qpel.put_h264_qpel_pixels_tab[0][dy+(dx>>2)](dst + 16 + y*stride, src + 19 + (y+3)*stride,stride);
  1726. + }
  1727. + }else if(b_w==b_h)
  1728. + s->h264qpel.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst,src + 3 + 3*stride,stride);
  1729. + else if(b_w==2*b_h){
  1730. + s->h264qpel.put_h264_qpel_pixels_tab[tab_index+1][dy+(dx>>2)](dst ,src + 3 + 3*stride,stride);
  1731. + s->h264qpel.put_h264_qpel_pixels_tab[tab_index+1][dy+(dx>>2)](dst+b_h,src + 3 + b_h + 3*stride,stride);
  1732. + }else{
  1733. + av_assert2(2*b_w==b_h);
  1734. + s->h264qpel.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst ,src + 3 + 3*stride ,stride);
  1735. + s->h264qpel.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst+b_w*stride,src + 3 + 3*stride+b_w*stride,stride);
  1736. + }
  1737. + }
  1738. +}
  1739. +
  1740. +#define mca(dx,dy,b_w)\
  1741. +static void mc_block_hpel ## dx ## dy ## b_w(uint8_t *dst, const uint8_t *src, ptrdiff_t stride, int h){\
  1742. + av_assert2(h==b_w);\
  1743. + mc_block(NULL, dst, src-(HTAPS_MAX/2-1)-(HTAPS_MAX/2-1)*stride, stride, b_w, b_w, dx, dy);\
  1744. +}
  1745. +
  1746. +mca( 0, 0,16)
  1747. +mca( 8, 0,16)
  1748. +mca( 0, 8,16)
  1749. +mca( 8, 8,16)
  1750. +mca( 0, 0,8)
  1751. +mca( 8, 0,8)
  1752. +mca( 0, 8,8)
  1753. +mca( 8, 8,8)
  1754. +
  1755. +av_cold int ff_obmc_common_init(OBMCContext *s, AVCodecContext *avctx)
  1756. +{
  1757. + s->avctx = avctx;
  1758. +
  1759. + s->obmc_coder.priv_data = NULL;
  1760. + s->obmc_coder.avctx = NULL;
  1761. +
  1762. + int width, height, i, j;
  1763. +
  1764. + width = avctx->width;
  1765. + height = avctx->height;
  1766. +
  1767. + ff_me_cmp_init(&s->mecc, avctx);
  1768. + ff_hpeldsp_init(&s->hdsp, avctx->flags);
  1769. + ff_videodsp_init(&s->vdsp, 8);
  1770. + ff_dwt_init(&s->dwt);
  1771. + ff_h264qpel_init(&s->h264qpel, 8);
  1772. +
  1773. + s->max_ref_frames = 1; //just make sure it's not an invalid value in case of no initial keyframe
  1774. +
  1775. +#define mcf(dx,dy)\
  1776. + s->qdsp.put_qpel_pixels_tab [0][dy+dx/4]=\
  1777. + s->qdsp.put_no_rnd_qpel_pixels_tab[0][dy+dx/4]=\
  1778. + s->h264qpel.put_h264_qpel_pixels_tab[0][dy+dx/4];\
  1779. + s->qdsp.put_qpel_pixels_tab [1][dy+dx/4]=\
  1780. + s->qdsp.put_no_rnd_qpel_pixels_tab[1][dy+dx/4]=\
  1781. + s->h264qpel.put_h264_qpel_pixels_tab[1][dy+dx/4];
  1782. +
  1783. + mcf( 0, 0)
  1784. + mcf( 4, 0)
  1785. + mcf( 8, 0)
  1786. + mcf(12, 0)
  1787. + mcf( 0, 4)
  1788. + mcf( 4, 4)
  1789. + mcf( 8, 4)
  1790. + mcf(12, 4)
  1791. + mcf( 0, 8)
  1792. + mcf( 4, 8)
  1793. + mcf( 8, 8)
  1794. + mcf(12, 8)
  1795. + mcf( 0,12)
  1796. + mcf( 4,12)
  1797. + mcf( 8,12)
  1798. + mcf(12,12)
  1799. +
  1800. +#define mcfh(dx,dy)\
  1801. + s->hdsp.put_pixels_tab [0][dy/4+dx/8]=\
  1802. + s->hdsp.put_no_rnd_pixels_tab[0][dy/4+dx/8]=\
  1803. + mc_block_hpel ## dx ## dy ## 16;\
  1804. + s->hdsp.put_pixels_tab [1][dy/4+dx/8]=\
  1805. + s->hdsp.put_no_rnd_pixels_tab[1][dy/4+dx/8]=\
  1806. + mc_block_hpel ## dx ## dy ## 8;
  1807. +
  1808. + mcfh(0, 0)
  1809. + mcfh(8, 0)
  1810. + mcfh(0, 8)
  1811. + mcfh(8, 8)
  1812. +
  1813. + init_qexp();
  1814. +
  1815. + FF_ALLOCZ_ARRAY_OR_GOTO(avctx, s->spatial_idwt_buffer, width, height * sizeof(IDWTELEM), fail);
  1816. +
  1817. + for(i=0; i<MAX_REF_FRAMES; i++) {
  1818. + for(j=0; j<MAX_REF_FRAMES; j++)
  1819. + ff_scale_mv_ref[i][j] = 256*(i+1)/(j+1);
  1820. + s->last_pictures[i] = av_frame_alloc();
  1821. + if (!s->last_pictures[i])
  1822. + goto fail;
  1823. + }
  1824. +
  1825. + s->mconly_picture = av_frame_alloc();
  1826. + s->current_picture = av_frame_alloc();
  1827. + if (!s->mconly_picture || !s->current_picture)
  1828. + goto fail;
  1829. +
  1830. + return 0;
  1831. +fail:
  1832. + return AVERROR(ENOMEM);
  1833. +}
  1834. +
  1835. +int ff_obmc_common_init_after_header(OBMCContext *s)
  1836. +{
  1837. + int plane_index;
  1838. + int ret, emu_buf_size;
  1839. +
  1840. + if(!s->scratchbuf) {
  1841. + if ((ret = ff_get_buffer(s->avctx, s->mconly_picture, AV_GET_BUFFER_FLAG_REF)) < 0)
  1842. + return ret;
  1843. + FF_ALLOCZ_ARRAY_OR_GOTO(s->avctx, s->scratchbuf, FFMAX(s->mconly_picture->linesize[0], 2*s->avctx->width+256), 7*MB_SIZE, fail);
  1844. + emu_buf_size = FFMAX(s->mconly_picture->linesize[0], 2*s->avctx->width+256) * (2 * MB_SIZE + HTAPS_MAX - 1);
  1845. + FF_ALLOC_OR_GOTO(s->avctx, s->emu_edge_buffer, emu_buf_size, fail);
  1846. + }
  1847. +
  1848. + if(s->mconly_picture->format != s->avctx->pix_fmt) {
  1849. + av_log(s->avctx, AV_LOG_ERROR, "pixel format changed\n");
  1850. + return AVERROR_INVALIDDATA;
  1851. + }
  1852. +
  1853. + for(plane_index=0; plane_index < s->nb_planes; plane_index++){
  1854. + int w= s->avctx->width;
  1855. + int h= s->avctx->height;
  1856. +
  1857. + if(plane_index){
  1858. + w = AV_CEIL_RSHIFT(w, s->chroma_h_shift);
  1859. + h = AV_CEIL_RSHIFT(h, s->chroma_v_shift);
  1860. + }
  1861. + s->plane[plane_index].width = w;
  1862. + s->plane[plane_index].height= h;
  1863. + }
  1864. +
  1865. + return 0;
  1866. +fail:
  1867. + return AVERROR(ENOMEM);
  1868. +}
  1869. +
  1870. +static int halfpel_interpol(OBMCContext *s, uint8_t *halfpel[4][4], AVFrame *frame)
  1871. +{
  1872. + int p,x,y;
  1873. +
  1874. + for(p=0; p < s->nb_planes; p++){
  1875. + int is_chroma= !!p;
  1876. + int w= is_chroma ? AV_CEIL_RSHIFT(s->avctx->width, s->chroma_h_shift) : s->avctx->width;
  1877. + int h= is_chroma ? AV_CEIL_RSHIFT(s->avctx->height, s->chroma_v_shift) : s->avctx->height;
  1878. + int ls= frame->linesize[p];
  1879. + uint8_t *src= frame->data[p];
  1880. +
  1881. + halfpel[1][p] = av_malloc_array(ls, (h + 2 * EDGE_WIDTH));
  1882. + halfpel[2][p] = av_malloc_array(ls, (h + 2 * EDGE_WIDTH));
  1883. + halfpel[3][p] = av_malloc_array(ls, (h + 2 * EDGE_WIDTH));
  1884. + if (!halfpel[1][p] || !halfpel[2][p] || !halfpel[3][p]) {
  1885. + av_freep(&halfpel[1][p]);
  1886. + av_freep(&halfpel[2][p]);
  1887. + av_freep(&halfpel[3][p]);
  1888. + return AVERROR(ENOMEM);
  1889. + }
  1890. + halfpel[1][p] += EDGE_WIDTH * (1 + ls);
  1891. + halfpel[2][p] += EDGE_WIDTH * (1 + ls);
  1892. + halfpel[3][p] += EDGE_WIDTH * (1 + ls);
  1893. +
  1894. + halfpel[0][p]= src;
  1895. + for(y=0; y<h; y++){
  1896. + for(x=0; x<w; x++){
  1897. + int i= y*ls + x;
  1898. +
  1899. + halfpel[1][p][i]= (
  1900. + 20*(src[i] + src[i+1<h*w?i+1:i-1]) -
  1901. + 5*(src[i-1<0?i+1:i-1] + src[i+2<h*w?i+2:i-2]) +
  1902. + (src[i-2<0?i+2:i-2] + src[i+3<h*w?i+3:i-3]) + 16
  1903. + )>>5;
  1904. + }
  1905. + }
  1906. + for(y=0; y<h; y++){
  1907. + for(x=0; x<w; x++){
  1908. + int i= y*ls + x;
  1909. +
  1910. + halfpel[2][p][i]= (
  1911. + 20*(src[i] + src[i+ls<h*w?i+ls:i-ls]) -
  1912. + 5*(src[i-ls<0?i+ls:i-ls] + src[i+2*ls<h*w?i+2*ls:i-2*ls]) +
  1913. + (src[i-2*ls<0?i+2*ls:i-2*ls] + src[i+3*ls<h*w?i+3*ls:i-3*ls]) + 16
  1914. + )>>5;
  1915. + }
  1916. + }
  1917. + src= halfpel[1][p];
  1918. + for(y=0; y<h; y++){
  1919. + for(x=0; x<w; x++){
  1920. + int i= y*ls + x;
  1921. +
  1922. + halfpel[3][p][i]= (
  1923. + 20*(src[i] + src[i+ls<h*w?i+ls:i-ls]) -
  1924. + 5*(src[i-ls<0?i+ls:i-ls] + src[i+2*ls<h*w?i+2*ls:i-2*ls]) +
  1925. + (src[i-2*ls<0?i+2*ls:i-2*ls] + src[i+3*ls<h*w?i+3*ls:i-3*ls]) + 16
  1926. + )>>5;
  1927. + }
  1928. + }
  1929. +
  1930. +//FIXME border!
  1931. + }
  1932. + return 0;
  1933. +}
  1934. +
  1935. +void ff_obmc_release_buffer(OBMCContext *s)
  1936. +{
  1937. + int i;
  1938. +
  1939. + if(s->last_pictures[s->max_ref_frames-1]->data[0]){
  1940. + av_frame_unref(s->last_pictures[s->max_ref_frames-1]);
  1941. + for(i=0; i<9; i++)
  1942. + if(s->halfpel_plane[s->max_ref_frames-1][1+i/3][i%3]) {
  1943. + av_free(s->halfpel_plane[s->max_ref_frames-1][1+i/3][i%3] - EDGE_WIDTH*(1+s->current_picture->linesize[i%3]));
  1944. + s->halfpel_plane[s->max_ref_frames-1][1+i/3][i%3] = NULL;
  1945. + }
  1946. + }
  1947. +}
  1948. +
  1949. +int ff_obmc_frame_start(OBMCContext *s)
  1950. +{
  1951. + AVFrame *tmp;
  1952. + int i, ret;
  1953. +
  1954. + int USE_HALFPEL_PLANE = 0;
  1955. +
  1956. + switch(s->avctx->codec_id) {
  1957. + case AV_CODEC_ID_FFV1:
  1958. + USE_HALFPEL_PLANE = 1;
  1959. + break;
  1960. + default:
  1961. + break;
  1962. + }
  1963. +
  1964. + ff_obmc_release_buffer(s);
  1965. +
  1966. + tmp= s->last_pictures[s->max_ref_frames-1];
  1967. + for(i=s->max_ref_frames-1; i>0; i--)
  1968. + s->last_pictures[i] = s->last_pictures[i-1];
  1969. + memmove(s->halfpel_plane+1, s->halfpel_plane, (s->max_ref_frames-1)*sizeof(void*)*4*4);
  1970. + if(USE_HALFPEL_PLANE && s->current_picture->data[0]) {
  1971. + if((ret = halfpel_interpol(s, s->halfpel_plane[0], s->current_picture)) < 0)
  1972. + return ret;
  1973. + }
  1974. + s->last_pictures[0] = s->current_picture;
  1975. + s->current_picture = tmp;
  1976. + av_frame_copy_props(s->current_picture, s->last_pictures[0]);
  1977. +
  1978. + if (s->keyframe) {
  1979. + s->ref_frames= 0;
  1980. + } else {
  1981. + int i;
  1982. + for(i=0; i<s->max_ref_frames && s->last_pictures[i]->data[0]; i++)
  1983. + if(i && s->last_pictures[i-1]->key_frame)
  1984. + break;
  1985. + s->ref_frames= i;
  1986. + if(s->ref_frames==0){
  1987. + av_log(s->avctx,AV_LOG_ERROR, "No reference frames\n");
  1988. + return AVERROR_INVALIDDATA;
  1989. + }
  1990. + }
  1991. + if ((ret = ff_obmc_get_buffer(s, s->current_picture)) < 0)
  1992. + return ret;
  1993. +
  1994. + s->current_picture->key_frame= s->keyframe;
  1995. +
  1996. + return 0;
  1997. +}
  1998. +
  1999. +av_cold int ff_obmc_close(OBMCContext *s)
  2000. +{
  2001. + int i;
  2002. +
  2003. + av_freep(&s->spatial_idwt_buffer);
  2004. +
  2005. + s->m.me.temp= NULL;
  2006. + av_freep(&s->m.me.scratchpad);
  2007. + av_freep(&s->m.me.map);
  2008. + av_freep(&s->m.me.score_map);
  2009. + av_freep(&s->m.sc.obmc_scratchpad);
  2010. +
  2011. + av_freep(&s->block);
  2012. + av_freep(&s->scratchbuf);
  2013. + av_freep(&s->emu_edge_buffer);
  2014. +
  2015. + for(i=0; i<MAX_REF_FRAMES; i++){
  2016. + av_freep(&s->ref_mvs[i]);
  2017. + av_freep(&s->ref_scores[i]);
  2018. + if(s->last_pictures[i] && s->last_pictures[i]->data[0]) {
  2019. + av_assert0(s->last_pictures[i]->data[0] != s->current_picture->data[0]);
  2020. + }
  2021. + av_frame_free(&s->last_pictures[i]);
  2022. + }
  2023. +
  2024. + av_frame_free(&s->mconly_picture);
  2025. + av_frame_free(&s->current_picture);
  2026. +
  2027. + return 0;
  2028. +}
  2029. diff --git a/libavcodec/obmemc.h b/libavcodec/obmemc.h
  2030. new file mode 100644
  2031. index 0000000..00e079c
  2032. --- /dev/null
  2033. +++ b/libavcodec/obmemc.h
  2034. @@ -0,0 +1,522 @@
  2035. +/*
  2036. + * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
  2037. + * Copyright (C) 2006 Robert Edele <yartrebo@earthlink.net>
  2038. + *
  2039. + * This file is part of FFmpeg.
  2040. + *
  2041. + * FFmpeg is free software; you can redistribute it and/or
  2042. + * modify it under the terms of the GNU Lesser General Public
  2043. + * License as published by the Free Software Foundation; either
  2044. + * version 2.1 of the License, or (at your option) any later version.
  2045. + *
  2046. + * FFmpeg is distributed in the hope that it will be useful,
  2047. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2048. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  2049. + * Lesser General Public License for more details.
  2050. + *
  2051. + * You should have received a copy of the GNU Lesser General Public
  2052. + * License along with FFmpeg; if not, write to the Free Software
  2053. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  2054. + */
  2055. +
  2056. + /**
  2057. + * @file obmemc.h
  2058. + * @brief Overlapped block motion estimation and compensation
  2059. + */
  2060. +
  2061. +#ifndef AVCODEC_OBMEMC_H
  2062. +#define AVCODEC_OBMEMC_H
  2063. +
  2064. +#include "libavutil/imgutils.h"
  2065. +#include "libavutil/pixdesc.h"
  2066. +#include "libavutil/motion_vector.h"
  2067. +
  2068. +#include "rangecoder.h"
  2069. +
  2070. +#include "hpeldsp.h"
  2071. +#include "me_cmp.h"
  2072. +#include "qpeldsp.h"
  2073. +#include "snow_dwt.h"
  2074. +
  2075. +#include "mpegvideo.h"
  2076. +#include "h264qpel.h"
  2077. +
  2078. +#define MAX_PLANES 4
  2079. +#define QSHIFT 5
  2080. +#define QROOT (1<<QSHIFT)
  2081. +#define LOSSLESS_QLOG -128
  2082. +#define FRAC_BITS 4
  2083. +#define MAX_REF_FRAMES 8
  2084. +
  2085. +#define LOG2_OBMC_MAX 8
  2086. +#define OBMC_MAX (1<<(LOG2_OBMC_MAX))
  2087. +typedef struct BlockNode{
  2088. + int16_t mx; ///< Motion vector component X, see mv_scale
  2089. + int16_t my; ///< Motion vector component Y, see mv_scale
  2090. + uint8_t ref; ///< Reference frame index
  2091. + uint8_t color[3]; ///< Color for intra
  2092. + uint8_t type; ///< Bitfield of BLOCK_*
  2093. +//#define TYPE_SPLIT 1
  2094. +#define BLOCK_INTRA 1 ///< Intra block, inter otherwise
  2095. +#define BLOCK_OPT 2 ///< Block needs no checks in this round of iterative motion estiation
  2096. +//#define TYPE_NOCOLOR 4
  2097. + uint8_t level; //FIXME merge into type?
  2098. +}BlockNode;
  2099. +
  2100. +static const BlockNode null_block= { //FIXME add border maybe
  2101. + .color= {128,128,128},
  2102. + .mx= 0,
  2103. + .my= 0,
  2104. + .ref= 0,
  2105. + .type= 0,
  2106. + .level= 0,
  2107. +};
  2108. +
  2109. +#define LOG2_MB_SIZE 4
  2110. +#define MB_SIZE (1<<LOG2_MB_SIZE)
  2111. +#define ENCODER_EXTRA_BITS 4
  2112. +#define HTAPS_MAX 8
  2113. +
  2114. +typedef struct PlaneObmc{
  2115. + int width;
  2116. + int height;
  2117. +
  2118. + int htaps;
  2119. + int8_t hcoeff[HTAPS_MAX/2];
  2120. + int diag_mc;
  2121. + int fast_mc;
  2122. +
  2123. + int last_htaps;
  2124. + int8_t last_hcoeff[HTAPS_MAX/2];
  2125. + int last_diag_mc;
  2126. +} PlaneObmc;
  2127. +
  2128. +/**
  2129. + * @struct ObmcCoderContext
  2130. + * @file obmemc.h
  2131. + * @brief struct that stores callbacks to interact with the bitstream encoder
  2132. + */
  2133. +typedef struct ObmcCoderContext {
  2134. + void *priv_data;
  2135. + AVCodecContext *avctx;
  2136. +
  2137. + int (*get_bits) (struct ObmcCoderContext *);
  2138. + int (*available_bytes) (struct ObmcCoderContext *);
  2139. +
  2140. + // UTILS
  2141. + void (*init_frame_coder) (AVCodecContext *, struct ObmcCoderContext *);
  2142. + void (*copy_coder) (struct ObmcCoderContext *);
  2143. + void (*reset_coder) (struct ObmcCoderContext *);
  2144. + void (*free) (struct ObmcCoderContext *);
  2145. +
  2146. + // ENCODER
  2147. + void (*put_level_break) (struct ObmcCoderContext *, int, int);
  2148. + void (*put_block_type) (struct ObmcCoderContext *, int, int);
  2149. + void (*put_best_ref) (struct ObmcCoderContext *, int, int);
  2150. + void (*put_block_mv) (struct ObmcCoderContext *, int, int, int, int);
  2151. + void (*put_block_color) (struct ObmcCoderContext *, int, int, int, int, int, int);
  2152. +
  2153. +} ObmcCoderContext;
  2154. +
  2155. +typedef struct OBMCContext {
  2156. + AVCodecContext *avctx;
  2157. + int keyframe;
  2158. + int chroma_h_shift, chroma_v_shift;
  2159. +
  2160. + ObmcCoderContext obmc_coder;
  2161. +
  2162. + /* ME/MC part */
  2163. + MECmpContext mecc;
  2164. + HpelDSPContext hdsp;
  2165. + QpelDSPContext qdsp;
  2166. + VideoDSPContext vdsp;
  2167. + H264QpelContext h264qpel;
  2168. + MpegvideoEncDSPContext mpvencdsp;
  2169. + SnowDWTContext dwt;
  2170. +
  2171. + AVFrame *input_picture; ///< new_picture with the internal linesizes
  2172. + AVFrame *current_picture;
  2173. + AVFrame *last_pictures[MAX_REF_FRAMES];
  2174. + uint8_t *halfpel_plane[MAX_REF_FRAMES][4][4];
  2175. + AVFrame *mconly_picture;
  2176. +
  2177. + MpegEncContext m; // needed for motion estimation, should not be used for anything else, the idea is to eventually make the motion estimation independent of MpegEncContext, so this will be removed then (FIXME/XXX)
  2178. +
  2179. + uint8_t *scratchbuf;
  2180. + uint8_t *emu_edge_buffer;
  2181. +
  2182. + AVMotionVector *avmv;
  2183. + int avmv_index;
  2184. +
  2185. + int motion_est;
  2186. + int intra_penalty;
  2187. + int lambda;
  2188. + int lambda2;
  2189. + int max_ref_frames;
  2190. + int ref_frames;
  2191. +
  2192. + BlockNode *block;
  2193. + int b_width;
  2194. + int b_height;
  2195. + int block_max_depth;
  2196. + int last_block_max_depth;
  2197. +
  2198. + uint32_t *ref_scores[MAX_REF_FRAMES];
  2199. + int16_t (*ref_mvs[MAX_REF_FRAMES])[2];
  2200. +
  2201. +#define ME_CACHE_SIZE 1024
  2202. + unsigned me_cache[ME_CACHE_SIZE];
  2203. + unsigned me_cache_generation;
  2204. + int mv_scale;
  2205. + int last_mv_scale;
  2206. + int iterative_dia_size;
  2207. +
  2208. + IDWTELEM *spatial_idwt_buffer;
  2209. +
  2210. + PlaneObmc plane[MAX_PLANES];
  2211. +
  2212. + int nb_planes;
  2213. +} OBMCContext;
  2214. +
  2215. +/* Tables */
  2216. +extern const uint8_t * const ff_obmc_tab[4];
  2217. +extern uint8_t ff_qexp[QROOT];
  2218. +extern int ff_scale_mv_ref[MAX_REF_FRAMES][MAX_REF_FRAMES];
  2219. +
  2220. +int ff_obmc_get_buffer(OBMCContext *s, AVFrame *frame);
  2221. +void ff_obmc_release_buffer(OBMCContext *s);
  2222. +int ff_obmc_common_init_after_header(OBMCContext *s);
  2223. +int ff_obmc_frame_start(OBMCContext *f);
  2224. +int ff_obmc_alloc_blocks(OBMCContext *s);
  2225. +int ff_obmc_common_init(OBMCContext *s, AVCodecContext *avctx);
  2226. +int ff_obmc_close(OBMCContext *s);
  2227. +void ff_obmc_pred_block(OBMCContext *s, uint8_t *dst, uint8_t *tmp, ptrdiff_t stride,
  2228. + int sx, int sy, int b_w, int b_h, const BlockNode *block,
  2229. + int plane_index, int w, int h);
  2230. +
  2231. +static inline void pred_mv(OBMCContext *s, int *mx, int *my, int ref,
  2232. + const BlockNode *left, const BlockNode *top, const BlockNode *tr){
  2233. + if(s->ref_frames == 1){
  2234. + *mx = mid_pred(left->mx, top->mx, tr->mx);
  2235. + *my = mid_pred(left->my, top->my, tr->my);
  2236. + }else{
  2237. + const int *scale = ff_scale_mv_ref[ref];
  2238. + *mx = mid_pred((left->mx * scale[left->ref] + 128) >>8,
  2239. + (top ->mx * scale[top ->ref] + 128) >>8,
  2240. + (tr ->mx * scale[tr ->ref] + 128) >>8);
  2241. + *my = mid_pred((left->my * scale[left->ref] + 128) >>8,
  2242. + (top ->my * scale[top ->ref] + 128) >>8,
  2243. + (tr ->my * scale[tr ->ref] + 128) >>8);
  2244. + }
  2245. +}
  2246. +
  2247. +static av_always_inline int same_block(BlockNode *a, BlockNode *b){
  2248. + if((a->type&BLOCK_INTRA) && (b->type&BLOCK_INTRA)){
  2249. + return !((a->color[0] - b->color[0]) | (a->color[1] - b->color[1]) | (a->color[2] - b->color[2]));
  2250. + }else{
  2251. + return !((a->mx - b->mx) | (a->my - b->my) | (a->ref - b->ref) | ((a->type ^ b->type)&BLOCK_INTRA));
  2252. + }
  2253. +}
  2254. +
  2255. +static av_always_inline void add_yblock(OBMCContext *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){
  2256. + const int b_width = s->b_width << s->block_max_depth;
  2257. + const int b_height= s->b_height << s->block_max_depth;
  2258. + const int b_stride= b_width;
  2259. + BlockNode *lt= &s->block[b_x + b_y*b_stride];
  2260. + BlockNode *rt= lt+1;
  2261. + BlockNode *lb= lt+b_stride;
  2262. + BlockNode *rb= lb+1;
  2263. + uint8_t *block[4];
  2264. + // When src_stride is large enough, it is possible to interleave the blocks.
  2265. + // Otherwise the blocks are written sequentially in the tmp buffer.
  2266. + int tmp_step= src_stride >= 7*MB_SIZE ? MB_SIZE : MB_SIZE*src_stride;
  2267. + uint8_t *tmp = s->scratchbuf;
  2268. + uint8_t *ptmp;
  2269. + int x,y;
  2270. +
  2271. + if(b_x<0){
  2272. + lt= rt;
  2273. + lb= rb;
  2274. + }else if(b_x + 1 >= b_width){
  2275. + rt= lt;
  2276. + rb= lb;
  2277. + }
  2278. + if(b_y<0){
  2279. + lt= lb;
  2280. + rt= rb;
  2281. + }else if(b_y + 1 >= b_height){
  2282. + lb= lt;
  2283. + rb= rt;
  2284. + }
  2285. +
  2286. + if(src_x<0){ //FIXME merge with prev & always round internal width up to *16
  2287. + obmc -= src_x;
  2288. + b_w += src_x;
  2289. + if(!sliced && !offset_dst)
  2290. + dst -= src_x;
  2291. + src_x=0;
  2292. + }
  2293. + if(src_x + b_w > w){
  2294. + b_w = w - src_x;
  2295. + }
  2296. + if(src_y<0){
  2297. + obmc -= src_y*obmc_stride;
  2298. + b_h += src_y;
  2299. + if(!sliced && !offset_dst)
  2300. + dst -= src_y*dst_stride;
  2301. + src_y=0;
  2302. + }
  2303. + if(src_y + b_h> h){
  2304. + b_h = h - src_y;
  2305. + }
  2306. +
  2307. + if(b_w<=0 || b_h<=0) return;
  2308. +
  2309. + if(!sliced && offset_dst)
  2310. + dst += src_x + src_y*dst_stride;
  2311. + dst8+= src_x + src_y*src_stride;
  2312. +// src += src_x + src_y*src_stride;
  2313. +
  2314. + ptmp= tmp + 3*tmp_step;
  2315. + block[0]= ptmp;
  2316. + ptmp+=tmp_step;
  2317. + ff_obmc_pred_block(s, block[0], tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h);
  2318. +
  2319. + if(same_block(lt, rt)){
  2320. + block[1]= block[0];
  2321. + }else{
  2322. + block[1]= ptmp;
  2323. + ptmp+=tmp_step;
  2324. + ff_obmc_pred_block(s, block[1], tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h);
  2325. + }
  2326. +
  2327. + if(same_block(lt, lb)){
  2328. + block[2]= block[0];
  2329. + }else if(same_block(rt, lb)){
  2330. + block[2]= block[1];
  2331. + }else{
  2332. + block[2]= ptmp;
  2333. + ptmp+=tmp_step;
  2334. + ff_obmc_pred_block(s, block[2], tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h);
  2335. + }
  2336. +
  2337. + if(same_block(lt, rb) ){
  2338. + block[3]= block[0];
  2339. + }else if(same_block(rt, rb)){
  2340. + block[3]= block[1];
  2341. + }else if(same_block(lb, rb)){
  2342. + block[3]= block[2];
  2343. + }else{
  2344. + block[3]= ptmp;
  2345. + ff_obmc_pred_block(s, block[3], tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h);
  2346. + }
  2347. + if(sliced){
  2348. + s->dwt.inner_add_yblock(obmc, obmc_stride, block, b_w, b_h, src_x,src_y, src_stride, sb, add, dst8);
  2349. + }else{
  2350. + for(y=0; y<b_h; y++){
  2351. + //FIXME ugly misuse of obmc_stride
  2352. + const uint8_t *obmc1= obmc + y*obmc_stride;
  2353. + const uint8_t *obmc2= obmc1+ (obmc_stride>>1);
  2354. + const uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
  2355. + const uint8_t *obmc4= obmc3+ (obmc_stride>>1);
  2356. + for(x=0; x<b_w; x++){
  2357. + int v= obmc1[x] * block[3][x + y*src_stride]
  2358. + +obmc2[x] * block[2][x + y*src_stride]
  2359. + +obmc3[x] * block[1][x + y*src_stride]
  2360. + +obmc4[x] * block[0][x + y*src_stride];
  2361. +
  2362. + v <<= 8 - LOG2_OBMC_MAX;
  2363. + if(FRAC_BITS != 8) {
  2364. + v >>= 8 - FRAC_BITS;
  2365. + }
  2366. + if(add) {
  2367. + v += dst[x + y*dst_stride];
  2368. + v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
  2369. + if(v&(~255)) v= ~(v>>31);
  2370. + dst8[x + y*src_stride] = v;
  2371. + } else {
  2372. + dst[x + y*dst_stride] -= v;
  2373. + }
  2374. + }
  2375. + }
  2376. + }
  2377. +}
  2378. +
  2379. +static av_always_inline void predict_slice(OBMCContext *s, IDWTELEM *buf, int plane_index, int add, int mb_y){
  2380. + PlaneObmc *p= &s->plane[plane_index];
  2381. + const int mb_w= s->b_width << s->block_max_depth;
  2382. + const int mb_h= s->b_height << s->block_max_depth;
  2383. + int x, y, mb_x;
  2384. + int block_size = MB_SIZE >> s->block_max_depth;
  2385. + int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
  2386. + int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
  2387. + const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];
  2388. + const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
  2389. + int ref_stride= s->current_picture->linesize[plane_index];
  2390. + uint8_t *dst8= s->current_picture->data[plane_index];
  2391. + int w= p->width;
  2392. + int h= p->height;
  2393. + av_assert2(s->chroma_h_shift == s->chroma_v_shift); // obmc params assume squares
  2394. + if(s->keyframe || (s->avctx->debug&512)){
  2395. + if(mb_y==mb_h)
  2396. + return;
  2397. +
  2398. + if(add){
  2399. + for(y=block_h*mb_y; y<FFMIN(h,block_h*(mb_y+1)); y++){
  2400. + for(x=0; x<w; x++){
  2401. + int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  2402. + v >>= FRAC_BITS;
  2403. + if(v&(~255)) v= ~(v>>31);
  2404. + dst8[x + y*ref_stride]= v;
  2405. + }
  2406. + }
  2407. + }else{
  2408. + for(y=block_h*mb_y; y<FFMIN(h,block_h*(mb_y+1)); y++){
  2409. + for(x=0; x<w; x++){
  2410. + buf[x + y*w]-= 128<<FRAC_BITS;
  2411. + }
  2412. + }
  2413. + }
  2414. +
  2415. + return;
  2416. + }
  2417. +
  2418. + for(mb_x=0; mb_x<=mb_w; mb_x++){
  2419. + add_yblock(s, 0, NULL, buf, dst8, obmc,
  2420. + block_w*mb_x - block_w/2,
  2421. + block_h*mb_y - block_h/2,
  2422. + block_w, block_h,
  2423. + w, h,
  2424. + w, ref_stride, obmc_stride,
  2425. + mb_x - 1, mb_y - 1,
  2426. + add, 1, plane_index);
  2427. + }
  2428. +}
  2429. +
  2430. +static av_always_inline void predict_slice_buffered(OBMCContext *s, slice_buffer * sb, IDWTELEM * old_buffer, int plane_index, int add, int mb_y){
  2431. + PlaneObmc *p= &s->plane[plane_index];
  2432. + const int mb_w= s->b_width << s->block_max_depth;
  2433. + const int mb_h= s->b_height << s->block_max_depth;
  2434. + int x, y, mb_x;
  2435. + int block_size = MB_SIZE >> s->block_max_depth;
  2436. + int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
  2437. + int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
  2438. + const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];
  2439. + int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
  2440. + int ref_stride= s->current_picture->linesize[plane_index];
  2441. + uint8_t *dst8= s->current_picture->data[plane_index];
  2442. + int w= p->width;
  2443. + int h= p->height;
  2444. +
  2445. + if(s->keyframe || (s->avctx->debug&512)){
  2446. + if(mb_y==mb_h)
  2447. + return;
  2448. +
  2449. + if(add){
  2450. + for(y=block_h*mb_y; y<FFMIN(h,block_h*(mb_y+1)); y++){
  2451. + IDWTELEM * line = sb->line[y];
  2452. + for(x=0; x<w; x++){
  2453. + int v= line[x] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  2454. + v >>= FRAC_BITS;
  2455. + if(v&(~255)) v= ~(v>>31);
  2456. + dst8[x + y*ref_stride]= v;
  2457. + }
  2458. + }
  2459. + }else{
  2460. + for(y=block_h*mb_y; y<FFMIN(h,block_h*(mb_y+1)); y++){
  2461. + IDWTELEM * line = sb->line[y];
  2462. + for(x=0; x<w; x++){
  2463. + line[x] -= 128 << FRAC_BITS;
  2464. + }
  2465. + }
  2466. + }
  2467. +
  2468. + return;
  2469. + }
  2470. +
  2471. + for(mb_x=0; mb_x<=mb_w; mb_x++){
  2472. + add_yblock(s, 1, sb, old_buffer, dst8, obmc,
  2473. + block_w*mb_x - block_w/2,
  2474. + block_h*mb_y - block_h/2,
  2475. + block_w, block_h,
  2476. + w, h,
  2477. + w, ref_stride, obmc_stride,
  2478. + mb_x - 1, mb_y - 1,
  2479. + add, 0, plane_index);
  2480. + }
  2481. +
  2482. + if(s->avmv && mb_y < mb_h && plane_index == 0)
  2483. + for(mb_x=0; mb_x<mb_w; mb_x++){
  2484. + AVMotionVector *avmv = s->avmv + s->avmv_index;
  2485. + const int b_width = s->b_width << s->block_max_depth;
  2486. + const int b_stride= b_width;
  2487. + BlockNode *bn= &s->block[mb_x + mb_y*b_stride];
  2488. +
  2489. + if (bn->type)
  2490. + continue;
  2491. +
  2492. + s->avmv_index++;
  2493. +
  2494. + avmv->w = block_w;
  2495. + avmv->h = block_h;
  2496. + avmv->dst_x = block_w*mb_x - block_w/2;
  2497. + avmv->dst_y = block_h*mb_y - block_h/2;
  2498. + avmv->motion_scale = 8;
  2499. + avmv->motion_x = bn->mx * s->mv_scale;
  2500. + avmv->motion_y = bn->my * s->mv_scale;
  2501. + avmv->src_x = avmv->dst_x + avmv->motion_x / 8;
  2502. + avmv->src_y = avmv->dst_y + avmv->motion_y / 8;
  2503. + avmv->source= -1 - bn->ref;
  2504. + avmv->flags = 0;
  2505. + }
  2506. +}
  2507. +
  2508. +static av_always_inline void predict_plane(OBMCContext *s, IDWTELEM *buf, int plane_index, int add){
  2509. + const int mb_h= s->b_height << s->block_max_depth;
  2510. + int mb_y;
  2511. + for(mb_y=0; mb_y<=mb_h; mb_y++)
  2512. + predict_slice(s, buf, plane_index, add, mb_y);
  2513. +}
  2514. +
  2515. +static inline void set_blocks(OBMCContext *s, int level, int x, int y, int l, int cb, int cr, int mx, int my, int ref, int type)
  2516. +{
  2517. + const int w= s->b_width << s->block_max_depth;
  2518. + const int rem_depth= s->block_max_depth - level;
  2519. + const int index= (x + y*w) << rem_depth;
  2520. + const int block_w= 1<<rem_depth;
  2521. + const int block_h= 1<<rem_depth; //FIXME "w!=h"
  2522. + BlockNode block;
  2523. + int i,j;
  2524. +
  2525. + block.color[0]= l;
  2526. + block.color[1]= cb;
  2527. + block.color[2]= cr;
  2528. + block.mx= mx;
  2529. + block.my= my;
  2530. + block.ref= ref;
  2531. + block.type= type;
  2532. + block.level= level;
  2533. +
  2534. + for(j=0; j<block_h; j++){
  2535. + for(i=0; i<block_w; i++){
  2536. + s->block[index + i + j*w]= block;
  2537. + }
  2538. + }
  2539. +}
  2540. +
  2541. +static inline void init_ref(MotionEstContext *c, uint8_t *src[3], uint8_t *ref[3], uint8_t *ref2[3], int x, int y, int ref_index){
  2542. + OBMCContext *s = c->avctx->priv_data;
  2543. + const int offset[3]= {
  2544. + y*c-> stride + x,
  2545. + ((y*c->uvstride + x)>>s->chroma_h_shift),
  2546. + ((y*c->uvstride + x)>>s->chroma_h_shift),
  2547. + };
  2548. + int i;
  2549. + for(i=0; i<3; i++){
  2550. + c->src[0][i]= src [i];
  2551. + c->ref[0][i]= ref [i] + offset[i];
  2552. + }
  2553. + av_assert2(!ref_index);
  2554. +}
  2555. +
  2556. +#endif /* AVCODEC_OBMEMC_H */
  2557. diff --git a/libavcodec/obmemc_data.h b/libavcodec/obmemc_data.h
  2558. new file mode 100644
  2559. index 0000000..a32c27e
  2560. --- /dev/null
  2561. +++ b/libavcodec/obmemc_data.h
  2562. @@ -0,0 +1,132 @@
  2563. +/*
  2564. + * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
  2565. + * Copyright (C) 2006 Robert Edele <yartrebo@earthlink.net>
  2566. + *
  2567. + * This file is part of FFmpeg.
  2568. + *
  2569. + * FFmpeg is free software; you can redistribute it and/or
  2570. + * modify it under the terms of the GNU Lesser General Public
  2571. + * License as published by the Free Software Foundation; either
  2572. + * version 2.1 of the License, or (at your option) any later version.
  2573. + *
  2574. + * FFmpeg is distributed in the hope that it will be useful,
  2575. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2576. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  2577. + * Lesser General Public License for more details.
  2578. + *
  2579. + * You should have received a copy of the GNU Lesser General Public
  2580. + * License along with FFmpeg; if not, write to the Free Software
  2581. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  2582. + */
  2583. +
  2584. +#ifndef AVCODEC_OBMEMC_DATA_H
  2585. +#define AVCODEC_OBMEMC_DATA_H
  2586. +
  2587. +#include "obmemc.h"
  2588. +
  2589. +static const uint8_t obmc32[1024]={
  2590. + 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0,
  2591. + 0, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 16, 20, 20, 20, 24, 24, 20, 20, 20, 16, 16, 16, 12, 12, 8, 8, 8, 4, 4, 4, 0,
  2592. + 0, 4, 8, 8, 12, 12, 16, 20, 20, 24, 28, 28, 32, 32, 36, 40, 40, 36, 32, 32, 28, 28, 24, 20, 20, 16, 12, 12, 8, 8, 4, 0,
  2593. + 0, 4, 8, 12, 16, 20, 24, 28, 28, 32, 36, 40, 44, 48, 52, 56, 56, 52, 48, 44, 40, 36, 32, 28, 28, 24, 20, 16, 12, 8, 4, 0,
  2594. + 4, 8, 12, 16, 20, 24, 28, 32, 40, 44, 48, 52, 56, 60, 64, 68, 68, 64, 60, 56, 52, 48, 44, 40, 32, 28, 24, 20, 16, 12, 8, 4,
  2595. + 4, 8, 12, 20, 24, 32, 36, 40, 48, 52, 56, 64, 68, 76, 80, 84, 84, 80, 76, 68, 64, 56, 52, 48, 40, 36, 32, 24, 20, 12, 8, 4,
  2596. + 4, 8, 16, 24, 28, 36, 44, 48, 56, 60, 68, 76, 80, 88, 96,100,100, 96, 88, 80, 76, 68, 60, 56, 48, 44, 36, 28, 24, 16, 8, 4,
  2597. + 4, 12, 20, 28, 32, 40, 48, 56, 64, 72, 80, 88, 92,100,108,116,116,108,100, 92, 88, 80, 72, 64, 56, 48, 40, 32, 28, 20, 12, 4,
  2598. + 4, 12, 20, 28, 40, 48, 56, 64, 72, 80, 88, 96,108,116,124,132,132,124,116,108, 96, 88, 80, 72, 64, 56, 48, 40, 28, 20, 12, 4,
  2599. + 4, 16, 24, 32, 44, 52, 60, 72, 80, 92,100,108,120,128,136,148,148,136,128,120,108,100, 92, 80, 72, 60, 52, 44, 32, 24, 16, 4,
  2600. + 4, 16, 28, 36, 48, 56, 68, 80, 88,100,112,120,132,140,152,164,164,152,140,132,120,112,100, 88, 80, 68, 56, 48, 36, 28, 16, 4,
  2601. + 4, 16, 28, 40, 52, 64, 76, 88, 96,108,120,132,144,156,168,180,180,168,156,144,132,120,108, 96, 88, 76, 64, 52, 40, 28, 16, 4,
  2602. + 8, 20, 32, 44, 56, 68, 80, 92,108,120,132,144,156,168,180,192,192,180,168,156,144,132,120,108, 92, 80, 68, 56, 44, 32, 20, 8,
  2603. + 8, 20, 32, 48, 60, 76, 88,100,116,128,140,156,168,184,196,208,208,196,184,168,156,140,128,116,100, 88, 76, 60, 48, 32, 20, 8,
  2604. + 8, 20, 36, 52, 64, 80, 96,108,124,136,152,168,180,196,212,224,224,212,196,180,168,152,136,124,108, 96, 80, 64, 52, 36, 20, 8,
  2605. + 8, 24, 40, 56, 68, 84,100,116,132,148,164,180,192,208,224,240,240,224,208,192,180,164,148,132,116,100, 84, 68, 56, 40, 24, 8,
  2606. + 8, 24, 40, 56, 68, 84,100,116,132,148,164,180,192,208,224,240,240,224,208,192,180,164,148,132,116,100, 84, 68, 56, 40, 24, 8,
  2607. + 8, 20, 36, 52, 64, 80, 96,108,124,136,152,168,180,196,212,224,224,212,196,180,168,152,136,124,108, 96, 80, 64, 52, 36, 20, 8,
  2608. + 8, 20, 32, 48, 60, 76, 88,100,116,128,140,156,168,184,196,208,208,196,184,168,156,140,128,116,100, 88, 76, 60, 48, 32, 20, 8,
  2609. + 8, 20, 32, 44, 56, 68, 80, 92,108,120,132,144,156,168,180,192,192,180,168,156,144,132,120,108, 92, 80, 68, 56, 44, 32, 20, 8,
  2610. + 4, 16, 28, 40, 52, 64, 76, 88, 96,108,120,132,144,156,168,180,180,168,156,144,132,120,108, 96, 88, 76, 64, 52, 40, 28, 16, 4,
  2611. + 4, 16, 28, 36, 48, 56, 68, 80, 88,100,112,120,132,140,152,164,164,152,140,132,120,112,100, 88, 80, 68, 56, 48, 36, 28, 16, 4,
  2612. + 4, 16, 24, 32, 44, 52, 60, 72, 80, 92,100,108,120,128,136,148,148,136,128,120,108,100, 92, 80, 72, 60, 52, 44, 32, 24, 16, 4,
  2613. + 4, 12, 20, 28, 40, 48, 56, 64, 72, 80, 88, 96,108,116,124,132,132,124,116,108, 96, 88, 80, 72, 64, 56, 48, 40, 28, 20, 12, 4,
  2614. + 4, 12, 20, 28, 32, 40, 48, 56, 64, 72, 80, 88, 92,100,108,116,116,108,100, 92, 88, 80, 72, 64, 56, 48, 40, 32, 28, 20, 12, 4,
  2615. + 4, 8, 16, 24, 28, 36, 44, 48, 56, 60, 68, 76, 80, 88, 96,100,100, 96, 88, 80, 76, 68, 60, 56, 48, 44, 36, 28, 24, 16, 8, 4,
  2616. + 4, 8, 12, 20, 24, 32, 36, 40, 48, 52, 56, 64, 68, 76, 80, 84, 84, 80, 76, 68, 64, 56, 52, 48, 40, 36, 32, 24, 20, 12, 8, 4,
  2617. + 4, 8, 12, 16, 20, 24, 28, 32, 40, 44, 48, 52, 56, 60, 64, 68, 68, 64, 60, 56, 52, 48, 44, 40, 32, 28, 24, 20, 16, 12, 8, 4,
  2618. + 0, 4, 8, 12, 16, 20, 24, 28, 28, 32, 36, 40, 44, 48, 52, 56, 56, 52, 48, 44, 40, 36, 32, 28, 28, 24, 20, 16, 12, 8, 4, 0,
  2619. + 0, 4, 8, 8, 12, 12, 16, 20, 20, 24, 28, 28, 32, 32, 36, 40, 40, 36, 32, 32, 28, 28, 24, 20, 20, 16, 12, 12, 8, 8, 4, 0,
  2620. + 0, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 16, 20, 20, 20, 24, 24, 20, 20, 20, 16, 16, 16, 12, 12, 8, 8, 8, 4, 4, 4, 0,
  2621. + 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0,
  2622. + //error:0.000020
  2623. +};
  2624. +static const uint8_t obmc16[256]={
  2625. + 0, 4, 4, 8, 8, 12, 12, 16, 16, 12, 12, 8, 8, 4, 4, 0,
  2626. + 4, 8, 16, 20, 28, 32, 40, 44, 44, 40, 32, 28, 20, 16, 8, 4,
  2627. + 4, 16, 24, 36, 44, 56, 64, 76, 76, 64, 56, 44, 36, 24, 16, 4,
  2628. + 8, 20, 36, 48, 64, 76, 92,104,104, 92, 76, 64, 48, 36, 20, 8,
  2629. + 8, 28, 44, 64, 80,100,116,136,136,116,100, 80, 64, 44, 28, 8,
  2630. + 12, 32, 56, 76,100,120,144,164,164,144,120,100, 76, 56, 32, 12,
  2631. + 12, 40, 64, 92,116,144,168,196,196,168,144,116, 92, 64, 40, 12,
  2632. + 16, 44, 76,104,136,164,196,224,224,196,164,136,104, 76, 44, 16,
  2633. + 16, 44, 76,104,136,164,196,224,224,196,164,136,104, 76, 44, 16,
  2634. + 12, 40, 64, 92,116,144,168,196,196,168,144,116, 92, 64, 40, 12,
  2635. + 12, 32, 56, 76,100,120,144,164,164,144,120,100, 76, 56, 32, 12,
  2636. + 8, 28, 44, 64, 80,100,116,136,136,116,100, 80, 64, 44, 28, 8,
  2637. + 8, 20, 36, 48, 64, 76, 92,104,104, 92, 76, 64, 48, 36, 20, 8,
  2638. + 4, 16, 24, 36, 44, 56, 64, 76, 76, 64, 56, 44, 36, 24, 16, 4,
  2639. + 4, 8, 16, 20, 28, 32, 40, 44, 44, 40, 32, 28, 20, 16, 8, 4,
  2640. + 0, 4, 4, 8, 8, 12, 12, 16, 16, 12, 12, 8, 8, 4, 4, 0,
  2641. +//error:0.000015
  2642. +};
  2643. +
  2644. +//linear *64
  2645. +static const uint8_t obmc8[64]={
  2646. + 4, 12, 20, 28, 28, 20, 12, 4,
  2647. + 12, 36, 60, 84, 84, 60, 36, 12,
  2648. + 20, 60,100,140,140,100, 60, 20,
  2649. + 28, 84,140,196,196,140, 84, 28,
  2650. + 28, 84,140,196,196,140, 84, 28,
  2651. + 20, 60,100,140,140,100, 60, 20,
  2652. + 12, 36, 60, 84, 84, 60, 36, 12,
  2653. + 4, 12, 20, 28, 28, 20, 12, 4,
  2654. +//error:0.000000
  2655. +};
  2656. +
  2657. +//linear *64
  2658. +static const uint8_t obmc4[16]={
  2659. + 16, 48, 48, 16,
  2660. + 48,144,144, 48,
  2661. + 48,144,144, 48,
  2662. + 16, 48, 48, 16,
  2663. +//error:0.000000
  2664. +};
  2665. +
  2666. +const int8_t ff_quant3bA[256]={
  2667. + 0, 0, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  2668. + 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  2669. + 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  2670. + 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  2671. + 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  2672. + 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  2673. + 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  2674. + 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  2675. + 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  2676. + 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  2677. + 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  2678. + 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  2679. + 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  2680. + 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  2681. + 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  2682. + 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  2683. +};
  2684. +
  2685. +const uint8_t * const ff_obmc_tab[4]= {
  2686. + obmc32, obmc16, obmc8, obmc4
  2687. +};
  2688. +
  2689. +/* runtime generated tables */
  2690. +uint8_t ff_qexp[QROOT];
  2691. +int ff_scale_mv_ref[MAX_REF_FRAMES][MAX_REF_FRAMES];
  2692. +
  2693. +
  2694. +#endif /* AVCODEC_OBMEMC_DATA_H */
  2695. diff --git a/libavcodec/snow.c b/libavcodec/snow.c
  2696. index a3e6afc..7b210ea 100644
  2697. --- a/libavcodec/snow.c
  2698. +++ b/libavcodec/snow.c
  2699. @@ -26,7 +26,6 @@
  2700. #include "snow_dwt.h"
  2701. #include "internal.h"
  2702. #include "snow.h"
  2703. -#include "snowdata.h"
  2704.  
  2705. #include "rangecoder.h"
  2706. #include "mathops.h"
  2707. @@ -66,40 +65,16 @@ void ff_snow_inner_add_yblock(const uint8_t *obmc, const int obmc_stride, uint8_
  2708. }
  2709. }
  2710.  
  2711. -int ff_snow_get_buffer(SnowContext *s, AVFrame *frame)
  2712. -{
  2713. - int ret, i;
  2714. - int edges_needed = av_codec_is_encoder(s->avctx->codec);
  2715. -
  2716. - frame->width = s->avctx->width ;
  2717. - frame->height = s->avctx->height;
  2718. - if (edges_needed) {
  2719. - frame->width += 2 * EDGE_WIDTH;
  2720. - frame->height += 2 * EDGE_WIDTH;
  2721. - }
  2722. - if ((ret = ff_get_buffer(s->avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
  2723. - return ret;
  2724. - if (edges_needed) {
  2725. - for (i = 0; frame->data[i]; i++) {
  2726. - int offset = (EDGE_WIDTH >> (i ? s->chroma_v_shift : 0)) *
  2727. - frame->linesize[i] +
  2728. - (EDGE_WIDTH >> (i ? s->chroma_h_shift : 0));
  2729. - frame->data[i] += offset;
  2730. - }
  2731. - frame->width = s->avctx->width;
  2732. - frame->height = s->avctx->height;
  2733. - }
  2734. -
  2735. - return 0;
  2736. -}
  2737. -
  2738. void ff_snow_reset_contexts(SnowContext *s){ //FIXME better initial contexts
  2739. int plane_index, level, orientation;
  2740.  
  2741. for(plane_index=0; plane_index<3; plane_index++){
  2742. for(level=0; level<MAX_DECOMPOSITIONS; level++){
  2743. for(orientation=level ? 1:0; orientation<4; orientation++){
  2744. - memset(s->plane[plane_index].band[level][orientation].state, MID_STATE, sizeof(s->plane[plane_index].band[level][orientation].state));
  2745. + memset(
  2746. + s->plane[plane_index].band[level][orientation].state,
  2747. + MID_STATE,
  2748. + sizeof(s->plane[plane_index].band[level][orientation].state));
  2749. }
  2750. }
  2751. }
  2752. @@ -107,404 +82,25 @@ void ff_snow_reset_contexts(SnowContext *s){ //FIXME better initial contexts
  2753. memset(s->block_state, MID_STATE, sizeof(s->block_state));
  2754. }
  2755.  
  2756. -int ff_snow_alloc_blocks(SnowContext *s){
  2757. - int w= AV_CEIL_RSHIFT(s->avctx->width, LOG2_MB_SIZE);
  2758. - int h= AV_CEIL_RSHIFT(s->avctx->height, LOG2_MB_SIZE);
  2759. -
  2760. - s->b_width = w;
  2761. - s->b_height= h;
  2762. -
  2763. - av_free(s->block);
  2764. - s->block= av_mallocz_array(w * h, sizeof(BlockNode) << (s->block_max_depth*2));
  2765. - if (!s->block)
  2766. - return AVERROR(ENOMEM);
  2767. -
  2768. - return 0;
  2769. -}
  2770. -
  2771. -static av_cold void init_qexp(void){
  2772. - int i;
  2773. - double v=128;
  2774. -
  2775. - for(i=0; i<QROOT; i++){
  2776. - ff_qexp[i]= lrintf(v);
  2777. - v *= pow(2, 1.0 / QROOT);
  2778. - }
  2779. -}
  2780. -static void mc_block(Plane *p, uint8_t *dst, const uint8_t *src, int stride, int b_w, int b_h, int dx, int dy){
  2781. - static const uint8_t weight[64]={
  2782. - 8,7,6,5,4,3,2,1,
  2783. - 7,7,0,0,0,0,0,1,
  2784. - 6,0,6,0,0,0,2,0,
  2785. - 5,0,0,5,0,3,0,0,
  2786. - 4,0,0,0,4,0,0,0,
  2787. - 3,0,0,5,0,3,0,0,
  2788. - 2,0,6,0,0,0,2,0,
  2789. - 1,7,0,0,0,0,0,1,
  2790. - };
  2791. -
  2792. - static const uint8_t brane[256]={
  2793. - 0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x11,0x12,0x12,0x12,0x12,0x12,0x12,0x12,
  2794. - 0x04,0x05,0xcc,0xcc,0xcc,0xcc,0xcc,0x41,0x15,0x16,0xcc,0xcc,0xcc,0xcc,0xcc,0x52,
  2795. - 0x04,0xcc,0x05,0xcc,0xcc,0xcc,0x41,0xcc,0x15,0xcc,0x16,0xcc,0xcc,0xcc,0x52,0xcc,
  2796. - 0x04,0xcc,0xcc,0x05,0xcc,0x41,0xcc,0xcc,0x15,0xcc,0xcc,0x16,0xcc,0x52,0xcc,0xcc,
  2797. - 0x04,0xcc,0xcc,0xcc,0x41,0xcc,0xcc,0xcc,0x15,0xcc,0xcc,0xcc,0x16,0xcc,0xcc,0xcc,
  2798. - 0x04,0xcc,0xcc,0x41,0xcc,0x05,0xcc,0xcc,0x15,0xcc,0xcc,0x52,0xcc,0x16,0xcc,0xcc,
  2799. - 0x04,0xcc,0x41,0xcc,0xcc,0xcc,0x05,0xcc,0x15,0xcc,0x52,0xcc,0xcc,0xcc,0x16,0xcc,
  2800. - 0x04,0x41,0xcc,0xcc,0xcc,0xcc,0xcc,0x05,0x15,0x52,0xcc,0xcc,0xcc,0xcc,0xcc,0x16,
  2801. - 0x44,0x45,0x45,0x45,0x45,0x45,0x45,0x45,0x55,0x56,0x56,0x56,0x56,0x56,0x56,0x56,
  2802. - 0x48,0x49,0xcc,0xcc,0xcc,0xcc,0xcc,0x85,0x59,0x5A,0xcc,0xcc,0xcc,0xcc,0xcc,0x96,
  2803. - 0x48,0xcc,0x49,0xcc,0xcc,0xcc,0x85,0xcc,0x59,0xcc,0x5A,0xcc,0xcc,0xcc,0x96,0xcc,
  2804. - 0x48,0xcc,0xcc,0x49,0xcc,0x85,0xcc,0xcc,0x59,0xcc,0xcc,0x5A,0xcc,0x96,0xcc,0xcc,
  2805. - 0x48,0xcc,0xcc,0xcc,0x49,0xcc,0xcc,0xcc,0x59,0xcc,0xcc,0xcc,0x96,0xcc,0xcc,0xcc,
  2806. - 0x48,0xcc,0xcc,0x85,0xcc,0x49,0xcc,0xcc,0x59,0xcc,0xcc,0x96,0xcc,0x5A,0xcc,0xcc,
  2807. - 0x48,0xcc,0x85,0xcc,0xcc,0xcc,0x49,0xcc,0x59,0xcc,0x96,0xcc,0xcc,0xcc,0x5A,0xcc,
  2808. - 0x48,0x85,0xcc,0xcc,0xcc,0xcc,0xcc,0x49,0x59,0x96,0xcc,0xcc,0xcc,0xcc,0xcc,0x5A,
  2809. - };
  2810. -
  2811. - static const uint8_t needs[16]={
  2812. - 0,1,0,0,
  2813. - 2,4,2,0,
  2814. - 0,1,0,0,
  2815. - 15
  2816. - };
  2817. -
  2818. - int x, y, b, r, l;
  2819. - int16_t tmpIt [64*(32+HTAPS_MAX)];
  2820. - uint8_t tmp2t[3][64*(32+HTAPS_MAX)];
  2821. - int16_t *tmpI= tmpIt;
  2822. - uint8_t *tmp2= tmp2t[0];
  2823. - const uint8_t *hpel[11];
  2824. - av_assert2(dx<16 && dy<16);
  2825. - r= brane[dx + 16*dy]&15;
  2826. - l= brane[dx + 16*dy]>>4;
  2827. -
  2828. - b= needs[l] | needs[r];
  2829. - if(p && !p->diag_mc)
  2830. - b= 15;
  2831. -
  2832. - if(b&5){
  2833. - for(y=0; y < b_h+HTAPS_MAX-1; y++){
  2834. - for(x=0; x < b_w; x++){
  2835. - int a_1=src[x + HTAPS_MAX/2-4];
  2836. - int a0= src[x + HTAPS_MAX/2-3];
  2837. - int a1= src[x + HTAPS_MAX/2-2];
  2838. - int a2= src[x + HTAPS_MAX/2-1];
  2839. - int a3= src[x + HTAPS_MAX/2+0];
  2840. - int a4= src[x + HTAPS_MAX/2+1];
  2841. - int a5= src[x + HTAPS_MAX/2+2];
  2842. - int a6= src[x + HTAPS_MAX/2+3];
  2843. - int am=0;
  2844. - if(!p || p->fast_mc){
  2845. - am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
  2846. - tmpI[x]= am;
  2847. - am= (am+16)>>5;
  2848. - }else{
  2849. - am= p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6);
  2850. - tmpI[x]= am;
  2851. - am= (am+32)>>6;
  2852. - }
  2853. -
  2854. - if(am&(~255)) am= ~(am>>31);
  2855. - tmp2[x]= am;
  2856. - }
  2857. - tmpI+= 64;
  2858. - tmp2+= 64;
  2859. - src += stride;
  2860. - }
  2861. - src -= stride*y;
  2862. - }
  2863. - src += HTAPS_MAX/2 - 1;
  2864. - tmp2= tmp2t[1];
  2865. -
  2866. - if(b&2){
  2867. - for(y=0; y < b_h; y++){
  2868. - for(x=0; x < b_w+1; x++){
  2869. - int a_1=src[x + (HTAPS_MAX/2-4)*stride];
  2870. - int a0= src[x + (HTAPS_MAX/2-3)*stride];
  2871. - int a1= src[x + (HTAPS_MAX/2-2)*stride];
  2872. - int a2= src[x + (HTAPS_MAX/2-1)*stride];
  2873. - int a3= src[x + (HTAPS_MAX/2+0)*stride];
  2874. - int a4= src[x + (HTAPS_MAX/2+1)*stride];
  2875. - int a5= src[x + (HTAPS_MAX/2+2)*stride];
  2876. - int a6= src[x + (HTAPS_MAX/2+3)*stride];
  2877. - int am=0;
  2878. - if(!p || p->fast_mc)
  2879. - am= (20*(a2+a3) - 5*(a1+a4) + (a0+a5) + 16)>>5;
  2880. - else
  2881. - am= (p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6) + 32)>>6;
  2882. -
  2883. - if(am&(~255)) am= ~(am>>31);
  2884. - tmp2[x]= am;
  2885. - }
  2886. - src += stride;
  2887. - tmp2+= 64;
  2888. - }
  2889. - src -= stride*y;
  2890. - }
  2891. - src += stride*(HTAPS_MAX/2 - 1);
  2892. - tmp2= tmp2t[2];
  2893. - tmpI= tmpIt;
  2894. - if(b&4){
  2895. - for(y=0; y < b_h; y++){
  2896. - for(x=0; x < b_w; x++){
  2897. - int a_1=tmpI[x + (HTAPS_MAX/2-4)*64];
  2898. - int a0= tmpI[x + (HTAPS_MAX/2-3)*64];
  2899. - int a1= tmpI[x + (HTAPS_MAX/2-2)*64];
  2900. - int a2= tmpI[x + (HTAPS_MAX/2-1)*64];
  2901. - int a3= tmpI[x + (HTAPS_MAX/2+0)*64];
  2902. - int a4= tmpI[x + (HTAPS_MAX/2+1)*64];
  2903. - int a5= tmpI[x + (HTAPS_MAX/2+2)*64];
  2904. - int a6= tmpI[x + (HTAPS_MAX/2+3)*64];
  2905. - int am=0;
  2906. - if(!p || p->fast_mc)
  2907. - am= (20*(a2+a3) - 5*(a1+a4) + (a0+a5) + 512)>>10;
  2908. - else
  2909. - am= (p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6) + 2048)>>12;
  2910. - if(am&(~255)) am= ~(am>>31);
  2911. - tmp2[x]= am;
  2912. - }
  2913. - tmpI+= 64;
  2914. - tmp2+= 64;
  2915. - }
  2916. - }
  2917. -
  2918. - hpel[ 0]= src;
  2919. - hpel[ 1]= tmp2t[0] + 64*(HTAPS_MAX/2-1);
  2920. - hpel[ 2]= src + 1;
  2921. -
  2922. - hpel[ 4]= tmp2t[1];
  2923. - hpel[ 5]= tmp2t[2];
  2924. - hpel[ 6]= tmp2t[1] + 1;
  2925. -
  2926. - hpel[ 8]= src + stride;
  2927. - hpel[ 9]= hpel[1] + 64;
  2928. - hpel[10]= hpel[8] + 1;
  2929. -
  2930. -#define MC_STRIDE(x) (needs[x] ? 64 : stride)
  2931. -
  2932. - if(b==15){
  2933. - int dxy = dx / 8 + dy / 8 * 4;
  2934. - const uint8_t *src1 = hpel[dxy ];
  2935. - const uint8_t *src2 = hpel[dxy + 1];
  2936. - const uint8_t *src3 = hpel[dxy + 4];
  2937. - const uint8_t *src4 = hpel[dxy + 5];
  2938. - int stride1 = MC_STRIDE(dxy);
  2939. - int stride2 = MC_STRIDE(dxy + 1);
  2940. - int stride3 = MC_STRIDE(dxy + 4);
  2941. - int stride4 = MC_STRIDE(dxy + 5);
  2942. - dx&=7;
  2943. - dy&=7;
  2944. - for(y=0; y < b_h; y++){
  2945. - for(x=0; x < b_w; x++){
  2946. - dst[x]= ((8-dx)*(8-dy)*src1[x] + dx*(8-dy)*src2[x]+
  2947. - (8-dx)* dy *src3[x] + dx* dy *src4[x]+32)>>6;
  2948. - }
  2949. - src1+=stride1;
  2950. - src2+=stride2;
  2951. - src3+=stride3;
  2952. - src4+=stride4;
  2953. - dst +=stride;
  2954. - }
  2955. - }else{
  2956. - const uint8_t *src1= hpel[l];
  2957. - const uint8_t *src2= hpel[r];
  2958. - int stride1 = MC_STRIDE(l);
  2959. - int stride2 = MC_STRIDE(r);
  2960. - int a= weight[((dx&7) + (8*(dy&7)))];
  2961. - int b= 8-a;
  2962. - for(y=0; y < b_h; y++){
  2963. - for(x=0; x < b_w; x++){
  2964. - dst[x]= (a*src1[x] + b*src2[x] + 4)>>3;
  2965. - }
  2966. - src1+=stride1;
  2967. - src2+=stride2;
  2968. - dst +=stride;
  2969. - }
  2970. - }
  2971. -}
  2972. -
  2973. -void ff_snow_pred_block(SnowContext *s, uint8_t *dst, uint8_t *tmp, ptrdiff_t stride, int sx, int sy, int b_w, int b_h, const BlockNode *block, int plane_index, int w, int h){
  2974. - if(block->type & BLOCK_INTRA){
  2975. - int x, y;
  2976. - const unsigned color = block->color[plane_index];
  2977. - const unsigned color4 = color*0x01010101;
  2978. - if(b_w==32){
  2979. - for(y=0; y < b_h; y++){
  2980. - *(uint32_t*)&dst[0 + y*stride]= color4;
  2981. - *(uint32_t*)&dst[4 + y*stride]= color4;
  2982. - *(uint32_t*)&dst[8 + y*stride]= color4;
  2983. - *(uint32_t*)&dst[12+ y*stride]= color4;
  2984. - *(uint32_t*)&dst[16+ y*stride]= color4;
  2985. - *(uint32_t*)&dst[20+ y*stride]= color4;
  2986. - *(uint32_t*)&dst[24+ y*stride]= color4;
  2987. - *(uint32_t*)&dst[28+ y*stride]= color4;
  2988. - }
  2989. - }else if(b_w==16){
  2990. - for(y=0; y < b_h; y++){
  2991. - *(uint32_t*)&dst[0 + y*stride]= color4;
  2992. - *(uint32_t*)&dst[4 + y*stride]= color4;
  2993. - *(uint32_t*)&dst[8 + y*stride]= color4;
  2994. - *(uint32_t*)&dst[12+ y*stride]= color4;
  2995. - }
  2996. - }else if(b_w==8){
  2997. - for(y=0; y < b_h; y++){
  2998. - *(uint32_t*)&dst[0 + y*stride]= color4;
  2999. - *(uint32_t*)&dst[4 + y*stride]= color4;
  3000. - }
  3001. - }else if(b_w==4){
  3002. - for(y=0; y < b_h; y++){
  3003. - *(uint32_t*)&dst[0 + y*stride]= color4;
  3004. - }
  3005. - }else{
  3006. - for(y=0; y < b_h; y++){
  3007. - for(x=0; x < b_w; x++){
  3008. - dst[x + y*stride]= color;
  3009. - }
  3010. - }
  3011. - }
  3012. - }else{
  3013. - uint8_t *src= s->last_picture[block->ref]->data[plane_index];
  3014. - const int scale= plane_index ? (2*s->mv_scale)>>s->chroma_h_shift : 2*s->mv_scale;
  3015. - int mx= block->mx*scale;
  3016. - int my= block->my*scale;
  3017. - const int dx= mx&15;
  3018. - const int dy= my&15;
  3019. - const int tab_index= 3 - (b_w>>2) + (b_w>>4);
  3020. - sx += (mx>>4) - (HTAPS_MAX/2-1);
  3021. - sy += (my>>4) - (HTAPS_MAX/2-1);
  3022. - src += sx + sy*stride;
  3023. - if( (unsigned)sx >= FFMAX(w - b_w - (HTAPS_MAX-2), 0)
  3024. - || (unsigned)sy >= FFMAX(h - b_h - (HTAPS_MAX-2), 0)){
  3025. - s->vdsp.emulated_edge_mc(tmp + MB_SIZE, src,
  3026. - stride, stride,
  3027. - b_w+HTAPS_MAX-1, b_h+HTAPS_MAX-1,
  3028. - sx, sy, w, h);
  3029. - src= tmp + MB_SIZE;
  3030. - }
  3031. -
  3032. - av_assert2(s->chroma_h_shift == s->chroma_v_shift); // only one mv_scale
  3033. -
  3034. - av_assert2((tab_index>=0 && tab_index<4) || b_w==32);
  3035. - if( (dx&3) || (dy&3)
  3036. - || !(b_w == b_h || 2*b_w == b_h || b_w == 2*b_h)
  3037. - || (b_w&(b_w-1))
  3038. - || b_w == 1
  3039. - || b_h == 1
  3040. - || !s->plane[plane_index].fast_mc )
  3041. - mc_block(&s->plane[plane_index], dst, src, stride, b_w, b_h, dx, dy);
  3042. - else if(b_w==32){
  3043. - int y;
  3044. - for(y=0; y<b_h; y+=16){
  3045. - s->h264qpel.put_h264_qpel_pixels_tab[0][dy+(dx>>2)](dst + y*stride, src + 3 + (y+3)*stride,stride);
  3046. - s->h264qpel.put_h264_qpel_pixels_tab[0][dy+(dx>>2)](dst + 16 + y*stride, src + 19 + (y+3)*stride,stride);
  3047. - }
  3048. - }else if(b_w==b_h)
  3049. - s->h264qpel.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst,src + 3 + 3*stride,stride);
  3050. - else if(b_w==2*b_h){
  3051. - s->h264qpel.put_h264_qpel_pixels_tab[tab_index+1][dy+(dx>>2)](dst ,src + 3 + 3*stride,stride);
  3052. - s->h264qpel.put_h264_qpel_pixels_tab[tab_index+1][dy+(dx>>2)](dst+b_h,src + 3 + b_h + 3*stride,stride);
  3053. - }else{
  3054. - av_assert2(2*b_w==b_h);
  3055. - s->h264qpel.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst ,src + 3 + 3*stride ,stride);
  3056. - s->h264qpel.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst+b_w*stride,src + 3 + 3*stride+b_w*stride,stride);
  3057. - }
  3058. - }
  3059. -}
  3060. -
  3061. -#define mca(dx,dy,b_w)\
  3062. -static void mc_block_hpel ## dx ## dy ## b_w(uint8_t *dst, const uint8_t *src, ptrdiff_t stride, int h){\
  3063. - av_assert2(h==b_w);\
  3064. - mc_block(NULL, dst, src-(HTAPS_MAX/2-1)-(HTAPS_MAX/2-1)*stride, stride, b_w, b_w, dx, dy);\
  3065. -}
  3066. -
  3067. -mca( 0, 0,16)
  3068. -mca( 8, 0,16)
  3069. -mca( 0, 8,16)
  3070. -mca( 8, 8,16)
  3071. -mca( 0, 0,8)
  3072. -mca( 8, 0,8)
  3073. -mca( 0, 8,8)
  3074. -mca( 8, 8,8)
  3075. -
  3076. av_cold int ff_snow_common_init(AVCodecContext *avctx){
  3077. SnowContext *s = avctx->priv_data;
  3078. - int width, height;
  3079. - int i, j;
  3080. + int width, height, ret;
  3081.  
  3082. s->avctx= avctx;
  3083. - s->max_ref_frames=1; //just make sure it's not an invalid value in case of no initial keyframe
  3084. s->spatial_decomposition_count = 1;
  3085.  
  3086. - ff_me_cmp_init(&s->mecc, avctx);
  3087. - ff_hpeldsp_init(&s->hdsp, avctx->flags);
  3088. - ff_videodsp_init(&s->vdsp, 8);
  3089. - ff_dwt_init(&s->dwt);
  3090. - ff_h264qpel_init(&s->h264qpel, 8);
  3091. -
  3092. -#define mcf(dx,dy)\
  3093. - s->qdsp.put_qpel_pixels_tab [0][dy+dx/4]=\
  3094. - s->qdsp.put_no_rnd_qpel_pixels_tab[0][dy+dx/4]=\
  3095. - s->h264qpel.put_h264_qpel_pixels_tab[0][dy+dx/4];\
  3096. - s->qdsp.put_qpel_pixels_tab [1][dy+dx/4]=\
  3097. - s->qdsp.put_no_rnd_qpel_pixels_tab[1][dy+dx/4]=\
  3098. - s->h264qpel.put_h264_qpel_pixels_tab[1][dy+dx/4];
  3099. -
  3100. - mcf( 0, 0)
  3101. - mcf( 4, 0)
  3102. - mcf( 8, 0)
  3103. - mcf(12, 0)
  3104. - mcf( 0, 4)
  3105. - mcf( 4, 4)
  3106. - mcf( 8, 4)
  3107. - mcf(12, 4)
  3108. - mcf( 0, 8)
  3109. - mcf( 4, 8)
  3110. - mcf( 8, 8)
  3111. - mcf(12, 8)
  3112. - mcf( 0,12)
  3113. - mcf( 4,12)
  3114. - mcf( 8,12)
  3115. - mcf(12,12)
  3116. -
  3117. -#define mcfh(dx,dy)\
  3118. - s->hdsp.put_pixels_tab [0][dy/4+dx/8]=\
  3119. - s->hdsp.put_no_rnd_pixels_tab[0][dy/4+dx/8]=\
  3120. - mc_block_hpel ## dx ## dy ## 16;\
  3121. - s->hdsp.put_pixels_tab [1][dy/4+dx/8]=\
  3122. - s->hdsp.put_no_rnd_pixels_tab[1][dy/4+dx/8]=\
  3123. - mc_block_hpel ## dx ## dy ## 8;
  3124. -
  3125. - mcfh(0, 0)
  3126. - mcfh(8, 0)
  3127. - mcfh(0, 8)
  3128. - mcfh(8, 8)
  3129. -
  3130. - init_qexp();
  3131. -
  3132. // dec += FFMAX(s->chroma_h_shift, s->chroma_v_shift);
  3133.  
  3134. width= s->avctx->width;
  3135. height= s->avctx->height;
  3136.  
  3137. - FF_ALLOCZ_ARRAY_OR_GOTO(avctx, s->spatial_idwt_buffer, width, height * sizeof(IDWTELEM), fail);
  3138. FF_ALLOCZ_ARRAY_OR_GOTO(avctx, s->spatial_dwt_buffer, width, height * sizeof(DWTELEM), fail); //FIXME this does not belong here
  3139. FF_ALLOCZ_ARRAY_OR_GOTO(avctx, s->temp_dwt_buffer, width, sizeof(DWTELEM), fail);
  3140. FF_ALLOCZ_ARRAY_OR_GOTO(avctx, s->temp_idwt_buffer, width, sizeof(IDWTELEM), fail);
  3141. FF_ALLOC_ARRAY_OR_GOTO(avctx, s->run_buffer, ((width + 1) >> 1), ((height + 1) >> 1) * sizeof(*s->run_buffer), fail);
  3142.  
  3143. - for(i=0; i<MAX_REF_FRAMES; i++) {
  3144. - for(j=0; j<MAX_REF_FRAMES; j++)
  3145. - ff_scale_mv_ref[i][j] = 256*(i+1)/(j+1);
  3146. - s->last_picture[i] = av_frame_alloc();
  3147. - if (!s->last_picture[i])
  3148. - goto fail;
  3149. - }
  3150. -
  3151. - s->mconly_picture = av_frame_alloc();
  3152. - s->current_picture = av_frame_alloc();
  3153. - if (!s->mconly_picture || !s->current_picture)
  3154. - goto fail;
  3155. + if ((ret = ff_obmc_common_init(&s->obmc, avctx)) < 0)
  3156. + return ret;
  3157.  
  3158. return 0;
  3159. fail:
  3160. @@ -513,22 +109,10 @@ fail:
  3161.  
  3162. int ff_snow_common_init_after_header(AVCodecContext *avctx) {
  3163. SnowContext *s = avctx->priv_data;
  3164. - int plane_index, level, orientation;
  3165. - int ret, emu_buf_size;
  3166. -
  3167. - if(!s->scratchbuf) {
  3168. - if ((ret = ff_get_buffer(s->avctx, s->mconly_picture,
  3169. - AV_GET_BUFFER_FLAG_REF)) < 0)
  3170. - return ret;
  3171. - FF_ALLOCZ_ARRAY_OR_GOTO(avctx, s->scratchbuf, FFMAX(s->mconly_picture->linesize[0], 2*avctx->width+256), 7*MB_SIZE, fail);
  3172. - emu_buf_size = FFMAX(s->mconly_picture->linesize[0], 2*avctx->width+256) * (2 * MB_SIZE + HTAPS_MAX - 1);
  3173. - FF_ALLOC_OR_GOTO(avctx, s->emu_edge_buffer, emu_buf_size, fail);
  3174. - }
  3175. + int plane_index, level, orientation, ret;
  3176.  
  3177. - if(s->mconly_picture->format != avctx->pix_fmt) {
  3178. - av_log(avctx, AV_LOG_ERROR, "pixel format changed\n");
  3179. - return AVERROR_INVALIDDATA;
  3180. - }
  3181. + if ((ret = ff_obmc_common_init_after_header(&s->obmc)) < 0)
  3182. + return ret;
  3183.  
  3184. for(plane_index=0; plane_index < s->nb_planes; plane_index++){
  3185. int w= s->avctx->width;
  3186. @@ -563,7 +147,7 @@ int ff_snow_common_init_after_header(AVCodecContext *avctx) {
  3187. b->buf += b->stride>>1;
  3188. b->buf_y_offset = b->stride_line >> 1;
  3189. }
  3190. - b->ibuf= s->spatial_idwt_buffer + (b->buf - s->spatial_dwt_buffer);
  3191. + b->ibuf= s->obmc.spatial_idwt_buffer + (b->buf - s->spatial_dwt_buffer);
  3192.  
  3193. if(level)
  3194. b->parent= &s->plane[plane_index].band[level-1][orientation];
  3195. @@ -583,142 +167,15 @@ fail:
  3196. return AVERROR(ENOMEM);
  3197. }
  3198.  
  3199. -#define USE_HALFPEL_PLANE 0
  3200. -
  3201. -static int halfpel_interpol(SnowContext *s, uint8_t *halfpel[4][4], AVFrame *frame){
  3202. - int p,x,y;
  3203. -
  3204. - for(p=0; p < s->nb_planes; p++){
  3205. - int is_chroma= !!p;
  3206. - int w= is_chroma ? AV_CEIL_RSHIFT(s->avctx->width, s->chroma_h_shift) : s->avctx->width;
  3207. - int h= is_chroma ? AV_CEIL_RSHIFT(s->avctx->height, s->chroma_v_shift) : s->avctx->height;
  3208. - int ls= frame->linesize[p];
  3209. - uint8_t *src= frame->data[p];
  3210. -
  3211. - halfpel[1][p] = av_malloc_array(ls, (h + 2 * EDGE_WIDTH));
  3212. - halfpel[2][p] = av_malloc_array(ls, (h + 2 * EDGE_WIDTH));
  3213. - halfpel[3][p] = av_malloc_array(ls, (h + 2 * EDGE_WIDTH));
  3214. - if (!halfpel[1][p] || !halfpel[2][p] || !halfpel[3][p]) {
  3215. - av_freep(&halfpel[1][p]);
  3216. - av_freep(&halfpel[2][p]);
  3217. - av_freep(&halfpel[3][p]);
  3218. - return AVERROR(ENOMEM);
  3219. - }
  3220. - halfpel[1][p] += EDGE_WIDTH * (1 + ls);
  3221. - halfpel[2][p] += EDGE_WIDTH * (1 + ls);
  3222. - halfpel[3][p] += EDGE_WIDTH * (1 + ls);
  3223. -
  3224. - halfpel[0][p]= src;
  3225. - for(y=0; y<h; y++){
  3226. - for(x=0; x<w; x++){
  3227. - int i= y*ls + x;
  3228. -
  3229. - halfpel[1][p][i]= (20*(src[i] + src[i+1]) - 5*(src[i-1] + src[i+2]) + (src[i-2] + src[i+3]) + 16 )>>5;
  3230. - }
  3231. - }
  3232. - for(y=0; y<h; y++){
  3233. - for(x=0; x<w; x++){
  3234. - int i= y*ls + x;
  3235. -
  3236. - halfpel[2][p][i]= (20*(src[i] + src[i+ls]) - 5*(src[i-ls] + src[i+2*ls]) + (src[i-2*ls] + src[i+3*ls]) + 16 )>>5;
  3237. - }
  3238. - }
  3239. - src= halfpel[1][p];
  3240. - for(y=0; y<h; y++){
  3241. - for(x=0; x<w; x++){
  3242. - int i= y*ls + x;
  3243. -
  3244. - halfpel[3][p][i]= (20*(src[i] + src[i+ls]) - 5*(src[i-ls] + src[i+2*ls]) + (src[i-2*ls] + src[i+3*ls]) + 16 )>>5;
  3245. - }
  3246. - }
  3247. -
  3248. -//FIXME border!
  3249. - }
  3250. - return 0;
  3251. -}
  3252. -
  3253. -void ff_snow_release_buffer(AVCodecContext *avctx)
  3254. -{
  3255. - SnowContext *s = avctx->priv_data;
  3256. - int i;
  3257. -
  3258. - if(s->last_picture[s->max_ref_frames-1]->data[0]){
  3259. - av_frame_unref(s->last_picture[s->max_ref_frames-1]);
  3260. - for(i=0; i<9; i++)
  3261. - if(s->halfpel_plane[s->max_ref_frames-1][1+i/3][i%3]) {
  3262. - av_free(s->halfpel_plane[s->max_ref_frames-1][1+i/3][i%3] - EDGE_WIDTH*(1+s->current_picture->linesize[i%3]));
  3263. - s->halfpel_plane[s->max_ref_frames-1][1+i/3][i%3] = NULL;
  3264. - }
  3265. - }
  3266. -}
  3267. -
  3268. -int ff_snow_frame_start(SnowContext *s){
  3269. - AVFrame *tmp;
  3270. - int i, ret;
  3271. -
  3272. - ff_snow_release_buffer(s->avctx);
  3273. -
  3274. - tmp= s->last_picture[s->max_ref_frames-1];
  3275. - for(i=s->max_ref_frames-1; i>0; i--)
  3276. - s->last_picture[i] = s->last_picture[i-1];
  3277. - memmove(s->halfpel_plane+1, s->halfpel_plane, (s->max_ref_frames-1)*sizeof(void*)*4*4);
  3278. - if(USE_HALFPEL_PLANE && s->current_picture->data[0]) {
  3279. - if((ret = halfpel_interpol(s, s->halfpel_plane[0], s->current_picture)) < 0)
  3280. - return ret;
  3281. - }
  3282. - s->last_picture[0] = s->current_picture;
  3283. - s->current_picture = tmp;
  3284. -
  3285. - if(s->keyframe){
  3286. - s->ref_frames= 0;
  3287. - }else{
  3288. - int i;
  3289. - for(i=0; i<s->max_ref_frames && s->last_picture[i]->data[0]; i++)
  3290. - if(i && s->last_picture[i-1]->key_frame)
  3291. - break;
  3292. - s->ref_frames= i;
  3293. - if(s->ref_frames==0){
  3294. - av_log(s->avctx,AV_LOG_ERROR, "No reference frames\n");
  3295. - return AVERROR_INVALIDDATA;
  3296. - }
  3297. - }
  3298. - if ((ret = ff_snow_get_buffer(s, s->current_picture)) < 0)
  3299. - return ret;
  3300. -
  3301. - s->current_picture->key_frame= s->keyframe;
  3302. -
  3303. - return 0;
  3304. -}
  3305. -
  3306. av_cold void ff_snow_common_end(SnowContext *s)
  3307. {
  3308. - int plane_index, level, orientation, i;
  3309. + int plane_index, level, orientation;
  3310.  
  3311. av_freep(&s->spatial_dwt_buffer);
  3312. av_freep(&s->temp_dwt_buffer);
  3313. - av_freep(&s->spatial_idwt_buffer);
  3314. av_freep(&s->temp_idwt_buffer);
  3315. av_freep(&s->run_buffer);
  3316.  
  3317. - s->m.me.temp= NULL;
  3318. - av_freep(&s->m.me.scratchpad);
  3319. - av_freep(&s->m.me.map);
  3320. - av_freep(&s->m.me.score_map);
  3321. - av_freep(&s->m.sc.obmc_scratchpad);
  3322. -
  3323. - av_freep(&s->block);
  3324. - av_freep(&s->scratchbuf);
  3325. - av_freep(&s->emu_edge_buffer);
  3326. -
  3327. - for(i=0; i<MAX_REF_FRAMES; i++){
  3328. - av_freep(&s->ref_mvs[i]);
  3329. - av_freep(&s->ref_scores[i]);
  3330. - if(s->last_picture[i] && s->last_picture[i]->data[0]) {
  3331. - av_assert0(s->last_picture[i]->data[0] != s->current_picture->data[0]);
  3332. - }
  3333. - av_frame_free(&s->last_picture[i]);
  3334. - }
  3335. -
  3336. for(plane_index=0; plane_index < MAX_PLANES; plane_index++){
  3337. for(level=MAX_DECOMPOSITIONS-1; level>=0; level--){
  3338. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3339. @@ -728,6 +185,6 @@ av_cold void ff_snow_common_end(SnowContext *s)
  3340. }
  3341. }
  3342. }
  3343. - av_frame_free(&s->mconly_picture);
  3344. - av_frame_free(&s->current_picture);
  3345. +
  3346. + ff_obmc_close(&s->obmc);
  3347. }
  3348. diff --git a/libavcodec/snow.h b/libavcodec/snow.h
  3349. index 59c710b..712ed7d 100644
  3350. --- a/libavcodec/snow.h
  3351. +++ b/libavcodec/snow.h
  3352. @@ -32,48 +32,11 @@
  3353. #include "rangecoder.h"
  3354. #include "mathops.h"
  3355.  
  3356. -#define FF_MPV_OFFSET(x) (offsetof(MpegEncContext, x) + offsetof(SnowContext, m))
  3357. -#include "mpegvideo.h"
  3358. -#include "h264qpel.h"
  3359. +#define FF_MPV_OFFSET(x) (offsetof(MpegEncContext, x) + offsetof(SnowContext, obmc.m))
  3360. +#include "obmemc.h"
  3361.  
  3362. #define MID_STATE 128
  3363.  
  3364. -#define MAX_PLANES 4
  3365. -#define QSHIFT 5
  3366. -#define QROOT (1<<QSHIFT)
  3367. -#define LOSSLESS_QLOG -128
  3368. -#define FRAC_BITS 4
  3369. -#define MAX_REF_FRAMES 8
  3370. -
  3371. -#define LOG2_OBMC_MAX 8
  3372. -#define OBMC_MAX (1<<(LOG2_OBMC_MAX))
  3373. -typedef struct BlockNode{
  3374. - int16_t mx; ///< Motion vector component X, see mv_scale
  3375. - int16_t my; ///< Motion vector component Y, see mv_scale
  3376. - uint8_t ref; ///< Reference frame index
  3377. - uint8_t color[3]; ///< Color for intra
  3378. - uint8_t type; ///< Bitfield of BLOCK_*
  3379. -//#define TYPE_SPLIT 1
  3380. -#define BLOCK_INTRA 1 ///< Intra block, inter otherwise
  3381. -#define BLOCK_OPT 2 ///< Block needs no checks in this round of iterative motion estiation
  3382. -//#define TYPE_NOCOLOR 4
  3383. - uint8_t level; //FIXME merge into type?
  3384. -}BlockNode;
  3385. -
  3386. -static const BlockNode null_block= { //FIXME add border maybe
  3387. - .color= {128,128,128},
  3388. - .mx= 0,
  3389. - .my= 0,
  3390. - .ref= 0,
  3391. - .type= 0,
  3392. - .level= 0,
  3393. -};
  3394. -
  3395. -#define LOG2_MB_SIZE 4
  3396. -#define MB_SIZE (1<<LOG2_MB_SIZE)
  3397. -#define ENCODER_EXTRA_BITS 4
  3398. -#define HTAPS_MAX 8
  3399. -
  3400. typedef struct x_and_coeff{
  3401. int16_t x;
  3402. uint16_t coeff;
  3403. @@ -99,33 +62,12 @@ typedef struct Plane{
  3404. int width;
  3405. int height;
  3406. SubBand band[MAX_DECOMPOSITIONS][4];
  3407. -
  3408. - int htaps;
  3409. - int8_t hcoeff[HTAPS_MAX/2];
  3410. - int diag_mc;
  3411. - int fast_mc;
  3412. -
  3413. - int last_htaps;
  3414. - int8_t last_hcoeff[HTAPS_MAX/2];
  3415. - int last_diag_mc;
  3416. -}Plane;
  3417. +} Plane;
  3418.  
  3419. typedef struct SnowContext{
  3420. AVClass *class;
  3421. AVCodecContext *avctx;
  3422. RangeCoder c;
  3423. - MECmpContext mecc;
  3424. - HpelDSPContext hdsp;
  3425. - QpelDSPContext qdsp;
  3426. - VideoDSPContext vdsp;
  3427. - H264QpelContext h264qpel;
  3428. - MpegvideoEncDSPContext mpvencdsp;
  3429. - SnowDWTContext dwt;
  3430. - AVFrame *input_picture; ///< new_picture with the internal linesizes
  3431. - AVFrame *current_picture;
  3432. - AVFrame *last_picture[MAX_REF_FRAMES];
  3433. - uint8_t *halfpel_plane[MAX_REF_FRAMES][4][4];
  3434. - AVFrame *mconly_picture;
  3435. // uint8_t q_context[16];
  3436. uint8_t header_state[32];
  3437. uint8_t block_state[128 + 32*128];
  3438. @@ -138,13 +80,8 @@ typedef struct SnowContext{
  3439. int spatial_decomposition_count;
  3440. int last_spatial_decomposition_count;
  3441. int temporal_decomposition_count;
  3442. - int max_ref_frames;
  3443. - int ref_frames;
  3444. - int16_t (*ref_mvs[MAX_REF_FRAMES])[2];
  3445. - uint32_t *ref_scores[MAX_REF_FRAMES];
  3446. DWTELEM *spatial_dwt_buffer;
  3447. DWTELEM *temp_dwt_buffer;
  3448. - IDWTELEM *spatial_idwt_buffer;
  3449. IDWTELEM *temp_idwt_buffer;
  3450. int *run_buffer;
  3451. int colorspace_type;
  3452. @@ -153,49 +90,24 @@ typedef struct SnowContext{
  3453. int spatial_scalability;
  3454. int qlog;
  3455. int last_qlog;
  3456. - int lambda;
  3457. - int lambda2;
  3458. int pass1_rc;
  3459. - int mv_scale;
  3460. - int last_mv_scale;
  3461. int qbias;
  3462. int last_qbias;
  3463. #define QBIAS_SHIFT 3
  3464. - int b_width;
  3465. - int b_height;
  3466. - int block_max_depth;
  3467. - int last_block_max_depth;
  3468. int nb_planes;
  3469. Plane plane[MAX_PLANES];
  3470. - BlockNode *block;
  3471. -#define ME_CACHE_SIZE 1024
  3472. - unsigned me_cache[ME_CACHE_SIZE];
  3473. - unsigned me_cache_generation;
  3474. slice_buffer sb;
  3475. int memc_only;
  3476. int no_bitstream;
  3477. - int intra_penalty;
  3478. - int motion_est;
  3479. - int iterative_dia_size;
  3480. int scenechange_threshold;
  3481.  
  3482. - MpegEncContext m; // needed for motion estimation, should not be used for anything else, the idea is to eventually make the motion estimation independent of MpegEncContext, so this will be removed then (FIXME/XXX)
  3483. -
  3484. - uint8_t *scratchbuf;
  3485. - uint8_t *emu_edge_buffer;
  3486. -
  3487. - AVMotionVector *avmv;
  3488. - int avmv_index;
  3489. uint64_t encoding_error[AV_NUM_DATA_POINTERS];
  3490.  
  3491. + OBMCContext obmc;
  3492. +
  3493. int pred;
  3494. }SnowContext;
  3495.  
  3496. -/* Tables */
  3497. -extern const uint8_t * const ff_obmc_tab[4];
  3498. -extern uint8_t ff_qexp[QROOT];
  3499. -extern int ff_scale_mv_ref[MAX_REF_FRAMES][MAX_REF_FRAMES];
  3500. -
  3501. /* C bits used by mmx/sse2/altivec */
  3502.  
  3503. static av_always_inline void snow_interleave_line_header(int * i, int width, IDWTELEM * low, IDWTELEM * high){
  3504. @@ -239,265 +151,7 @@ static av_always_inline void snow_horizontal_compose_liftS_lead_out(int i, IDWTE
  3505. int ff_snow_common_init(AVCodecContext *avctx);
  3506. int ff_snow_common_init_after_header(AVCodecContext *avctx);
  3507. void ff_snow_common_end(SnowContext *s);
  3508. -void ff_snow_release_buffer(AVCodecContext *avctx);
  3509. void ff_snow_reset_contexts(SnowContext *s);
  3510. -int ff_snow_alloc_blocks(SnowContext *s);
  3511. -int ff_snow_frame_start(SnowContext *s);
  3512. -void ff_snow_pred_block(SnowContext *s, uint8_t *dst, uint8_t *tmp, ptrdiff_t stride,
  3513. - int sx, int sy, int b_w, int b_h, const BlockNode *block,
  3514. - int plane_index, int w, int h);
  3515. -int ff_snow_get_buffer(SnowContext *s, AVFrame *frame);
  3516. -/* common inline functions */
  3517. -//XXX doublecheck all of them should stay inlined
  3518. -
  3519. -static inline void pred_mv(SnowContext *s, int *mx, int *my, int ref,
  3520. - const BlockNode *left, const BlockNode *top, const BlockNode *tr){
  3521. - if(s->ref_frames == 1){
  3522. - *mx = mid_pred(left->mx, top->mx, tr->mx);
  3523. - *my = mid_pred(left->my, top->my, tr->my);
  3524. - }else{
  3525. - const int *scale = ff_scale_mv_ref[ref];
  3526. - *mx = mid_pred((left->mx * scale[left->ref] + 128) >>8,
  3527. - (top ->mx * scale[top ->ref] + 128) >>8,
  3528. - (tr ->mx * scale[tr ->ref] + 128) >>8);
  3529. - *my = mid_pred((left->my * scale[left->ref] + 128) >>8,
  3530. - (top ->my * scale[top ->ref] + 128) >>8,
  3531. - (tr ->my * scale[tr ->ref] + 128) >>8);
  3532. - }
  3533. -}
  3534. -
  3535. -static av_always_inline int same_block(BlockNode *a, BlockNode *b){
  3536. - if((a->type&BLOCK_INTRA) && (b->type&BLOCK_INTRA)){
  3537. - return !((a->color[0] - b->color[0]) | (a->color[1] - b->color[1]) | (a->color[2] - b->color[2]));
  3538. - }else{
  3539. - return !((a->mx - b->mx) | (a->my - b->my) | (a->ref - b->ref) | ((a->type ^ b->type)&BLOCK_INTRA));
  3540. - }
  3541. -}
  3542. -
  3543. -//FIXME name cleanup (b_w, block_w, b_width stuff)
  3544. -//XXX should we really inline it?
  3545. -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){
  3546. - const int b_width = s->b_width << s->block_max_depth;
  3547. - const int b_height= s->b_height << s->block_max_depth;
  3548. - const int b_stride= b_width;
  3549. - BlockNode *lt= &s->block[b_x + b_y*b_stride];
  3550. - BlockNode *rt= lt+1;
  3551. - BlockNode *lb= lt+b_stride;
  3552. - BlockNode *rb= lb+1;
  3553. - uint8_t *block[4];
  3554. - // When src_stride is large enough, it is possible to interleave the blocks.
  3555. - // Otherwise the blocks are written sequentially in the tmp buffer.
  3556. - int tmp_step= src_stride >= 7*MB_SIZE ? MB_SIZE : MB_SIZE*src_stride;
  3557. - uint8_t *tmp = s->scratchbuf;
  3558. - uint8_t *ptmp;
  3559. - int x,y;
  3560. -
  3561. - if(b_x<0){
  3562. - lt= rt;
  3563. - lb= rb;
  3564. - }else if(b_x + 1 >= b_width){
  3565. - rt= lt;
  3566. - rb= lb;
  3567. - }
  3568. - if(b_y<0){
  3569. - lt= lb;
  3570. - rt= rb;
  3571. - }else if(b_y + 1 >= b_height){
  3572. - lb= lt;
  3573. - rb= rt;
  3574. - }
  3575. -
  3576. - if(src_x<0){ //FIXME merge with prev & always round internal width up to *16
  3577. - obmc -= src_x;
  3578. - b_w += src_x;
  3579. - if(!sliced && !offset_dst)
  3580. - dst -= src_x;
  3581. - src_x=0;
  3582. - }
  3583. - if(src_x + b_w > w){
  3584. - b_w = w - src_x;
  3585. - }
  3586. - if(src_y<0){
  3587. - obmc -= src_y*obmc_stride;
  3588. - b_h += src_y;
  3589. - if(!sliced && !offset_dst)
  3590. - dst -= src_y*dst_stride;
  3591. - src_y=0;
  3592. - }
  3593. - if(src_y + b_h> h){
  3594. - b_h = h - src_y;
  3595. - }
  3596. -
  3597. - if(b_w<=0 || b_h<=0) return;
  3598. -
  3599. - if(!sliced && offset_dst)
  3600. - dst += src_x + src_y*dst_stride;
  3601. - dst8+= src_x + src_y*src_stride;
  3602. -// src += src_x + src_y*src_stride;
  3603. -
  3604. - ptmp= tmp + 3*tmp_step;
  3605. - block[0]= ptmp;
  3606. - ptmp+=tmp_step;
  3607. - ff_snow_pred_block(s, block[0], tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h);
  3608. -
  3609. - if(same_block(lt, rt)){
  3610. - block[1]= block[0];
  3611. - }else{
  3612. - block[1]= ptmp;
  3613. - ptmp+=tmp_step;
  3614. - ff_snow_pred_block(s, block[1], tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h);
  3615. - }
  3616. -
  3617. - if(same_block(lt, lb)){
  3618. - block[2]= block[0];
  3619. - }else if(same_block(rt, lb)){
  3620. - block[2]= block[1];
  3621. - }else{
  3622. - block[2]= ptmp;
  3623. - ptmp+=tmp_step;
  3624. - ff_snow_pred_block(s, block[2], tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h);
  3625. - }
  3626. -
  3627. - if(same_block(lt, rb) ){
  3628. - block[3]= block[0];
  3629. - }else if(same_block(rt, rb)){
  3630. - block[3]= block[1];
  3631. - }else if(same_block(lb, rb)){
  3632. - block[3]= block[2];
  3633. - }else{
  3634. - block[3]= ptmp;
  3635. - ff_snow_pred_block(s, block[3], tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h);
  3636. - }
  3637. - if(sliced){
  3638. - s->dwt.inner_add_yblock(obmc, obmc_stride, block, b_w, b_h, src_x,src_y, src_stride, sb, add, dst8);
  3639. - }else{
  3640. - for(y=0; y<b_h; y++){
  3641. - //FIXME ugly misuse of obmc_stride
  3642. - const uint8_t *obmc1= obmc + y*obmc_stride;
  3643. - const uint8_t *obmc2= obmc1+ (obmc_stride>>1);
  3644. - const uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
  3645. - const uint8_t *obmc4= obmc3+ (obmc_stride>>1);
  3646. - for(x=0; x<b_w; x++){
  3647. - int v= obmc1[x] * block[3][x + y*src_stride]
  3648. - +obmc2[x] * block[2][x + y*src_stride]
  3649. - +obmc3[x] * block[1][x + y*src_stride]
  3650. - +obmc4[x] * block[0][x + y*src_stride];
  3651. -
  3652. - v <<= 8 - LOG2_OBMC_MAX;
  3653. - if(FRAC_BITS != 8){
  3654. - v >>= 8 - FRAC_BITS;
  3655. - }
  3656. - if(add){
  3657. - v += dst[x + y*dst_stride];
  3658. - v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
  3659. - if(v&(~255)) v= ~(v>>31);
  3660. - dst8[x + y*src_stride] = v;
  3661. - }else{
  3662. - dst[x + y*dst_stride] -= v;
  3663. - }
  3664. - }
  3665. - }
  3666. - }
  3667. -}
  3668. -
  3669. -static av_always_inline void predict_slice(SnowContext *s, IDWTELEM *buf, int plane_index, int add, int mb_y){
  3670. - Plane *p= &s->plane[plane_index];
  3671. - const int mb_w= s->b_width << s->block_max_depth;
  3672. - const int mb_h= s->b_height << s->block_max_depth;
  3673. - int x, y, mb_x;
  3674. - int block_size = MB_SIZE >> s->block_max_depth;
  3675. - int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
  3676. - int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
  3677. - const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];
  3678. - const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
  3679. - int ref_stride= s->current_picture->linesize[plane_index];
  3680. - uint8_t *dst8= s->current_picture->data[plane_index];
  3681. - int w= p->width;
  3682. - int h= p->height;
  3683. - av_assert2(s->chroma_h_shift == s->chroma_v_shift); // obmc params assume squares
  3684. - if(s->keyframe || (s->avctx->debug&512)){
  3685. - if(mb_y==mb_h)
  3686. - return;
  3687. -
  3688. - if(add){
  3689. - for(y=block_h*mb_y; y<FFMIN(h,block_h*(mb_y+1)); y++){
  3690. - for(x=0; x<w; x++){
  3691. - int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  3692. - v >>= FRAC_BITS;
  3693. - if(v&(~255)) v= ~(v>>31);
  3694. - dst8[x + y*ref_stride]= v;
  3695. - }
  3696. - }
  3697. - }else{
  3698. - for(y=block_h*mb_y; y<FFMIN(h,block_h*(mb_y+1)); y++){
  3699. - for(x=0; x<w; x++){
  3700. - buf[x + y*w]-= 128<<FRAC_BITS;
  3701. - }
  3702. - }
  3703. - }
  3704. -
  3705. - return;
  3706. - }
  3707. -
  3708. - for(mb_x=0; mb_x<=mb_w; mb_x++){
  3709. - add_yblock(s, 0, NULL, buf, dst8, obmc,
  3710. - block_w*mb_x - block_w/2,
  3711. - block_h*mb_y - block_h/2,
  3712. - block_w, block_h,
  3713. - w, h,
  3714. - w, ref_stride, obmc_stride,
  3715. - mb_x - 1, mb_y - 1,
  3716. - add, 1, plane_index);
  3717. - }
  3718. -}
  3719. -
  3720. -static av_always_inline void predict_plane(SnowContext *s, IDWTELEM *buf, int plane_index, int add){
  3721. - const int mb_h= s->b_height << s->block_max_depth;
  3722. - int mb_y;
  3723. - for(mb_y=0; mb_y<=mb_h; mb_y++)
  3724. - predict_slice(s, buf, plane_index, add, mb_y);
  3725. -}
  3726. -
  3727. -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){
  3728. - const int w= s->b_width << s->block_max_depth;
  3729. - const int rem_depth= s->block_max_depth - level;
  3730. - const int index= (x + y*w) << rem_depth;
  3731. - const int block_w= 1<<rem_depth;
  3732. - const int block_h= 1<<rem_depth; //FIXME "w!=h"
  3733. - BlockNode block;
  3734. - int i,j;
  3735. -
  3736. - block.color[0]= l;
  3737. - block.color[1]= cb;
  3738. - block.color[2]= cr;
  3739. - block.mx= mx;
  3740. - block.my= my;
  3741. - block.ref= ref;
  3742. - block.type= type;
  3743. - block.level= level;
  3744. -
  3745. - for(j=0; j<block_h; j++){
  3746. - for(i=0; i<block_w; i++){
  3747. - s->block[index + i + j*w]= block;
  3748. - }
  3749. - }
  3750. -}
  3751. -
  3752. -static inline void init_ref(MotionEstContext *c, uint8_t *src[3], uint8_t *ref[3], uint8_t *ref2[3], int x, int y, int ref_index){
  3753. - SnowContext *s = c->avctx->priv_data;
  3754. - const int offset[3]= {
  3755. - y*c-> stride + x,
  3756. - ((y*c->uvstride + x)>>s->chroma_h_shift),
  3757. - ((y*c->uvstride + x)>>s->chroma_h_shift),
  3758. - };
  3759. - int i;
  3760. - for(i=0; i<3; i++){
  3761. - c->src[0][i]= src [i];
  3762. - c->ref[0][i]= ref [i] + offset[i];
  3763. - }
  3764. - av_assert2(!ref_index);
  3765. -}
  3766. -
  3767.  
  3768. /* bitstream functions */
  3769.  
  3770. diff --git a/libavcodec/snowdata.h b/libavcodec/snowdata.h
  3771. deleted file mode 100644
  3772. index 490fdf8..0000000
  3773. --- a/libavcodec/snowdata.h
  3774. +++ /dev/null
  3775. @@ -1,132 +0,0 @@
  3776. -/*
  3777. - * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
  3778. - * Copyright (C) 2006 Robert Edele <yartrebo@earthlink.net>
  3779. - *
  3780. - * This file is part of FFmpeg.
  3781. - *
  3782. - * FFmpeg is free software; you can redistribute it and/or
  3783. - * modify it under the terms of the GNU Lesser General Public
  3784. - * License as published by the Free Software Foundation; either
  3785. - * version 2.1 of the License, or (at your option) any later version.
  3786. - *
  3787. - * FFmpeg is distributed in the hope that it will be useful,
  3788. - * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3789. - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  3790. - * Lesser General Public License for more details.
  3791. - *
  3792. - * You should have received a copy of the GNU Lesser General Public
  3793. - * License along with FFmpeg; if not, write to the Free Software
  3794. - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  3795. - */
  3796. -
  3797. -#ifndef AVCODEC_SNOWDATA_H
  3798. -#define AVCODEC_SNOWDATA_H
  3799. -
  3800. -#include "snow.h"
  3801. -
  3802. -static const uint8_t obmc32[1024]={
  3803. - 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0,
  3804. - 0, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 16, 20, 20, 20, 24, 24, 20, 20, 20, 16, 16, 16, 12, 12, 8, 8, 8, 4, 4, 4, 0,
  3805. - 0, 4, 8, 8, 12, 12, 16, 20, 20, 24, 28, 28, 32, 32, 36, 40, 40, 36, 32, 32, 28, 28, 24, 20, 20, 16, 12, 12, 8, 8, 4, 0,
  3806. - 0, 4, 8, 12, 16, 20, 24, 28, 28, 32, 36, 40, 44, 48, 52, 56, 56, 52, 48, 44, 40, 36, 32, 28, 28, 24, 20, 16, 12, 8, 4, 0,
  3807. - 4, 8, 12, 16, 20, 24, 28, 32, 40, 44, 48, 52, 56, 60, 64, 68, 68, 64, 60, 56, 52, 48, 44, 40, 32, 28, 24, 20, 16, 12, 8, 4,
  3808. - 4, 8, 12, 20, 24, 32, 36, 40, 48, 52, 56, 64, 68, 76, 80, 84, 84, 80, 76, 68, 64, 56, 52, 48, 40, 36, 32, 24, 20, 12, 8, 4,
  3809. - 4, 8, 16, 24, 28, 36, 44, 48, 56, 60, 68, 76, 80, 88, 96,100,100, 96, 88, 80, 76, 68, 60, 56, 48, 44, 36, 28, 24, 16, 8, 4,
  3810. - 4, 12, 20, 28, 32, 40, 48, 56, 64, 72, 80, 88, 92,100,108,116,116,108,100, 92, 88, 80, 72, 64, 56, 48, 40, 32, 28, 20, 12, 4,
  3811. - 4, 12, 20, 28, 40, 48, 56, 64, 72, 80, 88, 96,108,116,124,132,132,124,116,108, 96, 88, 80, 72, 64, 56, 48, 40, 28, 20, 12, 4,
  3812. - 4, 16, 24, 32, 44, 52, 60, 72, 80, 92,100,108,120,128,136,148,148,136,128,120,108,100, 92, 80, 72, 60, 52, 44, 32, 24, 16, 4,
  3813. - 4, 16, 28, 36, 48, 56, 68, 80, 88,100,112,120,132,140,152,164,164,152,140,132,120,112,100, 88, 80, 68, 56, 48, 36, 28, 16, 4,
  3814. - 4, 16, 28, 40, 52, 64, 76, 88, 96,108,120,132,144,156,168,180,180,168,156,144,132,120,108, 96, 88, 76, 64, 52, 40, 28, 16, 4,
  3815. - 8, 20, 32, 44, 56, 68, 80, 92,108,120,132,144,156,168,180,192,192,180,168,156,144,132,120,108, 92, 80, 68, 56, 44, 32, 20, 8,
  3816. - 8, 20, 32, 48, 60, 76, 88,100,116,128,140,156,168,184,196,208,208,196,184,168,156,140,128,116,100, 88, 76, 60, 48, 32, 20, 8,
  3817. - 8, 20, 36, 52, 64, 80, 96,108,124,136,152,168,180,196,212,224,224,212,196,180,168,152,136,124,108, 96, 80, 64, 52, 36, 20, 8,
  3818. - 8, 24, 40, 56, 68, 84,100,116,132,148,164,180,192,208,224,240,240,224,208,192,180,164,148,132,116,100, 84, 68, 56, 40, 24, 8,
  3819. - 8, 24, 40, 56, 68, 84,100,116,132,148,164,180,192,208,224,240,240,224,208,192,180,164,148,132,116,100, 84, 68, 56, 40, 24, 8,
  3820. - 8, 20, 36, 52, 64, 80, 96,108,124,136,152,168,180,196,212,224,224,212,196,180,168,152,136,124,108, 96, 80, 64, 52, 36, 20, 8,
  3821. - 8, 20, 32, 48, 60, 76, 88,100,116,128,140,156,168,184,196,208,208,196,184,168,156,140,128,116,100, 88, 76, 60, 48, 32, 20, 8,
  3822. - 8, 20, 32, 44, 56, 68, 80, 92,108,120,132,144,156,168,180,192,192,180,168,156,144,132,120,108, 92, 80, 68, 56, 44, 32, 20, 8,
  3823. - 4, 16, 28, 40, 52, 64, 76, 88, 96,108,120,132,144,156,168,180,180,168,156,144,132,120,108, 96, 88, 76, 64, 52, 40, 28, 16, 4,
  3824. - 4, 16, 28, 36, 48, 56, 68, 80, 88,100,112,120,132,140,152,164,164,152,140,132,120,112,100, 88, 80, 68, 56, 48, 36, 28, 16, 4,
  3825. - 4, 16, 24, 32, 44, 52, 60, 72, 80, 92,100,108,120,128,136,148,148,136,128,120,108,100, 92, 80, 72, 60, 52, 44, 32, 24, 16, 4,
  3826. - 4, 12, 20, 28, 40, 48, 56, 64, 72, 80, 88, 96,108,116,124,132,132,124,116,108, 96, 88, 80, 72, 64, 56, 48, 40, 28, 20, 12, 4,
  3827. - 4, 12, 20, 28, 32, 40, 48, 56, 64, 72, 80, 88, 92,100,108,116,116,108,100, 92, 88, 80, 72, 64, 56, 48, 40, 32, 28, 20, 12, 4,
  3828. - 4, 8, 16, 24, 28, 36, 44, 48, 56, 60, 68, 76, 80, 88, 96,100,100, 96, 88, 80, 76, 68, 60, 56, 48, 44, 36, 28, 24, 16, 8, 4,
  3829. - 4, 8, 12, 20, 24, 32, 36, 40, 48, 52, 56, 64, 68, 76, 80, 84, 84, 80, 76, 68, 64, 56, 52, 48, 40, 36, 32, 24, 20, 12, 8, 4,
  3830. - 4, 8, 12, 16, 20, 24, 28, 32, 40, 44, 48, 52, 56, 60, 64, 68, 68, 64, 60, 56, 52, 48, 44, 40, 32, 28, 24, 20, 16, 12, 8, 4,
  3831. - 0, 4, 8, 12, 16, 20, 24, 28, 28, 32, 36, 40, 44, 48, 52, 56, 56, 52, 48, 44, 40, 36, 32, 28, 28, 24, 20, 16, 12, 8, 4, 0,
  3832. - 0, 4, 8, 8, 12, 12, 16, 20, 20, 24, 28, 28, 32, 32, 36, 40, 40, 36, 32, 32, 28, 28, 24, 20, 20, 16, 12, 12, 8, 8, 4, 0,
  3833. - 0, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 16, 20, 20, 20, 24, 24, 20, 20, 20, 16, 16, 16, 12, 12, 8, 8, 8, 4, 4, 4, 0,
  3834. - 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0,
  3835. - //error:0.000020
  3836. -};
  3837. -static const uint8_t obmc16[256]={
  3838. - 0, 4, 4, 8, 8, 12, 12, 16, 16, 12, 12, 8, 8, 4, 4, 0,
  3839. - 4, 8, 16, 20, 28, 32, 40, 44, 44, 40, 32, 28, 20, 16, 8, 4,
  3840. - 4, 16, 24, 36, 44, 56, 64, 76, 76, 64, 56, 44, 36, 24, 16, 4,
  3841. - 8, 20, 36, 48, 64, 76, 92,104,104, 92, 76, 64, 48, 36, 20, 8,
  3842. - 8, 28, 44, 64, 80,100,116,136,136,116,100, 80, 64, 44, 28, 8,
  3843. - 12, 32, 56, 76,100,120,144,164,164,144,120,100, 76, 56, 32, 12,
  3844. - 12, 40, 64, 92,116,144,168,196,196,168,144,116, 92, 64, 40, 12,
  3845. - 16, 44, 76,104,136,164,196,224,224,196,164,136,104, 76, 44, 16,
  3846. - 16, 44, 76,104,136,164,196,224,224,196,164,136,104, 76, 44, 16,
  3847. - 12, 40, 64, 92,116,144,168,196,196,168,144,116, 92, 64, 40, 12,
  3848. - 12, 32, 56, 76,100,120,144,164,164,144,120,100, 76, 56, 32, 12,
  3849. - 8, 28, 44, 64, 80,100,116,136,136,116,100, 80, 64, 44, 28, 8,
  3850. - 8, 20, 36, 48, 64, 76, 92,104,104, 92, 76, 64, 48, 36, 20, 8,
  3851. - 4, 16, 24, 36, 44, 56, 64, 76, 76, 64, 56, 44, 36, 24, 16, 4,
  3852. - 4, 8, 16, 20, 28, 32, 40, 44, 44, 40, 32, 28, 20, 16, 8, 4,
  3853. - 0, 4, 4, 8, 8, 12, 12, 16, 16, 12, 12, 8, 8, 4, 4, 0,
  3854. -//error:0.000015
  3855. -};
  3856. -
  3857. -//linear *64
  3858. -static const uint8_t obmc8[64]={
  3859. - 4, 12, 20, 28, 28, 20, 12, 4,
  3860. - 12, 36, 60, 84, 84, 60, 36, 12,
  3861. - 20, 60,100,140,140,100, 60, 20,
  3862. - 28, 84,140,196,196,140, 84, 28,
  3863. - 28, 84,140,196,196,140, 84, 28,
  3864. - 20, 60,100,140,140,100, 60, 20,
  3865. - 12, 36, 60, 84, 84, 60, 36, 12,
  3866. - 4, 12, 20, 28, 28, 20, 12, 4,
  3867. -//error:0.000000
  3868. -};
  3869. -
  3870. -//linear *64
  3871. -static const uint8_t obmc4[16]={
  3872. - 16, 48, 48, 16,
  3873. - 48,144,144, 48,
  3874. - 48,144,144, 48,
  3875. - 16, 48, 48, 16,
  3876. -//error:0.000000
  3877. -};
  3878. -
  3879. -const int8_t ff_quant3bA[256]={
  3880. - 0, 0, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  3881. - 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  3882. - 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  3883. - 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  3884. - 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  3885. - 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  3886. - 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  3887. - 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  3888. - 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  3889. - 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  3890. - 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  3891. - 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  3892. - 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  3893. - 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  3894. - 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  3895. - 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  3896. -};
  3897. -
  3898. -const uint8_t * const ff_obmc_tab[4]= {
  3899. - obmc32, obmc16, obmc8, obmc4
  3900. -};
  3901. -
  3902. -/* runtime generated tables */
  3903. -uint8_t ff_qexp[QROOT];
  3904. -int ff_scale_mv_ref[MAX_REF_FRAMES][MAX_REF_FRAMES];
  3905. -
  3906. -
  3907. -#endif /* AVCODEC_SNOW_H */
  3908. diff --git a/libavcodec/snowdec.c b/libavcodec/snowdec.c
  3909. index 042aecb..b224470 100644
  3910. --- a/libavcodec/snowdec.c
  3911. +++ b/libavcodec/snowdec.c
  3912. @@ -29,90 +29,7 @@
  3913. #include "rangecoder.h"
  3914. #include "mathops.h"
  3915.  
  3916. -#include "mpegvideo.h"
  3917. -#include "h263.h"
  3918. -
  3919. -static av_always_inline void predict_slice_buffered(SnowContext *s, slice_buffer * sb, IDWTELEM * old_buffer, int plane_index, int add, int mb_y){
  3920. - Plane *p= &s->plane[plane_index];
  3921. - const int mb_w= s->b_width << s->block_max_depth;
  3922. - const int mb_h= s->b_height << s->block_max_depth;
  3923. - int x, y, mb_x;
  3924. - int block_size = MB_SIZE >> s->block_max_depth;
  3925. - int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
  3926. - int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
  3927. - const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];
  3928. - int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
  3929. - int ref_stride= s->current_picture->linesize[plane_index];
  3930. - uint8_t *dst8= s->current_picture->data[plane_index];
  3931. - int w= p->width;
  3932. - int h= p->height;
  3933. -
  3934. - if(s->keyframe || (s->avctx->debug&512)){
  3935. - if(mb_y==mb_h)
  3936. - return;
  3937. -
  3938. - if(add){
  3939. - for(y=block_h*mb_y; y<FFMIN(h,block_h*(mb_y+1)); y++){
  3940. -// DWTELEM * line = slice_buffer_get_line(sb, y);
  3941. - IDWTELEM * line = sb->line[y];
  3942. - for(x=0; x<w; x++){
  3943. -// int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  3944. - int v= line[x] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  3945. - v >>= FRAC_BITS;
  3946. - if(v&(~255)) v= ~(v>>31);
  3947. - dst8[x + y*ref_stride]= v;
  3948. - }
  3949. - }
  3950. - }else{
  3951. - for(y=block_h*mb_y; y<FFMIN(h,block_h*(mb_y+1)); y++){
  3952. -// DWTELEM * line = slice_buffer_get_line(sb, y);
  3953. - IDWTELEM * line = sb->line[y];
  3954. - for(x=0; x<w; x++){
  3955. - line[x] -= 128 << FRAC_BITS;
  3956. -// buf[x + y*w]-= 128<<FRAC_BITS;
  3957. - }
  3958. - }
  3959. - }
  3960. -
  3961. - return;
  3962. - }
  3963. -
  3964. - for(mb_x=0; mb_x<=mb_w; mb_x++){
  3965. - add_yblock(s, 1, sb, old_buffer, dst8, obmc,
  3966. - block_w*mb_x - block_w/2,
  3967. - block_h*mb_y - block_h/2,
  3968. - block_w, block_h,
  3969. - w, h,
  3970. - w, ref_stride, obmc_stride,
  3971. - mb_x - 1, mb_y - 1,
  3972. - add, 0, plane_index);
  3973. - }
  3974. -
  3975. - if(s->avmv && mb_y < mb_h && plane_index == 0)
  3976. - for(mb_x=0; mb_x<mb_w; mb_x++){
  3977. - AVMotionVector *avmv = s->avmv + s->avmv_index;
  3978. - const int b_width = s->b_width << s->block_max_depth;
  3979. - const int b_stride= b_width;
  3980. - BlockNode *bn= &s->block[mb_x + mb_y*b_stride];
  3981. -
  3982. - if (bn->type)
  3983. - continue;
  3984. -
  3985. - s->avmv_index++;
  3986. -
  3987. - avmv->w = block_w;
  3988. - avmv->h = block_h;
  3989. - avmv->dst_x = block_w*mb_x - block_w/2;
  3990. - avmv->dst_y = block_h*mb_y - block_h/2;
  3991. - avmv->motion_scale = 8;
  3992. - avmv->motion_x = bn->mx * s->mv_scale;
  3993. - avmv->motion_y = bn->my * s->mv_scale;
  3994. - avmv->src_x = avmv->dst_x + avmv->motion_x / 8;
  3995. - avmv->src_y = avmv->dst_y + avmv->motion_y / 8;
  3996. - avmv->source= -1 - bn->ref;
  3997. - avmv->flags = 0;
  3998. - }
  3999. -}
  4000. +#include "obmc.h"
  4001.  
  4002. static inline void decode_subband_slice_buffered(SnowContext *s, SubBand *b, slice_buffer * sb, int start_y, int h, int save_state[1]){
  4003. const int w= b->width;
  4004. @@ -122,7 +39,7 @@ static inline void decode_subband_slice_buffered(SnowContext *s, SubBand *b, sli
  4005. int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  4006. int new_index = 0;
  4007.  
  4008. - if(b->ibuf == s->spatial_idwt_buffer || s->qlog == LOSSLESS_QLOG){
  4009. + if(b->ibuf == s->obmc.spatial_idwt_buffer || s->qlog == LOSSLESS_QLOG){
  4010. qadd= 0;
  4011. qmul= 1<<QEXPSHIFT;
  4012. }
  4013. @@ -156,23 +73,25 @@ static inline void decode_subband_slice_buffered(SnowContext *s, SubBand *b, sli
  4014. }
  4015.  
  4016. static int decode_q_branch(SnowContext *s, int level, int x, int y){
  4017. - const int w= s->b_width << s->block_max_depth;
  4018. - const int rem_depth= s->block_max_depth - level;
  4019. + RangeCoder *const c = &s->c;
  4020. + OBMCContext *oc = &s->obmc;
  4021. + const int w= oc->b_width << oc->block_max_depth;
  4022. + const int rem_depth= oc->block_max_depth - level;
  4023. const int index= (x + y*w) << rem_depth;
  4024. int trx= (x+1)<<rem_depth;
  4025. - const BlockNode *left = x ? &s->block[index-1] : &null_block;
  4026. - const BlockNode *top = y ? &s->block[index-w] : &null_block;
  4027. - const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  4028. - const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  4029. + const BlockNode *left = x ? &oc->block[index-1] : &null_block;
  4030. + const BlockNode *top = y ? &oc->block[index-w] : &null_block;
  4031. + const BlockNode *tl = y && x ? &oc->block[index-w-1] : left;
  4032. + const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &oc->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  4033. int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  4034. int res;
  4035.  
  4036. - if(s->keyframe){
  4037. - set_blocks(s, level, x, y, null_block.color[0], null_block.color[1], null_block.color[2], null_block.mx, null_block.my, null_block.ref, BLOCK_INTRA);
  4038. + if(oc->keyframe){
  4039. + set_blocks(oc, level, x, y, null_block.color[0], null_block.color[1], null_block.color[2], null_block.mx, null_block.my, null_block.ref, BLOCK_INTRA);
  4040. return 0;
  4041. }
  4042.  
  4043. - if(level==s->block_max_depth || get_rac(&s->c, &s->block_state[4 + s_context])){
  4044. + if(level==oc->block_max_depth || get_rac(c, &s->block_state[4 + s_context])){
  4045. int type, mx, my;
  4046. int l = left->color[0];
  4047. int cb= left->color[1];
  4048. @@ -182,27 +101,27 @@ static int decode_q_branch(SnowContext *s, int level, int x, int y){
  4049. int mx_context= av_log2(2*FFABS(left->mx - top->mx)) + 0*av_log2(2*FFABS(tr->mx - top->mx));
  4050. int my_context= av_log2(2*FFABS(left->my - top->my)) + 0*av_log2(2*FFABS(tr->my - top->my));
  4051.  
  4052. - type= get_rac(&s->c, &s->block_state[1 + left->type + top->type]) ? BLOCK_INTRA : 0;
  4053. + type= get_rac(c, &s->block_state[1 + left->type + top->type]) ? BLOCK_INTRA : 0;
  4054.  
  4055. if(type){
  4056. - pred_mv(s, &mx, &my, 0, left, top, tr);
  4057. - l += get_symbol(&s->c, &s->block_state[32], 1);
  4058. - if (s->nb_planes > 2) {
  4059. - cb+= get_symbol(&s->c, &s->block_state[64], 1);
  4060. - cr+= get_symbol(&s->c, &s->block_state[96], 1);
  4061. + pred_mv(oc, &mx, &my, 0, left, top, tr);
  4062. + l += get_symbol(c, &s->block_state[32], 1);
  4063. + if (s->obmc.nb_planes > 2) {
  4064. + cb += get_symbol(c, &s->block_state[64], 1);
  4065. + cr += get_symbol(c, &s->block_state[96], 1);
  4066. }
  4067. }else{
  4068. - if(s->ref_frames > 1)
  4069. - ref= get_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], 0);
  4070. - if (ref >= s->ref_frames) {
  4071. + if(oc->ref_frames > 1)
  4072. + ref = get_symbol(c, &s->block_state[128 + 1024 + 32*ref_context], 0);
  4073. + if (ref >= oc->ref_frames) {
  4074. av_log(s->avctx, AV_LOG_ERROR, "Invalid ref\n");
  4075. return AVERROR_INVALIDDATA;
  4076. }
  4077. - pred_mv(s, &mx, &my, ref, left, top, tr);
  4078. - mx+= get_symbol(&s->c, &s->block_state[128 + 32*(mx_context + 16*!!ref)], 1);
  4079. - my+= get_symbol(&s->c, &s->block_state[128 + 32*(my_context + 16*!!ref)], 1);
  4080. + pred_mv(oc, &mx, &my, ref, left, top, tr);
  4081. + mx += get_symbol(c, &s->block_state[128 + 32*(mx_context + 16*!!ref)], 1);
  4082. + my += get_symbol(c, &s->block_state[128 + 32*(my_context + 16*!!ref)], 1);
  4083. }
  4084. - set_blocks(s, level, x, y, l, cb, cr, mx, my, ref, type);
  4085. + set_blocks(oc, level, x, y, l, cb, cr, mx, my, ref, type);
  4086. }else{
  4087. if ((res = decode_q_branch(s, level+1, 2*x+0, 2*y+0)) < 0 ||
  4088. (res = decode_q_branch(s, level+1, 2*x+1, 2*y+0)) < 0 ||
  4089. @@ -291,19 +210,20 @@ static void decode_qlogs(SnowContext *s){
  4090. dst= tmp;
  4091.  
  4092. static int decode_header(SnowContext *s){
  4093. - int plane_index, tmp;
  4094. + int plane_index, tmp, ret;
  4095. uint8_t kstate[32];
  4096.  
  4097. memset(kstate, MID_STATE, sizeof(kstate));
  4098.  
  4099. s->keyframe= get_rac(&s->c, kstate);
  4100. + s->obmc.keyframe = s->keyframe;
  4101. if(s->keyframe || s->always_reset){
  4102. ff_snow_reset_contexts(s);
  4103. s->spatial_decomposition_type=
  4104. s->qlog=
  4105. s->qbias=
  4106. - s->mv_scale=
  4107. - s->block_max_depth= 0;
  4108. + s->obmc.mv_scale=
  4109. + s->obmc.block_max_depth= 0;
  4110. }
  4111. if(s->keyframe){
  4112. GET_S(s->version, tmp <= 0U)
  4113. @@ -342,8 +262,8 @@ static int decode_header(SnowContext *s){
  4114.  
  4115. s->spatial_scalability= get_rac(&s->c, s->header_state);
  4116. // s->rate_scalability= get_rac(&s->c, s->header_state);
  4117. - GET_S(s->max_ref_frames, tmp < (unsigned)MAX_REF_FRAMES)
  4118. - s->max_ref_frames++;
  4119. + GET_S(s->obmc.max_ref_frames, tmp < (unsigned)MAX_REF_FRAMES)
  4120. + s->obmc.max_ref_frames++;
  4121.  
  4122. decode_qlogs(s);
  4123. }
  4124. @@ -352,7 +272,7 @@ static int decode_header(SnowContext *s){
  4125. if(get_rac(&s->c, s->header_state)){
  4126. for(plane_index=0; plane_index<FFMIN(s->nb_planes, 2); plane_index++){
  4127. int htaps, i, sum=0;
  4128. - Plane *p= &s->plane[plane_index];
  4129. + PlaneObmc *p= &s->obmc.plane[plane_index];
  4130. p->diag_mc= get_rac(&s->c, s->header_state);
  4131. htaps= get_symbol(&s->c, s->header_state, 0)*2 + 2;
  4132. if((unsigned)htaps > HTAPS_MAX || htaps==0)
  4133. @@ -364,9 +284,9 @@ static int decode_header(SnowContext *s){
  4134. }
  4135. p->hcoeff[0]= 32-sum;
  4136. }
  4137. - s->plane[2].diag_mc= s->plane[1].diag_mc;
  4138. - s->plane[2].htaps = s->plane[1].htaps;
  4139. - memcpy(s->plane[2].hcoeff, s->plane[1].hcoeff, sizeof(s->plane[1].hcoeff));
  4140. + s->obmc.plane[2].diag_mc= s->obmc.plane[1].diag_mc;
  4141. + s->obmc.plane[2].htaps = s->obmc.plane[1].htaps;
  4142. + memcpy(s->obmc.plane[2].hcoeff, s->obmc.plane[1].hcoeff, sizeof(s->obmc.plane[1].hcoeff));
  4143. }
  4144. if(get_rac(&s->c, s->header_state)){
  4145. GET_S(s->spatial_decomposition_count, 0 < tmp && tmp <= MAX_DECOMPOSITIONS)
  4146. @@ -386,16 +306,19 @@ static int decode_header(SnowContext *s){
  4147. }
  4148.  
  4149.  
  4150. - s->qlog += get_symbol(&s->c, s->header_state, 1);
  4151. - s->mv_scale += get_symbol(&s->c, s->header_state, 1);
  4152. - s->qbias += get_symbol(&s->c, s->header_state, 1);
  4153. - s->block_max_depth+= get_symbol(&s->c, s->header_state, 1);
  4154. - if(s->block_max_depth > 1 || s->block_max_depth < 0){
  4155. - av_log(s->avctx, AV_LOG_ERROR, "block_max_depth= %d is too large\n", s->block_max_depth);
  4156. - s->block_max_depth= 0;
  4157. + s->qlog += get_symbol(&s->c, s->header_state, 1);
  4158. + s->obmc.mv_scale += get_symbol(&s->c, s->header_state, 1);
  4159. + s->qbias += get_symbol(&s->c, s->header_state, 1);
  4160. + s->obmc.block_max_depth += get_symbol(&s->c, s->header_state, 1);
  4161. + if(s->obmc.block_max_depth > 1 || s->obmc.block_max_depth < 0){
  4162. + av_log(s->avctx, AV_LOG_ERROR, "block_max_depth= %d is too large\n", s->obmc.block_max_depth);
  4163. + s->obmc.block_max_depth= 0;
  4164. return AVERROR_INVALIDDATA;
  4165. }
  4166.  
  4167. + if ((ret = ff_obmc_decode_init(&s->obmc)) < 0)
  4168. + return ret;
  4169. +
  4170. return 0;
  4171. }
  4172.  
  4173. @@ -412,8 +335,8 @@ static av_cold int decode_init(AVCodecContext *avctx)
  4174.  
  4175. static int decode_blocks(SnowContext *s){
  4176. int x, y;
  4177. - int w= s->b_width;
  4178. - int h= s->b_height;
  4179. + int w= s->obmc.b_width;
  4180. + int h= s->obmc.b_height;
  4181. int res;
  4182.  
  4183. for(y=0; y<h; y++){
  4184. @@ -440,7 +363,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  4185. ff_init_range_decoder(c, buf, buf_size);
  4186. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  4187.  
  4188. - s->current_picture->pict_type= AV_PICTURE_TYPE_I; //FIXME I vs. P
  4189. + s->obmc.current_picture->pict_type= AV_PICTURE_TYPE_I; //FIXME I vs. P
  4190. if ((res = decode_header(s)) < 0)
  4191. return res;
  4192. if ((res=ff_snow_common_init_after_header(avctx)) < 0)
  4193. @@ -448,42 +371,25 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  4194.  
  4195. // realloc slice buffer for the case that spatial_decomposition_count changed
  4196. ff_slice_buffer_destroy(&s->sb);
  4197. - if ((res = ff_slice_buffer_init(&s->sb, s->plane[0].height,
  4198. - (MB_SIZE >> s->block_max_depth) +
  4199. + if ((res = ff_slice_buffer_init(&s->sb, s->obmc.plane[0].height,
  4200. + (MB_SIZE >> s->obmc.block_max_depth) +
  4201. s->spatial_decomposition_count * 11 + 1,
  4202. - s->plane[0].width,
  4203. - s->spatial_idwt_buffer)) < 0)
  4204. - return res;
  4205. -
  4206. - for(plane_index=0; plane_index < s->nb_planes; plane_index++){
  4207. - Plane *p= &s->plane[plane_index];
  4208. - p->fast_mc= p->diag_mc && p->htaps==6 && p->hcoeff[0]==40
  4209. - && p->hcoeff[1]==-10
  4210. - && p->hcoeff[2]==2;
  4211. - }
  4212. -
  4213. - ff_snow_alloc_blocks(s);
  4214. -
  4215. - if((res = ff_snow_frame_start(s)) < 0)
  4216. + s->obmc.plane[0].width,
  4217. + s->obmc.spatial_idwt_buffer)) < 0)
  4218. return res;
  4219.  
  4220. - s->current_picture->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  4221. -
  4222. //keyframe flag duplication mess FIXME
  4223. if(avctx->debug&FF_DEBUG_PICT_INFO)
  4224. av_log(avctx, AV_LOG_ERROR,
  4225. "keyframe:%d qlog:%d qbias: %d mvscale: %d "
  4226. "decomposition_type:%d decomposition_count:%d\n",
  4227. - s->keyframe, s->qlog, s->qbias, s->mv_scale,
  4228. + s->keyframe, s->qlog, s->qbias, s->obmc.mv_scale,
  4229. s->spatial_decomposition_type,
  4230. s->spatial_decomposition_count
  4231. );
  4232.  
  4233. - av_assert0(!s->avmv);
  4234. - if (s->avctx->flags2 & AV_CODEC_FLAG2_EXPORT_MVS) {
  4235. - s->avmv = av_malloc_array(s->b_width * s->b_height, sizeof(AVMotionVector) << (s->block_max_depth*2));
  4236. - }
  4237. - s->avmv_index = 0;
  4238. + if ((res = ff_obmc_predecode_frame(&s->obmc)) < 0)
  4239. + return res;
  4240.  
  4241. if ((res = decode_blocks(s)) < 0)
  4242. return res;
  4243. @@ -497,12 +403,12 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  4244.  
  4245. if(s->avctx->debug&2048){
  4246. memset(s->spatial_dwt_buffer, 0, sizeof(DWTELEM)*w*h);
  4247. - predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
  4248. + predict_plane(&s->obmc, s->obmc.spatial_idwt_buffer, plane_index, 1);
  4249.  
  4250. for(y=0; y<h; y++){
  4251. for(x=0; x<w; x++){
  4252. - int v= s->current_picture->data[plane_index][y*s->current_picture->linesize[plane_index] + x];
  4253. - s->mconly_picture->data[plane_index][y*s->mconly_picture->linesize[plane_index] + x]= v;
  4254. + int v= s->obmc.current_picture->data[plane_index][y*s->obmc.current_picture->linesize[plane_index] + x];
  4255. + s->obmc.mconly_picture->data[plane_index][y*s->obmc.mconly_picture->linesize[plane_index] + x]= v;
  4256. }
  4257. }
  4258. }
  4259. @@ -517,8 +423,8 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  4260. }
  4261.  
  4262. {
  4263. - const int mb_h= s->b_height << s->block_max_depth;
  4264. - const int block_size = MB_SIZE >> s->block_max_depth;
  4265. + const int mb_h= s->obmc.b_height << s->obmc.block_max_depth;
  4266. + const int block_size = MB_SIZE >> s->obmc.block_max_depth;
  4267. const int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
  4268. int mb_y;
  4269. DWTCompose cs[MAX_DECOMPOSITIONS];
  4270. @@ -570,7 +476,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  4271. }
  4272.  
  4273. for(; yd<slice_h; yd+=4){
  4274. - ff_spatial_idwt_buffered_slice(&s->dwt, cs, &s->sb, s->temp_idwt_buffer, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count, yd);
  4275. + ff_spatial_idwt_buffered_slice(&s->obmc.dwt, cs, &s->sb, s->temp_idwt_buffer, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count, yd);
  4276. }
  4277.  
  4278. if(s->qlog == LOSSLESS_QLOG){
  4279. @@ -582,7 +488,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  4280. }
  4281. }
  4282.  
  4283. - predict_slice_buffered(s, &s->sb, s->spatial_idwt_buffer, plane_index, 1, mb_y);
  4284. + predict_slice_buffered(&s->obmc, &s->sb, s->obmc.spatial_idwt_buffer, plane_index, 1, mb_y);
  4285.  
  4286. y = FFMIN(p->height, slice_starty);
  4287. end_y = FFMIN(p->height, slice_h);
  4288. @@ -597,22 +503,22 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  4289.  
  4290. emms_c();
  4291.  
  4292. - ff_snow_release_buffer(avctx);
  4293. + ff_obmc_release_buffer(&s->obmc);
  4294.  
  4295. if(!(s->avctx->debug&2048))
  4296. - res = av_frame_ref(picture, s->current_picture);
  4297. + res = av_frame_ref(picture, s->obmc.current_picture);
  4298. else
  4299. - res = av_frame_ref(picture, s->mconly_picture);
  4300. - if (res >= 0 && s->avmv_index) {
  4301. + res = av_frame_ref(picture, s->obmc.mconly_picture);
  4302. + if (res >= 0 && s->obmc.avmv_index) {
  4303. AVFrameSideData *sd;
  4304.  
  4305. - sd = av_frame_new_side_data(picture, AV_FRAME_DATA_MOTION_VECTORS, s->avmv_index * sizeof(AVMotionVector));
  4306. + sd = av_frame_new_side_data(picture, AV_FRAME_DATA_MOTION_VECTORS, s->obmc.avmv_index * sizeof(AVMotionVector));
  4307. if (!sd)
  4308. return AVERROR(ENOMEM);
  4309. - memcpy(sd->data, s->avmv, s->avmv_index * sizeof(AVMotionVector));
  4310. + memcpy(sd->data, s->obmc.avmv, s->obmc.avmv_index * sizeof(AVMotionVector));
  4311. }
  4312.  
  4313. - av_freep(&s->avmv);
  4314. + av_freep(&s->obmc.avmv);
  4315.  
  4316. if (res < 0)
  4317. return res;
  4318. diff --git a/libavcodec/snowenc.c b/libavcodec/snowenc.c
  4319. index 00aef57..40bc84b 100644
  4320. --- a/libavcodec/snowenc.c
  4321. +++ b/libavcodec/snowenc.c
  4322. @@ -30,16 +30,164 @@
  4323. #include "rangecoder.h"
  4324. #include "mathops.h"
  4325.  
  4326. -#include "mpegvideo.h"
  4327. -#include "h263.h"
  4328. +#include "obme.h"
  4329.  
  4330. #define FF_ME_ITER 50
  4331.  
  4332. +typedef struct RangeEncoderContext {
  4333. + RangeCoder c;
  4334. + uint8_t buffer[1024];
  4335. + uint8_t state[128 + 32*128];
  4336. + uint8_t *pbbak;
  4337. + uint8_t *pbbak_start;
  4338. + int base_bits;
  4339. +} RangeEncoderContext;
  4340. +
  4341. +static void put_encoder_rac(ObmcCoderContext *c, int ctx, int v)
  4342. +{
  4343. + SnowContext *s = (SnowContext *)c->avctx->priv_data;
  4344. + RangeCoder *rc = &s->c;
  4345. + uint8_t *state = s->block_state;
  4346. + if (c->priv_data) {
  4347. + RangeEncoderContext *coder = (RangeEncoderContext *)c->priv_data;
  4348. + rc = &coder->c; state = coder->state;
  4349. + }
  4350. + put_rac(rc, &state[ctx], v);
  4351. +}
  4352. +
  4353. +static void put_encoder_symbol(ObmcCoderContext *c, int ctx, int v, int sign)
  4354. +{
  4355. + SnowContext *s = (SnowContext *)c->avctx->priv_data;
  4356. + RangeCoder *rc = &s->c;
  4357. + uint8_t *state = s->block_state;
  4358. + if (c->priv_data) {
  4359. + RangeEncoderContext *coder = (RangeEncoderContext *)c->priv_data;
  4360. + rc = &coder->c; state = coder->state;
  4361. + }
  4362. + put_symbol(rc, &state[ctx], v, sign);
  4363. +}
  4364. +
  4365. +static void ff_snow_init_encode_callbacks(ObmcCoderContext *, AVCodecContext *);
  4366. +
  4367. +static void init_frame_encoder(AVCodecContext *avctx, ObmcCoderContext *c)
  4368. +{
  4369. + SnowContext *f = (SnowContext *)avctx->priv_data;
  4370. + RangeEncoderContext *coder = av_mallocz(sizeof(RangeEncoderContext));
  4371. + c->priv_data = coder;
  4372. +
  4373. + coder->pbbak = f->c.bytestream;
  4374. + coder->pbbak_start = f->c.bytestream_start;
  4375. + coder->base_bits = get_rac_count(&f->c) - 8*(f->c.bytestream - f->c.bytestream_start);
  4376. + coder->c = f->c;
  4377. + coder->c.bytestream_start = coder->c.bytestream= coder->buffer; //FIXME end/start? and at the other stoo
  4378. + memcpy(coder->state, f->block_state, sizeof(f->block_state));
  4379. +
  4380. + ff_snow_init_encode_callbacks(c, avctx);
  4381. +}
  4382. +
  4383. +static void free_coder (ObmcCoderContext *c)
  4384. +{
  4385. + av_freep(&c->priv_data);
  4386. +}
  4387. +
  4388. +static void copy_coder (struct ObmcCoderContext *c)
  4389. +{
  4390. + SnowContext *f = (SnowContext *)c->avctx->priv_data;
  4391. + RangeEncoderContext *coder = (RangeEncoderContext *)c->priv_data;
  4392. +
  4393. + int len = coder->c.bytestream - coder->c.bytestream_start;
  4394. +
  4395. + memcpy(coder->pbbak, coder->buffer, len);
  4396. + f->c = coder->c;
  4397. + f->c.bytestream_start= coder->pbbak_start;
  4398. + f->c.bytestream= coder->pbbak + len;
  4399. + memcpy(f->block_state, coder->state, sizeof(f->block_state));
  4400. +}
  4401. +
  4402. +static void reset_coder (struct ObmcCoderContext *c)
  4403. +{
  4404. + SnowContext *f = (SnowContext *)c->avctx->priv_data;
  4405. + RangeEncoderContext *coder = (RangeEncoderContext *)c->priv_data;
  4406. +
  4407. + f->c = coder->c;
  4408. + f->c.bytestream_start= coder->pbbak_start;
  4409. + f->c.bytestream= coder->pbbak;
  4410. + memcpy(f->block_state, coder->state, sizeof(f->block_state));
  4411. +}
  4412. +
  4413. +static void put_level_break(ObmcCoderContext *c, int ctx, int v)
  4414. +{
  4415. + put_encoder_rac(c, ctx, v);
  4416. +}
  4417. +
  4418. +static void put_block_type (struct ObmcCoderContext *c, int ctx, int type)
  4419. +{
  4420. + put_encoder_rac(c, ctx, type);
  4421. +}
  4422. +
  4423. +static void put_best_ref (struct ObmcCoderContext *c, int ctx, int best_ref)
  4424. +{
  4425. + put_encoder_symbol(c, ctx, best_ref, 0);
  4426. +}
  4427. +
  4428. +static void put_block_mv (struct ObmcCoderContext *c, int ctx_mx, int ctx_my, int mx, int my)
  4429. +{
  4430. + put_encoder_symbol(c, ctx_mx, mx, 1);
  4431. + put_encoder_symbol(c, ctx_my, my, 1);
  4432. +}
  4433. +
  4434. +static void put_block_color (struct ObmcCoderContext *c, int ctx_l, int ctx_cb, int ctx_cr, int l, int cb, int cr)
  4435. +{
  4436. + SnowContext *s = (SnowContext *)c->avctx->priv_data;
  4437. + put_encoder_symbol(c, ctx_l, l, 1);
  4438. + if (s->obmc.nb_planes > 2) {
  4439. + put_encoder_symbol(c, ctx_cb, cb, 1);
  4440. + put_encoder_symbol(c, ctx_cr, cr, 1);
  4441. + }
  4442. +}
  4443. +
  4444. +static int get_coder_bits(ObmcCoderContext *c)
  4445. +{
  4446. + RangeEncoderContext *coder = (RangeEncoderContext *)c->priv_data;
  4447. + return get_rac_count(&coder->c) - coder->base_bits;
  4448. +}
  4449. +
  4450. +static int get_coder_available_bytes(ObmcCoderContext *c)
  4451. +{
  4452. + SnowContext *s = (SnowContext *)c->avctx->priv_data;
  4453. + RangeCoder *rc = &s->c;
  4454. + if (c->priv_data) {
  4455. + RangeEncoderContext *coder = (RangeEncoderContext *)c->priv_data;
  4456. + rc = &coder->c;
  4457. + }
  4458. + return rc->bytestream_end - rc->bytestream;
  4459. +}
  4460. +
  4461. +static void ff_snow_init_encode_callbacks(ObmcCoderContext *c, AVCodecContext *avctx)
  4462. +{
  4463. + SnowContext *s = (SnowContext *)c->avctx->priv_data;
  4464. + av_assert0(sizeof(s->block_state) >= 256);
  4465. +
  4466. + c->avctx = avctx;
  4467. + c->put_level_break = put_level_break;
  4468. + c->put_block_type = put_block_type;
  4469. + c->put_block_color = put_block_color;
  4470. + c->put_best_ref = put_best_ref;
  4471. + c->put_block_mv = put_block_mv;
  4472. +
  4473. + c->init_frame_coder = init_frame_encoder;
  4474. + c->reset_coder = reset_coder;
  4475. + c->copy_coder = copy_coder;
  4476. + c->free = free_coder;
  4477. +
  4478. + c->get_bits = get_coder_bits;
  4479. + c->available_bytes = get_coder_available_bytes;
  4480. +}
  4481. +
  4482. static av_cold int encode_init(AVCodecContext *avctx)
  4483. {
  4484. SnowContext *s = avctx->priv_data;
  4485. - int plane_index, ret;
  4486. - int i;
  4487. + int ret;
  4488.  
  4489. #if FF_API_PRIVATE_OPT
  4490. FF_DISABLE_DEPRECATION_WARNINGS
  4491. @@ -54,51 +202,15 @@ FF_ENABLE_DEPRECATION_WARNINGS
  4492. av_log(avctx, AV_LOG_ERROR, "The 9/7 wavelet is incompatible with lossless mode.\n");
  4493. return -1;
  4494. }
  4495. -#if FF_API_MOTION_EST
  4496. -FF_DISABLE_DEPRECATION_WARNINGS
  4497. - if (avctx->me_method == ME_ITER)
  4498. - s->motion_est = FF_ME_ITER;
  4499. -FF_ENABLE_DEPRECATION_WARNINGS
  4500. -#endif
  4501.  
  4502. s->spatial_decomposition_type= s->pred; //FIXME add decorrelator type r transform_type
  4503.  
  4504. - s->mv_scale = (avctx->flags & AV_CODEC_FLAG_QPEL) ? 2 : 4;
  4505. - s->block_max_depth= (avctx->flags & AV_CODEC_FLAG_4MV ) ? 1 : 0;
  4506. -
  4507. - for(plane_index=0; plane_index<3; plane_index++){
  4508. - s->plane[plane_index].diag_mc= 1;
  4509. - s->plane[plane_index].htaps= 6;
  4510. - s->plane[plane_index].hcoeff[0]= 40;
  4511. - s->plane[plane_index].hcoeff[1]= -10;
  4512. - s->plane[plane_index].hcoeff[2]= 2;
  4513. - s->plane[plane_index].fast_mc= 1;
  4514. - }
  4515. -
  4516. if ((ret = ff_snow_common_init(avctx)) < 0) {
  4517. return ret;
  4518. }
  4519. - ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
  4520. -
  4521. - ff_snow_alloc_blocks(s);
  4522.  
  4523. s->version=0;
  4524.  
  4525. - s->m.avctx = avctx;
  4526. - s->m.bit_rate= avctx->bit_rate;
  4527. -
  4528. - s->m.me.temp =
  4529. - s->m.me.scratchpad= av_mallocz_array((avctx->width+64), 2*16*2*sizeof(uint8_t));
  4530. - s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  4531. - s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  4532. - s->m.sc.obmc_scratchpad= av_mallocz(MB_SIZE*MB_SIZE*12*sizeof(uint32_t));
  4533. - if (!s->m.me.scratchpad || !s->m.me.map || !s->m.me.score_map || !s->m.sc.obmc_scratchpad)
  4534. - return AVERROR(ENOMEM);
  4535. -
  4536. - ff_h263_encode_init(&s->m); //mv_penalty
  4537. -
  4538. - s->max_ref_frames = av_clip(avctx->refs, 1, MAX_REF_FRAMES);
  4539. -
  4540. if(avctx->flags&AV_CODEC_FLAG_PASS1){
  4541. if(!avctx->stats_out)
  4542. avctx->stats_out = av_mallocz(256);
  4543. @@ -106,11 +218,6 @@ FF_ENABLE_DEPRECATION_WARNINGS
  4544. if (!avctx->stats_out)
  4545. return AVERROR(ENOMEM);
  4546. }
  4547. - if((avctx->flags&AV_CODEC_FLAG_PASS2) || !(avctx->flags&CODEC_FLAG_QSCALE)){
  4548. - if(ff_rate_control_init(&s->m) < 0)
  4549. - return -1;
  4550. - }
  4551. - s->pass1_rc= !(avctx->flags & (AV_CODEC_FLAG_QSCALE|CODEC_FLAG_PASS2));
  4552.  
  4553. switch(avctx->pix_fmt){
  4554. case AV_PIX_FMT_YUV444P:
  4555. @@ -134,644 +241,18 @@ FF_ENABLE_DEPRECATION_WARNINGS
  4556. }
  4557. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  4558.  
  4559. - ff_set_cmp(&s->mecc, s->mecc.me_cmp, s->avctx->me_cmp);
  4560. - ff_set_cmp(&s->mecc, s->mecc.me_sub_cmp, s->avctx->me_sub_cmp);
  4561. + ff_obmc_encode_init(&s->obmc, avctx);
  4562. + ff_snow_init_encode_callbacks(&s->obmc.obmc_coder, avctx);
  4563.  
  4564. - s->input_picture = av_frame_alloc();
  4565. - if (!s->input_picture)
  4566. - return AVERROR(ENOMEM);
  4567. -
  4568. - if ((ret = ff_snow_get_buffer(s, s->input_picture)) < 0)
  4569. - return ret;
  4570. -
  4571. - if(s->motion_est == FF_ME_ITER){
  4572. - int size= s->b_width * s->b_height << 2*s->block_max_depth;
  4573. - for(i=0; i<s->max_ref_frames; i++){
  4574. - s->ref_mvs[i]= av_mallocz_array(size, sizeof(int16_t[2]));
  4575. - s->ref_scores[i]= av_mallocz_array(size, sizeof(uint32_t));
  4576. - if (!s->ref_mvs[i] || !s->ref_scores[i])
  4577. - return AVERROR(ENOMEM);
  4578. - }
  4579. + if((avctx->flags&AV_CODEC_FLAG_PASS2) || !(avctx->flags&CODEC_FLAG_QSCALE)){
  4580. + if(ff_rate_control_init(&s->obmc.m) < 0)
  4581. + return -1;
  4582. }
  4583. + s->pass1_rc= !(avctx->flags & (AV_CODEC_FLAG_QSCALE|CODEC_FLAG_PASS2));
  4584.  
  4585. return 0;
  4586. }
  4587.  
  4588. -//near copy & paste from dsputil, FIXME
  4589. -static int pix_sum(uint8_t * pix, int line_size, int w, int h)
  4590. -{
  4591. - int s, i, j;
  4592. -
  4593. - s = 0;
  4594. - for (i = 0; i < h; i++) {
  4595. - for (j = 0; j < w; j++) {
  4596. - s += pix[0];
  4597. - pix ++;
  4598. - }
  4599. - pix += line_size - w;
  4600. - }
  4601. - return s;
  4602. -}
  4603. -
  4604. -//near copy & paste from dsputil, FIXME
  4605. -static int pix_norm1(uint8_t * pix, int line_size, int w)
  4606. -{
  4607. - int s, i, j;
  4608. - uint32_t *sq = ff_square_tab + 256;
  4609. -
  4610. - s = 0;
  4611. - for (i = 0; i < w; i++) {
  4612. - for (j = 0; j < w; j ++) {
  4613. - s += sq[pix[0]];
  4614. - pix ++;
  4615. - }
  4616. - pix += line_size - w;
  4617. - }
  4618. - return s;
  4619. -}
  4620. -
  4621. -static inline int get_penalty_factor(int lambda, int lambda2, int type){
  4622. - switch(type&0xFF){
  4623. - default:
  4624. - case FF_CMP_SAD:
  4625. - return lambda>>FF_LAMBDA_SHIFT;
  4626. - case FF_CMP_DCT:
  4627. - return (3*lambda)>>(FF_LAMBDA_SHIFT+1);
  4628. - case FF_CMP_W53:
  4629. - return (4*lambda)>>(FF_LAMBDA_SHIFT);
  4630. - case FF_CMP_W97:
  4631. - return (2*lambda)>>(FF_LAMBDA_SHIFT);
  4632. - case FF_CMP_SATD:
  4633. - case FF_CMP_DCT264:
  4634. - return (2*lambda)>>FF_LAMBDA_SHIFT;
  4635. - case FF_CMP_RD:
  4636. - case FF_CMP_PSNR:
  4637. - case FF_CMP_SSE:
  4638. - case FF_CMP_NSSE:
  4639. - return lambda2>>FF_LAMBDA_SHIFT;
  4640. - case FF_CMP_BIT:
  4641. - return 1;
  4642. - }
  4643. -}
  4644. -
  4645. -//FIXME copy&paste
  4646. -#define P_LEFT P[1]
  4647. -#define P_TOP P[2]
  4648. -#define P_TOPRIGHT P[3]
  4649. -#define P_MEDIAN P[4]
  4650. -#define P_MV1 P[9]
  4651. -#define FLAG_QPEL 1 //must be 1
  4652. -
  4653. -static int encode_q_branch(SnowContext *s, int level, int x, int y){
  4654. - uint8_t p_buffer[1024];
  4655. - uint8_t i_buffer[1024];
  4656. - uint8_t p_state[sizeof(s->block_state)];
  4657. - uint8_t i_state[sizeof(s->block_state)];
  4658. - RangeCoder pc, ic;
  4659. - uint8_t *pbbak= s->c.bytestream;
  4660. - uint8_t *pbbak_start= s->c.bytestream_start;
  4661. - int score, score2, iscore, i_len, p_len, block_s, sum, base_bits;
  4662. - const int w= s->b_width << s->block_max_depth;
  4663. - const int h= s->b_height << s->block_max_depth;
  4664. - const int rem_depth= s->block_max_depth - level;
  4665. - const int index= (x + y*w) << rem_depth;
  4666. - const int block_w= 1<<(LOG2_MB_SIZE - level);
  4667. - int trx= (x+1)<<rem_depth;
  4668. - int try= (y+1)<<rem_depth;
  4669. - const BlockNode *left = x ? &s->block[index-1] : &null_block;
  4670. - const BlockNode *top = y ? &s->block[index-w] : &null_block;
  4671. - const BlockNode *right = trx<w ? &s->block[index+1] : &null_block;
  4672. - const BlockNode *bottom= try<h ? &s->block[index+w] : &null_block;
  4673. - const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  4674. - const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  4675. - int pl = left->color[0];
  4676. - int pcb= left->color[1];
  4677. - int pcr= left->color[2];
  4678. - int pmx, pmy;
  4679. - int mx=0, my=0;
  4680. - int l,cr,cb;
  4681. - const int stride= s->current_picture->linesize[0];
  4682. - const int uvstride= s->current_picture->linesize[1];
  4683. - uint8_t *current_data[3]= { s->input_picture->data[0] + (x + y* stride)*block_w,
  4684. - s->input_picture->data[1] + ((x*block_w)>>s->chroma_h_shift) + ((y*uvstride*block_w)>>s->chroma_v_shift),
  4685. - s->input_picture->data[2] + ((x*block_w)>>s->chroma_h_shift) + ((y*uvstride*block_w)>>s->chroma_v_shift)};
  4686. - int P[10][2];
  4687. - int16_t last_mv[3][2];
  4688. - int qpel= !!(s->avctx->flags & AV_CODEC_FLAG_QPEL); //unused
  4689. - const int shift= 1+qpel;
  4690. - MotionEstContext *c= &s->m.me;
  4691. - int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
  4692. - int mx_context= av_log2(2*FFABS(left->mx - top->mx));
  4693. - int my_context= av_log2(2*FFABS(left->my - top->my));
  4694. - int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  4695. - int ref, best_ref, ref_score, ref_mx, ref_my;
  4696. -
  4697. - av_assert0(sizeof(s->block_state) >= 256);
  4698. - if(s->keyframe){
  4699. - set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
  4700. - return 0;
  4701. - }
  4702. -
  4703. -// clip predictors / edge ?
  4704. -
  4705. - P_LEFT[0]= left->mx;
  4706. - P_LEFT[1]= left->my;
  4707. - P_TOP [0]= top->mx;
  4708. - P_TOP [1]= top->my;
  4709. - P_TOPRIGHT[0]= tr->mx;
  4710. - P_TOPRIGHT[1]= tr->my;
  4711. -
  4712. - last_mv[0][0]= s->block[index].mx;
  4713. - last_mv[0][1]= s->block[index].my;
  4714. - last_mv[1][0]= right->mx;
  4715. - last_mv[1][1]= right->my;
  4716. - last_mv[2][0]= bottom->mx;
  4717. - last_mv[2][1]= bottom->my;
  4718. -
  4719. - s->m.mb_stride=2;
  4720. - s->m.mb_x=
  4721. - s->m.mb_y= 0;
  4722. - c->skip= 0;
  4723. -
  4724. - av_assert1(c-> stride == stride);
  4725. - av_assert1(c->uvstride == uvstride);
  4726. -
  4727. - c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  4728. - c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  4729. - c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  4730. - c->current_mv_penalty= c->mv_penalty[s->m.f_code=1] + MAX_DMV;
  4731. -
  4732. - c->xmin = - x*block_w - 16+3;
  4733. - c->ymin = - y*block_w - 16+3;
  4734. - c->xmax = - (x+1)*block_w + (w<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-3;
  4735. - c->ymax = - (y+1)*block_w + (h<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-3;
  4736. -
  4737. - if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  4738. - if(P_LEFT[1] > (c->ymax<<shift)) P_LEFT[1] = (c->ymax<<shift);
  4739. - if(P_TOP[0] > (c->xmax<<shift)) P_TOP[0] = (c->xmax<<shift);
  4740. - if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
  4741. - if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  4742. - if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift); //due to pmx no clip
  4743. - if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  4744. -
  4745. - P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  4746. - P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  4747. -
  4748. - if (!y) {
  4749. - c->pred_x= P_LEFT[0];
  4750. - c->pred_y= P_LEFT[1];
  4751. - } else {
  4752. - c->pred_x = P_MEDIAN[0];
  4753. - c->pred_y = P_MEDIAN[1];
  4754. - }
  4755. -
  4756. - score= INT_MAX;
  4757. - best_ref= 0;
  4758. - for(ref=0; ref<s->ref_frames; ref++){
  4759. - init_ref(c, current_data, s->last_picture[ref]->data, NULL, block_w*x, block_w*y, 0);
  4760. -
  4761. - ref_score= ff_epzs_motion_search(&s->m, &ref_mx, &ref_my, P, 0, /*ref_index*/ 0, last_mv,
  4762. - (1<<16)>>shift, level-LOG2_MB_SIZE+4, block_w);
  4763. -
  4764. - av_assert2(ref_mx >= c->xmin);
  4765. - av_assert2(ref_mx <= c->xmax);
  4766. - av_assert2(ref_my >= c->ymin);
  4767. - av_assert2(ref_my <= c->ymax);
  4768. -
  4769. - ref_score= c->sub_motion_search(&s->m, &ref_mx, &ref_my, ref_score, 0, 0, level-LOG2_MB_SIZE+4, block_w);
  4770. - ref_score= ff_get_mb_score(&s->m, ref_mx, ref_my, 0, 0, level-LOG2_MB_SIZE+4, block_w, 0);
  4771. - ref_score+= 2*av_log2(2*ref)*c->penalty_factor;
  4772. - if(s->ref_mvs[ref]){
  4773. - s->ref_mvs[ref][index][0]= ref_mx;
  4774. - s->ref_mvs[ref][index][1]= ref_my;
  4775. - s->ref_scores[ref][index]= ref_score;
  4776. - }
  4777. - if(score > ref_score){
  4778. - score= ref_score;
  4779. - best_ref= ref;
  4780. - mx= ref_mx;
  4781. - my= ref_my;
  4782. - }
  4783. - }
  4784. - //FIXME if mb_cmp != SSE then intra cannot be compared currently and mb_penalty vs. lambda2
  4785. -
  4786. - // subpel search
  4787. - base_bits= get_rac_count(&s->c) - 8*(s->c.bytestream - s->c.bytestream_start);
  4788. - pc= s->c;
  4789. - pc.bytestream_start=
  4790. - pc.bytestream= p_buffer; //FIXME end/start? and at the other stoo
  4791. - memcpy(p_state, s->block_state, sizeof(s->block_state));
  4792. -
  4793. - if(level!=s->block_max_depth)
  4794. - put_rac(&pc, &p_state[4 + s_context], 1);
  4795. - put_rac(&pc, &p_state[1 + left->type + top->type], 0);
  4796. - if(s->ref_frames > 1)
  4797. - put_symbol(&pc, &p_state[128 + 1024 + 32*ref_context], best_ref, 0);
  4798. - pred_mv(s, &pmx, &pmy, best_ref, left, top, tr);
  4799. - put_symbol(&pc, &p_state[128 + 32*(mx_context + 16*!!best_ref)], mx - pmx, 1);
  4800. - put_symbol(&pc, &p_state[128 + 32*(my_context + 16*!!best_ref)], my - pmy, 1);
  4801. - p_len= pc.bytestream - pc.bytestream_start;
  4802. - score += (s->lambda2*(get_rac_count(&pc)-base_bits))>>FF_LAMBDA_SHIFT;
  4803. -
  4804. - block_s= block_w*block_w;
  4805. - sum = pix_sum(current_data[0], stride, block_w, block_w);
  4806. - l= (sum + block_s/2)/block_s;
  4807. - iscore = pix_norm1(current_data[0], stride, block_w) - 2*l*sum + l*l*block_s;
  4808. -
  4809. - if (s->nb_planes > 2) {
  4810. - block_s= block_w*block_w>>(s->chroma_h_shift + s->chroma_v_shift);
  4811. - sum = pix_sum(current_data[1], uvstride, block_w>>s->chroma_h_shift, block_w>>s->chroma_v_shift);
  4812. - cb= (sum + block_s/2)/block_s;
  4813. - // iscore += pix_norm1(&current_mb[1][0], uvstride, block_w>>1) - 2*cb*sum + cb*cb*block_s;
  4814. - sum = pix_sum(current_data[2], uvstride, block_w>>s->chroma_h_shift, block_w>>s->chroma_v_shift);
  4815. - cr= (sum + block_s/2)/block_s;
  4816. - // iscore += pix_norm1(&current_mb[2][0], uvstride, block_w>>1) - 2*cr*sum + cr*cr*block_s;
  4817. - }else
  4818. - cb = cr = 0;
  4819. -
  4820. - ic= s->c;
  4821. - ic.bytestream_start=
  4822. - ic.bytestream= i_buffer; //FIXME end/start? and at the other stoo
  4823. - memcpy(i_state, s->block_state, sizeof(s->block_state));
  4824. - if(level!=s->block_max_depth)
  4825. - put_rac(&ic, &i_state[4 + s_context], 1);
  4826. - put_rac(&ic, &i_state[1 + left->type + top->type], 1);
  4827. - put_symbol(&ic, &i_state[32], l-pl , 1);
  4828. - if (s->nb_planes > 2) {
  4829. - put_symbol(&ic, &i_state[64], cb-pcb, 1);
  4830. - put_symbol(&ic, &i_state[96], cr-pcr, 1);
  4831. - }
  4832. - i_len= ic.bytestream - ic.bytestream_start;
  4833. - iscore += (s->lambda2*(get_rac_count(&ic)-base_bits))>>FF_LAMBDA_SHIFT;
  4834. -
  4835. - av_assert1(iscore < 255*255*256 + s->lambda2*10);
  4836. - av_assert1(iscore >= 0);
  4837. - av_assert1(l>=0 && l<=255);
  4838. - av_assert1(pl>=0 && pl<=255);
  4839. -
  4840. - if(level==0){
  4841. - int varc= iscore >> 8;
  4842. - int vard= score >> 8;
  4843. - if (vard <= 64 || vard < varc)
  4844. - c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
  4845. - else
  4846. - c->scene_change_score+= s->m.qscale;
  4847. - }
  4848. -
  4849. - if(level!=s->block_max_depth){
  4850. - put_rac(&s->c, &s->block_state[4 + s_context], 0);
  4851. - score2 = encode_q_branch(s, level+1, 2*x+0, 2*y+0);
  4852. - score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+0);
  4853. - score2+= encode_q_branch(s, level+1, 2*x+0, 2*y+1);
  4854. - score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+1);
  4855. - score2+= s->lambda2>>FF_LAMBDA_SHIFT; //FIXME exact split overhead
  4856. -
  4857. - if(score2 < score && score2 < iscore)
  4858. - return score2;
  4859. - }
  4860. -
  4861. - if(iscore < score){
  4862. - pred_mv(s, &pmx, &pmy, 0, left, top, tr);
  4863. - memcpy(pbbak, i_buffer, i_len);
  4864. - s->c= ic;
  4865. - s->c.bytestream_start= pbbak_start;
  4866. - s->c.bytestream= pbbak + i_len;
  4867. - set_blocks(s, level, x, y, l, cb, cr, pmx, pmy, 0, BLOCK_INTRA);
  4868. - memcpy(s->block_state, i_state, sizeof(s->block_state));
  4869. - return iscore;
  4870. - }else{
  4871. - memcpy(pbbak, p_buffer, p_len);
  4872. - s->c= pc;
  4873. - s->c.bytestream_start= pbbak_start;
  4874. - s->c.bytestream= pbbak + p_len;
  4875. - set_blocks(s, level, x, y, pl, pcb, pcr, mx, my, best_ref, 0);
  4876. - memcpy(s->block_state, p_state, sizeof(s->block_state));
  4877. - return score;
  4878. - }
  4879. -}
  4880. -
  4881. -static void encode_q_branch2(SnowContext *s, int level, int x, int y){
  4882. - const int w= s->b_width << s->block_max_depth;
  4883. - const int rem_depth= s->block_max_depth - level;
  4884. - const int index= (x + y*w) << rem_depth;
  4885. - int trx= (x+1)<<rem_depth;
  4886. - BlockNode *b= &s->block[index];
  4887. - const BlockNode *left = x ? &s->block[index-1] : &null_block;
  4888. - const BlockNode *top = y ? &s->block[index-w] : &null_block;
  4889. - const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  4890. - const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  4891. - int pl = left->color[0];
  4892. - int pcb= left->color[1];
  4893. - int pcr= left->color[2];
  4894. - int pmx, pmy;
  4895. - int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
  4896. - int mx_context= av_log2(2*FFABS(left->mx - top->mx)) + 16*!!b->ref;
  4897. - int my_context= av_log2(2*FFABS(left->my - top->my)) + 16*!!b->ref;
  4898. - int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  4899. -
  4900. - if(s->keyframe){
  4901. - set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
  4902. - return;
  4903. - }
  4904. -
  4905. - if(level!=s->block_max_depth){
  4906. - if(same_block(b,b+1) && same_block(b,b+w) && same_block(b,b+w+1)){
  4907. - put_rac(&s->c, &s->block_state[4 + s_context], 1);
  4908. - }else{
  4909. - put_rac(&s->c, &s->block_state[4 + s_context], 0);
  4910. - encode_q_branch2(s, level+1, 2*x+0, 2*y+0);
  4911. - encode_q_branch2(s, level+1, 2*x+1, 2*y+0);
  4912. - encode_q_branch2(s, level+1, 2*x+0, 2*y+1);
  4913. - encode_q_branch2(s, level+1, 2*x+1, 2*y+1);
  4914. - return;
  4915. - }
  4916. - }
  4917. - if(b->type & BLOCK_INTRA){
  4918. - pred_mv(s, &pmx, &pmy, 0, left, top, tr);
  4919. - put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 1);
  4920. - put_symbol(&s->c, &s->block_state[32], b->color[0]-pl , 1);
  4921. - if (s->nb_planes > 2) {
  4922. - put_symbol(&s->c, &s->block_state[64], b->color[1]-pcb, 1);
  4923. - put_symbol(&s->c, &s->block_state[96], b->color[2]-pcr, 1);
  4924. - }
  4925. - set_blocks(s, level, x, y, b->color[0], b->color[1], b->color[2], pmx, pmy, 0, BLOCK_INTRA);
  4926. - }else{
  4927. - pred_mv(s, &pmx, &pmy, b->ref, left, top, tr);
  4928. - put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 0);
  4929. - if(s->ref_frames > 1)
  4930. - put_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], b->ref, 0);
  4931. - put_symbol(&s->c, &s->block_state[128 + 32*mx_context], b->mx - pmx, 1);
  4932. - put_symbol(&s->c, &s->block_state[128 + 32*my_context], b->my - pmy, 1);
  4933. - set_blocks(s, level, x, y, pl, pcb, pcr, b->mx, b->my, b->ref, 0);
  4934. - }
  4935. -}
  4936. -
  4937. -static int get_dc(SnowContext *s, int mb_x, int mb_y, int plane_index){
  4938. - int i, x2, y2;
  4939. - Plane *p= &s->plane[plane_index];
  4940. - const int block_size = MB_SIZE >> s->block_max_depth;
  4941. - const int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
  4942. - const int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
  4943. - const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];
  4944. - const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
  4945. - const int ref_stride= s->current_picture->linesize[plane_index];
  4946. - uint8_t *src= s-> input_picture->data[plane_index];
  4947. - IDWTELEM *dst= (IDWTELEM*)s->m.sc.obmc_scratchpad + plane_index*block_size*block_size*4; //FIXME change to unsigned
  4948. - const int b_stride = s->b_width << s->block_max_depth;
  4949. - const int w= p->width;
  4950. - const int h= p->height;
  4951. - int index= mb_x + mb_y*b_stride;
  4952. - BlockNode *b= &s->block[index];
  4953. - BlockNode backup= *b;
  4954. - int ab=0;
  4955. - int aa=0;
  4956. -
  4957. - av_assert2(s->chroma_h_shift == s->chroma_v_shift); //obmc stuff above
  4958. -
  4959. - b->type|= BLOCK_INTRA;
  4960. - b->color[plane_index]= 0;
  4961. - memset(dst, 0, obmc_stride*obmc_stride*sizeof(IDWTELEM));
  4962. -
  4963. - for(i=0; i<4; i++){
  4964. - int mb_x2= mb_x + (i &1) - 1;
  4965. - int mb_y2= mb_y + (i>>1) - 1;
  4966. - int x= block_w*mb_x2 + block_w/2;
  4967. - int y= block_h*mb_y2 + block_h/2;
  4968. -
  4969. - add_yblock(s, 0, NULL, dst + (i&1)*block_w + (i>>1)*obmc_stride*block_h, NULL, obmc,
  4970. - x, y, block_w, block_h, w, h, obmc_stride, ref_stride, obmc_stride, mb_x2, mb_y2, 0, 0, plane_index);
  4971. -
  4972. - for(y2= FFMAX(y, 0); y2<FFMIN(h, y+block_h); y2++){
  4973. - for(x2= FFMAX(x, 0); x2<FFMIN(w, x+block_w); x2++){
  4974. - int index= x2-(block_w*mb_x - block_w/2) + (y2-(block_h*mb_y - block_h/2))*obmc_stride;
  4975. - int obmc_v= obmc[index];
  4976. - int d;
  4977. - if(y<0) obmc_v += obmc[index + block_h*obmc_stride];
  4978. - if(x<0) obmc_v += obmc[index + block_w];
  4979. - if(y+block_h>h) obmc_v += obmc[index - block_h*obmc_stride];
  4980. - if(x+block_w>w) obmc_v += obmc[index - block_w];
  4981. - //FIXME precalculate this or simplify it somehow else
  4982. -
  4983. - d = -dst[index] + (1<<(FRAC_BITS-1));
  4984. - dst[index] = d;
  4985. - ab += (src[x2 + y2*ref_stride] - (d>>FRAC_BITS)) * obmc_v;
  4986. - aa += obmc_v * obmc_v; //FIXME precalculate this
  4987. - }
  4988. - }
  4989. - }
  4990. - *b= backup;
  4991. -
  4992. - return av_clip_uint8( ROUNDED_DIV(ab<<LOG2_OBMC_MAX, aa) ); //FIXME we should not need clipping
  4993. -}
  4994. -
  4995. -static inline int get_block_bits(SnowContext *s, int x, int y, int w){
  4996. - const int b_stride = s->b_width << s->block_max_depth;
  4997. - const int b_height = s->b_height<< s->block_max_depth;
  4998. - int index= x + y*b_stride;
  4999. - const BlockNode *b = &s->block[index];
  5000. - const BlockNode *left = x ? &s->block[index-1] : &null_block;
  5001. - const BlockNode *top = y ? &s->block[index-b_stride] : &null_block;
  5002. - const BlockNode *tl = y && x ? &s->block[index-b_stride-1] : left;
  5003. - const BlockNode *tr = y && x+w<b_stride ? &s->block[index-b_stride+w] : tl;
  5004. - int dmx, dmy;
  5005. -// int mx_context= av_log2(2*FFABS(left->mx - top->mx));
  5006. -// int my_context= av_log2(2*FFABS(left->my - top->my));
  5007. -
  5008. - if(x<0 || x>=b_stride || y>=b_height)
  5009. - return 0;
  5010. -/*
  5011. -1 0 0
  5012. -01X 1-2 1
  5013. -001XX 3-6 2-3
  5014. -0001XXX 7-14 4-7
  5015. -00001XXXX 15-30 8-15
  5016. -*/
  5017. -//FIXME try accurate rate
  5018. -//FIXME intra and inter predictors if surrounding blocks are not the same type
  5019. - if(b->type & BLOCK_INTRA){
  5020. - return 3+2*( av_log2(2*FFABS(left->color[0] - b->color[0]))
  5021. - + av_log2(2*FFABS(left->color[1] - b->color[1]))
  5022. - + av_log2(2*FFABS(left->color[2] - b->color[2])));
  5023. - }else{
  5024. - pred_mv(s, &dmx, &dmy, b->ref, left, top, tr);
  5025. - dmx-= b->mx;
  5026. - dmy-= b->my;
  5027. - return 2*(1 + av_log2(2*FFABS(dmx)) //FIXME kill the 2* can be merged in lambda
  5028. - + av_log2(2*FFABS(dmy))
  5029. - + av_log2(2*b->ref));
  5030. - }
  5031. -}
  5032. -
  5033. -static int get_block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index, uint8_t (*obmc_edged)[MB_SIZE * 2]){
  5034. - Plane *p= &s->plane[plane_index];
  5035. - const int block_size = MB_SIZE >> s->block_max_depth;
  5036. - const int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
  5037. - const int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
  5038. - const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
  5039. - const int ref_stride= s->current_picture->linesize[plane_index];
  5040. - uint8_t *dst= s->current_picture->data[plane_index];
  5041. - uint8_t *src= s-> input_picture->data[plane_index];
  5042. - IDWTELEM *pred= (IDWTELEM*)s->m.sc.obmc_scratchpad + plane_index*block_size*block_size*4;
  5043. - uint8_t *cur = s->scratchbuf;
  5044. - uint8_t *tmp = s->emu_edge_buffer;
  5045. - const int b_stride = s->b_width << s->block_max_depth;
  5046. - const int b_height = s->b_height<< s->block_max_depth;
  5047. - const int w= p->width;
  5048. - const int h= p->height;
  5049. - int distortion;
  5050. - int rate= 0;
  5051. - const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
  5052. - int sx= block_w*mb_x - block_w/2;
  5053. - int sy= block_h*mb_y - block_h/2;
  5054. - int x0= FFMAX(0,-sx);
  5055. - int y0= FFMAX(0,-sy);
  5056. - int x1= FFMIN(block_w*2, w-sx);
  5057. - int y1= FFMIN(block_h*2, h-sy);
  5058. - int i,x,y;
  5059. -
  5060. - av_assert2(s->chroma_h_shift == s->chroma_v_shift); //obmc and square assumtions below chckinhg only block_w
  5061. -
  5062. - ff_snow_pred_block(s, cur, tmp, ref_stride, sx, sy, block_w*2, block_h*2, &s->block[mb_x + mb_y*b_stride], plane_index, w, h);
  5063. -
  5064. - for(y=y0; y<y1; y++){
  5065. - const uint8_t *obmc1= obmc_edged[y];
  5066. - const IDWTELEM *pred1 = pred + y*obmc_stride;
  5067. - uint8_t *cur1 = cur + y*ref_stride;
  5068. - uint8_t *dst1 = dst + sx + (sy+y)*ref_stride;
  5069. - for(x=x0; x<x1; x++){
  5070. -#if FRAC_BITS >= LOG2_OBMC_MAX
  5071. - int v = (cur1[x] * obmc1[x]) << (FRAC_BITS - LOG2_OBMC_MAX);
  5072. -#else
  5073. - int v = (cur1[x] * obmc1[x] + (1<<(LOG2_OBMC_MAX - FRAC_BITS-1))) >> (LOG2_OBMC_MAX - FRAC_BITS);
  5074. -#endif
  5075. - v = (v + pred1[x]) >> FRAC_BITS;
  5076. - if(v&(~255)) v= ~(v>>31);
  5077. - dst1[x] = v;
  5078. - }
  5079. - }
  5080. -
  5081. - /* copy the regions where obmc[] = (uint8_t)256 */
  5082. - if(LOG2_OBMC_MAX == 8
  5083. - && (mb_x == 0 || mb_x == b_stride-1)
  5084. - && (mb_y == 0 || mb_y == b_height-1)){
  5085. - if(mb_x == 0)
  5086. - x1 = block_w;
  5087. - else
  5088. - x0 = block_w;
  5089. - if(mb_y == 0)
  5090. - y1 = block_h;
  5091. - else
  5092. - y0 = block_h;
  5093. - for(y=y0; y<y1; y++)
  5094. - memcpy(dst + sx+x0 + (sy+y)*ref_stride, cur + x0 + y*ref_stride, x1-x0);
  5095. - }
  5096. -
  5097. - if(block_w==16){
  5098. - /* FIXME rearrange dsputil to fit 32x32 cmp functions */
  5099. - /* FIXME check alignment of the cmp wavelet vs the encoding wavelet */
  5100. - /* FIXME cmps overlap but do not cover the wavelet's whole support.
  5101. - * So improving the score of one block is not strictly guaranteed
  5102. - * to improve the score of the whole frame, thus iterative motion
  5103. - * estimation does not always converge. */
  5104. - if(s->avctx->me_cmp == FF_CMP_W97)
  5105. - distortion = ff_w97_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
  5106. - else if(s->avctx->me_cmp == FF_CMP_W53)
  5107. - distortion = ff_w53_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
  5108. - else{
  5109. - distortion = 0;
  5110. - for(i=0; i<4; i++){
  5111. - int off = sx+16*(i&1) + (sy+16*(i>>1))*ref_stride;
  5112. - distortion += s->mecc.me_cmp[0](&s->m, src + off, dst + off, ref_stride, 16);
  5113. - }
  5114. - }
  5115. - }else{
  5116. - av_assert2(block_w==8);
  5117. - distortion = s->mecc.me_cmp[0](&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, block_w*2);
  5118. - }
  5119. -
  5120. - if(plane_index==0){
  5121. - for(i=0; i<4; i++){
  5122. -/* ..RRr
  5123. - * .RXx.
  5124. - * rxx..
  5125. - */
  5126. - rate += get_block_bits(s, mb_x + (i&1) - (i>>1), mb_y + (i>>1), 1);
  5127. - }
  5128. - if(mb_x == b_stride-2)
  5129. - rate += get_block_bits(s, mb_x + 1, mb_y + 1, 1);
  5130. - }
  5131. - return distortion + rate*penalty_factor;
  5132. -}
  5133. -
  5134. -static int get_4block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index){
  5135. - int i, y2;
  5136. - Plane *p= &s->plane[plane_index];
  5137. - const int block_size = MB_SIZE >> s->block_max_depth;
  5138. - const int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
  5139. - const int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
  5140. - const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];
  5141. - const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
  5142. - const int ref_stride= s->current_picture->linesize[plane_index];
  5143. - uint8_t *dst= s->current_picture->data[plane_index];
  5144. - uint8_t *src= s-> input_picture->data[plane_index];
  5145. - //FIXME zero_dst is const but add_yblock changes dst if add is 0 (this is never the case for dst=zero_dst
  5146. - // const has only been removed from zero_dst to suppress a warning
  5147. - static IDWTELEM zero_dst[4096]; //FIXME
  5148. - const int b_stride = s->b_width << s->block_max_depth;
  5149. - const int w= p->width;
  5150. - const int h= p->height;
  5151. - int distortion= 0;
  5152. - int rate= 0;
  5153. - const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
  5154. -
  5155. - av_assert2(s->chroma_h_shift == s->chroma_v_shift); //obmc and square assumtions below
  5156. -
  5157. - for(i=0; i<9; i++){
  5158. - int mb_x2= mb_x + (i%3) - 1;
  5159. - int mb_y2= mb_y + (i/3) - 1;
  5160. - int x= block_w*mb_x2 + block_w/2;
  5161. - int y= block_h*mb_y2 + block_h/2;
  5162. -
  5163. - add_yblock(s, 0, NULL, zero_dst, dst, obmc,
  5164. - x, y, block_w, block_h, w, h, /*dst_stride*/0, ref_stride, obmc_stride, mb_x2, mb_y2, 1, 1, plane_index);
  5165. -
  5166. - //FIXME find a cleaner/simpler way to skip the outside stuff
  5167. - for(y2= y; y2<0; y2++)
  5168. - memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
  5169. - for(y2= h; y2<y+block_h; y2++)
  5170. - memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
  5171. - if(x<0){
  5172. - for(y2= y; y2<y+block_h; y2++)
  5173. - memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, -x);
  5174. - }
  5175. - if(x+block_w > w){
  5176. - for(y2= y; y2<y+block_h; y2++)
  5177. - memcpy(dst + w + y2*ref_stride, src + w + y2*ref_stride, x+block_w - w);
  5178. - }
  5179. -
  5180. - av_assert1(block_w== 8 || block_w==16);
  5181. - distortion += s->mecc.me_cmp[block_w==8](&s->m, src + x + y*ref_stride, dst + x + y*ref_stride, ref_stride, block_h);
  5182. - }
  5183. -
  5184. - if(plane_index==0){
  5185. - BlockNode *b= &s->block[mb_x+mb_y*b_stride];
  5186. - int merged= same_block(b,b+1) && same_block(b,b+b_stride) && same_block(b,b+b_stride+1);
  5187. -
  5188. -/* ..RRRr
  5189. - * .RXXx.
  5190. - * .RXXx.
  5191. - * rxxx.
  5192. - */
  5193. - if(merged)
  5194. - rate = get_block_bits(s, mb_x, mb_y, 2);
  5195. - for(i=merged?4:0; i<9; i++){
  5196. - static const int dxy[9][2] = {{0,0},{1,0},{0,1},{1,1},{2,0},{2,1},{-1,2},{0,2},{1,2}};
  5197. - rate += get_block_bits(s, mb_x + dxy[i][0], mb_y + dxy[i][1], 1);
  5198. - }
  5199. - }
  5200. - return distortion + rate*penalty_factor;
  5201. -}
  5202. -
  5203. static int encode_subband_c0run(SnowContext *s, SubBand *b, const IDWTELEM *src, const IDWTELEM *parent, int stride, int orientation){
  5204. const int w= b->width;
  5205. const int h= b->height;
  5206. @@ -899,347 +380,6 @@ static int encode_subband(SnowContext *s, SubBand *b, const IDWTELEM *src, const
  5207. // encode_subband_dzr(s, b, src, parent, stride, orientation);
  5208. }
  5209.  
  5210. -static av_always_inline int check_block(SnowContext *s, int mb_x, int mb_y, int p[3], int intra, uint8_t (*obmc_edged)[MB_SIZE * 2], int *best_rd){
  5211. - const int b_stride= s->b_width << s->block_max_depth;
  5212. - BlockNode *block= &s->block[mb_x + mb_y * b_stride];
  5213. - BlockNode backup= *block;
  5214. - unsigned value;
  5215. - int rd, index;
  5216. -
  5217. - av_assert2(mb_x>=0 && mb_y>=0);
  5218. - av_assert2(mb_x<b_stride);
  5219. -
  5220. - if(intra){
  5221. - block->color[0] = p[0];
  5222. - block->color[1] = p[1];
  5223. - block->color[2] = p[2];
  5224. - block->type |= BLOCK_INTRA;
  5225. - }else{
  5226. - index= (p[0] + 31*p[1]) & (ME_CACHE_SIZE-1);
  5227. - value= s->me_cache_generation + (p[0]>>10) + (p[1]<<6) + (block->ref<<12);
  5228. - if(s->me_cache[index] == value)
  5229. - return 0;
  5230. - s->me_cache[index]= value;
  5231. -
  5232. - block->mx= p[0];
  5233. - block->my= p[1];
  5234. - block->type &= ~BLOCK_INTRA;
  5235. - }
  5236. -
  5237. - rd= get_block_rd(s, mb_x, mb_y, 0, obmc_edged) + s->intra_penalty * !!intra;
  5238. -
  5239. -//FIXME chroma
  5240. - if(rd < *best_rd){
  5241. - *best_rd= rd;
  5242. - return 1;
  5243. - }else{
  5244. - *block= backup;
  5245. - return 0;
  5246. - }
  5247. -}
  5248. -
  5249. -/* special case for int[2] args we discard afterwards,
  5250. - * fixes compilation problem with gcc 2.95 */
  5251. -static av_always_inline int check_block_inter(SnowContext *s, int mb_x, int mb_y, int p0, int p1, uint8_t (*obmc_edged)[MB_SIZE * 2], int *best_rd){
  5252. - int p[2] = {p0, p1};
  5253. - return check_block(s, mb_x, mb_y, p, 0, obmc_edged, best_rd);
  5254. -}
  5255. -
  5256. -static av_always_inline int check_4block_inter(SnowContext *s, int mb_x, int mb_y, int p0, int p1, int ref, int *best_rd){
  5257. - const int b_stride= s->b_width << s->block_max_depth;
  5258. - BlockNode *block= &s->block[mb_x + mb_y * b_stride];
  5259. - BlockNode backup[4];
  5260. - unsigned value;
  5261. - int rd, index;
  5262. -
  5263. - /* We don't initialize backup[] during variable declaration, because
  5264. - * that fails to compile on MSVC: "cannot convert from 'BlockNode' to
  5265. - * 'int16_t'". */
  5266. - backup[0] = block[0];
  5267. - backup[1] = block[1];
  5268. - backup[2] = block[b_stride];
  5269. - backup[3] = block[b_stride + 1];
  5270. -
  5271. - av_assert2(mb_x>=0 && mb_y>=0);
  5272. - av_assert2(mb_x<b_stride);
  5273. - av_assert2(((mb_x|mb_y)&1) == 0);
  5274. -
  5275. - index= (p0 + 31*p1) & (ME_CACHE_SIZE-1);
  5276. - value= s->me_cache_generation + (p0>>10) + (p1<<6) + (block->ref<<12);
  5277. - if(s->me_cache[index] == value)
  5278. - return 0;
  5279. - s->me_cache[index]= value;
  5280. -
  5281. - block->mx= p0;
  5282. - block->my= p1;
  5283. - block->ref= ref;
  5284. - block->type &= ~BLOCK_INTRA;
  5285. - block[1]= block[b_stride]= block[b_stride+1]= *block;
  5286. -
  5287. - rd= get_4block_rd(s, mb_x, mb_y, 0);
  5288. -
  5289. -//FIXME chroma
  5290. - if(rd < *best_rd){
  5291. - *best_rd= rd;
  5292. - return 1;
  5293. - }else{
  5294. - block[0]= backup[0];
  5295. - block[1]= backup[1];
  5296. - block[b_stride]= backup[2];
  5297. - block[b_stride+1]= backup[3];
  5298. - return 0;
  5299. - }
  5300. -}
  5301. -
  5302. -static void iterative_me(SnowContext *s){
  5303. - int pass, mb_x, mb_y;
  5304. - const int b_width = s->b_width << s->block_max_depth;
  5305. - const int b_height= s->b_height << s->block_max_depth;
  5306. - const int b_stride= b_width;
  5307. - int color[3];
  5308. -
  5309. - {
  5310. - RangeCoder r = s->c;
  5311. - uint8_t state[sizeof(s->block_state)];
  5312. - memcpy(state, s->block_state, sizeof(s->block_state));
  5313. - for(mb_y= 0; mb_y<s->b_height; mb_y++)
  5314. - for(mb_x= 0; mb_x<s->b_width; mb_x++)
  5315. - encode_q_branch(s, 0, mb_x, mb_y);
  5316. - s->c = r;
  5317. - memcpy(s->block_state, state, sizeof(s->block_state));
  5318. - }
  5319. -
  5320. - for(pass=0; pass<25; pass++){
  5321. - int change= 0;
  5322. -
  5323. - for(mb_y= 0; mb_y<b_height; mb_y++){
  5324. - for(mb_x= 0; mb_x<b_width; mb_x++){
  5325. - int dia_change, i, j, ref;
  5326. - int best_rd= INT_MAX, ref_rd;
  5327. - BlockNode backup, ref_b;
  5328. - const int index= mb_x + mb_y * b_stride;
  5329. - BlockNode *block= &s->block[index];
  5330. - BlockNode *tb = mb_y ? &s->block[index-b_stride ] : NULL;
  5331. - BlockNode *lb = mb_x ? &s->block[index -1] : NULL;
  5332. - BlockNode *rb = mb_x+1<b_width ? &s->block[index +1] : NULL;
  5333. - BlockNode *bb = mb_y+1<b_height ? &s->block[index+b_stride ] : NULL;
  5334. - BlockNode *tlb= mb_x && mb_y ? &s->block[index-b_stride-1] : NULL;
  5335. - BlockNode *trb= mb_x+1<b_width && mb_y ? &s->block[index-b_stride+1] : NULL;
  5336. - BlockNode *blb= mb_x && mb_y+1<b_height ? &s->block[index+b_stride-1] : NULL;
  5337. - BlockNode *brb= mb_x+1<b_width && mb_y+1<b_height ? &s->block[index+b_stride+1] : NULL;
  5338. - const int b_w= (MB_SIZE >> s->block_max_depth);
  5339. - uint8_t obmc_edged[MB_SIZE * 2][MB_SIZE * 2];
  5340. -
  5341. - if(pass && (block->type & BLOCK_OPT))
  5342. - continue;
  5343. - block->type |= BLOCK_OPT;
  5344. -
  5345. - backup= *block;
  5346. -
  5347. - if(!s->me_cache_generation)
  5348. - memset(s->me_cache, 0, sizeof(s->me_cache));
  5349. - s->me_cache_generation += 1<<22;
  5350. -
  5351. - //FIXME precalculate
  5352. - {
  5353. - int x, y;
  5354. - for (y = 0; y < b_w * 2; y++)
  5355. - memcpy(obmc_edged[y], ff_obmc_tab[s->block_max_depth] + y * b_w * 2, b_w * 2);
  5356. - if(mb_x==0)
  5357. - for(y=0; y<b_w*2; y++)
  5358. - memset(obmc_edged[y], obmc_edged[y][0] + obmc_edged[y][b_w-1], b_w);
  5359. - if(mb_x==b_stride-1)
  5360. - for(y=0; y<b_w*2; y++)
  5361. - memset(obmc_edged[y]+b_w, obmc_edged[y][b_w] + obmc_edged[y][b_w*2-1], b_w);
  5362. - if(mb_y==0){
  5363. - for(x=0; x<b_w*2; x++)
  5364. - obmc_edged[0][x] += obmc_edged[b_w-1][x];
  5365. - for(y=1; y<b_w; y++)
  5366. - memcpy(obmc_edged[y], obmc_edged[0], b_w*2);
  5367. - }
  5368. - if(mb_y==b_height-1){
  5369. - for(x=0; x<b_w*2; x++)
  5370. - obmc_edged[b_w*2-1][x] += obmc_edged[b_w][x];
  5371. - for(y=b_w; y<b_w*2-1; y++)
  5372. - memcpy(obmc_edged[y], obmc_edged[b_w*2-1], b_w*2);
  5373. - }
  5374. - }
  5375. -
  5376. - //skip stuff outside the picture
  5377. - if(mb_x==0 || mb_y==0 || mb_x==b_width-1 || mb_y==b_height-1){
  5378. - uint8_t *src= s-> input_picture->data[0];
  5379. - uint8_t *dst= s->current_picture->data[0];
  5380. - const int stride= s->current_picture->linesize[0];
  5381. - const int block_w= MB_SIZE >> s->block_max_depth;
  5382. - const int block_h= MB_SIZE >> s->block_max_depth;
  5383. - const int sx= block_w*mb_x - block_w/2;
  5384. - const int sy= block_h*mb_y - block_h/2;
  5385. - const int w= s->plane[0].width;
  5386. - const int h= s->plane[0].height;
  5387. - int y;
  5388. -
  5389. - for(y=sy; y<0; y++)
  5390. - memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
  5391. - for(y=h; y<sy+block_h*2; y++)
  5392. - memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
  5393. - if(sx<0){
  5394. - for(y=sy; y<sy+block_h*2; y++)
  5395. - memcpy(dst + sx + y*stride, src + sx + y*stride, -sx);
  5396. - }
  5397. - if(sx+block_w*2 > w){
  5398. - for(y=sy; y<sy+block_h*2; y++)
  5399. - memcpy(dst + w + y*stride, src + w + y*stride, sx+block_w*2 - w);
  5400. - }
  5401. - }
  5402. -
  5403. - // intra(black) = neighbors' contribution to the current block
  5404. - for(i=0; i < s->nb_planes; i++)
  5405. - color[i]= get_dc(s, mb_x, mb_y, i);
  5406. -
  5407. - // get previous score (cannot be cached due to OBMC)
  5408. - if(pass > 0 && (block->type&BLOCK_INTRA)){
  5409. - int color0[3]= {block->color[0], block->color[1], block->color[2]};
  5410. - check_block(s, mb_x, mb_y, color0, 1, obmc_edged, &best_rd);
  5411. - }else
  5412. - check_block_inter(s, mb_x, mb_y, block->mx, block->my, obmc_edged, &best_rd);
  5413. -
  5414. - ref_b= *block;
  5415. - ref_rd= best_rd;
  5416. - for(ref=0; ref < s->ref_frames; ref++){
  5417. - int16_t (*mvr)[2]= &s->ref_mvs[ref][index];
  5418. - if(s->ref_scores[ref][index] > s->ref_scores[ref_b.ref][index]*3/2) //FIXME tune threshold
  5419. - continue;
  5420. - block->ref= ref;
  5421. - best_rd= INT_MAX;
  5422. -
  5423. - check_block_inter(s, mb_x, mb_y, mvr[0][0], mvr[0][1], obmc_edged, &best_rd);
  5424. - check_block_inter(s, mb_x, mb_y, 0, 0, obmc_edged, &best_rd);
  5425. - if(tb)
  5426. - check_block_inter(s, mb_x, mb_y, mvr[-b_stride][0], mvr[-b_stride][1], obmc_edged, &best_rd);
  5427. - if(lb)
  5428. - check_block_inter(s, mb_x, mb_y, mvr[-1][0], mvr[-1][1], obmc_edged, &best_rd);
  5429. - if(rb)
  5430. - check_block_inter(s, mb_x, mb_y, mvr[1][0], mvr[1][1], obmc_edged, &best_rd);
  5431. - if(bb)
  5432. - check_block_inter(s, mb_x, mb_y, mvr[b_stride][0], mvr[b_stride][1], obmc_edged, &best_rd);
  5433. -
  5434. - /* fullpel ME */
  5435. - //FIXME avoid subpel interpolation / round to nearest integer
  5436. - do{
  5437. - int newx = block->mx;
  5438. - int newy = block->my;
  5439. - int dia_size = s->iterative_dia_size ? s->iterative_dia_size : FFMAX(s->avctx->dia_size, 1);
  5440. - dia_change=0;
  5441. - for(i=0; i < dia_size; i++){
  5442. - for(j=0; j<i; j++){
  5443. - dia_change |= check_block_inter(s, mb_x, mb_y, newx+4*(i-j), newy+(4*j), obmc_edged, &best_rd);
  5444. - dia_change |= check_block_inter(s, mb_x, mb_y, newx-4*(i-j), newy-(4*j), obmc_edged, &best_rd);
  5445. - dia_change |= check_block_inter(s, mb_x, mb_y, newx-(4*j), newy+4*(i-j), obmc_edged, &best_rd);
  5446. - dia_change |= check_block_inter(s, mb_x, mb_y, newx+(4*j), newy-4*(i-j), obmc_edged, &best_rd);
  5447. - }
  5448. - }
  5449. - }while(dia_change);
  5450. - /* subpel ME */
  5451. - do{
  5452. - static const int square[8][2]= {{+1, 0},{-1, 0},{ 0,+1},{ 0,-1},{+1,+1},{-1,-1},{+1,-1},{-1,+1},};
  5453. - dia_change=0;
  5454. - for(i=0; i<8; i++)
  5455. - dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+square[i][0], block->my+square[i][1], obmc_edged, &best_rd);
  5456. - }while(dia_change);
  5457. - //FIXME or try the standard 2 pass qpel or similar
  5458. -
  5459. - mvr[0][0]= block->mx;
  5460. - mvr[0][1]= block->my;
  5461. - if(ref_rd > best_rd){
  5462. - ref_rd= best_rd;
  5463. - ref_b= *block;
  5464. - }
  5465. - }
  5466. - best_rd= ref_rd;
  5467. - *block= ref_b;
  5468. - check_block(s, mb_x, mb_y, color, 1, obmc_edged, &best_rd);
  5469. - //FIXME RD style color selection
  5470. - if(!same_block(block, &backup)){
  5471. - if(tb ) tb ->type &= ~BLOCK_OPT;
  5472. - if(lb ) lb ->type &= ~BLOCK_OPT;
  5473. - if(rb ) rb ->type &= ~BLOCK_OPT;
  5474. - if(bb ) bb ->type &= ~BLOCK_OPT;
  5475. - if(tlb) tlb->type &= ~BLOCK_OPT;
  5476. - if(trb) trb->type &= ~BLOCK_OPT;
  5477. - if(blb) blb->type &= ~BLOCK_OPT;
  5478. - if(brb) brb->type &= ~BLOCK_OPT;
  5479. - change ++;
  5480. - }
  5481. - }
  5482. - }
  5483. - av_log(s->avctx, AV_LOG_DEBUG, "pass:%d changed:%d\n", pass, change);
  5484. - if(!change)
  5485. - break;
  5486. - }
  5487. -
  5488. - if(s->block_max_depth == 1){
  5489. - int change= 0;
  5490. - for(mb_y= 0; mb_y<b_height; mb_y+=2){
  5491. - for(mb_x= 0; mb_x<b_width; mb_x+=2){
  5492. - int i;
  5493. - int best_rd, init_rd;
  5494. - const int index= mb_x + mb_y * b_stride;
  5495. - BlockNode *b[4];
  5496. -
  5497. - b[0]= &s->block[index];
  5498. - b[1]= b[0]+1;
  5499. - b[2]= b[0]+b_stride;
  5500. - b[3]= b[2]+1;
  5501. - if(same_block(b[0], b[1]) &&
  5502. - same_block(b[0], b[2]) &&
  5503. - same_block(b[0], b[3]))
  5504. - continue;
  5505. -
  5506. - if(!s->me_cache_generation)
  5507. - memset(s->me_cache, 0, sizeof(s->me_cache));
  5508. - s->me_cache_generation += 1<<22;
  5509. -
  5510. - init_rd= best_rd= get_4block_rd(s, mb_x, mb_y, 0);
  5511. -
  5512. - //FIXME more multiref search?
  5513. - check_4block_inter(s, mb_x, mb_y,
  5514. - (b[0]->mx + b[1]->mx + b[2]->mx + b[3]->mx + 2) >> 2,
  5515. - (b[0]->my + b[1]->my + b[2]->my + b[3]->my + 2) >> 2, 0, &best_rd);
  5516. -
  5517. - for(i=0; i<4; i++)
  5518. - if(!(b[i]->type&BLOCK_INTRA))
  5519. - check_4block_inter(s, mb_x, mb_y, b[i]->mx, b[i]->my, b[i]->ref, &best_rd);
  5520. -
  5521. - if(init_rd != best_rd)
  5522. - change++;
  5523. - }
  5524. - }
  5525. - av_log(s->avctx, AV_LOG_ERROR, "pass:4mv changed:%d\n", change*4);
  5526. - }
  5527. -}
  5528. -
  5529. -static void encode_blocks(SnowContext *s, int search){
  5530. - int x, y;
  5531. - int w= s->b_width;
  5532. - int h= s->b_height;
  5533. -
  5534. - if(s->motion_est == FF_ME_ITER && !s->keyframe && search)
  5535. - iterative_me(s);
  5536. -
  5537. - for(y=0; y<h; y++){
  5538. - if(s->c.bytestream_end - s->c.bytestream < w*MB_SIZE*MB_SIZE*3){ //FIXME nicer limit
  5539. - av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  5540. - return;
  5541. - }
  5542. - for(x=0; x<w; x++){
  5543. - if(s->motion_est == FF_ME_ITER || !search)
  5544. - encode_q_branch2(s, 0, x, y);
  5545. - else
  5546. - encode_q_branch (s, 0, x, y);
  5547. - }
  5548. - }
  5549. -}
  5550. -
  5551. static void quantize(SnowContext *s, SubBand *b, IDWTELEM *dst, DWTELEM *src, int stride, int bias){
  5552. const int w= b->width;
  5553. const int h= b->height;
  5554. @@ -1396,10 +536,10 @@ static void encode_header(SnowContext *s){
  5555. s->last_spatial_decomposition_type=
  5556. s->last_qlog=
  5557. s->last_qbias=
  5558. - s->last_mv_scale=
  5559. - s->last_block_max_depth= 0;
  5560. + s->obmc.last_mv_scale=
  5561. + s->obmc.last_block_max_depth= 0;
  5562. for(plane_index=0; plane_index<2; plane_index++){
  5563. - Plane *p= &s->plane[plane_index];
  5564. + PlaneObmc *p= &s->obmc.plane[plane_index];
  5565. p->last_htaps=0;
  5566. p->last_diag_mc=0;
  5567. memset(p->last_hcoeff, 0, sizeof(p->last_hcoeff));
  5568. @@ -1418,7 +558,7 @@ static void encode_header(SnowContext *s){
  5569. }
  5570. put_rac(&s->c, s->header_state, s->spatial_scalability);
  5571. // put_rac(&s->c, s->header_state, s->rate_scalability);
  5572. - put_symbol(&s->c, s->header_state, s->max_ref_frames-1, 0);
  5573. + put_symbol(&s->c, s->header_state, s->obmc.max_ref_frames-1, 0);
  5574.  
  5575. encode_qlogs(s);
  5576. }
  5577. @@ -1426,7 +566,7 @@ static void encode_header(SnowContext *s){
  5578. if(!s->keyframe){
  5579. int update_mc=0;
  5580. for(plane_index=0; plane_index<FFMIN(s->nb_planes, 2); plane_index++){
  5581. - Plane *p= &s->plane[plane_index];
  5582. + PlaneObmc *p= &s->obmc.plane[plane_index];
  5583. update_mc |= p->last_htaps != p->htaps;
  5584. update_mc |= p->last_diag_mc != p->diag_mc;
  5585. update_mc |= !!memcmp(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
  5586. @@ -1434,7 +574,7 @@ static void encode_header(SnowContext *s){
  5587. put_rac(&s->c, s->header_state, update_mc);
  5588. if(update_mc){
  5589. for(plane_index=0; plane_index<FFMIN(s->nb_planes, 2); plane_index++){
  5590. - Plane *p= &s->plane[plane_index];
  5591. + PlaneObmc *p= &s->obmc.plane[plane_index];
  5592. put_rac(&s->c, s->header_state, p->diag_mc);
  5593. put_symbol(&s->c, s->header_state, p->htaps/2-1, 0);
  5594. for(i= p->htaps/2; i; i--)
  5595. @@ -1450,10 +590,10 @@ static void encode_header(SnowContext *s){
  5596. }
  5597.  
  5598. put_symbol(&s->c, s->header_state, s->spatial_decomposition_type - s->last_spatial_decomposition_type, 1);
  5599. - put_symbol(&s->c, s->header_state, s->qlog - s->last_qlog , 1);
  5600. - put_symbol(&s->c, s->header_state, s->mv_scale - s->last_mv_scale, 1);
  5601. - put_symbol(&s->c, s->header_state, s->qbias - s->last_qbias , 1);
  5602. - put_symbol(&s->c, s->header_state, s->block_max_depth - s->last_block_max_depth, 1);
  5603. + put_symbol(&s->c, s->header_state, s->qlog - s->last_qlog , 1);
  5604. + put_symbol(&s->c, s->header_state, s->obmc.mv_scale - s->obmc.last_mv_scale, 1);
  5605. + put_symbol(&s->c, s->header_state, s->qbias - s->last_qbias , 1);
  5606. + put_symbol(&s->c, s->header_state, s->obmc.block_max_depth - s->obmc.last_block_max_depth, 1);
  5607.  
  5608. }
  5609.  
  5610. @@ -1462,7 +602,7 @@ static void update_last_header_values(SnowContext *s){
  5611.  
  5612. if(!s->keyframe){
  5613. for(plane_index=0; plane_index<2; plane_index++){
  5614. - Plane *p= &s->plane[plane_index];
  5615. + PlaneObmc *p= &s->obmc.plane[plane_index];
  5616. p->last_diag_mc= p->diag_mc;
  5617. p->last_htaps = p->htaps;
  5618. memcpy(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
  5619. @@ -1472,8 +612,8 @@ static void update_last_header_values(SnowContext *s){
  5620. s->last_spatial_decomposition_type = s->spatial_decomposition_type;
  5621. s->last_qlog = s->qlog;
  5622. s->last_qbias = s->qbias;
  5623. - s->last_mv_scale = s->mv_scale;
  5624. - s->last_block_max_depth = s->block_max_depth;
  5625. + s->obmc.last_mv_scale = s->obmc.mv_scale;
  5626. + s->obmc.last_block_max_depth = s->obmc.block_max_depth;
  5627. s->last_spatial_decomposition_count = s->spatial_decomposition_count;
  5628. }
  5629.  
  5630. @@ -1518,17 +658,17 @@ static int ratecontrol_1pass(SnowContext *s, AVFrame *pict)
  5631. coef_sum = (uint64_t)coef_sum * coef_sum >> 16;
  5632.  
  5633. if(pict->pict_type == AV_PICTURE_TYPE_I){
  5634. - s->m.current_picture.mb_var_sum= coef_sum;
  5635. - s->m.current_picture.mc_mb_var_sum= 0;
  5636. + s->obmc.m.current_picture.mb_var_sum= coef_sum;
  5637. + s->obmc.m.current_picture.mc_mb_var_sum= 0;
  5638. }else{
  5639. - s->m.current_picture.mc_mb_var_sum= coef_sum;
  5640. - s->m.current_picture.mb_var_sum= 0;
  5641. + s->obmc.m.current_picture.mc_mb_var_sum= coef_sum;
  5642. + s->obmc.m.current_picture.mb_var_sum= 0;
  5643. }
  5644.  
  5645. - pict->quality= ff_rate_estimate_qscale(&s->m, 1);
  5646. + pict->quality= ff_rate_estimate_qscale(&s->obmc.m, 1);
  5647. if (pict->quality < 0)
  5648. return INT_MIN;
  5649. - s->lambda= pict->quality * 3/2;
  5650. + s->obmc.lambda= pict->quality * 3/2;
  5651. delta_qlog= qscale2qlog(pict->quality) - s->qlog;
  5652. s->qlog+= delta_qlog;
  5653. return delta_qlog;
  5654. @@ -1545,12 +685,12 @@ static void calculate_visual_weight(SnowContext *s, Plane *p){
  5655. IDWTELEM *ibuf= b->ibuf;
  5656. int64_t error=0;
  5657.  
  5658. - memset(s->spatial_idwt_buffer, 0, sizeof(*s->spatial_idwt_buffer)*width*height);
  5659. + memset(s->obmc.spatial_idwt_buffer, 0, sizeof(*s->obmc.spatial_idwt_buffer)*width*height);
  5660. ibuf[b->width/2 + b->height/2*b->stride]= 256*16;
  5661. - ff_spatial_idwt(s->spatial_idwt_buffer, s->temp_idwt_buffer, width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  5662. + ff_spatial_idwt(s->obmc.spatial_idwt_buffer, s->temp_idwt_buffer, width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  5663. for(y=0; y<height; y++){
  5664. for(x=0; x<width; x++){
  5665. - int64_t d= s->spatial_idwt_buffer[x + y*width]*16;
  5666. + int64_t d= s->obmc.spatial_idwt_buffer[x + y*width]*16;
  5667. error += d*d;
  5668. }
  5669. }
  5670. @@ -1572,7 +712,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  5671. uint8_t rc_header_bak[sizeof(s->header_state)];
  5672. uint8_t rc_block_bak[sizeof(s->block_state)];
  5673.  
  5674. - if ((ret = ff_alloc_packet2(avctx, pkt, s->b_width*s->b_height*MB_SIZE*MB_SIZE*3 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
  5675. + if ((ret = ff_alloc_packet2(avctx, pkt, s->obmc.b_width*s->obmc.b_height*MB_SIZE*MB_SIZE*3 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
  5676. return ret;
  5677.  
  5678. ff_init_range_encoder(c, pkt->data, pkt->size);
  5679. @@ -1582,124 +722,55 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  5680. int hshift= i ? s->chroma_h_shift : 0;
  5681. int vshift= i ? s->chroma_v_shift : 0;
  5682. for(y=0; y<AV_CEIL_RSHIFT(height, vshift); y++)
  5683. - memcpy(&s->input_picture->data[i][y * s->input_picture->linesize[i]],
  5684. + memcpy(&s->obmc.input_picture->data[i][y * s->obmc.input_picture->linesize[i]],
  5685. &pict->data[i][y * pict->linesize[i]],
  5686. AV_CEIL_RSHIFT(width, hshift));
  5687. - s->mpvencdsp.draw_edges(s->input_picture->data[i], s->input_picture->linesize[i],
  5688. + s->obmc.mpvencdsp.draw_edges(s->obmc.input_picture->data[i], s->obmc.input_picture->linesize[i],
  5689. AV_CEIL_RSHIFT(width, hshift), AV_CEIL_RSHIFT(height, vshift),
  5690. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
  5691. EDGE_TOP | EDGE_BOTTOM);
  5692.  
  5693. }
  5694. emms_c();
  5695. - pic = s->input_picture;
  5696. + pic = s->obmc.input_picture;
  5697. pic->pict_type = pict->pict_type;
  5698. pic->quality = pict->quality;
  5699.  
  5700. - s->m.picture_number= avctx->frame_number;
  5701. + s->obmc.m.picture_number= avctx->frame_number;
  5702. if(avctx->flags&AV_CODEC_FLAG_PASS2){
  5703. - s->m.pict_type = pic->pict_type = s->m.rc_context.entry[avctx->frame_number].new_pict_type;
  5704. + s->obmc.m.pict_type = pic->pict_type = s->obmc.m.rc_context.entry[avctx->frame_number].new_pict_type;
  5705. s->keyframe = pic->pict_type == AV_PICTURE_TYPE_I;
  5706. + s->obmc.keyframe = s->keyframe;
  5707. if(!(avctx->flags&AV_CODEC_FLAG_QSCALE)) {
  5708. - pic->quality = ff_rate_estimate_qscale(&s->m, 0);
  5709. + pic->quality = ff_rate_estimate_qscale(&s->obmc.m, 0);
  5710. if (pic->quality < 0)
  5711. return -1;
  5712. }
  5713. }else{
  5714. s->keyframe= avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;
  5715. - s->m.pict_type = pic->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  5716. + s->obmc.m.pict_type = pic->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  5717. + s->obmc.keyframe = s->keyframe;
  5718. }
  5719.  
  5720. if(s->pass1_rc && avctx->frame_number == 0)
  5721. pic->quality = 2*FF_QP2LAMBDA;
  5722. if (pic->quality) {
  5723. s->qlog = qscale2qlog(pic->quality);
  5724. - s->lambda = pic->quality * 3/2;
  5725. + s->obmc.lambda = pic->quality * 3/2;
  5726. }
  5727. if (s->qlog < 0 || (!pic->quality && (avctx->flags & AV_CODEC_FLAG_QSCALE))) {
  5728. s->qlog= LOSSLESS_QLOG;
  5729. - s->lambda = 0;
  5730. + s->obmc.lambda = 0;
  5731. }//else keep previous frame's qlog until after motion estimation
  5732.  
  5733. - if (s->current_picture->data[0]
  5734. -#if FF_API_EMU_EDGE
  5735. - && !(s->avctx->flags&CODEC_FLAG_EMU_EDGE)
  5736. -#endif
  5737. - ) {
  5738. - int w = s->avctx->width;
  5739. - int h = s->avctx->height;
  5740. -
  5741. - s->mpvencdsp.draw_edges(s->current_picture->data[0],
  5742. - s->current_picture->linesize[0], w , h ,
  5743. - EDGE_WIDTH , EDGE_WIDTH , EDGE_TOP | EDGE_BOTTOM);
  5744. - if (s->current_picture->data[2]) {
  5745. - s->mpvencdsp.draw_edges(s->current_picture->data[1],
  5746. - s->current_picture->linesize[1], w>>s->chroma_h_shift, h>>s->chroma_v_shift,
  5747. - EDGE_WIDTH>>s->chroma_h_shift, EDGE_WIDTH>>s->chroma_v_shift, EDGE_TOP | EDGE_BOTTOM);
  5748. - s->mpvencdsp.draw_edges(s->current_picture->data[2],
  5749. - s->current_picture->linesize[2], w>>s->chroma_h_shift, h>>s->chroma_v_shift,
  5750. - EDGE_WIDTH>>s->chroma_h_shift, EDGE_WIDTH>>s->chroma_v_shift, EDGE_TOP | EDGE_BOTTOM);
  5751. - }
  5752. - }
  5753. -
  5754. - ff_snow_frame_start(s);
  5755. - av_frame_unref(avctx->coded_frame);
  5756. - ret = av_frame_ref(avctx->coded_frame, s->current_picture);
  5757. - if (ret < 0)
  5758. - return ret;
  5759. -
  5760. - s->m.current_picture_ptr= &s->m.current_picture;
  5761. - s->m.current_picture.f = s->current_picture;
  5762. - s->m.current_picture.f->pts = pict->pts;
  5763. - if(pic->pict_type == AV_PICTURE_TYPE_P){
  5764. - int block_width = (width +15)>>4;
  5765. - int block_height= (height+15)>>4;
  5766. - int stride= s->current_picture->linesize[0];
  5767. -
  5768. - av_assert0(s->current_picture->data[0]);
  5769. - av_assert0(s->last_picture[0]->data[0]);
  5770. -
  5771. - s->m.avctx= s->avctx;
  5772. - s->m. last_picture.f = s->last_picture[0];
  5773. - s->m. new_picture.f = s->input_picture;
  5774. - s->m. last_picture_ptr= &s->m. last_picture;
  5775. - s->m.linesize = stride;
  5776. - s->m.uvlinesize= s->current_picture->linesize[1];
  5777. - s->m.width = width;
  5778. - s->m.height= height;
  5779. - s->m.mb_width = block_width;
  5780. - s->m.mb_height= block_height;
  5781. - s->m.mb_stride= s->m.mb_width+1;
  5782. - s->m.b8_stride= 2*s->m.mb_width+1;
  5783. - s->m.f_code=1;
  5784. - s->m.pict_type = pic->pict_type;
  5785. -#if FF_API_MOTION_EST
  5786. - s->m.me_method= s->avctx->me_method;
  5787. -#endif
  5788. - s->m.motion_est= s->motion_est;
  5789. - s->m.me.scene_change_score=0;
  5790. - s->m.me.dia_size = avctx->dia_size;
  5791. - s->m.quarter_sample= (s->avctx->flags & AV_CODEC_FLAG_QPEL)!=0;
  5792. - s->m.out_format= FMT_H263;
  5793. - s->m.unrestricted_mv= 1;
  5794. -
  5795. - s->m.lambda = s->lambda;
  5796. - s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
  5797. - s->lambda2= s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
  5798. -
  5799. - s->m.mecc= s->mecc; //move
  5800. - s->m.qdsp= s->qdsp; //move
  5801. - s->m.hdsp = s->hdsp;
  5802. - ff_init_me(&s->m);
  5803. - s->hdsp = s->m.hdsp;
  5804. - s->mecc= s->m.mecc;
  5805. - }
  5806. -
  5807. if(s->pass1_rc){
  5808. memcpy(rc_header_bak, s->header_state, sizeof(s->header_state));
  5809. memcpy(rc_block_bak, s->block_state, sizeof(s->block_state));
  5810. }
  5811.  
  5812. + if ((ret = ff_obmc_pre_encode_frame(&s->obmc, avctx, pict)) < 0)
  5813. + return ret;
  5814. +
  5815. redo_frame:
  5816.  
  5817. s->spatial_decomposition_count= 5;
  5818. @@ -1713,7 +784,7 @@ redo_frame:
  5819. return AVERROR(EINVAL);
  5820. }
  5821.  
  5822. - s->m.pict_type = pic->pict_type;
  5823. + s->obmc.m.pict_type = pic->pict_type;
  5824. s->qbias = pic->pict_type == AV_PICTURE_TYPE_P ? 2 : 0;
  5825.  
  5826. ff_snow_common_init_after_header(avctx);
  5827. @@ -1725,9 +796,9 @@ redo_frame:
  5828. }
  5829.  
  5830. encode_header(s);
  5831. - s->m.misc_bits = 8*(s->c.bytestream - s->c.bytestream_start);
  5832. - encode_blocks(s, 1);
  5833. - s->m.mv_bits = 8*(s->c.bytestream - s->c.bytestream_start) - s->m.misc_bits;
  5834. + s->obmc.m.misc_bits = 8*(s->c.bytestream - s->c.bytestream_start);
  5835. + ff_obmc_encode_blocks(&s->obmc, 1);
  5836. + s->obmc.m.mv_bits = 8*(s->c.bytestream - s->c.bytestream_start) - s->obmc.m.misc_bits;
  5837.  
  5838. for(plane_index=0; plane_index < s->nb_planes; plane_index++){
  5839. Plane *p= &s->plane[plane_index];
  5840. @@ -1741,10 +812,10 @@ redo_frame:
  5841. if(pict->data[plane_index]) //FIXME gray hack
  5842. for(y=0; y<h; y++){
  5843. for(x=0; x<w; x++){
  5844. - s->spatial_idwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<FRAC_BITS;
  5845. + s->obmc.spatial_idwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<FRAC_BITS;
  5846. }
  5847. }
  5848. - predict_plane(s, s->spatial_idwt_buffer, plane_index, 0);
  5849. + predict_plane(&s->obmc, s->obmc.spatial_idwt_buffer, plane_index, 0);
  5850.  
  5851. #if FF_API_PRIVATE_OPT
  5852. FF_DISABLE_DEPRECATION_WARNINGS
  5853. @@ -1756,25 +827,26 @@ FF_ENABLE_DEPRECATION_WARNINGS
  5854. if( plane_index==0
  5855. && pic->pict_type == AV_PICTURE_TYPE_P
  5856. && !(avctx->flags&AV_CODEC_FLAG_PASS2)
  5857. - && s->m.me.scene_change_score > s->scenechange_threshold){
  5858. + && s->obmc.m.me.scene_change_score > s->scenechange_threshold){
  5859. ff_init_range_encoder(c, pkt->data, pkt->size);
  5860. ff_build_rac_states(c, (1LL<<32)/20, 256-8);
  5861. pic->pict_type= AV_PICTURE_TYPE_I;
  5862. s->keyframe=1;
  5863. - s->current_picture->key_frame=1;
  5864. + s->obmc.keyframe = 1;
  5865. + s->obmc.current_picture->key_frame=1;
  5866. goto redo_frame;
  5867. }
  5868.  
  5869. if(s->qlog == LOSSLESS_QLOG){
  5870. for(y=0; y<h; y++){
  5871. for(x=0; x<w; x++){
  5872. - s->spatial_dwt_buffer[y*w + x]= (s->spatial_idwt_buffer[y*w + x] + (1<<(FRAC_BITS-1))-1)>>FRAC_BITS;
  5873. + s->spatial_dwt_buffer[y*w + x]= (s->obmc.spatial_idwt_buffer[y*w + x] + (1<<(FRAC_BITS-1))-1)>>FRAC_BITS;
  5874. }
  5875. }
  5876. }else{
  5877. for(y=0; y<h; y++){
  5878. for(x=0; x<w; x++){
  5879. - s->spatial_dwt_buffer[y*w + x]=s->spatial_idwt_buffer[y*w + x]<<ENCODER_EXTRA_BITS;
  5880. + s->spatial_dwt_buffer[y*w + x]=s->obmc.spatial_idwt_buffer[y*w + x]<<ENCODER_EXTRA_BITS;
  5881. }
  5882. }
  5883. }
  5884. @@ -1791,7 +863,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
  5885. memcpy(s->header_state, rc_header_bak, sizeof(s->header_state));
  5886. memcpy(s->block_state, rc_block_bak, sizeof(s->block_state));
  5887. encode_header(s);
  5888. - encode_blocks(s, 0);
  5889. + ff_obmc_encode_blocks(&s->obmc, 0);
  5890. }
  5891. }
  5892.  
  5893. @@ -1818,27 +890,27 @@ FF_ENABLE_DEPRECATION_WARNINGS
  5894. }
  5895. }
  5896.  
  5897. - ff_spatial_idwt(s->spatial_idwt_buffer, s->temp_idwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  5898. + ff_spatial_idwt(s->obmc.spatial_idwt_buffer, s->temp_idwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  5899. if(s->qlog == LOSSLESS_QLOG){
  5900. for(y=0; y<h; y++){
  5901. for(x=0; x<w; x++){
  5902. - s->spatial_idwt_buffer[y*w + x]<<=FRAC_BITS;
  5903. + s->obmc.spatial_idwt_buffer[y*w + x]<<=FRAC_BITS;
  5904. }
  5905. }
  5906. }
  5907. - predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
  5908. + predict_plane(&s->obmc, s->obmc.spatial_idwt_buffer, plane_index, 1);
  5909. }else{
  5910. //ME/MC only
  5911. if(pic->pict_type == AV_PICTURE_TYPE_I){
  5912. for(y=0; y<h; y++){
  5913. for(x=0; x<w; x++){
  5914. - s->current_picture->data[plane_index][y*s->current_picture->linesize[plane_index] + x]=
  5915. + s->obmc.current_picture->data[plane_index][y*s->obmc.current_picture->linesize[plane_index] + x]=
  5916. pict->data[plane_index][y*pict->linesize[plane_index] + x];
  5917. }
  5918. }
  5919. }else{
  5920. - memset(s->spatial_idwt_buffer, 0, sizeof(IDWTELEM)*w*h);
  5921. - predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
  5922. + memset(s->obmc.spatial_idwt_buffer, 0, sizeof(IDWTELEM)*w*h);
  5923. + predict_plane(&s->obmc, s->obmc.spatial_idwt_buffer, plane_index, 1);
  5924. }
  5925. }
  5926. if(s->avctx->flags&AV_CODEC_FLAG_PSNR){
  5927. @@ -1847,7 +919,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
  5928. if(pict->data[plane_index]) //FIXME gray hack
  5929. for(y=0; y<h; y++){
  5930. for(x=0; x<w; x++){
  5931. - int d= s->current_picture->data[plane_index][y*s->current_picture->linesize[plane_index] + x] - pict->data[plane_index][y*pict->linesize[plane_index] + x];
  5932. + int d= s->obmc.current_picture->data[plane_index][y*s->obmc.current_picture->linesize[plane_index] + x] - pict->data[plane_index][y*pict->linesize[plane_index] + x];
  5933. error += d*d;
  5934. }
  5935. }
  5936. @@ -1859,43 +931,43 @@ FF_ENABLE_DEPRECATION_WARNINGS
  5937.  
  5938. update_last_header_values(s);
  5939.  
  5940. - ff_snow_release_buffer(avctx);
  5941. -
  5942. - s->current_picture->coded_picture_number = avctx->frame_number;
  5943. - s->current_picture->pict_type = pic->pict_type;
  5944. - s->current_picture->quality = pic->quality;
  5945. - s->m.frame_bits = 8*(s->c.bytestream - s->c.bytestream_start);
  5946. - s->m.p_tex_bits = s->m.frame_bits - s->m.misc_bits - s->m.mv_bits;
  5947. - s->m.current_picture.f->display_picture_number =
  5948. - s->m.current_picture.f->coded_picture_number = avctx->frame_number;
  5949. - s->m.current_picture.f->quality = pic->quality;
  5950. - s->m.total_bits += 8*(s->c.bytestream - s->c.bytestream_start);
  5951. + ff_obmc_release_buffer(&s->obmc);
  5952. +
  5953. + s->obmc.current_picture->coded_picture_number = avctx->frame_number;
  5954. + s->obmc.current_picture->pict_type = pic->pict_type;
  5955. + s->obmc.current_picture->quality = pic->quality;
  5956. + s->obmc.m.frame_bits = 8*(s->c.bytestream - s->c.bytestream_start);
  5957. + s->obmc.m.p_tex_bits = s->obmc.m.frame_bits - s->obmc.m.misc_bits - s->obmc.m.mv_bits;
  5958. + s->obmc.m.current_picture.f->display_picture_number =
  5959. + s->obmc.m.current_picture.f->coded_picture_number = avctx->frame_number;
  5960. + s->obmc.m.current_picture.f->quality = pic->quality;
  5961. + s->obmc.m.total_bits += 8*(s->c.bytestream - s->c.bytestream_start);
  5962. if(s->pass1_rc)
  5963. - if (ff_rate_estimate_qscale(&s->m, 0) < 0)
  5964. + if (ff_rate_estimate_qscale(&s->obmc.m, 0) < 0)
  5965. return -1;
  5966. if(avctx->flags&AV_CODEC_FLAG_PASS1)
  5967. - ff_write_pass1_stats(&s->m);
  5968. - s->m.last_pict_type = s->m.pict_type;
  5969. - avctx->frame_bits = s->m.frame_bits;
  5970. - avctx->mv_bits = s->m.mv_bits;
  5971. - avctx->misc_bits = s->m.misc_bits;
  5972. - avctx->p_tex_bits = s->m.p_tex_bits;
  5973. + ff_write_pass1_stats(&s->obmc.m);
  5974. + s->obmc.m.last_pict_type = s->obmc.m.pict_type;
  5975. + avctx->frame_bits = s->obmc.m.frame_bits;
  5976. + avctx->mv_bits = s->obmc.m.mv_bits;
  5977. + avctx->misc_bits = s->obmc.m.misc_bits;
  5978. + avctx->p_tex_bits = s->obmc.m.p_tex_bits;
  5979.  
  5980. emms_c();
  5981.  
  5982. - ff_side_data_set_encoder_stats(pkt, s->current_picture->quality,
  5983. + ff_side_data_set_encoder_stats(pkt, s->obmc.current_picture->quality,
  5984. s->encoding_error,
  5985. (s->avctx->flags&AV_CODEC_FLAG_PSNR) ? 4 : 0,
  5986. - s->current_picture->pict_type);
  5987. + s->obmc.current_picture->pict_type);
  5988.  
  5989. #if FF_API_ERROR_FRAME
  5990. FF_DISABLE_DEPRECATION_WARNINGS
  5991. - memcpy(s->current_picture->error, s->encoding_error, sizeof(s->encoding_error));
  5992. + memcpy(s->obmc.current_picture->error, s->encoding_error, sizeof(s->encoding_error));
  5993. FF_ENABLE_DEPRECATION_WARNINGS
  5994. #endif
  5995.  
  5996. pkt->size = ff_rac_terminate(c);
  5997. - if (s->current_picture->key_frame)
  5998. + if (s->obmc.current_picture->key_frame)
  5999. pkt->flags |= AV_PKT_FLAG_KEY;
  6000. *got_packet = 1;
  6001.  
  6002. @@ -1907,22 +979,23 @@ static av_cold int encode_end(AVCodecContext *avctx)
  6003. SnowContext *s = avctx->priv_data;
  6004.  
  6005. ff_snow_common_end(s);
  6006. - ff_rate_control_uninit(&s->m);
  6007. - av_frame_free(&s->input_picture);
  6008. + ff_rate_control_uninit(&s->obmc.m);
  6009. + av_frame_free(&s->obmc.input_picture);
  6010. av_freep(&avctx->stats_out);
  6011.  
  6012. return 0;
  6013. }
  6014.  
  6015. #define OFFSET(x) offsetof(SnowContext, x)
  6016. +#define OFFSET_OBMC(x) offsetof(SnowContext, obmc) + offsetof(OBMCContext, x)
  6017. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  6018. static const AVOption options[] = {
  6019. FF_MPV_COMMON_OPTS
  6020. { "iter", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_ME_ITER }, 0, 0, FF_MPV_OPT_FLAGS, "motion_est" },
  6021. { "memc_only", "Only do ME/MC (I frames -> ref, P frame -> ME+MC).", OFFSET(memc_only), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
  6022. { "no_bitstream", "Skip final bitstream writeout.", OFFSET(no_bitstream), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
  6023. - { "intra_penalty", "Penalty for intra blocks in block decission", OFFSET(intra_penalty), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
  6024. - { "iterative_dia_size", "Dia size for the iterative ME", OFFSET(iterative_dia_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
  6025. + { "intra_penalty", "Penalty for intra blocks in block decission", OFFSET_OBMC(intra_penalty), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
  6026. + { "iterative_dia_size", "Dia size for the iterative ME", OFFSET_OBMC(iterative_dia_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
  6027. { "sc_threshold", "Scene change threshold", OFFSET(scenechange_threshold), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, VE },
  6028. { "pred", "Spatial decomposition type", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 0 }, DWT_97, DWT_53, VE, "pred" },
  6029. { "dwt97", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
  6030. --
  6031. 2.7.4 (Apple Git-66)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement