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.

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