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.

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