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.

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