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.

288 lines
9.5KB

  1. /*
  2. * XVideo Motion Compensation
  3. * Copyright (c) 2003 Ivan Kalvachev
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <limits.h>
  20. //avcodec include
  21. #include "avcodec.h"
  22. #include "dsputil.h"
  23. #include "mpegvideo.h"
  24. #undef NDEBUG
  25. #include <assert.h>
  26. #ifdef USE_FASTMEMCPY
  27. #include "fastmemcpy.h"
  28. #endif
  29. #ifdef HAVE_XVMC
  30. //X11 includes are in the xvmc_render.h
  31. //by replacing it with none-X one
  32. //XvMC emulation could be performed
  33. #include "xvmc_render.h"
  34. //#include "xvmc_debug.h"
  35. static int calc_cbp(MpegEncContext *s, int blocknum){
  36. /* compute cbp */
  37. // for I420 bit_offset=5
  38. int i,cbp = 0;
  39. for(i=0; i<blocknum; i++) {
  40. if(s->block_last_index[i] >= 0)
  41. cbp |= 1 << (5 - i);
  42. }
  43. return cbp;
  44. }
  45. //these functions should be called on every new field or/and frame
  46. //They should be safe if they are called few times for same field!
  47. int XVMC_field_start(MpegEncContext*s, AVCodecContext *avctx){
  48. xvmc_render_state_t * render,* last, * next;
  49. assert(avctx != NULL);
  50. render = (xvmc_render_state_t*)s->current_picture.data[2];
  51. assert(render != NULL);
  52. if( (render == NULL) || (render->magic != MP_XVMC_RENDER_MAGIC) )
  53. return -1;//make sure that this is render packet
  54. render->picture_structure = s->picture_structure;
  55. render->flags = (s->first_field)? 0: XVMC_SECOND_FIELD;
  56. //make sure that all data is drawn by XVMC_end_frame
  57. assert(render->filled_mv_blocks_num==0);
  58. render->p_future_surface = NULL;
  59. render->p_past_surface = NULL;
  60. switch(s->pict_type){
  61. case I_TYPE:
  62. return 0;// no prediction from other frames
  63. case B_TYPE:
  64. next = (xvmc_render_state_t*)s->next_picture.data[2];
  65. assert(next!=NULL);
  66. assert(next->state & MP_XVMC_STATE_PREDICTION);
  67. if(next == NULL) return -1;
  68. if(next->magic != MP_XVMC_RENDER_MAGIC) return -1;
  69. render->p_future_surface = next->p_surface;
  70. //no return here, going to set forward prediction
  71. case P_TYPE:
  72. last = (xvmc_render_state_t*)s->last_picture.data[2];
  73. if(last == NULL)// && !s->first_field)
  74. last = render;//predict second field from the first
  75. if(last->magic != MP_XVMC_RENDER_MAGIC) return -1;
  76. assert(last->state & MP_XVMC_STATE_PREDICTION);
  77. render->p_past_surface = last->p_surface;
  78. return 0;
  79. }
  80. return -1;
  81. }
  82. void XVMC_field_end(MpegEncContext *s){
  83. xvmc_render_state_t * render;
  84. render = (xvmc_render_state_t*)s->current_picture.data[2];
  85. assert(render != NULL);
  86. if(render->filled_mv_blocks_num > 0){
  87. // printf("xvmcvideo.c: rendering %d left blocks after last slice!!!\n",render->filled_mv_blocks_num );
  88. ff_draw_horiz_band(s,0,0);
  89. }
  90. }
  91. void XVMC_decode_mb(MpegEncContext *s, DCTELEM block[6][64]){
  92. XvMCMacroBlock * mv_block;
  93. xvmc_render_state_t * render;
  94. int i,cbp,blocks_per_mb;
  95. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  96. if(s->encoding){
  97. fprintf(stderr,"XVMC doesn't support encoding!!!\n");
  98. assert(0);
  99. return;
  100. }
  101. //from MPV_decode_mb(),
  102. /* update DC predictors for P macroblocks */
  103. if (!s->mb_intra) {
  104. s->last_dc[0] =
  105. s->last_dc[1] =
  106. s->last_dc[2] = 128 << s->intra_dc_precision;
  107. }
  108. //MC doesn't skip blocks
  109. s->mb_skiped = 0;
  110. // do I need to export quant when I could not perform postprocessing?
  111. // anyway, it doesn't hurrt
  112. s->current_picture.qscale_table[mb_xy] = s->qscale;
  113. //START OF XVMC specific code
  114. render = (xvmc_render_state_t*)s->current_picture.data[2];
  115. assert(render!=NULL);
  116. assert(render->magic==MP_XVMC_RENDER_MAGIC);
  117. assert(render->mv_blocks);
  118. //take the next free macroblock
  119. mv_block = &render->mv_blocks[render->start_mv_blocks_num +
  120. render->filled_mv_blocks_num ];
  121. // memset(mv_block,0,sizeof(XvMCMacroBlock));
  122. mv_block->x = s->mb_x;
  123. mv_block->y = s->mb_y;
  124. mv_block->dct_type = s->interlaced_dct;//XVMC_DCT_TYPE_FRAME/FIELD;
  125. // mv_block->motion_type = 0; //zero to silense warnings
  126. if(s->mb_intra){
  127. mv_block->macroblock_type = XVMC_MB_TYPE_INTRA;//no MC, all done
  128. }else{
  129. mv_block->macroblock_type = XVMC_MB_TYPE_PATTERN;
  130. if(s->mv_dir & MV_DIR_FORWARD){
  131. mv_block->macroblock_type|= XVMC_MB_TYPE_MOTION_FORWARD;
  132. //pmv[n][dir][xy]=mv[dir][n][xy]
  133. mv_block->PMV[0][0][0] = s->mv[0][0][0];
  134. mv_block->PMV[0][0][1] = s->mv[0][0][1];
  135. mv_block->PMV[1][0][0] = s->mv[0][1][0];
  136. mv_block->PMV[1][0][1] = s->mv[0][1][1];
  137. }
  138. if(s->mv_dir & MV_DIR_BACKWARD){
  139. mv_block->macroblock_type|=XVMC_MB_TYPE_MOTION_BACKWARD;
  140. mv_block->PMV[0][1][0] = s->mv[1][0][0];
  141. mv_block->PMV[0][1][1] = s->mv[1][0][1];
  142. mv_block->PMV[1][1][0] = s->mv[1][1][0];
  143. mv_block->PMV[1][1][1] = s->mv[1][1][1];
  144. }
  145. switch(s->mv_type){
  146. case MV_TYPE_16X16:
  147. mv_block->motion_type = XVMC_PREDICTION_FRAME;
  148. break;
  149. case MV_TYPE_16X8:
  150. mv_block->motion_type = XVMC_PREDICTION_16x8;
  151. break;
  152. case MV_TYPE_FIELD:
  153. mv_block->motion_type = XVMC_PREDICTION_FIELD;
  154. if(s->picture_structure == PICT_FRAME){
  155. mv_block->PMV[0][0][1]<<=1;
  156. mv_block->PMV[1][0][1]<<=1;
  157. mv_block->PMV[0][1][1]<<=1;
  158. mv_block->PMV[1][1][1]<<=1;
  159. }
  160. break;
  161. case MV_TYPE_DMV:
  162. mv_block->motion_type = XVMC_PREDICTION_DUAL_PRIME;
  163. if(s->picture_structure == PICT_FRAME){
  164. mv_block->PMV[0][0][0] = s->mv[0][0][0];//top from top
  165. mv_block->PMV[0][0][1] = s->mv[0][0][1]<<1;
  166. mv_block->PMV[0][1][0] = s->mv[0][0][0];//bottom from bottom
  167. mv_block->PMV[0][1][1] = s->mv[0][0][1]<<1;
  168. mv_block->PMV[1][0][0] = s->mv[0][2][0];//dmv00, top from bottom
  169. mv_block->PMV[1][0][1] = s->mv[0][2][1]<<1;//dmv01
  170. mv_block->PMV[1][1][0] = s->mv[0][3][0];//dmv10, bottom from top
  171. mv_block->PMV[1][1][1] = s->mv[0][3][1]<<1;//dmv11
  172. }else{
  173. mv_block->PMV[0][1][0] = s->mv[0][2][0];//dmv00
  174. mv_block->PMV[0][1][1] = s->mv[0][2][1];//dmv01
  175. }
  176. break;
  177. default:
  178. assert(0);
  179. }
  180. mv_block->motion_vertical_field_select = 0;
  181. //set correct field referenses
  182. if(s->mv_type == MV_TYPE_FIELD || s->mv_type == MV_TYPE_16X8){
  183. if( s->field_select[0][0] ) mv_block->motion_vertical_field_select|=1;
  184. if( s->field_select[1][0] ) mv_block->motion_vertical_field_select|=2;
  185. if( s->field_select[0][1] ) mv_block->motion_vertical_field_select|=4;
  186. if( s->field_select[1][1] ) mv_block->motion_vertical_field_select|=8;
  187. }
  188. }//!intra
  189. //time to handle data blocks;
  190. mv_block->index = render->next_free_data_block_num;
  191. blocks_per_mb = 6;
  192. /*
  193. switch( s->chroma_format){
  194. case CHROMA_422:
  195. blocks_per_mb = 8;
  196. break;
  197. case CHROMA_444:
  198. blocks_per_mb = 12;
  199. break;
  200. }
  201. */
  202. if(s->flags & CODEC_FLAG_GRAY){
  203. if(s->mb_intra){//intra frames are alwasy full chroma block
  204. memset(block[4],0,sizeof(short)*8*8);//so we need to clear them
  205. memset(block[5],0,sizeof(short)*8*8);
  206. if(!render->unsigned_intra)
  207. block[4][0] = block[5][0] = 1<<10;
  208. }
  209. else
  210. blocks_per_mb = 4;//Luminance blocks only
  211. };
  212. cbp = calc_cbp(s,blocks_per_mb);
  213. mv_block->coded_block_pattern = cbp;
  214. if(cbp == 0)
  215. mv_block->macroblock_type &= ~XVMC_MB_TYPE_PATTERN;
  216. for(i=0; i<blocks_per_mb; i++){
  217. if(s->block_last_index[i] >= 0){
  218. // i do not have unsigned_intra MOCO to test, hope it is OK
  219. if( (s->mb_intra) && ( render->idct || (!render->idct && !render->unsigned_intra)) )
  220. block[i][0]-=1<<10;
  221. if(!render->idct){
  222. s->dsp.idct(block[i]);
  223. //!!TODO!clip!!!
  224. }
  225. //TODO:avoid block copy by modifying s->block pointer
  226. memcpy(&render->data_blocks[(render->next_free_data_block_num++)*64],
  227. block[i],sizeof(short)*8*8);
  228. }
  229. }
  230. render->filled_mv_blocks_num++;
  231. assert(render->filled_mv_blocks_num <= render->total_number_of_mv_blocks);
  232. assert(render->next_free_data_block_num <= render->total_number_of_data_blocks);
  233. if(render->filled_mv_blocks_num >= render->total_number_of_mv_blocks)
  234. ff_draw_horiz_band(s,0,0);
  235. // DumpRenderInfo(render);
  236. // DumpMBlockInfo(mv_block);
  237. }
  238. #endif