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.

337 lines
12KB

  1. /*
  2. * XVideo Motion Compensation
  3. * Copyright (c) 2003 Ivan Kalvachev
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 <X11/extensions/XvMC.h>
  23. #include "avcodec.h"
  24. #include "mpegvideo.h"
  25. #undef NDEBUG
  26. #include <assert.h>
  27. #include "xvmc.h"
  28. #include "xvmc_internal.h"
  29. #include "version.h"
  30. #if FF_API_XVMC
  31. /**
  32. * Initialize the block field of the MpegEncContext pointer passed as
  33. * parameter after making sure that the data is not corrupted.
  34. * In order to implement something like direct rendering instead of decoding
  35. * coefficients in s->blocks and then copying them, copy them directly
  36. * into the data_blocks array provided by xvmc.
  37. */
  38. void ff_xvmc_init_block(MpegEncContext *s)
  39. {
  40. struct xvmc_pix_fmt *render = (struct xvmc_pix_fmt*)s->current_picture.f.data[2];
  41. assert(render && render->xvmc_id == AV_XVMC_ID);
  42. s->block = (int16_t (*)[64])(render->data_blocks + render->next_free_data_block_num * 64);
  43. }
  44. /**
  45. * Fill individual block pointers, so there are no gaps in the data_block array
  46. * in case not all blocks in the macroblock are coded.
  47. */
  48. void ff_xvmc_pack_pblocks(MpegEncContext *s, int cbp)
  49. {
  50. int i, j = 0;
  51. const int mb_block_count = 4 + (1 << s->chroma_format);
  52. cbp <<= 12-mb_block_count;
  53. for (i = 0; i < mb_block_count; i++) {
  54. if (cbp & (1 << 11))
  55. s->pblocks[i] = &s->block[j++];
  56. else
  57. s->pblocks[i] = NULL;
  58. cbp += cbp;
  59. }
  60. }
  61. /**
  62. * Find and store the surfaces that are used as reference frames.
  63. * This function should be called for every new field and/or frame.
  64. * It should be safe to call the function a few times for the same field.
  65. */
  66. int ff_xvmc_field_start(MpegEncContext *s, AVCodecContext *avctx)
  67. {
  68. struct xvmc_pix_fmt *last, *next, *render = (struct xvmc_pix_fmt*)s->current_picture.f.data[2];
  69. const int mb_block_count = 4 + (1 << s->chroma_format);
  70. assert(avctx);
  71. if (!render || render->xvmc_id != AV_XVMC_ID ||
  72. !render->data_blocks || !render->mv_blocks ||
  73. (unsigned int)render->allocated_mv_blocks > INT_MAX/(64*6) ||
  74. (unsigned int)render->allocated_data_blocks > INT_MAX/64 ||
  75. !render->p_surface) {
  76. av_log(avctx, AV_LOG_ERROR,
  77. "Render token doesn't look as expected.\n");
  78. return -1; // make sure that this is a render packet
  79. }
  80. if (render->filled_mv_blocks_num) {
  81. av_log(avctx, AV_LOG_ERROR,
  82. "Rendering surface contains %i unprocessed blocks.\n",
  83. render->filled_mv_blocks_num);
  84. return -1;
  85. }
  86. if (render->allocated_mv_blocks < 1 ||
  87. render->allocated_data_blocks < render->allocated_mv_blocks*mb_block_count ||
  88. render->start_mv_blocks_num >= render->allocated_mv_blocks ||
  89. render->next_free_data_block_num >
  90. render->allocated_data_blocks -
  91. mb_block_count*(render->allocated_mv_blocks-render->start_mv_blocks_num)) {
  92. av_log(avctx, AV_LOG_ERROR,
  93. "Rendering surface doesn't provide enough block structures to work with.\n");
  94. return -1;
  95. }
  96. render->picture_structure = s->picture_structure;
  97. render->flags = s->first_field ? 0 : XVMC_SECOND_FIELD;
  98. render->p_future_surface = NULL;
  99. render->p_past_surface = NULL;
  100. switch(s->pict_type) {
  101. case AV_PICTURE_TYPE_I:
  102. return 0; // no prediction from other frames
  103. case AV_PICTURE_TYPE_B:
  104. next = (struct xvmc_pix_fmt*)s->next_picture.f.data[2];
  105. if (!next)
  106. return -1;
  107. if (next->xvmc_id != AV_XVMC_ID)
  108. return -1;
  109. render->p_future_surface = next->p_surface;
  110. // no return here, going to set forward prediction
  111. case AV_PICTURE_TYPE_P:
  112. last = (struct xvmc_pix_fmt*)s->last_picture.f.data[2];
  113. if (!last)
  114. last = render; // predict second field from the first
  115. if (last->xvmc_id != AV_XVMC_ID)
  116. return -1;
  117. render->p_past_surface = last->p_surface;
  118. return 0;
  119. }
  120. return -1;
  121. }
  122. /**
  123. * Complete frame/field rendering by passing any remaining blocks.
  124. * Normally ff_draw_horiz_band() is called for each slice, however,
  125. * some leftover blocks, for example from error_resilience(), may remain.
  126. * It should be safe to call the function a few times for the same field.
  127. */
  128. void ff_xvmc_field_end(MpegEncContext *s)
  129. {
  130. struct xvmc_pix_fmt *render = (struct xvmc_pix_fmt*)s->current_picture.f.data[2];
  131. assert(render);
  132. if (render->filled_mv_blocks_num > 0)
  133. ff_mpeg_draw_horiz_band(s, 0, 0);
  134. }
  135. /**
  136. * Synthesize the data needed by XvMC to render one macroblock of data.
  137. * Fill all relevant fields, if necessary do IDCT.
  138. */
  139. void ff_xvmc_decode_mb(MpegEncContext *s)
  140. {
  141. XvMCMacroBlock *mv_block;
  142. struct xvmc_pix_fmt *render;
  143. int i, cbp, blocks_per_mb;
  144. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  145. if (s->encoding) {
  146. av_log(s->avctx, AV_LOG_ERROR, "XVMC doesn't support encoding!!!\n");
  147. return;
  148. }
  149. // from MPV_decode_mb(), update DC predictors for P macroblocks
  150. if (!s->mb_intra) {
  151. s->last_dc[0] =
  152. s->last_dc[1] =
  153. s->last_dc[2] = 128 << s->intra_dc_precision;
  154. }
  155. // MC doesn't skip blocks
  156. s->mb_skipped = 0;
  157. // Do I need to export quant when I could not perform postprocessing?
  158. // Anyway, it doesn't hurt.
  159. s->current_picture.qscale_table[mb_xy] = s->qscale;
  160. // start of XVMC-specific code
  161. render = (struct xvmc_pix_fmt*)s->current_picture.f.data[2];
  162. assert(render);
  163. assert(render->xvmc_id == AV_XVMC_ID);
  164. assert(render->mv_blocks);
  165. // take the next free macroblock
  166. mv_block = &render->mv_blocks[render->start_mv_blocks_num +
  167. render->filled_mv_blocks_num];
  168. mv_block->x = s->mb_x;
  169. mv_block->y = s->mb_y;
  170. mv_block->dct_type = s->interlaced_dct; // XVMC_DCT_TYPE_FRAME/FIELD;
  171. if (s->mb_intra) {
  172. mv_block->macroblock_type = XVMC_MB_TYPE_INTRA; // no MC, all done
  173. } else {
  174. mv_block->macroblock_type = XVMC_MB_TYPE_PATTERN;
  175. if (s->mv_dir & MV_DIR_FORWARD) {
  176. mv_block->macroblock_type |= XVMC_MB_TYPE_MOTION_FORWARD;
  177. // PMV[n][dir][xy] = mv[dir][n][xy]
  178. mv_block->PMV[0][0][0] = s->mv[0][0][0];
  179. mv_block->PMV[0][0][1] = s->mv[0][0][1];
  180. mv_block->PMV[1][0][0] = s->mv[0][1][0];
  181. mv_block->PMV[1][0][1] = s->mv[0][1][1];
  182. }
  183. if (s->mv_dir & MV_DIR_BACKWARD) {
  184. mv_block->macroblock_type |= XVMC_MB_TYPE_MOTION_BACKWARD;
  185. mv_block->PMV[0][1][0] = s->mv[1][0][0];
  186. mv_block->PMV[0][1][1] = s->mv[1][0][1];
  187. mv_block->PMV[1][1][0] = s->mv[1][1][0];
  188. mv_block->PMV[1][1][1] = s->mv[1][1][1];
  189. }
  190. switch(s->mv_type) {
  191. case MV_TYPE_16X16:
  192. mv_block->motion_type = XVMC_PREDICTION_FRAME;
  193. break;
  194. case MV_TYPE_16X8:
  195. mv_block->motion_type = XVMC_PREDICTION_16x8;
  196. break;
  197. case MV_TYPE_FIELD:
  198. mv_block->motion_type = XVMC_PREDICTION_FIELD;
  199. if (s->picture_structure == PICT_FRAME) {
  200. mv_block->PMV[0][0][1] <<= 1;
  201. mv_block->PMV[1][0][1] <<= 1;
  202. mv_block->PMV[0][1][1] <<= 1;
  203. mv_block->PMV[1][1][1] <<= 1;
  204. }
  205. break;
  206. case MV_TYPE_DMV:
  207. mv_block->motion_type = XVMC_PREDICTION_DUAL_PRIME;
  208. if (s->picture_structure == PICT_FRAME) {
  209. mv_block->PMV[0][0][0] = s->mv[0][0][0]; // top from top
  210. mv_block->PMV[0][0][1] = s->mv[0][0][1] << 1;
  211. mv_block->PMV[0][1][0] = s->mv[0][0][0]; // bottom from bottom
  212. mv_block->PMV[0][1][1] = s->mv[0][0][1] << 1;
  213. mv_block->PMV[1][0][0] = s->mv[0][2][0]; // dmv00, top from bottom
  214. mv_block->PMV[1][0][1] = s->mv[0][2][1] << 1; // dmv01
  215. mv_block->PMV[1][1][0] = s->mv[0][3][0]; // dmv10, bottom from top
  216. mv_block->PMV[1][1][1] = s->mv[0][3][1] << 1; // dmv11
  217. } else {
  218. mv_block->PMV[0][1][0] = s->mv[0][2][0]; // dmv00
  219. mv_block->PMV[0][1][1] = s->mv[0][2][1]; // dmv01
  220. }
  221. break;
  222. default:
  223. assert(0);
  224. }
  225. mv_block->motion_vertical_field_select = 0;
  226. // set correct field references
  227. if (s->mv_type == MV_TYPE_FIELD || s->mv_type == MV_TYPE_16X8) {
  228. mv_block->motion_vertical_field_select |= s->field_select[0][0];
  229. mv_block->motion_vertical_field_select |= s->field_select[1][0] << 1;
  230. mv_block->motion_vertical_field_select |= s->field_select[0][1] << 2;
  231. mv_block->motion_vertical_field_select |= s->field_select[1][1] << 3;
  232. }
  233. } // !intra
  234. // time to handle data blocks
  235. mv_block->index = render->next_free_data_block_num;
  236. blocks_per_mb = 6;
  237. if (s->chroma_format >= 2) {
  238. blocks_per_mb = 4 + (1 << s->chroma_format);
  239. }
  240. // calculate cbp
  241. cbp = 0;
  242. for (i = 0; i < blocks_per_mb; i++) {
  243. cbp += cbp;
  244. if (s->block_last_index[i] >= 0)
  245. cbp++;
  246. }
  247. if (s->flags & CODEC_FLAG_GRAY) {
  248. if (s->mb_intra) { // intra frames are always full chroma blocks
  249. for (i = 4; i < blocks_per_mb; i++) {
  250. memset(s->pblocks[i], 0, sizeof(*s->pblocks[i])); // so we need to clear them
  251. if (!render->unsigned_intra)
  252. *s->pblocks[i][0] = 1 << 10;
  253. }
  254. } else {
  255. cbp &= 0xf << (blocks_per_mb - 4);
  256. blocks_per_mb = 4; // luminance blocks only
  257. }
  258. }
  259. mv_block->coded_block_pattern = cbp;
  260. if (cbp == 0)
  261. mv_block->macroblock_type &= ~XVMC_MB_TYPE_PATTERN;
  262. for (i = 0; i < blocks_per_mb; i++) {
  263. if (s->block_last_index[i] >= 0) {
  264. // I do not have unsigned_intra MOCO to test, hope it is OK.
  265. if (s->mb_intra && (render->idct || !render->unsigned_intra))
  266. *s->pblocks[i][0] -= 1 << 10;
  267. if (!render->idct) {
  268. s->dsp.idct(*s->pblocks[i]);
  269. /* It is unclear if MC hardware requires pixel diff values to be
  270. * in the range [-255;255]. TODO: Clipping if such hardware is
  271. * ever found. As of now it would only be an unnecessary
  272. * slowdown. */
  273. }
  274. // copy blocks only if the codec doesn't support pblocks reordering
  275. if (s->avctx->xvmc_acceleration == 1) {
  276. memcpy(&render->data_blocks[render->next_free_data_block_num*64],
  277. s->pblocks[i], sizeof(*s->pblocks[i]));
  278. }
  279. render->next_free_data_block_num++;
  280. }
  281. }
  282. render->filled_mv_blocks_num++;
  283. assert(render->filled_mv_blocks_num <= render->allocated_mv_blocks);
  284. assert(render->next_free_data_block_num <= render->allocated_data_blocks);
  285. /* The above conditions should not be able to fail as long as this function
  286. * is used and the following 'if ()' automatically calls a callback to free
  287. * blocks. */
  288. if (render->filled_mv_blocks_num == render->allocated_mv_blocks)
  289. ff_mpeg_draw_horiz_band(s, 0, 0);
  290. }
  291. #endif /* FF_API_XVMC */