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.

317 lines
11KB

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