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.

630 lines
26KB

  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 Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. #include "thread.h"
  33. //#undef NDEBUG
  34. #include <assert.h>
  35. static int get_scale_factor(H264Context * const h, int poc, int poc1, int i){
  36. int poc0 = h->ref_list[0][i].poc;
  37. int td = av_clip(poc1 - poc0, -128, 127);
  38. if(td == 0 || h->ref_list[0][i].long_ref){
  39. return 256;
  40. }else{
  41. int tb = av_clip(poc - poc0, -128, 127);
  42. int tx = (16384 + (FFABS(td) >> 1)) / td;
  43. return av_clip((tb*tx + 32) >> 6, -1024, 1023);
  44. }
  45. }
  46. void ff_h264_direct_dist_scale_factor(H264Context * const h){
  47. const int poc = h->cur_pic_ptr->field_poc[h->picture_structure == PICT_BOTTOM_FIELD];
  48. const int poc1 = h->ref_list[1][0].poc;
  49. int i, field;
  50. if (FRAME_MBAFF)
  51. for (field = 0; field < 2; field++){
  52. const int poc = h->cur_pic_ptr->field_poc[field];
  53. const int poc1 = h->ref_list[1][0].field_poc[field];
  54. for (i = 0; i < 2 * h->ref_count[0]; i++)
  55. h->dist_scale_factor_field[field][i^field] =
  56. get_scale_factor(h, poc, poc1, i+16);
  57. }
  58. for (i = 0; i < h->ref_count[0]; i++){
  59. h->dist_scale_factor[i] = get_scale_factor(h, poc, poc1, i);
  60. }
  61. }
  62. static void fill_colmap(H264Context *h, int map[2][16+32], int list, int field, int colfield, int mbafi){
  63. Picture * const ref1 = &h->ref_list[1][0];
  64. int j, old_ref, rfield;
  65. int start= mbafi ? 16 : 0;
  66. int end = mbafi ? 16+2*h->ref_count[0] : h->ref_count[0];
  67. int interl= mbafi || h->picture_structure != PICT_FRAME;
  68. /* bogus; fills in for missing frames */
  69. memset(map[list], 0, sizeof(map[list]));
  70. for(rfield=0; rfield<2; rfield++){
  71. for(old_ref=0; old_ref<ref1->ref_count[colfield][list]; old_ref++){
  72. int poc = ref1->ref_poc[colfield][list][old_ref];
  73. if (!interl)
  74. poc |= 3;
  75. else if( interl && (poc&3) == 3) // FIXME: store all MBAFF references so this is not needed
  76. poc= (poc&~3) + rfield + 1;
  77. for(j=start; j<end; j++){
  78. if (4 * h->ref_list[0][j].frame_num + (h->ref_list[0][j].f.reference & 3) == poc) {
  79. int cur_ref= mbafi ? (j-16)^field : j;
  80. if (ref1->mbaff)
  81. map[list][2 * old_ref + (rfield^field) + 16] = cur_ref;
  82. if(rfield == field || !interl)
  83. map[list][old_ref] = cur_ref;
  84. break;
  85. }
  86. }
  87. }
  88. }
  89. }
  90. void ff_h264_direct_ref_list_init(H264Context * const h){
  91. Picture * const ref1 = &h->ref_list[1][0];
  92. Picture * const cur = h->cur_pic_ptr;
  93. int list, j, field;
  94. int sidx= (h->picture_structure&1)^1;
  95. int ref1sidx = (ref1->f.reference&1)^1;
  96. for(list=0; list<2; list++){
  97. cur->ref_count[sidx][list] = h->ref_count[list];
  98. for(j=0; j<h->ref_count[list]; j++)
  99. cur->ref_poc[sidx][list][j] = 4 * h->ref_list[list][j].frame_num + (h->ref_list[list][j].f.reference & 3);
  100. }
  101. if(h->picture_structure == PICT_FRAME){
  102. memcpy(cur->ref_count[1], cur->ref_count[0], sizeof(cur->ref_count[0]));
  103. memcpy(cur->ref_poc [1], cur->ref_poc [0], sizeof(cur->ref_poc [0]));
  104. }
  105. cur->mbaff= FRAME_MBAFF;
  106. h->col_fieldoff= 0;
  107. if(h->picture_structure == PICT_FRAME){
  108. int cur_poc = h->cur_pic_ptr->poc;
  109. int *col_poc = h->ref_list[1]->field_poc;
  110. h->col_parity= (FFABS(col_poc[0] - cur_poc) >= FFABS(col_poc[1] - cur_poc));
  111. ref1sidx=sidx= h->col_parity;
  112. } else if (!(h->picture_structure & h->ref_list[1][0].f.reference) && !h->ref_list[1][0].mbaff) { // FL -> FL & differ parity
  113. h->col_fieldoff = 2 * h->ref_list[1][0].f.reference - 3;
  114. }
  115. if (h->slice_type_nos != AV_PICTURE_TYPE_B || h->direct_spatial_mv_pred)
  116. return;
  117. for(list=0; list<2; list++){
  118. fill_colmap(h, h->map_col_to_list0, list, sidx, ref1sidx, 0);
  119. if(FRAME_MBAFF)
  120. for(field=0; field<2; field++)
  121. fill_colmap(h, h->map_col_to_list0_field[field], list, field, field, 1);
  122. }
  123. }
  124. static void await_reference_mb_row(H264Context * const h, Picture *ref, int mb_y)
  125. {
  126. int ref_field = ref->f.reference - 1;
  127. int ref_field_picture = ref->field_picture;
  128. int ref_height = 16*h->mb_height >> ref_field_picture;
  129. if(!HAVE_THREADS || !(h->avctx->active_thread_type&FF_THREAD_FRAME))
  130. return;
  131. //FIXME it can be safe to access mb stuff
  132. //even if pixels aren't deblocked yet
  133. ff_thread_await_progress(&ref->f,
  134. FFMIN(16 * mb_y >> ref_field_picture, ref_height - 1),
  135. ref_field_picture && ref_field);
  136. }
  137. static void pred_spatial_direct_motion(H264Context * const h, int *mb_type){
  138. int b8_stride = 2;
  139. int b4_stride = h->b_stride;
  140. int mb_xy = h->mb_xy, mb_y = h->mb_y;
  141. int mb_type_col[2];
  142. const int16_t (*l1mv0)[2], (*l1mv1)[2];
  143. const int8_t *l1ref0, *l1ref1;
  144. const int is_b8x8 = IS_8X8(*mb_type);
  145. unsigned int sub_mb_type= MB_TYPE_L0L1;
  146. int i8, i4;
  147. int ref[2];
  148. int mv[2];
  149. int list;
  150. assert(h->ref_list[1][0].f.reference & 3);
  151. await_reference_mb_row(h, &h->ref_list[1][0], h->mb_y + !!IS_INTERLACED(*mb_type));
  152. #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
  153. /* ref = min(neighbors) */
  154. for(list=0; list<2; list++){
  155. int left_ref = h->ref_cache[list][scan8[0] - 1];
  156. int top_ref = h->ref_cache[list][scan8[0] - 8];
  157. int refc = h->ref_cache[list][scan8[0] - 8 + 4];
  158. const int16_t *C= h->mv_cache[list][ scan8[0] - 8 + 4];
  159. if(refc == PART_NOT_AVAILABLE){
  160. refc = h->ref_cache[list][scan8[0] - 8 - 1];
  161. C = h-> mv_cache[list][scan8[0] - 8 - 1];
  162. }
  163. ref[list] = FFMIN3((unsigned)left_ref, (unsigned)top_ref, (unsigned)refc);
  164. if(ref[list] >= 0){
  165. //this is just pred_motion() but with the cases removed that cannot happen for direct blocks
  166. const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
  167. const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
  168. int match_count= (left_ref==ref[list]) + (top_ref==ref[list]) + (refc==ref[list]);
  169. if(match_count > 1){ //most common
  170. mv[list]= pack16to32(mid_pred(A[0], B[0], C[0]),
  171. mid_pred(A[1], B[1], C[1]) );
  172. }else {
  173. assert(match_count==1);
  174. if(left_ref==ref[list]){
  175. mv[list]= AV_RN32A(A);
  176. }else if(top_ref==ref[list]){
  177. mv[list]= AV_RN32A(B);
  178. }else{
  179. mv[list]= AV_RN32A(C);
  180. }
  181. }
  182. }else{
  183. int mask= ~(MB_TYPE_L0 << (2*list));
  184. mv[list] = 0;
  185. ref[list] = -1;
  186. if(!is_b8x8)
  187. *mb_type &= mask;
  188. sub_mb_type &= mask;
  189. }
  190. }
  191. if(ref[0] < 0 && ref[1] < 0){
  192. ref[0] = ref[1] = 0;
  193. if(!is_b8x8)
  194. *mb_type |= MB_TYPE_L0L1;
  195. sub_mb_type |= MB_TYPE_L0L1;
  196. }
  197. if(!(is_b8x8|mv[0]|mv[1])){
  198. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  199. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  200. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  201. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  202. *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;
  203. return;
  204. }
  205. if (IS_INTERLACED(h->ref_list[1][0].f.mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
  206. if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
  207. mb_y = (h->mb_y&~1) + h->col_parity;
  208. mb_xy= h->mb_x + ((h->mb_y&~1) + h->col_parity)*h->mb_stride;
  209. b8_stride = 0;
  210. }else{
  211. mb_y += h->col_fieldoff;
  212. mb_xy += h->mb_stride*h->col_fieldoff; // non zero for FL -> FL & differ parity
  213. }
  214. goto single_col;
  215. }else{ // AFL/AFR/FR/FL -> AFR/FR
  216. if(IS_INTERLACED(*mb_type)){ // AFL /FL -> AFR/FR
  217. mb_y = h->mb_y&~1;
  218. mb_xy= h->mb_x + (h->mb_y&~1)*h->mb_stride;
  219. mb_type_col[0] = h->ref_list[1][0].f.mb_type[mb_xy];
  220. mb_type_col[1] = h->ref_list[1][0].f.mb_type[mb_xy + h->mb_stride];
  221. b8_stride = 2+4*h->mb_stride;
  222. b4_stride *= 6;
  223. if (IS_INTERLACED(mb_type_col[0]) != IS_INTERLACED(mb_type_col[1])) {
  224. mb_type_col[0] &= ~MB_TYPE_INTERLACED;
  225. mb_type_col[1] &= ~MB_TYPE_INTERLACED;
  226. }
  227. sub_mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  228. if( (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
  229. && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
  230. && !is_b8x8){
  231. *mb_type |= MB_TYPE_16x8 |MB_TYPE_DIRECT2; /* B_16x8 */
  232. }else{
  233. *mb_type |= MB_TYPE_8x8;
  234. }
  235. }else{ // AFR/FR -> AFR/FR
  236. single_col:
  237. mb_type_col[0] =
  238. mb_type_col[1] = h->ref_list[1][0].f.mb_type[mb_xy];
  239. sub_mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  240. if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
  241. *mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_16x16 */
  242. }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
  243. *mb_type |= MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
  244. }else{
  245. if(!h->sps.direct_8x8_inference_flag){
  246. /* FIXME save sub mb types from previous frames (or derive from MVs)
  247. * so we know exactly what block size to use */
  248. sub_mb_type += (MB_TYPE_8x8-MB_TYPE_16x16); /* B_SUB_4x4 */
  249. }
  250. *mb_type |= MB_TYPE_8x8;
  251. }
  252. }
  253. }
  254. await_reference_mb_row(h, &h->ref_list[1][0], mb_y);
  255. l1mv0 = &h->ref_list[1][0].f.motion_val[0][h->mb2b_xy [mb_xy]];
  256. l1mv1 = &h->ref_list[1][0].f.motion_val[1][h->mb2b_xy [mb_xy]];
  257. l1ref0 = &h->ref_list[1][0].f.ref_index [0][4 * mb_xy];
  258. l1ref1 = &h->ref_list[1][0].f.ref_index [1][4 * mb_xy];
  259. if(!b8_stride){
  260. if(h->mb_y&1){
  261. l1ref0 += 2;
  262. l1ref1 += 2;
  263. l1mv0 += 2*b4_stride;
  264. l1mv1 += 2*b4_stride;
  265. }
  266. }
  267. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
  268. int n=0;
  269. for(i8=0; i8<4; i8++){
  270. int x8 = i8&1;
  271. int y8 = i8>>1;
  272. int xy8 = x8+y8*b8_stride;
  273. int xy4 = 3*x8+y8*b4_stride;
  274. int a,b;
  275. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  276. continue;
  277. h->sub_mb_type[i8] = sub_mb_type;
  278. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  279. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  280. if(!IS_INTRA(mb_type_col[y8]) && !h->ref_list[1][0].long_ref
  281. && ( (l1ref0[xy8] == 0 && FFABS(l1mv0[xy4][0]) <= 1 && FFABS(l1mv0[xy4][1]) <= 1)
  282. || (l1ref0[xy8] < 0 && l1ref1[xy8] == 0 && FFABS(l1mv1[xy4][0]) <= 1 && FFABS(l1mv1[xy4][1]) <= 1))){
  283. a=b=0;
  284. if(ref[0] > 0)
  285. a= mv[0];
  286. if(ref[1] > 0)
  287. b= mv[1];
  288. n++;
  289. }else{
  290. a= mv[0];
  291. b= mv[1];
  292. }
  293. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, a, 4);
  294. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, b, 4);
  295. }
  296. if(!is_b8x8 && !(n&3))
  297. *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;
  298. }else if(IS_16X16(*mb_type)){
  299. int a,b;
  300. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  301. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  302. if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref
  303. && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
  304. || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
  305. && h->x264_build>33U))){
  306. a=b=0;
  307. if(ref[0] > 0)
  308. a= mv[0];
  309. if(ref[1] > 0)
  310. b= mv[1];
  311. }else{
  312. a= mv[0];
  313. b= mv[1];
  314. }
  315. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
  316. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
  317. }else{
  318. int n=0;
  319. for(i8=0; i8<4; i8++){
  320. const int x8 = i8&1;
  321. const int y8 = i8>>1;
  322. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  323. continue;
  324. h->sub_mb_type[i8] = sub_mb_type;
  325. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, mv[0], 4);
  326. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, mv[1], 4);
  327. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  328. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  329. assert(b8_stride==2);
  330. /* col_zero_flag */
  331. if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref && ( l1ref0[i8] == 0
  332. || (l1ref0[i8] < 0 && l1ref1[i8] == 0
  333. && h->x264_build>33U))){
  334. const int16_t (*l1mv)[2]= l1ref0[i8] == 0 ? l1mv0 : l1mv1;
  335. if(IS_SUB_8X8(sub_mb_type)){
  336. const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
  337. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  338. if(ref[0] == 0)
  339. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  340. if(ref[1] == 0)
  341. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  342. n+=4;
  343. }
  344. }else{
  345. int m=0;
  346. for(i4=0; i4<4; i4++){
  347. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
  348. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  349. if(ref[0] == 0)
  350. AV_ZERO32(h->mv_cache[0][scan8[i8*4+i4]]);
  351. if(ref[1] == 0)
  352. AV_ZERO32(h->mv_cache[1][scan8[i8*4+i4]]);
  353. m++;
  354. }
  355. }
  356. if(!(m&3))
  357. h->sub_mb_type[i8]+= MB_TYPE_16x16 - MB_TYPE_8x8;
  358. n+=m;
  359. }
  360. }
  361. }
  362. if(!is_b8x8 && !(n&15))
  363. *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;
  364. }
  365. }
  366. static void pred_temp_direct_motion(H264Context * const h, int *mb_type){
  367. int b8_stride = 2;
  368. int b4_stride = h->b_stride;
  369. int mb_xy = h->mb_xy, mb_y = h->mb_y;
  370. int mb_type_col[2];
  371. const int16_t (*l1mv0)[2], (*l1mv1)[2];
  372. const int8_t *l1ref0, *l1ref1;
  373. const int is_b8x8 = IS_8X8(*mb_type);
  374. unsigned int sub_mb_type;
  375. int i8, i4;
  376. assert(h->ref_list[1][0].f.reference & 3);
  377. await_reference_mb_row(h, &h->ref_list[1][0], h->mb_y + !!IS_INTERLACED(*mb_type));
  378. if (IS_INTERLACED(h->ref_list[1][0].f.mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
  379. if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
  380. mb_y = (h->mb_y&~1) + h->col_parity;
  381. mb_xy= h->mb_x + ((h->mb_y&~1) + h->col_parity)*h->mb_stride;
  382. b8_stride = 0;
  383. }else{
  384. mb_y += h->col_fieldoff;
  385. mb_xy += h->mb_stride*h->col_fieldoff; // non zero for FL -> FL & differ parity
  386. }
  387. goto single_col;
  388. }else{ // AFL/AFR/FR/FL -> AFR/FR
  389. if(IS_INTERLACED(*mb_type)){ // AFL /FL -> AFR/FR
  390. mb_y = h->mb_y&~1;
  391. mb_xy= h->mb_x + (h->mb_y&~1)*h->mb_stride;
  392. mb_type_col[0] = h->ref_list[1][0].f.mb_type[mb_xy];
  393. mb_type_col[1] = h->ref_list[1][0].f.mb_type[mb_xy + h->mb_stride];
  394. b8_stride = 2+4*h->mb_stride;
  395. b4_stride *= 6;
  396. if (IS_INTERLACED(mb_type_col[0]) != IS_INTERLACED(mb_type_col[1])) {
  397. mb_type_col[0] &= ~MB_TYPE_INTERLACED;
  398. mb_type_col[1] &= ~MB_TYPE_INTERLACED;
  399. }
  400. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  401. if( (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
  402. && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
  403. && !is_b8x8){
  404. *mb_type |= MB_TYPE_16x8 |MB_TYPE_L0L1|MB_TYPE_DIRECT2; /* B_16x8 */
  405. }else{
  406. *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
  407. }
  408. }else{ // AFR/FR -> AFR/FR
  409. single_col:
  410. mb_type_col[0] =
  411. mb_type_col[1] = h->ref_list[1][0].f.mb_type[mb_xy];
  412. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  413. if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
  414. *mb_type |= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
  415. }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
  416. *mb_type |= MB_TYPE_L0L1|MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
  417. }else{
  418. if(!h->sps.direct_8x8_inference_flag){
  419. /* FIXME save sub mb types from previous frames (or derive from MVs)
  420. * so we know exactly what block size to use */
  421. sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
  422. }
  423. *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
  424. }
  425. }
  426. }
  427. await_reference_mb_row(h, &h->ref_list[1][0], mb_y);
  428. l1mv0 = &h->ref_list[1][0].f.motion_val[0][h->mb2b_xy [mb_xy]];
  429. l1mv1 = &h->ref_list[1][0].f.motion_val[1][h->mb2b_xy [mb_xy]];
  430. l1ref0 = &h->ref_list[1][0].f.ref_index [0][4 * mb_xy];
  431. l1ref1 = &h->ref_list[1][0].f.ref_index [1][4 * mb_xy];
  432. if(!b8_stride){
  433. if(h->mb_y&1){
  434. l1ref0 += 2;
  435. l1ref1 += 2;
  436. l1mv0 += 2*b4_stride;
  437. l1mv1 += 2*b4_stride;
  438. }
  439. }
  440. {
  441. const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
  442. const int *dist_scale_factor = h->dist_scale_factor;
  443. int ref_offset;
  444. if(FRAME_MBAFF && IS_INTERLACED(*mb_type)){
  445. map_col_to_list0[0] = h->map_col_to_list0_field[h->mb_y&1][0];
  446. map_col_to_list0[1] = h->map_col_to_list0_field[h->mb_y&1][1];
  447. dist_scale_factor =h->dist_scale_factor_field[h->mb_y&1];
  448. }
  449. 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
  450. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
  451. int y_shift = 2*!IS_INTERLACED(*mb_type);
  452. assert(h->sps.direct_8x8_inference_flag);
  453. for(i8=0; i8<4; i8++){
  454. const int x8 = i8&1;
  455. const int y8 = i8>>1;
  456. int ref0, scale;
  457. const int16_t (*l1mv)[2]= l1mv0;
  458. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  459. continue;
  460. h->sub_mb_type[i8] = sub_mb_type;
  461. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  462. if(IS_INTRA(mb_type_col[y8])){
  463. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  464. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  465. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  466. continue;
  467. }
  468. ref0 = l1ref0[x8 + y8*b8_stride];
  469. if(ref0 >= 0)
  470. ref0 = map_col_to_list0[0][ref0 + ref_offset];
  471. else{
  472. ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];
  473. l1mv= l1mv1;
  474. }
  475. scale = dist_scale_factor[ref0];
  476. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  477. {
  478. const int16_t *mv_col = l1mv[x8*3 + y8*b4_stride];
  479. int my_col = (mv_col[1]<<y_shift)/2;
  480. int mx = (scale * mv_col[0] + 128) >> 8;
  481. int my = (scale * my_col + 128) >> 8;
  482. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  483. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
  484. }
  485. }
  486. return;
  487. }
  488. /* one-to-one mv scaling */
  489. if(IS_16X16(*mb_type)){
  490. int ref, mv0, mv1;
  491. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
  492. if(IS_INTRA(mb_type_col[0])){
  493. ref=mv0=mv1=0;
  494. }else{
  495. const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
  496. : map_col_to_list0[1][l1ref1[0] + ref_offset];
  497. const int scale = dist_scale_factor[ref0];
  498. const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
  499. int mv_l0[2];
  500. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  501. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  502. ref= ref0;
  503. mv0= pack16to32(mv_l0[0],mv_l0[1]);
  504. mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  505. }
  506. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
  507. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
  508. fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
  509. }else{
  510. for(i8=0; i8<4; i8++){
  511. const int x8 = i8&1;
  512. const int y8 = i8>>1;
  513. int ref0, scale;
  514. const int16_t (*l1mv)[2]= l1mv0;
  515. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  516. continue;
  517. h->sub_mb_type[i8] = sub_mb_type;
  518. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  519. if(IS_INTRA(mb_type_col[0])){
  520. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  521. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  522. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  523. continue;
  524. }
  525. assert(b8_stride == 2);
  526. ref0 = l1ref0[i8];
  527. if(ref0 >= 0)
  528. ref0 = map_col_to_list0[0][ref0 + ref_offset];
  529. else{
  530. ref0 = map_col_to_list0[1][l1ref1[i8] + ref_offset];
  531. l1mv= l1mv1;
  532. }
  533. scale = dist_scale_factor[ref0];
  534. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  535. if(IS_SUB_8X8(sub_mb_type)){
  536. const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
  537. int mx = (scale * mv_col[0] + 128) >> 8;
  538. int my = (scale * mv_col[1] + 128) >> 8;
  539. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  540. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
  541. }else
  542. for(i4=0; i4<4; i4++){
  543. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
  544. int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
  545. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  546. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  547. AV_WN32A(h->mv_cache[1][scan8[i8*4+i4]],
  548. pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]));
  549. }
  550. }
  551. }
  552. }
  553. }
  554. void ff_h264_pred_direct_motion(H264Context * const h, int *mb_type){
  555. if(h->direct_spatial_mv_pred){
  556. pred_spatial_direct_motion(h, mb_type);
  557. }else{
  558. pred_temp_direct_motion(h, mb_type);
  559. }
  560. }