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.

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