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.

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