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

500 lines
22KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... direct mb/block decoding
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file libavcodec/h264_direct.c
  23. * H.264 / AVC / MPEG4 part10 direct mb/block decoding.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "internal.h"
  27. #include "dsputil.h"
  28. #include "avcodec.h"
  29. #include "mpegvideo.h"
  30. #include "h264.h"
  31. #include "rectangle.h"
  32. //#undef NDEBUG
  33. #include <assert.h>
  34. static int get_scale_factor(H264Context * const h, int poc, int poc1, int i){
  35. int poc0 = h->ref_list[0][i].poc;
  36. int td = av_clip(poc1 - poc0, -128, 127);
  37. if(td == 0 || h->ref_list[0][i].long_ref){
  38. return 256;
  39. }else{
  40. int tb = av_clip(poc - poc0, -128, 127);
  41. int tx = (16384 + (FFABS(td) >> 1)) / td;
  42. return av_clip((tb*tx + 32) >> 6, -1024, 1023);
  43. }
  44. }
  45. void ff_h264_direct_dist_scale_factor(H264Context * const h){
  46. MpegEncContext * const s = &h->s;
  47. const int poc = h->s.current_picture_ptr->field_poc[ s->picture_structure == PICT_BOTTOM_FIELD ];
  48. const int poc1 = h->ref_list[1][0].poc;
  49. int i, field;
  50. for(field=0; field<2; field++){
  51. const int poc = h->s.current_picture_ptr->field_poc[field];
  52. const int poc1 = h->ref_list[1][0].field_poc[field];
  53. for(i=0; i < 2*h->ref_count[0]; i++)
  54. h->dist_scale_factor_field[field][i^field] = get_scale_factor(h, poc, poc1, i+16);
  55. }
  56. for(i=0; i<h->ref_count[0]; i++){
  57. h->dist_scale_factor[i] = get_scale_factor(h, poc, poc1, i);
  58. }
  59. }
  60. static void fill_colmap(H264Context *h, int map[2][16+32], int list, int field, int colfield, int mbafi){
  61. MpegEncContext * const s = &h->s;
  62. Picture * const ref1 = &h->ref_list[1][0];
  63. int j, old_ref, rfield;
  64. int start= mbafi ? 16 : 0;
  65. int end = mbafi ? 16+2*h->ref_count[0] : h->ref_count[0];
  66. int interl= mbafi || s->picture_structure != PICT_FRAME;
  67. /* bogus; fills in for missing frames */
  68. memset(map[list], 0, sizeof(map[list]));
  69. for(rfield=0; rfield<2; rfield++){
  70. for(old_ref=0; old_ref<ref1->ref_count[colfield][list]; old_ref++){
  71. int poc = ref1->ref_poc[colfield][list][old_ref];
  72. if (!interl)
  73. poc |= 3;
  74. else if( interl && (poc&3) == 3) //FIXME store all MBAFF references so this isnt needed
  75. poc= (poc&~3) + rfield + 1;
  76. for(j=start; j<end; j++){
  77. if(4*h->ref_list[0][j].frame_num + (h->ref_list[0][j].reference&3) == poc){
  78. int cur_ref= mbafi ? (j-16)^field : j;
  79. map[list][2*old_ref + (rfield^field) + 16] = cur_ref;
  80. if(rfield == field || !interl)
  81. map[list][old_ref] = cur_ref;
  82. break;
  83. }
  84. }
  85. }
  86. }
  87. }
  88. void ff_h264_direct_ref_list_init(H264Context * const h){
  89. MpegEncContext * const s = &h->s;
  90. Picture * const ref1 = &h->ref_list[1][0];
  91. Picture * const cur = s->current_picture_ptr;
  92. int list, j, field;
  93. int sidx= (s->picture_structure&1)^1;
  94. int ref1sidx= (ref1->reference&1)^1;
  95. for(list=0; list<2; list++){
  96. cur->ref_count[sidx][list] = h->ref_count[list];
  97. for(j=0; j<h->ref_count[list]; j++)
  98. cur->ref_poc[sidx][list][j] = 4*h->ref_list[list][j].frame_num + (h->ref_list[list][j].reference&3);
  99. }
  100. if(s->picture_structure == PICT_FRAME){
  101. memcpy(cur->ref_count[1], cur->ref_count[0], sizeof(cur->ref_count[0]));
  102. memcpy(cur->ref_poc [1], cur->ref_poc [0], sizeof(cur->ref_poc [0]));
  103. }
  104. cur->mbaff= FRAME_MBAFF;
  105. h->col_fieldoff= 0;
  106. if(s->picture_structure == PICT_FRAME){
  107. int cur_poc = s->current_picture_ptr->poc;
  108. int *col_poc = h->ref_list[1]->field_poc;
  109. h->col_parity= (FFABS(col_poc[0] - cur_poc) >= FFABS(col_poc[1] - cur_poc));
  110. ref1sidx=sidx= h->col_parity;
  111. }else if(!(s->picture_structure & h->ref_list[1][0].reference) && !h->ref_list[1][0].mbaff){ // FL -> FL & differ parity
  112. h->col_fieldoff= s->mb_stride*(2*(h->ref_list[1][0].reference) - 3);
  113. }
  114. if(cur->pict_type != FF_B_TYPE || h->direct_spatial_mv_pred)
  115. return;
  116. for(list=0; list<2; list++){
  117. fill_colmap(h, h->map_col_to_list0, list, sidx, ref1sidx, 0);
  118. if(FRAME_MBAFF)
  119. for(field=0; field<2; field++)
  120. fill_colmap(h, h->map_col_to_list0_field[field], list, field, field, 1);
  121. }
  122. }
  123. void ff_h264_pred_direct_motion(H264Context * const h, int *mb_type){
  124. MpegEncContext * const s = &h->s;
  125. int b8_stride = h->b8_stride;
  126. int b4_stride = h->b_stride;
  127. int mb_xy = h->mb_xy;
  128. int mb_type_col[2];
  129. const int16_t (*l1mv0)[2], (*l1mv1)[2];
  130. const int8_t *l1ref0, *l1ref1;
  131. const int is_b8x8 = IS_8X8(*mb_type);
  132. unsigned int sub_mb_type;
  133. int i8, i4;
  134. assert(h->ref_list[1][0].reference&3);
  135. #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
  136. if(IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])){ // AFL/AFR/FR/FL -> AFL/FL
  137. if(!IS_INTERLACED(*mb_type)){ // AFR/FR -> AFL/FL
  138. mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride;
  139. b8_stride = 0;
  140. }else{
  141. mb_xy += h->col_fieldoff; // non zero for FL -> FL & differ parity
  142. }
  143. goto single_col;
  144. }else{ // AFL/AFR/FR/FL -> AFR/FR
  145. if(IS_INTERLACED(*mb_type)){ // AFL /FL -> AFR/FR
  146. mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride;
  147. mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy];
  148. mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + s->mb_stride];
  149. b8_stride *= 3;
  150. b4_stride *= 6;
  151. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  152. if( (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
  153. && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
  154. && !is_b8x8){
  155. *mb_type |= MB_TYPE_16x8 |MB_TYPE_L0L1|MB_TYPE_DIRECT2; /* B_16x8 */
  156. }else{
  157. *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
  158. }
  159. }else{ // AFR/FR -> AFR/FR
  160. single_col:
  161. mb_type_col[0] =
  162. mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy];
  163. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  164. if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
  165. *mb_type |= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
  166. }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
  167. *mb_type |= MB_TYPE_L0L1|MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
  168. }else{
  169. if(!h->sps.direct_8x8_inference_flag){
  170. /* FIXME save sub mb types from previous frames (or derive from MVs)
  171. * so we know exactly what block size to use */
  172. sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
  173. }
  174. *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
  175. }
  176. }
  177. }
  178. l1mv0 = &h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]];
  179. l1mv1 = &h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]];
  180. l1ref0 = &h->ref_list[1][0].ref_index [0][h->mb2b8_xy[mb_xy]];
  181. l1ref1 = &h->ref_list[1][0].ref_index [1][h->mb2b8_xy[mb_xy]];
  182. if(!b8_stride){
  183. if(s->mb_y&1){
  184. l1ref0 += h->b8_stride;
  185. l1ref1 += h->b8_stride;
  186. l1mv0 += 2*b4_stride;
  187. l1mv1 += 2*b4_stride;
  188. }
  189. }
  190. if(h->direct_spatial_mv_pred){
  191. int ref[2];
  192. int mv[2][2];
  193. int list;
  194. /* ref = min(neighbors) */
  195. for(list=0; list<2; list++){
  196. int left_ref = h->ref_cache[list][scan8[0] - 1];
  197. int top_ref = h->ref_cache[list][scan8[0] - 8];
  198. int refc = h->ref_cache[list][scan8[0] - 8 + 4];
  199. const int16_t *C= h->mv_cache[list][ scan8[0] - 8 + 4];
  200. if(refc == PART_NOT_AVAILABLE){
  201. refc = h->ref_cache[list][scan8[0] - 8 - 1];
  202. C = h-> mv_cache[list][scan8[0] - 8 - 1];
  203. }
  204. ref[list] = FFMIN3((unsigned)left_ref, (unsigned)top_ref, (unsigned)refc);
  205. if(ref[list] >= 0){
  206. //this is just pred_motion() but with the cases removed that cannot happen for direct blocks
  207. const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
  208. const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
  209. int match_count= (left_ref==ref[list]) + (top_ref==ref[list]) + (refc==ref[list]);
  210. if(match_count > 1){ //most common
  211. mv[list][0]= mid_pred(A[0], B[0], C[0]);
  212. mv[list][1]= mid_pred(A[1], B[1], C[1]);
  213. }else {
  214. assert(match_count==1);
  215. if(left_ref==ref[list]){
  216. mv[list][0]= A[0];
  217. mv[list][1]= A[1];
  218. }else if(top_ref==ref[list]){
  219. mv[list][0]= B[0];
  220. mv[list][1]= B[1];
  221. }else{
  222. mv[list][0]= C[0];
  223. mv[list][1]= C[1];
  224. }
  225. }
  226. }else{
  227. int mask= ~(MB_TYPE_L0 << (2*list));
  228. mv[list][0] = mv[list][1] = 0;
  229. ref[list] = -1;
  230. if(!is_b8x8)
  231. *mb_type &= mask;
  232. sub_mb_type &= mask;
  233. }
  234. }
  235. if(ref[0] < 0 && ref[1] < 0){
  236. ref[0] = ref[1] = 0;
  237. if(!is_b8x8)
  238. *mb_type |= MB_TYPE_L0L1;
  239. sub_mb_type |= MB_TYPE_L0L1;
  240. }
  241. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
  242. int n=0;
  243. for(i8=0; i8<4; i8++){
  244. int x8 = i8&1;
  245. int y8 = i8>>1;
  246. int xy8 = x8+y8*b8_stride;
  247. int xy4 = 3*x8+y8*b4_stride;
  248. int a,b;
  249. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  250. continue;
  251. h->sub_mb_type[i8] = sub_mb_type;
  252. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  253. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  254. if(!IS_INTRA(mb_type_col[y8]) && !h->ref_list[1][0].long_ref
  255. && ( (l1ref0[xy8] == 0 && FFABS(l1mv0[xy4][0]) <= 1 && FFABS(l1mv0[xy4][1]) <= 1)
  256. || (l1ref0[xy8] < 0 && l1ref1[xy8] == 0 && FFABS(l1mv1[xy4][0]) <= 1 && FFABS(l1mv1[xy4][1]) <= 1))){
  257. a=b=0;
  258. if(ref[0] > 0)
  259. a= pack16to32(mv[0][0],mv[0][1]);
  260. if(ref[1] > 0)
  261. b= pack16to32(mv[1][0],mv[1][1]);
  262. n++;
  263. }else{
  264. a= pack16to32(mv[0][0],mv[0][1]);
  265. b= pack16to32(mv[1][0],mv[1][1]);
  266. }
  267. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, a, 4);
  268. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, b, 4);
  269. }
  270. if(!is_b8x8 && !(n&3))
  271. *mb_type= (*mb_type & ~(MB_TYPE_8x8|MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_P1L0|MB_TYPE_P1L1))|MB_TYPE_16x16|MB_TYPE_DIRECT2;
  272. }else if(IS_16X16(*mb_type)){
  273. int a,b;
  274. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  275. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  276. if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref
  277. && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
  278. || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
  279. && h->x264_build>33U))){
  280. a=b=0;
  281. if(ref[0] > 0)
  282. a= pack16to32(mv[0][0],mv[0][1]);
  283. if(ref[1] > 0)
  284. b= pack16to32(mv[1][0],mv[1][1]);
  285. }else{
  286. a= pack16to32(mv[0][0],mv[0][1]);
  287. b= pack16to32(mv[1][0],mv[1][1]);
  288. }
  289. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
  290. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
  291. }else{
  292. int n=0;
  293. for(i8=0; i8<4; i8++){
  294. const int x8 = i8&1;
  295. const int y8 = i8>>1;
  296. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  297. continue;
  298. h->sub_mb_type[i8] = sub_mb_type;
  299. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  300. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  301. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  302. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  303. /* col_zero_flag */
  304. if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref && ( l1ref0[x8 + y8*b8_stride] == 0
  305. || (l1ref0[x8 + y8*b8_stride] < 0 && l1ref1[x8 + y8*b8_stride] == 0
  306. && h->x264_build>33U))){
  307. const int16_t (*l1mv)[2]= l1ref0[x8 + y8*b8_stride] == 0 ? l1mv0 : l1mv1;
  308. if(IS_SUB_8X8(sub_mb_type)){
  309. const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
  310. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  311. if(ref[0] == 0)
  312. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  313. if(ref[1] == 0)
  314. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  315. n+=4;
  316. }
  317. }else{
  318. int m=0;
  319. for(i4=0; i4<4; i4++){
  320. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
  321. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  322. if(ref[0] == 0)
  323. *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
  324. if(ref[1] == 0)
  325. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
  326. m++;
  327. }
  328. }
  329. if(!(m&3))
  330. h->sub_mb_type[i8]+= MB_TYPE_16x16 - MB_TYPE_8x8;
  331. n+=m;
  332. }
  333. }
  334. }
  335. if(!is_b8x8 && !(n&15))
  336. *mb_type= (*mb_type & ~(MB_TYPE_8x8|MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_P1L0|MB_TYPE_P1L1))|MB_TYPE_16x16|MB_TYPE_DIRECT2;
  337. }
  338. }else{ /* direct temporal mv pred */
  339. const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
  340. const int *dist_scale_factor = h->dist_scale_factor;
  341. int ref_offset;
  342. if(FRAME_MBAFF && IS_INTERLACED(*mb_type)){
  343. map_col_to_list0[0] = h->map_col_to_list0_field[s->mb_y&1][0];
  344. map_col_to_list0[1] = h->map_col_to_list0_field[s->mb_y&1][1];
  345. dist_scale_factor =h->dist_scale_factor_field[s->mb_y&1];
  346. }
  347. ref_offset = (h->ref_list[1][0].mbaff<<4) & (mb_type_col[0]>>3); //if(h->ref_list[1][0].mbaff && IS_INTERLACED(mb_type_col[0])) ref_offset=16 else 0
  348. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
  349. int y_shift = 2*!IS_INTERLACED(*mb_type);
  350. assert(h->sps.direct_8x8_inference_flag);
  351. for(i8=0; i8<4; i8++){
  352. const int x8 = i8&1;
  353. const int y8 = i8>>1;
  354. int ref0, scale;
  355. const int16_t (*l1mv)[2]= l1mv0;
  356. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  357. continue;
  358. h->sub_mb_type[i8] = sub_mb_type;
  359. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  360. if(IS_INTRA(mb_type_col[y8])){
  361. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  362. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  363. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  364. continue;
  365. }
  366. ref0 = l1ref0[x8 + y8*b8_stride];
  367. if(ref0 >= 0)
  368. ref0 = map_col_to_list0[0][ref0 + ref_offset];
  369. else{
  370. ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];
  371. l1mv= l1mv1;
  372. }
  373. scale = dist_scale_factor[ref0];
  374. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  375. {
  376. const int16_t *mv_col = l1mv[x8*3 + y8*b4_stride];
  377. int my_col = (mv_col[1]<<y_shift)/2;
  378. int mx = (scale * mv_col[0] + 128) >> 8;
  379. int my = (scale * my_col + 128) >> 8;
  380. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  381. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
  382. }
  383. }
  384. return;
  385. }
  386. /* one-to-one mv scaling */
  387. if(IS_16X16(*mb_type)){
  388. int ref, mv0, mv1;
  389. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
  390. if(IS_INTRA(mb_type_col[0])){
  391. ref=mv0=mv1=0;
  392. }else{
  393. const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
  394. : map_col_to_list0[1][l1ref1[0] + ref_offset];
  395. const int scale = dist_scale_factor[ref0];
  396. const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
  397. int mv_l0[2];
  398. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  399. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  400. ref= ref0;
  401. mv0= pack16to32(mv_l0[0],mv_l0[1]);
  402. mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  403. }
  404. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
  405. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
  406. fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
  407. }else{
  408. for(i8=0; i8<4; i8++){
  409. const int x8 = i8&1;
  410. const int y8 = i8>>1;
  411. int ref0, scale;
  412. const int16_t (*l1mv)[2]= l1mv0;
  413. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  414. continue;
  415. h->sub_mb_type[i8] = sub_mb_type;
  416. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  417. if(IS_INTRA(mb_type_col[0])){
  418. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  419. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  420. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  421. continue;
  422. }
  423. ref0 = l1ref0[x8 + y8*b8_stride];
  424. if(ref0 >= 0)
  425. ref0 = map_col_to_list0[0][ref0 + ref_offset];
  426. else{
  427. ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];
  428. l1mv= l1mv1;
  429. }
  430. scale = dist_scale_factor[ref0];
  431. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  432. if(IS_SUB_8X8(sub_mb_type)){
  433. const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
  434. int mx = (scale * mv_col[0] + 128) >> 8;
  435. int my = (scale * mv_col[1] + 128) >> 8;
  436. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  437. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
  438. }else
  439. for(i4=0; i4<4; i4++){
  440. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
  441. int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
  442. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  443. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  444. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
  445. pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  446. }
  447. }
  448. }
  449. }
  450. }