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.

319 lines
11KB

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