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.

627 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 FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. if(ref1->mbaff)
  81. map[list][2*old_ref + (rfield^field) + 16] = cur_ref;
  82. if(rfield == field || !interl)
  83. map[list][old_ref] = cur_ref;
  84. break;
  85. }
  86. }
  87. }
  88. }
  89. }
  90. void ff_h264_direct_ref_list_init(H264Context * const h){
  91. MpegEncContext * const s = &h->s;
  92. Picture * const ref1 = &h->ref_list[1][0];
  93. Picture * const cur = s->current_picture_ptr;
  94. int list, j, field;
  95. int sidx= (s->picture_structure&1)^1;
  96. int ref1sidx = (ref1->f.reference&1)^1;
  97. for(list=0; list<2; list++){
  98. cur->ref_count[sidx][list] = h->ref_count[list];
  99. for(j=0; j<h->ref_count[list]; j++)
  100. cur->ref_poc[sidx][list][j] = 4 * h->ref_list[list][j].frame_num + (h->ref_list[list][j].f.reference & 3);
  101. }
  102. if(s->picture_structure == PICT_FRAME){
  103. memcpy(cur->ref_count[1], cur->ref_count[0], sizeof(cur->ref_count[0]));
  104. memcpy(cur->ref_poc [1], cur->ref_poc [0], sizeof(cur->ref_poc [0]));
  105. }
  106. cur->mbaff= FRAME_MBAFF;
  107. h->col_fieldoff= 0;
  108. if(s->picture_structure == PICT_FRAME){
  109. int cur_poc = s->current_picture_ptr->poc;
  110. int *col_poc = h->ref_list[1]->field_poc;
  111. h->col_parity= (FFABS(col_poc[0] - cur_poc) >= FFABS(col_poc[1] - cur_poc));
  112. ref1sidx=sidx= h->col_parity;
  113. } else if (!(s->picture_structure & h->ref_list[1][0].f.reference) && !h->ref_list[1][0].mbaff) { // FL -> FL & differ parity
  114. h->col_fieldoff = 2 * h->ref_list[1][0].f.reference - 3;
  115. }
  116. if (cur->f.pict_type != AV_PICTURE_TYPE_B || h->direct_spatial_mv_pred)
  117. return;
  118. for(list=0; list<2; list++){
  119. fill_colmap(h, h->map_col_to_list0, list, sidx, ref1sidx, 0);
  120. if(FRAME_MBAFF)
  121. for(field=0; field<2; field++)
  122. fill_colmap(h, h->map_col_to_list0_field[field], list, field, field, 1);
  123. }
  124. }
  125. static void await_reference_mb_row(H264Context * const h, Picture *ref, int mb_y)
  126. {
  127. int ref_field = ref->f.reference - 1;
  128. int ref_field_picture = ref->field_picture;
  129. int ref_height = 16*h->s.mb_height >> ref_field_picture;
  130. if(!HAVE_THREADS || !(h->s.avctx->active_thread_type&FF_THREAD_FRAME))
  131. return;
  132. //FIXME it can be safe to access mb stuff
  133. //even if pixels aren't deblocked yet
  134. ff_thread_await_progress((AVFrame*)ref, 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. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  399. if( (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
  400. && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
  401. && !is_b8x8){
  402. *mb_type |= MB_TYPE_16x8 |MB_TYPE_L0L1|MB_TYPE_DIRECT2; /* B_16x8 */
  403. }else{
  404. *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
  405. }
  406. }else{ // AFR/FR -> AFR/FR
  407. single_col:
  408. mb_type_col[0] =
  409. mb_type_col[1] = h->ref_list[1][0].f.mb_type[mb_xy];
  410. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  411. if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
  412. *mb_type |= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
  413. }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
  414. *mb_type |= MB_TYPE_L0L1|MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
  415. }else{
  416. if(!h->sps.direct_8x8_inference_flag){
  417. /* FIXME save sub mb types from previous frames (or derive from MVs)
  418. * so we know exactly what block size to use */
  419. sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
  420. }
  421. *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
  422. }
  423. }
  424. }
  425. await_reference_mb_row(h, &h->ref_list[1][0], mb_y);
  426. l1mv0 = &h->ref_list[1][0].f.motion_val[0][h->mb2b_xy [mb_xy]];
  427. l1mv1 = &h->ref_list[1][0].f.motion_val[1][h->mb2b_xy [mb_xy]];
  428. l1ref0 = &h->ref_list[1][0].f.ref_index [0][4 * mb_xy];
  429. l1ref1 = &h->ref_list[1][0].f.ref_index [1][4 * mb_xy];
  430. if(!b8_stride){
  431. if(s->mb_y&1){
  432. l1ref0 += 2;
  433. l1ref1 += 2;
  434. l1mv0 += 2*b4_stride;
  435. l1mv1 += 2*b4_stride;
  436. }
  437. }
  438. {
  439. const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
  440. const int *dist_scale_factor = h->dist_scale_factor;
  441. int ref_offset;
  442. if(FRAME_MBAFF && IS_INTERLACED(*mb_type)){
  443. map_col_to_list0[0] = h->map_col_to_list0_field[s->mb_y&1][0];
  444. map_col_to_list0[1] = h->map_col_to_list0_field[s->mb_y&1][1];
  445. dist_scale_factor =h->dist_scale_factor_field[s->mb_y&1];
  446. }
  447. ref_offset = (h->ref_list[1][0].mbaff<<4) & (mb_type_col[0]>>3); //if(h->ref_list[1][0].mbaff && IS_INTERLACED(mb_type_col[0])) ref_offset=16 else 0
  448. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
  449. int y_shift = 2*!IS_INTERLACED(*mb_type);
  450. assert(h->sps.direct_8x8_inference_flag);
  451. for(i8=0; i8<4; i8++){
  452. const int x8 = i8&1;
  453. const int y8 = i8>>1;
  454. int ref0, scale;
  455. const int16_t (*l1mv)[2]= l1mv0;
  456. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  457. continue;
  458. h->sub_mb_type[i8] = sub_mb_type;
  459. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  460. if(IS_INTRA(mb_type_col[y8])){
  461. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  462. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  463. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  464. continue;
  465. }
  466. ref0 = l1ref0[x8 + y8*b8_stride];
  467. if(ref0 >= 0)
  468. ref0 = map_col_to_list0[0][ref0 + ref_offset];
  469. else{
  470. ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];
  471. l1mv= l1mv1;
  472. }
  473. scale = dist_scale_factor[ref0];
  474. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  475. {
  476. const int16_t *mv_col = l1mv[x8*3 + y8*b4_stride];
  477. int my_col = (mv_col[1]<<y_shift)/2;
  478. int mx = (scale * mv_col[0] + 128) >> 8;
  479. int my = (scale * my_col + 128) >> 8;
  480. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  481. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
  482. }
  483. }
  484. return;
  485. }
  486. /* one-to-one mv scaling */
  487. if(IS_16X16(*mb_type)){
  488. int ref, mv0, mv1;
  489. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
  490. if(IS_INTRA(mb_type_col[0])){
  491. ref=mv0=mv1=0;
  492. }else{
  493. const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
  494. : map_col_to_list0[1][l1ref1[0] + ref_offset];
  495. const int scale = dist_scale_factor[ref0];
  496. const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
  497. int mv_l0[2];
  498. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  499. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  500. ref= ref0;
  501. mv0= pack16to32(mv_l0[0],mv_l0[1]);
  502. mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  503. }
  504. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
  505. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
  506. fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
  507. }else{
  508. for(i8=0; i8<4; i8++){
  509. const int x8 = i8&1;
  510. const int y8 = i8>>1;
  511. int ref0, scale;
  512. const int16_t (*l1mv)[2]= l1mv0;
  513. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  514. continue;
  515. h->sub_mb_type[i8] = sub_mb_type;
  516. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  517. if(IS_INTRA(mb_type_col[0])){
  518. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  519. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  520. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  521. continue;
  522. }
  523. assert(b8_stride == 2);
  524. ref0 = l1ref0[i8];
  525. if(ref0 >= 0)
  526. ref0 = map_col_to_list0[0][ref0 + ref_offset];
  527. else{
  528. ref0 = map_col_to_list0[1][l1ref1[i8] + ref_offset];
  529. l1mv= l1mv1;
  530. }
  531. scale = dist_scale_factor[ref0];
  532. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  533. if(IS_SUB_8X8(sub_mb_type)){
  534. const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
  535. int mx = (scale * mv_col[0] + 128) >> 8;
  536. int my = (scale * mv_col[1] + 128) >> 8;
  537. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  538. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
  539. }else
  540. for(i4=0; i4<4; i4++){
  541. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
  542. int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
  543. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  544. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  545. AV_WN32A(h->mv_cache[1][scan8[i8*4+i4]],
  546. pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]));
  547. }
  548. }
  549. }
  550. }
  551. }
  552. void ff_h264_pred_direct_motion(H264Context * const h, int *mb_type){
  553. if(h->direct_spatial_mv_pred){
  554. pred_spatial_direct_motion(h, mb_type);
  555. }else{
  556. pred_temp_direct_motion(h, mb_type);
  557. }
  558. }