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.

591 lines
25KB

  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
  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. static void pred_spatial_direct_motion(H264Context * const h, int *mb_type){
  124. MpegEncContext * const s = &h->s;
  125. int b8_stride = 2;
  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= MB_TYPE_L0L1;
  133. int i8, i4;
  134. int ref[2];
  135. int mv[2];
  136. int list;
  137. assert(h->ref_list[1][0].reference&3);
  138. #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
  139. /* ref = min(neighbors) */
  140. for(list=0; list<2; list++){
  141. int left_ref = h->ref_cache[list][scan8[0] - 1];
  142. int top_ref = h->ref_cache[list][scan8[0] - 8];
  143. int refc = h->ref_cache[list][scan8[0] - 8 + 4];
  144. const int16_t *C= h->mv_cache[list][ scan8[0] - 8 + 4];
  145. if(refc == PART_NOT_AVAILABLE){
  146. refc = h->ref_cache[list][scan8[0] - 8 - 1];
  147. C = h-> mv_cache[list][scan8[0] - 8 - 1];
  148. }
  149. ref[list] = FFMIN3((unsigned)left_ref, (unsigned)top_ref, (unsigned)refc);
  150. if(ref[list] >= 0){
  151. //this is just pred_motion() but with the cases removed that cannot happen for direct blocks
  152. const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
  153. const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
  154. int match_count= (left_ref==ref[list]) + (top_ref==ref[list]) + (refc==ref[list]);
  155. if(match_count > 1){ //most common
  156. mv[list]= pack16to32(mid_pred(A[0], B[0], C[0]),
  157. mid_pred(A[1], B[1], C[1]) );
  158. }else {
  159. assert(match_count==1);
  160. if(left_ref==ref[list]){
  161. mv[list]= AV_RN32A(A);
  162. }else if(top_ref==ref[list]){
  163. mv[list]= AV_RN32A(B);
  164. }else{
  165. mv[list]= AV_RN32A(C);
  166. }
  167. }
  168. }else{
  169. int mask= ~(MB_TYPE_L0 << (2*list));
  170. mv[list] = 0;
  171. ref[list] = -1;
  172. if(!is_b8x8)
  173. *mb_type &= mask;
  174. sub_mb_type &= mask;
  175. }
  176. }
  177. if(ref[0] < 0 && ref[1] < 0){
  178. ref[0] = ref[1] = 0;
  179. if(!is_b8x8)
  180. *mb_type |= MB_TYPE_L0L1;
  181. sub_mb_type |= MB_TYPE_L0L1;
  182. }
  183. if(!(is_b8x8|mv[0]|mv[1])){
  184. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  185. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  186. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  187. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  188. *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;
  189. return;
  190. }
  191. if(IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])){ // AFL/AFR/FR/FL -> AFL/FL
  192. if(!IS_INTERLACED(*mb_type)){ // AFR/FR -> AFL/FL
  193. mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride;
  194. b8_stride = 0;
  195. }else{
  196. mb_xy += h->col_fieldoff; // non zero for FL -> FL & differ parity
  197. }
  198. goto single_col;
  199. }else{ // AFL/AFR/FR/FL -> AFR/FR
  200. if(IS_INTERLACED(*mb_type)){ // AFL /FL -> AFR/FR
  201. mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride;
  202. mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy];
  203. mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + s->mb_stride];
  204. b8_stride = 2+4*s->mb_stride;
  205. b4_stride *= 6;
  206. sub_mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  207. if( (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
  208. && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
  209. && !is_b8x8){
  210. *mb_type |= MB_TYPE_16x8 |MB_TYPE_DIRECT2; /* B_16x8 */
  211. }else{
  212. *mb_type |= MB_TYPE_8x8;
  213. }
  214. }else{ // AFR/FR -> AFR/FR
  215. single_col:
  216. mb_type_col[0] =
  217. mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy];
  218. sub_mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  219. if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
  220. *mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_16x16 */
  221. }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
  222. *mb_type |= MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
  223. }else{
  224. if(!h->sps.direct_8x8_inference_flag){
  225. /* FIXME save sub mb types from previous frames (or derive from MVs)
  226. * so we know exactly what block size to use */
  227. sub_mb_type += (MB_TYPE_8x8-MB_TYPE_16x16); /* B_SUB_4x4 */
  228. }
  229. *mb_type |= MB_TYPE_8x8;
  230. }
  231. }
  232. }
  233. l1mv0 = &h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]];
  234. l1mv1 = &h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]];
  235. l1ref0 = &h->ref_list[1][0].ref_index [0][4*mb_xy];
  236. l1ref1 = &h->ref_list[1][0].ref_index [1][4*mb_xy];
  237. if(!b8_stride){
  238. if(s->mb_y&1){
  239. l1ref0 += 2;
  240. l1ref1 += 2;
  241. l1mv0 += 2*b4_stride;
  242. l1mv1 += 2*b4_stride;
  243. }
  244. }
  245. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
  246. int n=0;
  247. for(i8=0; i8<4; i8++){
  248. int x8 = i8&1;
  249. int y8 = i8>>1;
  250. int xy8 = x8+y8*b8_stride;
  251. int xy4 = 3*x8+y8*b4_stride;
  252. int a,b;
  253. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  254. continue;
  255. h->sub_mb_type[i8] = sub_mb_type;
  256. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  257. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  258. if(!IS_INTRA(mb_type_col[y8]) && !h->ref_list[1][0].long_ref
  259. && ( (l1ref0[xy8] == 0 && FFABS(l1mv0[xy4][0]) <= 1 && FFABS(l1mv0[xy4][1]) <= 1)
  260. || (l1ref0[xy8] < 0 && l1ref1[xy8] == 0 && FFABS(l1mv1[xy4][0]) <= 1 && FFABS(l1mv1[xy4][1]) <= 1))){
  261. a=b=0;
  262. if(ref[0] > 0)
  263. a= mv[0];
  264. if(ref[1] > 0)
  265. b= mv[1];
  266. n++;
  267. }else{
  268. a= mv[0];
  269. b= mv[1];
  270. }
  271. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, a, 4);
  272. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, b, 4);
  273. }
  274. if(!is_b8x8 && !(n&3))
  275. *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;
  276. }else if(IS_16X16(*mb_type)){
  277. int a,b;
  278. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  279. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  280. if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref
  281. && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
  282. || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
  283. && h->x264_build>33U))){
  284. a=b=0;
  285. if(ref[0] > 0)
  286. a= mv[0];
  287. if(ref[1] > 0)
  288. b= mv[1];
  289. }else{
  290. a= mv[0];
  291. b= mv[1];
  292. }
  293. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
  294. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
  295. }else{
  296. int n=0;
  297. for(i8=0; i8<4; i8++){
  298. const int x8 = i8&1;
  299. const int y8 = i8>>1;
  300. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  301. continue;
  302. h->sub_mb_type[i8] = sub_mb_type;
  303. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, mv[0], 4);
  304. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, mv[1], 4);
  305. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  306. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  307. assert(b8_stride==2);
  308. /* col_zero_flag */
  309. if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref && ( l1ref0[i8] == 0
  310. || (l1ref0[i8] < 0 && l1ref1[i8] == 0
  311. && h->x264_build>33U))){
  312. const int16_t (*l1mv)[2]= l1ref0[i8] == 0 ? l1mv0 : l1mv1;
  313. if(IS_SUB_8X8(sub_mb_type)){
  314. const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
  315. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  316. if(ref[0] == 0)
  317. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  318. if(ref[1] == 0)
  319. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  320. n+=4;
  321. }
  322. }else{
  323. int m=0;
  324. for(i4=0; i4<4; i4++){
  325. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
  326. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  327. if(ref[0] == 0)
  328. AV_ZERO32(h->mv_cache[0][scan8[i8*4+i4]]);
  329. if(ref[1] == 0)
  330. AV_ZERO32(h->mv_cache[1][scan8[i8*4+i4]]);
  331. m++;
  332. }
  333. }
  334. if(!(m&3))
  335. h->sub_mb_type[i8]+= MB_TYPE_16x16 - MB_TYPE_8x8;
  336. n+=m;
  337. }
  338. }
  339. }
  340. if(!is_b8x8 && !(n&15))
  341. *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;
  342. }
  343. }
  344. static void pred_temp_direct_motion(H264Context * const h, int *mb_type){
  345. MpegEncContext * const s = &h->s;
  346. int b8_stride = 2;
  347. int b4_stride = h->b_stride;
  348. int mb_xy = h->mb_xy;
  349. int mb_type_col[2];
  350. const int16_t (*l1mv0)[2], (*l1mv1)[2];
  351. const int8_t *l1ref0, *l1ref1;
  352. const int is_b8x8 = IS_8X8(*mb_type);
  353. unsigned int sub_mb_type;
  354. int i8, i4;
  355. assert(h->ref_list[1][0].reference&3);
  356. if(IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])){ // AFL/AFR/FR/FL -> AFL/FL
  357. if(!IS_INTERLACED(*mb_type)){ // AFR/FR -> AFL/FL
  358. mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride;
  359. b8_stride = 0;
  360. }else{
  361. mb_xy += h->col_fieldoff; // non zero for FL -> FL & differ parity
  362. }
  363. goto single_col;
  364. }else{ // AFL/AFR/FR/FL -> AFR/FR
  365. if(IS_INTERLACED(*mb_type)){ // AFL /FL -> AFR/FR
  366. mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride;
  367. mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy];
  368. mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + s->mb_stride];
  369. b8_stride = 2+4*s->mb_stride;
  370. b4_stride *= 6;
  371. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  372. if( (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
  373. && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
  374. && !is_b8x8){
  375. *mb_type |= MB_TYPE_16x8 |MB_TYPE_L0L1|MB_TYPE_DIRECT2; /* B_16x8 */
  376. }else{
  377. *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
  378. }
  379. }else{ // AFR/FR -> AFR/FR
  380. single_col:
  381. mb_type_col[0] =
  382. mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy];
  383. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  384. if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
  385. *mb_type |= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
  386. }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
  387. *mb_type |= MB_TYPE_L0L1|MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
  388. }else{
  389. if(!h->sps.direct_8x8_inference_flag){
  390. /* FIXME save sub mb types from previous frames (or derive from MVs)
  391. * so we know exactly what block size to use */
  392. sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
  393. }
  394. *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
  395. }
  396. }
  397. }
  398. l1mv0 = &h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]];
  399. l1mv1 = &h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]];
  400. l1ref0 = &h->ref_list[1][0].ref_index [0][4*mb_xy];
  401. l1ref1 = &h->ref_list[1][0].ref_index [1][4*mb_xy];
  402. if(!b8_stride){
  403. if(s->mb_y&1){
  404. l1ref0 += 2;
  405. l1ref1 += 2;
  406. l1mv0 += 2*b4_stride;
  407. l1mv1 += 2*b4_stride;
  408. }
  409. }
  410. {
  411. const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
  412. const int *dist_scale_factor = h->dist_scale_factor;
  413. int ref_offset;
  414. if(FRAME_MBAFF && IS_INTERLACED(*mb_type)){
  415. map_col_to_list0[0] = h->map_col_to_list0_field[s->mb_y&1][0];
  416. map_col_to_list0[1] = h->map_col_to_list0_field[s->mb_y&1][1];
  417. dist_scale_factor =h->dist_scale_factor_field[s->mb_y&1];
  418. }
  419. 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
  420. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
  421. int y_shift = 2*!IS_INTERLACED(*mb_type);
  422. assert(h->sps.direct_8x8_inference_flag);
  423. for(i8=0; i8<4; i8++){
  424. const int x8 = i8&1;
  425. const int y8 = i8>>1;
  426. int ref0, scale;
  427. const int16_t (*l1mv)[2]= l1mv0;
  428. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  429. continue;
  430. h->sub_mb_type[i8] = sub_mb_type;
  431. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  432. if(IS_INTRA(mb_type_col[y8])){
  433. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  434. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  435. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  436. continue;
  437. }
  438. ref0 = l1ref0[x8 + y8*b8_stride];
  439. if(ref0 >= 0)
  440. ref0 = map_col_to_list0[0][ref0 + ref_offset];
  441. else{
  442. ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];
  443. l1mv= l1mv1;
  444. }
  445. scale = dist_scale_factor[ref0];
  446. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  447. {
  448. const int16_t *mv_col = l1mv[x8*3 + y8*b4_stride];
  449. int my_col = (mv_col[1]<<y_shift)/2;
  450. int mx = (scale * mv_col[0] + 128) >> 8;
  451. int my = (scale * my_col + 128) >> 8;
  452. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  453. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
  454. }
  455. }
  456. return;
  457. }
  458. /* one-to-one mv scaling */
  459. if(IS_16X16(*mb_type)){
  460. int ref, mv0, mv1;
  461. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
  462. if(IS_INTRA(mb_type_col[0])){
  463. ref=mv0=mv1=0;
  464. }else{
  465. const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
  466. : map_col_to_list0[1][l1ref1[0] + ref_offset];
  467. const int scale = dist_scale_factor[ref0];
  468. const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
  469. int mv_l0[2];
  470. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  471. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  472. ref= ref0;
  473. mv0= pack16to32(mv_l0[0],mv_l0[1]);
  474. mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  475. }
  476. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
  477. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
  478. fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
  479. }else{
  480. for(i8=0; i8<4; i8++){
  481. const int x8 = i8&1;
  482. const int y8 = i8>>1;
  483. int ref0, scale;
  484. const int16_t (*l1mv)[2]= l1mv0;
  485. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  486. continue;
  487. h->sub_mb_type[i8] = sub_mb_type;
  488. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  489. if(IS_INTRA(mb_type_col[0])){
  490. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  491. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  492. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  493. continue;
  494. }
  495. assert(b8_stride == 2);
  496. ref0 = l1ref0[i8];
  497. if(ref0 >= 0)
  498. ref0 = map_col_to_list0[0][ref0 + ref_offset];
  499. else{
  500. ref0 = map_col_to_list0[1][l1ref1[i8] + ref_offset];
  501. l1mv= l1mv1;
  502. }
  503. scale = dist_scale_factor[ref0];
  504. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  505. if(IS_SUB_8X8(sub_mb_type)){
  506. const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
  507. int mx = (scale * mv_col[0] + 128) >> 8;
  508. int my = (scale * mv_col[1] + 128) >> 8;
  509. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  510. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
  511. }else
  512. for(i4=0; i4<4; i4++){
  513. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
  514. int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
  515. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  516. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  517. AV_WN32A(h->mv_cache[1][scan8[i8*4+i4]],
  518. pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]));
  519. }
  520. }
  521. }
  522. }
  523. }
  524. void ff_h264_pred_direct_motion(H264Context * const h, int *mb_type){
  525. if(h->direct_spatial_mv_pred){
  526. pred_spatial_direct_motion(h, mb_type);
  527. }else{
  528. pred_temp_direct_motion(h, mb_type);
  529. }
  530. }