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.

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