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.

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