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.

302 lines
10KB

  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 libavcodec/xvmc.h
  29. //by replacing it with non-X one
  30. //XvMC emulation could be performed
  31. #include "xvmc.h"
  32. //set s->block
  33. void XVMC_init_block(MpegEncContext *s)
  34. {
  35. struct xvmc_render_state * render;
  36. render = (struct xvmc_render_state*)s->current_picture.data[2];
  37. assert(render);
  38. if (!render || (render->magic != AV_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. {
  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. }
  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. {
  63. struct xvmc_render_state * render, * last, * next;
  64. assert(avctx);
  65. render = (struct xvmc_render_state*)s->current_picture.data[2];
  66. assert(render);
  67. if (!render || (render->magic != AV_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 = (struct xvmc_render_state*)s->next_picture.data[2];
  80. assert(next);
  81. if (!next)
  82. return -1;
  83. if (next->magic != AV_XVMC_RENDER_MAGIC)
  84. return -1;
  85. render->p_future_surface = next->p_surface;
  86. //no return here, going to set forward prediction
  87. case FF_P_TYPE:
  88. last = (struct xvmc_render_state*)s->last_picture.data[2];
  89. if (!last)// && !s->first_field)
  90. last = render;//predict second field from the first
  91. if (last->magic != AV_XVMC_RENDER_MAGIC)
  92. return -1;
  93. render->p_past_surface = last->p_surface;
  94. return 0;
  95. }
  96. return -1;
  97. }
  98. void XVMC_field_end(MpegEncContext *s)
  99. {
  100. struct xvmc_render_state * render;
  101. render = (struct xvmc_render_state*)s->current_picture.data[2];
  102. assert(render);
  103. if (render->filled_mv_blocks_num > 0)
  104. ff_draw_horiz_band(s,0,0);
  105. }
  106. void XVMC_decode_mb(MpegEncContext *s)
  107. {
  108. XvMCMacroBlock * mv_block;
  109. struct xvmc_render_state * render;
  110. int i,cbp,blocks_per_mb;
  111. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  112. if (s->encoding) {
  113. av_log(s->avctx, AV_LOG_ERROR, "XVMC doesn't support encoding!!!\n");
  114. return;
  115. }
  116. //from MPV_decode_mb(),
  117. /* update DC predictors for P macroblocks */
  118. if (!s->mb_intra) {
  119. s->last_dc[0] =
  120. s->last_dc[1] =
  121. s->last_dc[2] = 128 << s->intra_dc_precision;
  122. }
  123. //MC doesn't skip blocks
  124. s->mb_skipped = 0;
  125. // Do I need to export quant when I could not perform postprocessing?
  126. // Anyway, it doesn't hurt.
  127. s->current_picture.qscale_table[mb_xy] = s->qscale;
  128. //START OF XVMC specific code
  129. render = (struct xvmc_render_state*)s->current_picture.data[2];
  130. assert(render);
  131. assert(render->magic==AV_XVMC_RENDER_MAGIC);
  132. assert(render->mv_blocks);
  133. //take the next free macroblock
  134. mv_block = &render->mv_blocks[render->start_mv_blocks_num +
  135. render->filled_mv_blocks_num ];
  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. 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])
  197. mv_block->motion_vertical_field_select |= 1;
  198. if (s->field_select[1][0])
  199. mv_block->motion_vertical_field_select |= 2;
  200. if (s->field_select[0][1])
  201. mv_block->motion_vertical_field_select |= 4;
  202. if (s->field_select[1][1])
  203. 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 always 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. }
  248. render->next_free_data_block_num++;
  249. }
  250. }
  251. render->filled_mv_blocks_num++;
  252. assert(render->filled_mv_blocks_num <= render->total_number_of_mv_blocks);
  253. assert(render->next_free_data_block_num <= render->total_number_of_data_blocks);
  254. if (render->filled_mv_blocks_num >= render->total_number_of_mv_blocks)
  255. ff_draw_horiz_band(s,0,0);
  256. }