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.

622 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. for(field=0; field<2; field++){
  52. const int poc = h->s.current_picture_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] = 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. MpegEncContext * const s = &h->s;
  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 || s->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 isnt 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].reference&3) == poc){
  79. int cur_ref= mbafi ? (j-16)^field : j;
  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. MpegEncContext * const s = &h->s;
  91. Picture * const ref1 = &h->ref_list[1][0];
  92. Picture * const cur = s->current_picture_ptr;
  93. int list, j, field;
  94. int sidx= (s->picture_structure&1)^1;
  95. int ref1sidx= (ref1->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].reference&3);
  100. }
  101. if(s->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(s->picture_structure == PICT_FRAME){
  108. int cur_poc = s->current_picture_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(!(s->picture_structure & h->ref_list[1][0].reference) && !h->ref_list[1][0].mbaff){ // FL -> FL & differ parity
  113. h->col_fieldoff= 2*(h->ref_list[1][0].reference) - 3;
  114. }
  115. if(cur->pict_type != 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->reference - 1;
  127. int ref_field_picture = ref->field_picture;
  128. int ref_height = 16*h->s.mb_height >> ref_field_picture;
  129. if(!HAVE_PTHREADS || !(h->s.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((AVFrame*)ref, 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. MpegEncContext * const s = &h->s;
  138. int b8_stride = 2;
  139. int b4_stride = h->b_stride;
  140. int mb_xy = h->mb_xy, mb_y = s->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].reference&3);
  151. await_reference_mb_row(h, &h->ref_list[1][0], s->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].mb_type[mb_xy])){ // AFL/AFR/FR/FL -> AFL/FL
  206. if(!IS_INTERLACED(*mb_type)){ // AFR/FR -> AFL/FL
  207. mb_y = (s->mb_y&~1) + h->col_parity;
  208. mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride;
  209. b8_stride = 0;
  210. }else{
  211. mb_y += h->col_fieldoff;
  212. mb_xy += s->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 = s->mb_y&~1;
  218. mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride;
  219. mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy];
  220. mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + s->mb_stride];
  221. b8_stride = 2+4*s->mb_stride;
  222. b4_stride *= 6;
  223. sub_mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  224. if( (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
  225. && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
  226. && !is_b8x8){
  227. *mb_type |= MB_TYPE_16x8 |MB_TYPE_DIRECT2; /* B_16x8 */
  228. }else{
  229. *mb_type |= MB_TYPE_8x8;
  230. }
  231. }else{ // AFR/FR -> AFR/FR
  232. single_col:
  233. mb_type_col[0] =
  234. mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy];
  235. sub_mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  236. if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
  237. *mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_16x16 */
  238. }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
  239. *mb_type |= MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
  240. }else{
  241. if(!h->sps.direct_8x8_inference_flag){
  242. /* FIXME save sub mb types from previous frames (or derive from MVs)
  243. * so we know exactly what block size to use */
  244. sub_mb_type += (MB_TYPE_8x8-MB_TYPE_16x16); /* B_SUB_4x4 */
  245. }
  246. *mb_type |= MB_TYPE_8x8;
  247. }
  248. }
  249. }
  250. await_reference_mb_row(h, &h->ref_list[1][0], mb_y);
  251. l1mv0 = &h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]];
  252. l1mv1 = &h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]];
  253. l1ref0 = &h->ref_list[1][0].ref_index [0][4*mb_xy];
  254. l1ref1 = &h->ref_list[1][0].ref_index [1][4*mb_xy];
  255. if(!b8_stride){
  256. if(s->mb_y&1){
  257. l1ref0 += 2;
  258. l1ref1 += 2;
  259. l1mv0 += 2*b4_stride;
  260. l1mv1 += 2*b4_stride;
  261. }
  262. }
  263. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
  264. int n=0;
  265. for(i8=0; i8<4; i8++){
  266. int x8 = i8&1;
  267. int y8 = i8>>1;
  268. int xy8 = x8+y8*b8_stride;
  269. int xy4 = 3*x8+y8*b4_stride;
  270. int a,b;
  271. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  272. continue;
  273. h->sub_mb_type[i8] = sub_mb_type;
  274. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  275. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  276. if(!IS_INTRA(mb_type_col[y8]) && !h->ref_list[1][0].long_ref
  277. && ( (l1ref0[xy8] == 0 && FFABS(l1mv0[xy4][0]) <= 1 && FFABS(l1mv0[xy4][1]) <= 1)
  278. || (l1ref0[xy8] < 0 && l1ref1[xy8] == 0 && FFABS(l1mv1[xy4][0]) <= 1 && FFABS(l1mv1[xy4][1]) <= 1))){
  279. a=b=0;
  280. if(ref[0] > 0)
  281. a= mv[0];
  282. if(ref[1] > 0)
  283. b= mv[1];
  284. n++;
  285. }else{
  286. a= mv[0];
  287. b= mv[1];
  288. }
  289. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, a, 4);
  290. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, b, 4);
  291. }
  292. if(!is_b8x8 && !(n&3))
  293. *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;
  294. }else if(IS_16X16(*mb_type)){
  295. int a,b;
  296. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  297. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  298. if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref
  299. && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
  300. || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
  301. && h->x264_build>33U))){
  302. a=b=0;
  303. if(ref[0] > 0)
  304. a= mv[0];
  305. if(ref[1] > 0)
  306. b= mv[1];
  307. }else{
  308. a= mv[0];
  309. b= mv[1];
  310. }
  311. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
  312. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
  313. }else{
  314. int n=0;
  315. for(i8=0; i8<4; i8++){
  316. const int x8 = i8&1;
  317. const int y8 = i8>>1;
  318. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  319. continue;
  320. h->sub_mb_type[i8] = sub_mb_type;
  321. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, mv[0], 4);
  322. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, mv[1], 4);
  323. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  324. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  325. assert(b8_stride==2);
  326. /* col_zero_flag */
  327. if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref && ( l1ref0[i8] == 0
  328. || (l1ref0[i8] < 0 && l1ref1[i8] == 0
  329. && h->x264_build>33U))){
  330. const int16_t (*l1mv)[2]= l1ref0[i8] == 0 ? l1mv0 : l1mv1;
  331. if(IS_SUB_8X8(sub_mb_type)){
  332. const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
  333. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  334. if(ref[0] == 0)
  335. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  336. if(ref[1] == 0)
  337. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  338. n+=4;
  339. }
  340. }else{
  341. int m=0;
  342. for(i4=0; i4<4; i4++){
  343. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
  344. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  345. if(ref[0] == 0)
  346. AV_ZERO32(h->mv_cache[0][scan8[i8*4+i4]]);
  347. if(ref[1] == 0)
  348. AV_ZERO32(h->mv_cache[1][scan8[i8*4+i4]]);
  349. m++;
  350. }
  351. }
  352. if(!(m&3))
  353. h->sub_mb_type[i8]+= MB_TYPE_16x16 - MB_TYPE_8x8;
  354. n+=m;
  355. }
  356. }
  357. }
  358. if(!is_b8x8 && !(n&15))
  359. *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;
  360. }
  361. }
  362. static void pred_temp_direct_motion(H264Context * const h, int *mb_type){
  363. MpegEncContext * const s = &h->s;
  364. int b8_stride = 2;
  365. int b4_stride = h->b_stride;
  366. int mb_xy = h->mb_xy, mb_y = s->mb_y;
  367. int mb_type_col[2];
  368. const int16_t (*l1mv0)[2], (*l1mv1)[2];
  369. const int8_t *l1ref0, *l1ref1;
  370. const int is_b8x8 = IS_8X8(*mb_type);
  371. unsigned int sub_mb_type;
  372. int i8, i4;
  373. assert(h->ref_list[1][0].reference&3);
  374. await_reference_mb_row(h, &h->ref_list[1][0], s->mb_y + !!IS_INTERLACED(*mb_type));
  375. if(IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])){ // AFL/AFR/FR/FL -> AFL/FL
  376. if(!IS_INTERLACED(*mb_type)){ // AFR/FR -> AFL/FL
  377. mb_y = (s->mb_y&~1) + h->col_parity;
  378. mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride;
  379. b8_stride = 0;
  380. }else{
  381. mb_y += h->col_fieldoff;
  382. mb_xy += s->mb_stride*h->col_fieldoff; // non zero for FL -> FL & differ parity
  383. }
  384. goto single_col;
  385. }else{ // AFL/AFR/FR/FL -> AFR/FR
  386. if(IS_INTERLACED(*mb_type)){ // AFL /FL -> AFR/FR
  387. mb_y = s->mb_y&~1;
  388. mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride;
  389. mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy];
  390. mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + s->mb_stride];
  391. b8_stride = 2+4*s->mb_stride;
  392. b4_stride *= 6;
  393. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  394. if( (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
  395. && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
  396. && !is_b8x8){
  397. *mb_type |= MB_TYPE_16x8 |MB_TYPE_L0L1|MB_TYPE_DIRECT2; /* B_16x8 */
  398. }else{
  399. *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
  400. }
  401. }else{ // AFR/FR -> AFR/FR
  402. single_col:
  403. mb_type_col[0] =
  404. mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy];
  405. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  406. if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
  407. *mb_type |= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
  408. }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
  409. *mb_type |= MB_TYPE_L0L1|MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
  410. }else{
  411. if(!h->sps.direct_8x8_inference_flag){
  412. /* FIXME save sub mb types from previous frames (or derive from MVs)
  413. * so we know exactly what block size to use */
  414. sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
  415. }
  416. *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
  417. }
  418. }
  419. }
  420. await_reference_mb_row(h, &h->ref_list[1][0], mb_y);
  421. l1mv0 = &h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]];
  422. l1mv1 = &h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]];
  423. l1ref0 = &h->ref_list[1][0].ref_index [0][4*mb_xy];
  424. l1ref1 = &h->ref_list[1][0].ref_index [1][4*mb_xy];
  425. if(!b8_stride){
  426. if(s->mb_y&1){
  427. l1ref0 += 2;
  428. l1ref1 += 2;
  429. l1mv0 += 2*b4_stride;
  430. l1mv1 += 2*b4_stride;
  431. }
  432. }
  433. {
  434. const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
  435. const int *dist_scale_factor = h->dist_scale_factor;
  436. int ref_offset;
  437. if(FRAME_MBAFF && IS_INTERLACED(*mb_type)){
  438. map_col_to_list0[0] = h->map_col_to_list0_field[s->mb_y&1][0];
  439. map_col_to_list0[1] = h->map_col_to_list0_field[s->mb_y&1][1];
  440. dist_scale_factor =h->dist_scale_factor_field[s->mb_y&1];
  441. }
  442. 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
  443. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
  444. int y_shift = 2*!IS_INTERLACED(*mb_type);
  445. assert(h->sps.direct_8x8_inference_flag);
  446. for(i8=0; i8<4; i8++){
  447. const int x8 = i8&1;
  448. const int y8 = i8>>1;
  449. int ref0, scale;
  450. const int16_t (*l1mv)[2]= l1mv0;
  451. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  452. continue;
  453. h->sub_mb_type[i8] = sub_mb_type;
  454. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  455. if(IS_INTRA(mb_type_col[y8])){
  456. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  457. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  458. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  459. continue;
  460. }
  461. ref0 = l1ref0[x8 + y8*b8_stride];
  462. if(ref0 >= 0)
  463. ref0 = map_col_to_list0[0][ref0 + ref_offset];
  464. else{
  465. ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];
  466. l1mv= l1mv1;
  467. }
  468. scale = dist_scale_factor[ref0];
  469. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  470. {
  471. const int16_t *mv_col = l1mv[x8*3 + y8*b4_stride];
  472. int my_col = (mv_col[1]<<y_shift)/2;
  473. int mx = (scale * mv_col[0] + 128) >> 8;
  474. int my = (scale * my_col + 128) >> 8;
  475. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  476. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
  477. }
  478. }
  479. return;
  480. }
  481. /* one-to-one mv scaling */
  482. if(IS_16X16(*mb_type)){
  483. int ref, mv0, mv1;
  484. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
  485. if(IS_INTRA(mb_type_col[0])){
  486. ref=mv0=mv1=0;
  487. }else{
  488. const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
  489. : map_col_to_list0[1][l1ref1[0] + ref_offset];
  490. const int scale = dist_scale_factor[ref0];
  491. const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
  492. int mv_l0[2];
  493. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  494. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  495. ref= ref0;
  496. mv0= pack16to32(mv_l0[0],mv_l0[1]);
  497. mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  498. }
  499. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
  500. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
  501. fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
  502. }else{
  503. for(i8=0; i8<4; i8++){
  504. const int x8 = i8&1;
  505. const int y8 = i8>>1;
  506. int ref0, scale;
  507. const int16_t (*l1mv)[2]= l1mv0;
  508. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  509. continue;
  510. h->sub_mb_type[i8] = sub_mb_type;
  511. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  512. if(IS_INTRA(mb_type_col[0])){
  513. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  514. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  515. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  516. continue;
  517. }
  518. assert(b8_stride == 2);
  519. ref0 = l1ref0[i8];
  520. if(ref0 >= 0)
  521. ref0 = map_col_to_list0[0][ref0 + ref_offset];
  522. else{
  523. ref0 = map_col_to_list0[1][l1ref1[i8] + ref_offset];
  524. l1mv= l1mv1;
  525. }
  526. scale = dist_scale_factor[ref0];
  527. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  528. if(IS_SUB_8X8(sub_mb_type)){
  529. const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
  530. int mx = (scale * mv_col[0] + 128) >> 8;
  531. int my = (scale * mv_col[1] + 128) >> 8;
  532. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  533. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
  534. }else
  535. for(i4=0; i4<4; i4++){
  536. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
  537. int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
  538. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  539. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  540. AV_WN32A(h->mv_cache[1][scan8[i8*4+i4]],
  541. pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]));
  542. }
  543. }
  544. }
  545. }
  546. }
  547. void ff_h264_pred_direct_motion(H264Context * const h, int *mb_type){
  548. if(h->direct_spatial_mv_pred){
  549. pred_spatial_direct_motion(h, mb_type);
  550. }else{
  551. pred_temp_direct_motion(h, mb_type);
  552. }
  553. }