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.

490 lines
21KB

  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. if(IS_8X8(mb_type_col[0]) && !h->sps.direct_8x8_inference_flag){
  164. /* FIXME save sub mb types from previous frames (or derive from MVs)
  165. * so we know exactly what block size to use */
  166. sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
  167. *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
  168. }else if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
  169. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  170. *mb_type |= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
  171. }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
  172. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  173. *mb_type |= MB_TYPE_L0L1|MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
  174. }else{
  175. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  176. *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
  177. }
  178. }
  179. }
  180. l1mv0 = &h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]];
  181. l1mv1 = &h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]];
  182. l1ref0 = &h->ref_list[1][0].ref_index [0][h->mb2b8_xy[mb_xy]];
  183. l1ref1 = &h->ref_list[1][0].ref_index [1][h->mb2b8_xy[mb_xy]];
  184. if(!b8_stride){
  185. if(s->mb_y&1){
  186. l1ref0 += h->b8_stride;
  187. l1ref1 += h->b8_stride;
  188. l1mv0 += 2*b4_stride;
  189. l1mv1 += 2*b4_stride;
  190. }
  191. }
  192. if(h->direct_spatial_mv_pred){
  193. int ref[2];
  194. int mv[2][2];
  195. int list;
  196. /* FIXME interlacing + spatial direct uses wrong colocated block positions */
  197. /* ref = min(neighbors) */
  198. for(list=0; list<2; list++){
  199. int left_ref = h->ref_cache[list][scan8[0] - 1];
  200. int top_ref = h->ref_cache[list][scan8[0] - 8];
  201. int refc = h->ref_cache[list][scan8[0] - 8 + 4];
  202. const int16_t *C= h->mv_cache[list][ scan8[0] - 8 + 4];
  203. if(refc == PART_NOT_AVAILABLE){
  204. refc = h->ref_cache[list][scan8[0] - 8 - 1];
  205. C = h-> mv_cache[list][scan8[0] - 8 - 1];
  206. }
  207. ref[list] = FFMIN3((unsigned)left_ref, (unsigned)top_ref, (unsigned)refc);
  208. if(ref[list] >= 0){
  209. //this is just pred_motion() but with the cases removed that cannot happen for direct blocks
  210. const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
  211. const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
  212. int match_count= (left_ref==ref[list]) + (top_ref==ref[list]) + (refc==ref[list]);
  213. if(match_count > 1){ //most common
  214. mv[list][0]= mid_pred(A[0], B[0], C[0]);
  215. mv[list][1]= mid_pred(A[1], B[1], C[1]);
  216. }else {
  217. assert(match_count==1);
  218. if(left_ref==ref[list]){
  219. mv[list][0]= A[0];
  220. mv[list][1]= A[1];
  221. }else if(top_ref==ref[list]){
  222. mv[list][0]= B[0];
  223. mv[list][1]= B[1];
  224. }else{
  225. mv[list][0]= C[0];
  226. mv[list][1]= C[1];
  227. }
  228. }
  229. }else{
  230. int mask= ~(MB_TYPE_L0 << (2*list));
  231. mv[list][0] = mv[list][1] = 0;
  232. ref[list] = -1;
  233. if(!is_b8x8)
  234. *mb_type &= mask;
  235. sub_mb_type &= mask;
  236. }
  237. }
  238. if(ref[0] < 0 && ref[1] < 0){
  239. ref[0] = ref[1] = 0;
  240. if(!is_b8x8)
  241. *mb_type |= MB_TYPE_L0L1;
  242. sub_mb_type |= MB_TYPE_L0L1;
  243. }
  244. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
  245. for(i8=0; i8<4; i8++){
  246. int x8 = i8&1;
  247. int y8 = i8>>1;
  248. int xy8 = x8+y8*b8_stride;
  249. int xy4 = 3*x8+y8*b4_stride;
  250. int a,b;
  251. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  252. continue;
  253. h->sub_mb_type[i8] = sub_mb_type;
  254. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  255. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  256. if(!IS_INTRA(mb_type_col[y8]) && !h->ref_list[1][0].long_ref
  257. && ( (l1ref0[xy8] == 0 && FFABS(l1mv0[xy4][0]) <= 1 && FFABS(l1mv0[xy4][1]) <= 1)
  258. || (l1ref0[xy8] < 0 && l1ref1[xy8] == 0 && FFABS(l1mv1[xy4][0]) <= 1 && FFABS(l1mv1[xy4][1]) <= 1))){
  259. a=b=0;
  260. if(ref[0] > 0)
  261. a= pack16to32(mv[0][0],mv[0][1]);
  262. if(ref[1] > 0)
  263. b= pack16to32(mv[1][0],mv[1][1]);
  264. }else{
  265. a= pack16to32(mv[0][0],mv[0][1]);
  266. b= pack16to32(mv[1][0],mv[1][1]);
  267. }
  268. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, a, 4);
  269. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, b, 4);
  270. }
  271. }else if(IS_16X16(*mb_type)){
  272. int a,b;
  273. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  274. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  275. if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref
  276. && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
  277. || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
  278. && h->x264_build>33U))){
  279. a=b=0;
  280. if(ref[0] > 0)
  281. a= pack16to32(mv[0][0],mv[0][1]);
  282. if(ref[1] > 0)
  283. b= pack16to32(mv[1][0],mv[1][1]);
  284. }else{
  285. a= pack16to32(mv[0][0],mv[0][1]);
  286. b= pack16to32(mv[1][0],mv[1][1]);
  287. }
  288. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
  289. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
  290. }else{
  291. for(i8=0; i8<4; i8++){
  292. const int x8 = i8&1;
  293. const int y8 = i8>>1;
  294. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  295. continue;
  296. h->sub_mb_type[i8] = sub_mb_type;
  297. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  298. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  299. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  300. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  301. /* col_zero_flag */
  302. if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref && ( l1ref0[x8 + y8*b8_stride] == 0
  303. || (l1ref0[x8 + y8*b8_stride] < 0 && l1ref1[x8 + y8*b8_stride] == 0
  304. && h->x264_build>33U))){
  305. const int16_t (*l1mv)[2]= l1ref0[x8 + y8*b8_stride] == 0 ? l1mv0 : l1mv1;
  306. if(IS_SUB_8X8(sub_mb_type)){
  307. const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
  308. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  309. if(ref[0] == 0)
  310. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  311. if(ref[1] == 0)
  312. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  313. }
  314. }else
  315. for(i4=0; i4<4; i4++){
  316. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
  317. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  318. if(ref[0] == 0)
  319. *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
  320. if(ref[1] == 0)
  321. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
  322. }
  323. }
  324. }
  325. }
  326. }
  327. }else{ /* direct temporal mv pred */
  328. const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
  329. const int *dist_scale_factor = h->dist_scale_factor;
  330. int ref_offset= 0;
  331. if(FRAME_MBAFF && IS_INTERLACED(*mb_type)){
  332. map_col_to_list0[0] = h->map_col_to_list0_field[s->mb_y&1][0];
  333. map_col_to_list0[1] = h->map_col_to_list0_field[s->mb_y&1][1];
  334. dist_scale_factor =h->dist_scale_factor_field[s->mb_y&1];
  335. }
  336. if(h->ref_list[1][0].mbaff && IS_INTERLACED(mb_type_col[0]))
  337. ref_offset += 16;
  338. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
  339. int y_shift = 2*!IS_INTERLACED(*mb_type);
  340. assert(h->sps.direct_8x8_inference_flag);
  341. for(i8=0; i8<4; i8++){
  342. const int x8 = i8&1;
  343. const int y8 = i8>>1;
  344. int ref0, scale;
  345. const int16_t (*l1mv)[2]= l1mv0;
  346. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  347. continue;
  348. h->sub_mb_type[i8] = sub_mb_type;
  349. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  350. if(IS_INTRA(mb_type_col[y8])){
  351. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  352. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  353. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  354. continue;
  355. }
  356. ref0 = l1ref0[x8 + y8*b8_stride];
  357. if(ref0 >= 0)
  358. ref0 = map_col_to_list0[0][ref0 + ref_offset];
  359. else{
  360. ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];
  361. l1mv= l1mv1;
  362. }
  363. scale = dist_scale_factor[ref0];
  364. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  365. {
  366. const int16_t *mv_col = l1mv[x8*3 + y8*b4_stride];
  367. int my_col = (mv_col[1]<<y_shift)/2;
  368. int mx = (scale * mv_col[0] + 128) >> 8;
  369. int my = (scale * my_col + 128) >> 8;
  370. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  371. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
  372. }
  373. }
  374. return;
  375. }
  376. /* one-to-one mv scaling */
  377. if(IS_16X16(*mb_type)){
  378. int ref, mv0, mv1;
  379. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
  380. if(IS_INTRA(mb_type_col[0])){
  381. ref=mv0=mv1=0;
  382. }else{
  383. const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
  384. : map_col_to_list0[1][l1ref1[0] + ref_offset];
  385. const int scale = dist_scale_factor[ref0];
  386. const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
  387. int mv_l0[2];
  388. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  389. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  390. ref= ref0;
  391. mv0= pack16to32(mv_l0[0],mv_l0[1]);
  392. mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  393. }
  394. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
  395. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
  396. fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
  397. }else{
  398. for(i8=0; i8<4; i8++){
  399. const int x8 = i8&1;
  400. const int y8 = i8>>1;
  401. int ref0, scale;
  402. const int16_t (*l1mv)[2]= l1mv0;
  403. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  404. continue;
  405. h->sub_mb_type[i8] = sub_mb_type;
  406. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  407. if(IS_INTRA(mb_type_col[0])){
  408. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  409. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  410. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  411. continue;
  412. }
  413. ref0 = l1ref0[x8 + y8*b8_stride];
  414. if(ref0 >= 0)
  415. ref0 = map_col_to_list0[0][ref0 + ref_offset];
  416. else{
  417. ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];
  418. l1mv= l1mv1;
  419. }
  420. scale = dist_scale_factor[ref0];
  421. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  422. if(IS_SUB_8X8(sub_mb_type)){
  423. const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
  424. int mx = (scale * mv_col[0] + 128) >> 8;
  425. int my = (scale * mv_col[1] + 128) >> 8;
  426. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  427. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
  428. }else
  429. for(i4=0; i4<4; i4++){
  430. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
  431. int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
  432. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  433. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  434. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
  435. pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  436. }
  437. }
  438. }
  439. }
  440. }