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.

324 lines
11KB

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