mpegvideo_xvmc.c 12 KB

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