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.

165 lines
6.4KB

  1. #include "h264.h"
  2. #include "thread.h"
  3. static inline int get_lowest_part_list_y(H264Context *h, Picture *pic, int n, int height,
  4. int y_offset, int list){
  5. int raw_my= h->mv_cache[list][ scan8[n] ][1];
  6. int filter_height= (raw_my&3) ? 2 : 0;
  7. int full_my= (raw_my>>2) + y_offset;
  8. int top = full_my - filter_height, bottom = full_my + height + filter_height;
  9. return FFMAX(abs(top), bottom);
  10. }
  11. static inline void get_lowest_part_y(H264Context *h, int refs[2][48], int n, int height,
  12. int y_offset, int list0, int list1, int *nrefs){
  13. MpegEncContext * const s = &h->s;
  14. int my;
  15. y_offset += 16*(s->mb_y >> MB_FIELD);
  16. if(list0){
  17. int ref_n = h->ref_cache[0][ scan8[n] ];
  18. Picture *ref= &h->ref_list[0][ref_n];
  19. // Error resilience puts the current picture in the ref list.
  20. // Don't try to wait on these as it will cause a deadlock.
  21. // Fields can wait on each other, though.
  22. if(ref->thread_opaque != s->current_picture.thread_opaque ||
  23. (ref->reference&3) != s->picture_structure) {
  24. my = get_lowest_part_list_y(h, ref, n, height, y_offset, 0);
  25. if (refs[0][ref_n] < 0) nrefs[0] += 1;
  26. refs[0][ref_n] = FFMAX(refs[0][ref_n], my);
  27. }
  28. }
  29. if(list1){
  30. int ref_n = h->ref_cache[1][ scan8[n] ];
  31. Picture *ref= &h->ref_list[1][ref_n];
  32. if(ref->thread_opaque != s->current_picture.thread_opaque ||
  33. (ref->reference&3) != s->picture_structure) {
  34. my = get_lowest_part_list_y(h, ref, n, height, y_offset, 1);
  35. if (refs[1][ref_n] < 0) nrefs[1] += 1;
  36. refs[1][ref_n] = FFMAX(refs[1][ref_n], my);
  37. }
  38. }
  39. }
  40. /**
  41. * Wait until all reference frames are available for MC operations.
  42. *
  43. * @param h the H264 context
  44. */
  45. static void await_references(H264Context *h){
  46. MpegEncContext * const s = &h->s;
  47. const int mb_xy= h->mb_xy;
  48. const int mb_type= s->current_picture.mb_type[mb_xy];
  49. int refs[2][48];
  50. int nrefs[2] = {0};
  51. int ref, list;
  52. memset(refs, -1, sizeof(refs));
  53. if(IS_16X16(mb_type)){
  54. get_lowest_part_y(h, refs, 0, 16, 0,
  55. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
  56. }else if(IS_16X8(mb_type)){
  57. get_lowest_part_y(h, refs, 0, 8, 0,
  58. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
  59. get_lowest_part_y(h, refs, 8, 8, 8,
  60. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
  61. }else if(IS_8X16(mb_type)){
  62. get_lowest_part_y(h, refs, 0, 16, 0,
  63. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
  64. get_lowest_part_y(h, refs, 4, 16, 0,
  65. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
  66. }else{
  67. int i;
  68. assert(IS_8X8(mb_type));
  69. for(i=0; i<4; i++){
  70. const int sub_mb_type= h->sub_mb_type[i];
  71. const int n= 4*i;
  72. int y_offset= (i&2)<<2;
  73. if(IS_SUB_8X8(sub_mb_type)){
  74. get_lowest_part_y(h, refs, n , 8, y_offset,
  75. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
  76. }else if(IS_SUB_8X4(sub_mb_type)){
  77. get_lowest_part_y(h, refs, n , 4, y_offset,
  78. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
  79. get_lowest_part_y(h, refs, n+2, 4, y_offset+4,
  80. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
  81. }else if(IS_SUB_4X8(sub_mb_type)){
  82. get_lowest_part_y(h, refs, n , 8, y_offset,
  83. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
  84. get_lowest_part_y(h, refs, n+1, 8, y_offset,
  85. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
  86. }else{
  87. int j;
  88. assert(IS_SUB_4X4(sub_mb_type));
  89. for(j=0; j<4; j++){
  90. int sub_y_offset= y_offset + 2*(j&2);
  91. get_lowest_part_y(h, refs, n+j, 4, sub_y_offset,
  92. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
  93. }
  94. }
  95. }
  96. }
  97. for(list=h->list_count-1; list>=0; list--){
  98. for(ref=0; ref<48 && nrefs[list]; ref++){
  99. int row = refs[list][ref];
  100. if(row >= 0){
  101. Picture *ref_pic = &h->ref_list[list][ref];
  102. int ref_field = ref_pic->reference - 1;
  103. int ref_field_picture = ref_pic->field_picture;
  104. int pic_height = 16*s->mb_height >> ref_field_picture;
  105. row <<= MB_MBAFF;
  106. nrefs[list]--;
  107. if(!FIELD_PICTURE && ref_field_picture){ // frame referencing two fields
  108. ff_thread_await_progress((AVFrame*)ref_pic, FFMIN((row >> 1) - !(row&1), pic_height-1), 1);
  109. ff_thread_await_progress((AVFrame*)ref_pic, FFMIN((row >> 1) , pic_height-1), 0);
  110. }else if(FIELD_PICTURE && !ref_field_picture){ // field referencing one field of a frame
  111. ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row*2 + ref_field , pic_height-1), 0);
  112. }else if(FIELD_PICTURE){
  113. ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row, pic_height-1), ref_field);
  114. }else{
  115. ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row, pic_height-1), 0);
  116. }
  117. }
  118. }
  119. }
  120. }
  121. #define FUNC(a) a ## _8
  122. #define PIXEL_SHIFT 0
  123. #include "h264_hl_motion.h"
  124. #undef PIXEL_SHIFT
  125. #undef FUNC
  126. #define FUNC(a) a ## _16
  127. #define PIXEL_SHIFT 1
  128. #include "h264_hl_motion.h"
  129. void ff_hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  130. qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
  131. qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
  132. h264_weight_func *weight_op, h264_biweight_func *weight_avg){
  133. if(h->pixel_shift){
  134. hl_motion_16(h, dest_y, dest_cb, dest_cr,
  135. qpix_put, chroma_put,
  136. qpix_avg, chroma_avg,
  137. weight_op, weight_avg);
  138. }else
  139. hl_motion_8(h, dest_y, dest_cb, dest_cr,
  140. qpix_put, chroma_put,
  141. qpix_avg, chroma_avg,
  142. weight_op, weight_avg);
  143. }