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.

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