FFmpeg
mpegvideo_xvmc.c
Go to the documentation of this file.
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 
22 #include <limits.h>
23 #include <X11/extensions/XvMC.h>
24 
25 #include "avcodec.h"
26 #include "mpegutils.h"
27 #include "mpegvideo.h"
28 #include "mpegvideodec.h"
29 
30 #undef NDEBUG
31 #include <assert.h>
32 
33 #include "xvmc.h"
34 #include "xvmc_internal.h"
35 #include "version.h"
36 
37 /**
38  * Initialize the block field of the MpegEncContext pointer passed as
39  * parameter after making sure that the data is not corrupted.
40  * In order to implement something like direct rendering instead of decoding
41  * coefficients in s->blocks and then copying them, copy them directly
42  * into the data_blocks array provided by xvmc.
43  */
45 {
46  struct xvmc_pix_fmt *render = (struct xvmc_pix_fmt*)s->current_picture.f->data[2];
47  assert(render && render->xvmc_id == AV_XVMC_ID);
48 
49  s->block = (int16_t (*)[64])(render->data_blocks + render->next_free_data_block_num * 64);
50 }
51 
53 {
54  int16_t (*tmp)[64];
55 
56  tmp = s->pblocks[4];
57  s->pblocks[4] = s->pblocks[5];
58  s->pblocks[5] = tmp;
59 }
60 
61 /**
62  * Fill individual block pointers, so there are no gaps in the data_block array
63  * in case not all blocks in the macroblock are coded.
64  */
66 {
67  int i, j = 0;
68  const int mb_block_count = 4 + (1 << s->chroma_format);
69 
70  cbp <<= 12-mb_block_count;
71  for (i = 0; i < mb_block_count; i++) {
72  if (cbp & (1 << 11))
73  s->pblocks[i] = &s->block[j++];
74  else
75  s->pblocks[i] = NULL;
76  cbp += cbp;
77  }
78  if (s->swap_uv) {
79  exchange_uv(s);
80  }
81 }
82 
83 /**
84  * Find and store the surfaces that are used as reference frames.
85  * This function should be called for every new field and/or frame.
86  * It should be safe to call the function a few times for the same field.
87  */
88 static int ff_xvmc_field_start(AVCodecContext *avctx, const uint8_t *buf, uint32_t buf_size)
89 {
90  struct MpegEncContext *s = avctx->priv_data;
91  struct xvmc_pix_fmt *last, *next, *render = (struct xvmc_pix_fmt*)s->current_picture.f->data[2];
92  const int mb_block_count = 4 + (1 << s->chroma_format);
93 
94  assert(avctx);
95  if (!render || render->xvmc_id != AV_XVMC_ID ||
96  !render->data_blocks || !render->mv_blocks ||
97  (unsigned int)render->allocated_mv_blocks > INT_MAX/(64*6) ||
98  (unsigned int)render->allocated_data_blocks > INT_MAX/64 ||
99  !render->p_surface) {
100  av_log(avctx, AV_LOG_ERROR,
101  "Render token doesn't look as expected.\n");
102  return -1; // make sure that this is a render packet
103  }
104 
105  if (render->filled_mv_blocks_num) {
106  av_log(avctx, AV_LOG_ERROR,
107  "Rendering surface contains %i unprocessed blocks.\n",
108  render->filled_mv_blocks_num);
109  return -1;
110  }
111  if (render->allocated_mv_blocks < 1 ||
112  render->allocated_data_blocks < render->allocated_mv_blocks*mb_block_count ||
113  render->start_mv_blocks_num >= render->allocated_mv_blocks ||
114  render->next_free_data_block_num >
115  render->allocated_data_blocks -
116  mb_block_count*(render->allocated_mv_blocks-render->start_mv_blocks_num)) {
117  av_log(avctx, AV_LOG_ERROR,
118  "Rendering surface doesn't provide enough block structures to work with.\n");
119  return -1;
120  }
121 
122  render->picture_structure = s->picture_structure;
123  render->flags = s->first_field ? 0 : XVMC_SECOND_FIELD;
124  render->p_future_surface = NULL;
125  render->p_past_surface = NULL;
126 
127  switch(s->pict_type) {
128  case AV_PICTURE_TYPE_I:
129  return 0; // no prediction from other frames
130  case AV_PICTURE_TYPE_B:
131  next = (struct xvmc_pix_fmt*)s->next_picture.f->data[2];
132  if (!next)
133  return -1;
134  if (next->xvmc_id != AV_XVMC_ID)
135  return -1;
136  render->p_future_surface = next->p_surface;
137  // no return here, going to set forward prediction
138  case AV_PICTURE_TYPE_P:
139  last = (struct xvmc_pix_fmt*)s->last_picture.f->data[2];
140  if (!last)
141  last = render; // predict second field from the first
142  if (last->xvmc_id != AV_XVMC_ID)
143  return -1;
144  render->p_past_surface = last->p_surface;
145  return 0;
146  }
147 
148 return -1;
149 }
150 
151 /**
152  * Complete frame/field rendering by passing any remaining blocks.
153  * Normally ff_draw_horiz_band() is called for each slice, however,
154  * some leftover blocks, for example from error_resilience(), may remain.
155  * It should be safe to call the function a few times for the same field.
156  */
158 {
159  struct MpegEncContext *s = avctx->priv_data;
160  struct xvmc_pix_fmt *render = (struct xvmc_pix_fmt*)s->current_picture.f->data[2];
161  assert(render);
162 
163  if (render->filled_mv_blocks_num > 0)
164  ff_mpeg_draw_horiz_band(s, 0, 0);
165  return 0;
166 }
167 
168 /**
169  * Synthesize the data needed by XvMC to render one macroblock of data.
170  * Fill all relevant fields, if necessary do IDCT.
171  */
172 static void ff_xvmc_decode_mb(struct MpegEncContext *s)
173 {
174  XvMCMacroBlock *mv_block;
175  struct xvmc_pix_fmt *render;
176  int i, cbp, blocks_per_mb;
177 
178  const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
179 
180 
181  if (s->encoding) {
182  av_log(s->avctx, AV_LOG_ERROR, "XVMC doesn't support encoding!!!\n");
183  return;
184  }
185 
186  // from ff_mpv_reconstruct_mb(), update DC predictors for P macroblocks
187  if (!s->mb_intra) {
188  s->last_dc[0] =
189  s->last_dc[1] =
190  s->last_dc[2] = 128 << s->intra_dc_precision;
191  }
192 
193  // MC doesn't skip blocks
194  s->mb_skipped = 0;
195 
196 
197  // Do I need to export quant when I could not perform postprocessing?
198  // Anyway, it doesn't hurt.
199  s->current_picture.qscale_table[mb_xy] = s->qscale;
200 
201  // start of XVMC-specific code
202  render = (struct xvmc_pix_fmt*)s->current_picture.f->data[2];
203  assert(render);
204  assert(render->xvmc_id == AV_XVMC_ID);
205  assert(render->mv_blocks);
206 
207  // take the next free macroblock
208  mv_block = &render->mv_blocks[render->start_mv_blocks_num +
209  render->filled_mv_blocks_num];
210 
211  mv_block->x = s->mb_x;
212  mv_block->y = s->mb_y;
213  mv_block->dct_type = s->interlaced_dct; // XVMC_DCT_TYPE_FRAME/FIELD;
214  if (s->mb_intra) {
215  mv_block->macroblock_type = XVMC_MB_TYPE_INTRA; // no MC, all done
216  } else {
217  mv_block->macroblock_type = XVMC_MB_TYPE_PATTERN;
218 
219  if (s->mv_dir & MV_DIR_FORWARD) {
220  mv_block->macroblock_type |= XVMC_MB_TYPE_MOTION_FORWARD;
221  // PMV[n][dir][xy] = mv[dir][n][xy]
222  mv_block->PMV[0][0][0] = s->mv[0][0][0];
223  mv_block->PMV[0][0][1] = s->mv[0][0][1];
224  mv_block->PMV[1][0][0] = s->mv[0][1][0];
225  mv_block->PMV[1][0][1] = s->mv[0][1][1];
226  }
227  if (s->mv_dir & MV_DIR_BACKWARD) {
228  mv_block->macroblock_type |= XVMC_MB_TYPE_MOTION_BACKWARD;
229  mv_block->PMV[0][1][0] = s->mv[1][0][0];
230  mv_block->PMV[0][1][1] = s->mv[1][0][1];
231  mv_block->PMV[1][1][0] = s->mv[1][1][0];
232  mv_block->PMV[1][1][1] = s->mv[1][1][1];
233  }
234 
235  switch(s->mv_type) {
236  case MV_TYPE_16X16:
237  mv_block->motion_type = XVMC_PREDICTION_FRAME;
238  break;
239  case MV_TYPE_16X8:
240  mv_block->motion_type = XVMC_PREDICTION_16x8;
241  break;
242  case MV_TYPE_FIELD:
243  mv_block->motion_type = XVMC_PREDICTION_FIELD;
244  if (s->picture_structure == PICT_FRAME) {
245  mv_block->PMV[0][0][1] <<= 1;
246  mv_block->PMV[1][0][1] <<= 1;
247  mv_block->PMV[0][1][1] <<= 1;
248  mv_block->PMV[1][1][1] <<= 1;
249  }
250  break;
251  case MV_TYPE_DMV:
252  mv_block->motion_type = XVMC_PREDICTION_DUAL_PRIME;
253  if (s->picture_structure == PICT_FRAME) {
254 
255  mv_block->PMV[0][0][0] = s->mv[0][0][0]; // top from top
256  mv_block->PMV[0][0][1] = s->mv[0][0][1] << 1;
257 
258  mv_block->PMV[0][1][0] = s->mv[0][0][0]; // bottom from bottom
259  mv_block->PMV[0][1][1] = s->mv[0][0][1] << 1;
260 
261  mv_block->PMV[1][0][0] = s->mv[0][2][0]; // dmv00, top from bottom
262  mv_block->PMV[1][0][1] = s->mv[0][2][1] << 1; // dmv01
263 
264  mv_block->PMV[1][1][0] = s->mv[0][3][0]; // dmv10, bottom from top
265  mv_block->PMV[1][1][1] = s->mv[0][3][1] << 1; // dmv11
266 
267  } else {
268  mv_block->PMV[0][1][0] = s->mv[0][2][0]; // dmv00
269  mv_block->PMV[0][1][1] = s->mv[0][2][1]; // dmv01
270  }
271  break;
272  default:
273  assert(0);
274  }
275 
276  mv_block->motion_vertical_field_select = 0;
277 
278  // set correct field references
279  if (s->mv_type == MV_TYPE_FIELD || s->mv_type == MV_TYPE_16X8) {
280  mv_block->motion_vertical_field_select |= s->field_select[0][0];
281  mv_block->motion_vertical_field_select |= s->field_select[1][0] << 1;
282  mv_block->motion_vertical_field_select |= s->field_select[0][1] << 2;
283  mv_block->motion_vertical_field_select |= s->field_select[1][1] << 3;
284  }
285  } // !intra
286  // time to handle data blocks
287  mv_block->index = render->next_free_data_block_num;
288 
289  blocks_per_mb = 6;
290  if (s->chroma_format >= 2) {
291  blocks_per_mb = 4 + (1 << s->chroma_format);
292  }
293 
294  // calculate cbp
295  cbp = 0;
296  for (i = 0; i < blocks_per_mb; i++) {
297  cbp += cbp;
298  if (s->block_last_index[i] >= 0)
299  cbp++;
300  }
301 
302  if (s->avctx->flags & AV_CODEC_FLAG_GRAY) {
303  if (s->mb_intra) { // intra frames are always full chroma blocks
304  for (i = 4; i < blocks_per_mb; i++) {
305  memset(s->pblocks[i], 0, sizeof(*s->pblocks[i])); // so we need to clear them
306  if (!render->unsigned_intra)
307  *s->pblocks[i][0] = 1 << 10;
308  }
309  } else {
310  cbp &= 0xf << (blocks_per_mb - 4);
311  blocks_per_mb = 4; // luminance blocks only
312  }
313  }
314  mv_block->coded_block_pattern = cbp;
315  if (cbp == 0)
316  mv_block->macroblock_type &= ~XVMC_MB_TYPE_PATTERN;
317 
318  for (i = 0; i < blocks_per_mb; i++) {
319  if (s->block_last_index[i] >= 0) {
320  // I do not have unsigned_intra MOCO to test, hope it is OK.
321  if (s->mb_intra && (render->idct || !render->unsigned_intra))
322  *s->pblocks[i][0] -= 1 << 10;
323  if (!render->idct) {
324  s->idsp.idct(*s->pblocks[i]);
325  /* It is unclear if MC hardware requires pixel diff values to be
326  * in the range [-255;255]. TODO: Clipping if such hardware is
327  * ever found. As of now it would only be an unnecessary
328  * slowdown. */
329  }
330  // copy blocks only if the codec doesn't support pblocks reordering
331  if (!s->pack_pblocks) {
332  memcpy(&render->data_blocks[render->next_free_data_block_num*64],
333  s->pblocks[i], sizeof(*s->pblocks[i]));
334  }
335  render->next_free_data_block_num++;
336  }
337  }
338  render->filled_mv_blocks_num++;
339 
340  assert(render->filled_mv_blocks_num <= render->allocated_mv_blocks);
341  assert(render->next_free_data_block_num <= render->allocated_data_blocks);
342  /* The above conditions should not be able to fail as long as this function
343  * is used and the following 'if ()' automatically calls a callback to free
344  * blocks. */
345 
346 
347  if (render->filled_mv_blocks_num == render->allocated_mv_blocks)
348  ff_mpeg_draw_horiz_band(s, 0, 0);
349 }
350 
351 #if CONFIG_MPEG1_XVMC_HWACCEL
353  .name = "mpeg1_xvmc",
354  .type = AVMEDIA_TYPE_VIDEO,
356  .pix_fmt = AV_PIX_FMT_XVMC,
357  .start_frame = ff_xvmc_field_start,
358  .end_frame = ff_xvmc_field_end,
359  .decode_slice = NULL,
360  .decode_mb = ff_xvmc_decode_mb,
361  .priv_data_size = 0,
362 };
363 #endif
364 
365 #if CONFIG_MPEG2_XVMC_HWACCEL
367  .name = "mpeg2_xvmc",
368  .type = AVMEDIA_TYPE_VIDEO,
370  .pix_fmt = AV_PIX_FMT_XVMC,
371  .start_frame = ff_xvmc_field_start,
372  .end_frame = ff_xvmc_field_end,
373  .decode_slice = NULL,
374  .decode_mb = ff_xvmc_decode_mb,
375  .priv_data_size = 0,
376 };
377 #endif
PICT_FRAME
#define PICT_FRAME
Definition: mpegutils.h:38
MV_TYPE_16X16
#define MV_TYPE_16X16
1 vector for the whole mb
Definition: mpegvideo.h:249
exchange_uv
static void exchange_uv(MpegEncContext *s)
Definition: mpegvideo_xvmc.c:52
xvmc.h
xvmc_pix_fmt::unsigned_intra
int unsigned_intra
In MoCo mode it indicates that intra macroblocks are assumed to be in unsigned format; same as the XV...
Definition: xvmc.h:96
ff_mpeg1_xvmc_hwaccel
const AVHWAccel ff_mpeg1_xvmc_hwaccel
MV_TYPE_16X8
#define MV_TYPE_16X8
2 vectors, one per 16x8 block
Definition: mpegvideo.h:251
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:26
xvmc_pix_fmt::start_mv_blocks_num
int start_mv_blocks_num
Number of macroblock descriptions in the mv_blocks array that have already been passed to the hardwar...
Definition: xvmc.h:141
xvmc_pix_fmt::filled_mv_blocks_num
int filled_mv_blocks_num
Number of new macroblock descriptions in the mv_blocks array (after start_mv_blocks_num) that are fil...
Definition: xvmc.h:150
version.h
xvmc_pix_fmt::idct
int idct
Indicate that the hardware would interpret data_blocks as IDCT coefficients and perform IDCT on them.
Definition: xvmc.h:89
mpegvideo.h
MpegEncContext::avctx
struct AVCodecContext * avctx
Definition: mpegvideo.h:81
mpegutils.h
MV_DIR_BACKWARD
#define MV_DIR_BACKWARD
Definition: mpegvideo.h:246
xvmc_pix_fmt::xvmc_id
int xvmc_id
The field contains the special constant value AV_XVMC_ID.
Definition: xvmc.h:53
MV_TYPE_DMV
#define MV_TYPE_DMV
2 vectors, special mpeg2 Dual Prime Vectors
Definition: mpegvideo.h:253
AVHWAccel
Definition: avcodec.h:2039
ff_xvmc_init_block
void ff_xvmc_init_block(MpegEncContext *s)
Initialize the block field of the MpegEncContext pointer passed as parameter after making sure that t...
Definition: mpegvideo_xvmc.c:44
xvmc_pix_fmt::flags
unsigned int flags
XVMC_SECOND_FIELD - 1st or 2nd field in the sequence.
Definition: xvmc.h:131
ff_xvmc_pack_pblocks
void ff_xvmc_pack_pblocks(MpegEncContext *s, int cbp)
Fill individual block pointers, so there are no gaps in the data_block array in case not all blocks i...
Definition: mpegvideo_xvmc.c:65
xvmc_pix_fmt::data_blocks
short * data_blocks
Pointer to the block array allocated by XvMCCreateBlocks().
Definition: xvmc.h:62
xvmc_pix_fmt::p_future_surface
XvMCSurface * p_future_surface
Pointer to the surface used as future reference.
Definition: xvmc.h:119
AV_XVMC_ID
#define AV_XVMC_ID
special value to ensure that regular pixel routines haven't corrupted the struct the number is 1337 s...
Definition: xvmc.h:43
mpegvideodec.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
xvmc_pix_fmt::next_free_data_block_num
int next_free_data_block_num
Number of the next free data block; one data block consists of 64 short values in the data_blocks arr...
Definition: xvmc.h:163
s
#define s(width, name)
Definition: cbs_vp9.c:256
xvmc_pix_fmt::allocated_data_blocks
int allocated_data_blocks
Number of blocks that can be stored at once in the data_blocks array.
Definition: xvmc.h:82
limits.h
if
if(ret)
Definition: filter_design.txt:179
xvmc_pix_fmt::allocated_mv_blocks
int allocated_mv_blocks
Number of macroblock descriptions that can be stored in the mv_blocks array.
Definition: xvmc.h:76
ff_xvmc_field_start
static int ff_xvmc_field_start(AVCodecContext *avctx, const uint8_t *buf, uint32_t buf_size)
Find and store the surfaces that are used as reference frames.
Definition: mpegvideo_xvmc.c:88
NULL
#define NULL
Definition: coverity.c:32
ff_xvmc_decode_mb
static void ff_xvmc_decode_mb(struct MpegEncContext *s)
Synthesize the data needed by XvMC to render one macroblock of data.
Definition: mpegvideo_xvmc.c:172
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
AV_PIX_FMT_XVMC
@ AV_PIX_FMT_XVMC
XVideo Motion Acceleration via common packet passing.
Definition: pixfmt.h:263
xvmc_internal.h
AV_CODEC_ID_MPEG1VIDEO
@ AV_CODEC_ID_MPEG1VIDEO
Definition: codec_id.h:51
ff_mpeg2_xvmc_hwaccel
const AVHWAccel ff_mpeg2_xvmc_hwaccel
AV_CODEC_FLAG_GRAY
#define AV_CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition: avcodec.h:243
xvmc_pix_fmt::mv_blocks
XvMCMacroBlock * mv_blocks
Pointer to the macroblock description array allocated by XvMCCreateMacroBlocks() and freed by XvMCDes...
Definition: xvmc.h:69
xvmc_pix_fmt::p_past_surface
XvMCSurface * p_past_surface
Set by the decoder before calling ff_draw_horiz_band(), needed by the XvMCRenderSurface function.
Definition: xvmc.h:113
ff_mpeg_draw_horiz_band
void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
Definition: mpegvideo_dec.c:543
xvmc_pix_fmt
Definition: xvmc.h:46
MV_TYPE_FIELD
#define MV_TYPE_FIELD
2 vectors, one per field
Definition: mpegvideo.h:252
AVHWAccel::name
const char * name
Name of the hardware accelerated codec.
Definition: avcodec.h:2045
xvmc_pix_fmt::picture_structure
unsigned int picture_structure
top/bottom field or frame
Definition: xvmc.h:125
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
avcodec.h
AVCodecContext
main external API structure.
Definition: avcodec.h:383
AV_PICTURE_TYPE_B
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition: avutil.h:276
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
ff_xvmc_field_end
static int ff_xvmc_field_end(AVCodecContext *avctx)
Complete frame/field rendering by passing any remaining blocks.
Definition: mpegvideo_xvmc.c:157
MV_DIR_FORWARD
#define MV_DIR_FORWARD
Definition: mpegvideo.h:245
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:410
xvmc_pix_fmt::p_surface
XvMCSurface * p_surface
Pointer to the surface allocated by XvMCCreateSurface().
Definition: xvmc.h:104
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AV_CODEC_ID_MPEG2VIDEO
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:52
MpegEncContext
MpegEncContext.
Definition: mpegvideo.h:64