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.

287 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. av_abort();
  99. }
  100. //from MPV_decode_mb(),
  101. /* update DC predictors for P macroblocks */
  102. if (!s->mb_intra) {
  103. s->last_dc[0] =
  104. s->last_dc[1] =
  105. s->last_dc[2] = 128 << s->intra_dc_precision;
  106. }
  107. //MC doesn't skip blocks
  108. s->mb_skiped = 0;
  109. // do I need to export quant when I could not perform postprocessing?
  110. // anyway, it doesn't hurrt
  111. s->current_picture.qscale_table[mb_xy] = s->qscale;
  112. //START OF XVMC specific code
  113. render = (xvmc_render_state_t*)s->current_picture.data[2];
  114. assert(render!=NULL);
  115. assert(render->magic==MP_XVMC_RENDER_MAGIC);
  116. assert(render->mv_blocks);
  117. //take the next free macroblock
  118. mv_block = &render->mv_blocks[render->start_mv_blocks_num +
  119. render->filled_mv_blocks_num ];
  120. // memset(mv_block,0,sizeof(XvMCMacroBlock));
  121. mv_block->x = s->mb_x;
  122. mv_block->y = s->mb_y;
  123. mv_block->dct_type = s->interlaced_dct;//XVMC_DCT_TYPE_FRAME/FIELD;
  124. // mv_block->motion_type = 0; //zero to silense warnings
  125. if(s->mb_intra){
  126. mv_block->macroblock_type = XVMC_MB_TYPE_INTRA;//no MC, all done
  127. }else{
  128. mv_block->macroblock_type = XVMC_MB_TYPE_PATTERN;
  129. if(s->mv_dir & MV_DIR_FORWARD){
  130. mv_block->macroblock_type|= XVMC_MB_TYPE_MOTION_FORWARD;
  131. //pmv[n][dir][xy]=mv[dir][n][xy]
  132. mv_block->PMV[0][0][0] = s->mv[0][0][0];
  133. mv_block->PMV[0][0][1] = s->mv[0][0][1];
  134. mv_block->PMV[1][0][0] = s->mv[0][1][0];
  135. mv_block->PMV[1][0][1] = s->mv[0][1][1];
  136. }
  137. if(s->mv_dir & MV_DIR_BACKWARD){
  138. mv_block->macroblock_type|=XVMC_MB_TYPE_MOTION_BACKWARD;
  139. mv_block->PMV[0][1][0] = s->mv[1][0][0];
  140. mv_block->PMV[0][1][1] = s->mv[1][0][1];
  141. mv_block->PMV[1][1][0] = s->mv[1][1][0];
  142. mv_block->PMV[1][1][1] = s->mv[1][1][1];
  143. }
  144. switch(s->mv_type){
  145. case MV_TYPE_16X16:
  146. mv_block->motion_type = XVMC_PREDICTION_FRAME;
  147. break;
  148. case MV_TYPE_16X8:
  149. mv_block->motion_type = XVMC_PREDICTION_16x8;
  150. break;
  151. case MV_TYPE_FIELD:
  152. mv_block->motion_type = XVMC_PREDICTION_FIELD;
  153. if(s->picture_structure == PICT_FRAME){
  154. mv_block->PMV[0][0][1]<<=1;
  155. mv_block->PMV[1][0][1]<<=1;
  156. mv_block->PMV[0][1][1]<<=1;
  157. mv_block->PMV[1][1][1]<<=1;
  158. }
  159. break;
  160. case MV_TYPE_DMV:
  161. mv_block->motion_type = XVMC_PREDICTION_DUAL_PRIME;
  162. if(s->picture_structure == PICT_FRAME){
  163. mv_block->PMV[0][0][0] = s->mv[0][0][0];//top from top
  164. mv_block->PMV[0][0][1] = s->mv[0][0][1]<<1;
  165. mv_block->PMV[0][1][0] = s->mv[0][0][0];//bottom from bottom
  166. mv_block->PMV[0][1][1] = s->mv[0][0][1]<<1;
  167. mv_block->PMV[1][0][0] = s->mv[0][2][0];//dmv00, top from bottom
  168. mv_block->PMV[1][0][1] = s->mv[0][2][1]<<1;//dmv01
  169. mv_block->PMV[1][1][0] = s->mv[0][3][0];//dmv10, bottom from top
  170. mv_block->PMV[1][1][1] = s->mv[0][3][1]<<1;//dmv11
  171. }else{
  172. mv_block->PMV[0][1][0] = s->mv[0][2][0];//dmv00
  173. mv_block->PMV[0][1][1] = s->mv[0][2][1];//dmv01
  174. }
  175. break;
  176. default:
  177. assert(0);
  178. }
  179. mv_block->motion_vertical_field_select = 0;
  180. //set correct field referenses
  181. if(s->mv_type == MV_TYPE_FIELD || s->mv_type == MV_TYPE_16X8){
  182. if( s->field_select[0][0] ) mv_block->motion_vertical_field_select|=1;
  183. if( s->field_select[1][0] ) mv_block->motion_vertical_field_select|=2;
  184. if( s->field_select[0][1] ) mv_block->motion_vertical_field_select|=4;
  185. if( s->field_select[1][1] ) mv_block->motion_vertical_field_select|=8;
  186. }
  187. }//!intra
  188. //time to handle data blocks;
  189. mv_block->index = render->next_free_data_block_num;
  190. blocks_per_mb = 6;
  191. /*
  192. switch( s->chroma_format){
  193. case CHROMA_422:
  194. blocks_per_mb = 8;
  195. break;
  196. case CHROMA_444:
  197. blocks_per_mb = 12;
  198. break;
  199. }
  200. */
  201. if(s->flags & CODEC_FLAG_GRAY){
  202. if(s->mb_intra){//intra frames are alwasy full chroma block
  203. memset(block[4],0,sizeof(short)*8*8);//so we need to clear them
  204. memset(block[5],0,sizeof(short)*8*8);
  205. if(!render->unsigned_intra)
  206. block[4][0] = block[5][0] = 1<<10;
  207. }
  208. else
  209. blocks_per_mb = 4;//Luminance blocks only
  210. };
  211. cbp = calc_cbp(s,blocks_per_mb);
  212. mv_block->coded_block_pattern = cbp;
  213. if(cbp == 0)
  214. mv_block->macroblock_type &= ~XVMC_MB_TYPE_PATTERN;
  215. for(i=0; i<blocks_per_mb; i++){
  216. if(s->block_last_index[i] >= 0){
  217. // i do not have unsigned_intra MOCO to test, hope it is OK
  218. if( (s->mb_intra) && ( render->idct || (!render->idct && !render->unsigned_intra)) )
  219. block[i][0]-=1<<10;
  220. if(!render->idct){
  221. s->dsp.idct(block[i]);
  222. //!!TODO!clip!!!
  223. }
  224. //TODO:avoid block copy by modifying s->block pointer
  225. memcpy(&render->data_blocks[(render->next_free_data_block_num++)*64],
  226. block[i],sizeof(short)*8*8);
  227. }
  228. }
  229. render->filled_mv_blocks_num++;
  230. assert(render->filled_mv_blocks_num <= render->total_number_of_mv_blocks);
  231. assert(render->next_free_data_block_num <= render->total_number_of_data_blocks);
  232. if(render->filled_mv_blocks_num >= render->total_number_of_mv_blocks)
  233. ff_draw_horiz_band(s,0,0);
  234. // DumpRenderInfo(render);
  235. // DumpMBlockInfo(mv_block);
  236. }
  237. #endif