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.

291 lines
9.6KB

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