h264_direct.c 25 KB

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