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.

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