Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpegvideo_xvmc.c @ 649c171a

History | View | Annotate | Download (12 KB)

1 302898fc Ivan Kalvachev
/*
2
 * XVideo Motion Compensation
3
 * Copyright (c) 2003 Ivan Kalvachev
4
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 302898fc Ivan Kalvachev
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 302898fc Ivan Kalvachev
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 302898fc Ivan Kalvachev
 * 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 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 302898fc Ivan Kalvachev
 */
21
22 2e7b4c84 Ivan Kalvachev
#include <limits.h>
23
24
#include "avcodec.h"
25
#include "dsputil.h"
26
#include "mpegvideo.h"
27
28
#undef NDEBUG
29
#include <assert.h>
30
31 fd949a63 Diego Biurrun
#include "xvmc.h"
32 4440bd0d Diego Biurrun
#include "xvmc_internal.h"
33 302898fc Ivan Kalvachev
34 8f38ff00 Diego Biurrun
/**
35
 * Initializes the block field of the MpegEncContext pointer passed as
36
 * parameter after making sure that the data is not corrupted.
37 d758a5cb Diego Biurrun
 * In order to implement something like direct rendering instead of decoding
38
 * coefficients in s->blocks and then copying them, copy them directly
39 3d43e42f Ivan Kalvachev
 * into the data_blocks array provided by xvmc.
40 8f38ff00 Diego Biurrun
 */
41 78f9a878 Ivan Kalvachev
void ff_xvmc_init_block(MpegEncContext *s)
42 148302e7 Diego Biurrun
{
43 f602b044 Diego Biurrun
    struct xvmc_pix_fmt *render = (struct xvmc_pix_fmt*)s->current_picture.data[2];
44 4e30fa6e Diego Biurrun
    assert(render && render->xvmc_id == AV_XVMC_ID);
45 d2d600b7 Ivan Kalvachev
46 9f00a41c Diego Biurrun
    s->block = (DCTELEM *)(render->data_blocks + render->next_free_data_block_num * 64);
47 a579db0c Ivan Kalvachev
}
48
49 3d43e42f Ivan Kalvachev
/**
50
 * Fill individual block pointers, so there are no gaps in the data_block array
51
 * in case not all blocks in MB are coded.
52
 */
53 78f9a878 Ivan Kalvachev
void ff_xvmc_pack_pblocks(MpegEncContext *s, int cbp)
54 148302e7 Diego Biurrun
{
55 b71bd0ed Diego Biurrun
    int i, j = 0;
56 148302e7 Diego Biurrun
    const int mb_block_count = 4 + (1 << s->chroma_format);
57 a579db0c Ivan Kalvachev
58 148302e7 Diego Biurrun
    cbp <<= 12-mb_block_count;
59
    for (i = 0; i < mb_block_count; i++) {
60 4ec58e13 Diego Biurrun
        if (cbp & (1 << 11))
61 4742762f Ivan Kalvachev
            s->pblocks[i] = (short *)(&s->block[j++]);
62 4ec58e13 Diego Biurrun
        else
63 39dba5e8 Diego Biurrun
            s->pblocks[i] = NULL;
64 42dfc2bf Diego Biurrun
        cbp += cbp;
65 a579db0c Ivan Kalvachev
    }
66 2e7b4c84 Ivan Kalvachev
}
67
68 9107f7cd Diego Biurrun
/**
69 3d43e42f Ivan Kalvachev
 * Find and store the surfaces that are used as reference frames.
70 9107f7cd Diego Biurrun
 * This function should be called for every new field and/or frame.
71
 * It should be safe to call the function a few times for the same field.
72
 */
73 6545996f Diego Biurrun
int ff_xvmc_field_start(MpegEncContext *s, AVCodecContext *avctx)
74 148302e7 Diego Biurrun
{
75 f602b044 Diego Biurrun
    struct xvmc_pix_fmt *last, *next, *render = (struct xvmc_pix_fmt*)s->current_picture.data[2];
76 d2d600b7 Ivan Kalvachev
    const int mb_block_count = 4 + (1 << s->chroma_format);
77 2e7b4c84 Ivan Kalvachev
78 3ae01928 Diego Biurrun
    assert(avctx);
79 4e30fa6e Diego Biurrun
    if (!render || render->xvmc_id != AV_XVMC_ID ||
80 9043202e Diego Biurrun
        !render->data_blocks || !render->mv_blocks) {
81 d2d600b7 Ivan Kalvachev
        av_log(avctx, AV_LOG_ERROR,
82
               "Render token doesn't look as expected.\n");
83 a1db3b93 Diego Biurrun
        return -1; // make sure that this is a render packet
84 d2d600b7 Ivan Kalvachev
    }
85 2e7b4c84 Ivan Kalvachev
86 cb18fb62 Diego Biurrun
    if (render->filled_mv_blocks_num) {
87 9939841f Ivan Kalvachev
        av_log(avctx, AV_LOG_ERROR,
88 d2d600b7 Ivan Kalvachev
               "Rendering surface contains %i unprocessed blocks.\n",
89 cb18fb62 Diego Biurrun
               render->filled_mv_blocks_num);
90 013cebfb Ivan Kalvachev
        return -1;
91 9939841f Ivan Kalvachev
    }
92 cad1928f Diego Biurrun
    if (render->allocated_mv_blocks   < 1 ||
93 649c171a Ivan Kalvachev
        render->allocated_data_blocks <  render->allocated_mv_blocks*mb_block_count ||
94
        render->start_mv_blocks_num   >= render->allocated_mv_blocks                ||
95
        render->next_free_data_block_num >
96
                        render->allocated_data_blocks -
97
                        mb_block_count*(render->allocated_mv_blocks-render->start_mv_blocks_num)) {
98 d2d600b7 Ivan Kalvachev
        av_log(avctx, AV_LOG_ERROR,
99
               "Rendering surface doesn't provide enough block structures to work with.\n");
100
        return -1;
101
    }
102 2e7b4c84 Ivan Kalvachev
103 d76c5ed5 Ivan Kalvachev
    render->picture_structure = s->picture_structure;
104
    render->flags             = s->first_field ? 0 : XVMC_SECOND_FIELD;
105 9e0a8a36 Ivan Kalvachev
    render->p_future_surface  = NULL;
106
    render->p_past_surface    = NULL;
107 2e7b4c84 Ivan Kalvachev
108 7e2e870e Diego Biurrun
    switch(s->pict_type) {
109 9701840b Aurelien Jacobs
        case  FF_I_TYPE:
110 1f7c1d14 Diego Biurrun
            return 0; // no prediction from other frames
111 9701840b Aurelien Jacobs
        case  FF_B_TYPE:
112 f602b044 Diego Biurrun
            next = (struct xvmc_pix_fmt*)s->next_picture.data[2];
113 3ae01928 Diego Biurrun
            if (!next)
114 148302e7 Diego Biurrun
                return -1;
115 4e30fa6e Diego Biurrun
            if (next->xvmc_id != AV_XVMC_ID)
116 148302e7 Diego Biurrun
                return -1;
117 2e7b4c84 Ivan Kalvachev
            render->p_future_surface = next->p_surface;
118 1f7c1d14 Diego Biurrun
            // no return here, going to set forward prediction
119 9701840b Aurelien Jacobs
        case  FF_P_TYPE:
120 f602b044 Diego Biurrun
            last = (struct xvmc_pix_fmt*)s->last_picture.data[2];
121 63a21bc0 Diego Biurrun
            if (!last)
122 1f7c1d14 Diego Biurrun
                last = render; // predict second field from the first
123 4e30fa6e Diego Biurrun
            if (last->xvmc_id != AV_XVMC_ID)
124 148302e7 Diego Biurrun
                return -1;
125 2e7b4c84 Ivan Kalvachev
            render->p_past_surface = last->p_surface;
126
            return 0;
127 5e5c247a Ivan Kalvachev
    }
128 2e7b4c84 Ivan Kalvachev
129
return -1;
130
}
131
132 0b2eb2e8 Diego Biurrun
/**
133 3d43e42f Ivan Kalvachev
 * Complete frame/field rendering by passing any remaining blocks.
134 d758a5cb Diego Biurrun
 * Normally ff_draw_horiz_band() is called for each slice, however,
135
 * some leftover blocks, for example from error_resilience(), may remain.
136 0b2eb2e8 Diego Biurrun
 * It should be safe to call the function a few times for the same field.
137
 */
138 78f9a878 Ivan Kalvachev
void ff_xvmc_field_end(MpegEncContext *s)
139 148302e7 Diego Biurrun
{
140 f602b044 Diego Biurrun
    struct xvmc_pix_fmt *render = (struct xvmc_pix_fmt*)s->current_picture.data[2];
141 3ae01928 Diego Biurrun
    assert(render);
142 2e7b4c84 Ivan Kalvachev
143 148302e7 Diego Biurrun
    if (render->filled_mv_blocks_num > 0)
144 99df0aa5 Diego Biurrun
        ff_draw_horiz_band(s, 0, 0);
145 2e7b4c84 Ivan Kalvachev
}
146
147 3d43e42f Ivan Kalvachev
/**
148
 * Synthesize the data needed by XvMC to render one macroblock of data.
149 864899c3 Diego Biurrun
 * Fill all relevant fields, if necessary do IDCT.
150 3d43e42f Ivan Kalvachev
 */
151 78f9a878 Ivan Kalvachev
void ff_xvmc_decode_mb(MpegEncContext *s)
152 148302e7 Diego Biurrun
{
153 7e2e870e Diego Biurrun
    XvMCMacroBlock *mv_block;
154 f602b044 Diego Biurrun
    struct xvmc_pix_fmt *render;
155 7e2e870e Diego Biurrun
    int i, cbp, blocks_per_mb;
156 2e7b4c84 Ivan Kalvachev
157 39dba5e8 Diego Biurrun
    const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
158 2e7b4c84 Ivan Kalvachev
159
160 148302e7 Diego Biurrun
    if (s->encoding) {
161 8ac5c1b2 Steven M. Schultz
        av_log(s->avctx, AV_LOG_ERROR, "XVMC doesn't support encoding!!!\n");
162 61b76987 Dominik Mierzejewski
        return;
163 2e7b4c84 Ivan Kalvachev
    }
164
165 1f7c1d14 Diego Biurrun
    // from MPV_decode_mb(), update DC predictors for P macroblocks
166 2e7b4c84 Ivan Kalvachev
    if (!s->mb_intra) {
167
        s->last_dc[0] =
168
        s->last_dc[1] =
169
        s->last_dc[2] =  128 << s->intra_dc_precision;
170
    }
171
172 1f7c1d14 Diego Biurrun
    // MC doesn't skip blocks
173 160d679c Mike Melanson
    s->mb_skipped = 0;
174 2e7b4c84 Ivan Kalvachev
175
176 d15876d3 Diego Biurrun
    // Do I need to export quant when I could not perform postprocessing?
177
    // Anyway, it doesn't hurt.
178 2e7b4c84 Ivan Kalvachev
    s->current_picture.qscale_table[mb_xy] = s->qscale;
179
180 a1db3b93 Diego Biurrun
    // start of XVMC-specific code
181 f602b044 Diego Biurrun
    render = (struct xvmc_pix_fmt*)s->current_picture.data[2];
182 3ae01928 Diego Biurrun
    assert(render);
183 4e30fa6e Diego Biurrun
    assert(render->xvmc_id == AV_XVMC_ID);
184 2e7b4c84 Ivan Kalvachev
    assert(render->mv_blocks);
185 302898fc Ivan Kalvachev
186 1f7c1d14 Diego Biurrun
    // take the next free macroblock
187 115329f1 Diego Biurrun
    mv_block = &render->mv_blocks[render->start_mv_blocks_num +
188 fe4be5db Diego Biurrun
                                  render->filled_mv_blocks_num];
189 2e7b4c84 Ivan Kalvachev
190 4ef690f5 Diego Biurrun
    mv_block->x        = s->mb_x;
191
    mv_block->y        = s->mb_y;
192 1f7c1d14 Diego Biurrun
    mv_block->dct_type = s->interlaced_dct; // XVMC_DCT_TYPE_FRAME/FIELD;
193 4ec58e13 Diego Biurrun
    if (s->mb_intra) {
194 1f7c1d14 Diego Biurrun
        mv_block->macroblock_type = XVMC_MB_TYPE_INTRA; // no MC, all done
195 4ec58e13 Diego Biurrun
    } else {
196 2e7b4c84 Ivan Kalvachev
        mv_block->macroblock_type = XVMC_MB_TYPE_PATTERN;
197
198 148302e7 Diego Biurrun
        if (s->mv_dir & MV_DIR_FORWARD) {
199 2d7d8cc7 Diego Biurrun
            mv_block->macroblock_type |= XVMC_MB_TYPE_MOTION_FORWARD;
200 0439c09d Diego Biurrun
            // PMV[n][dir][xy] = mv[dir][n][xy]
201 2e7b4c84 Ivan Kalvachev
            mv_block->PMV[0][0][0] = s->mv[0][0][0];
202
            mv_block->PMV[0][0][1] = s->mv[0][0][1];
203
            mv_block->PMV[1][0][0] = s->mv[0][1][0];
204
            mv_block->PMV[1][0][1] = s->mv[0][1][1];
205
        }
206 148302e7 Diego Biurrun
        if (s->mv_dir & MV_DIR_BACKWARD) {
207 2d7d8cc7 Diego Biurrun
            mv_block->macroblock_type |= XVMC_MB_TYPE_MOTION_BACKWARD;
208 2e7b4c84 Ivan Kalvachev
            mv_block->PMV[0][1][0] = s->mv[1][0][0];
209
            mv_block->PMV[0][1][1] = s->mv[1][0][1];
210
            mv_block->PMV[1][1][0] = s->mv[1][1][0];
211
            mv_block->PMV[1][1][1] = s->mv[1][1][1];
212
        }
213
214 99df0aa5 Diego Biurrun
        switch(s->mv_type) {
215 2e7b4c84 Ivan Kalvachev
            case  MV_TYPE_16X16:
216
                mv_block->motion_type = XVMC_PREDICTION_FRAME;
217
                break;
218
            case  MV_TYPE_16X8:
219
                mv_block->motion_type = XVMC_PREDICTION_16x8;
220
                break;
221
            case  MV_TYPE_FIELD:
222
                mv_block->motion_type = XVMC_PREDICTION_FIELD;
223 148302e7 Diego Biurrun
                if (s->picture_structure == PICT_FRAME) {
224
                    mv_block->PMV[0][0][1] <<= 1;
225
                    mv_block->PMV[1][0][1] <<= 1;
226
                    mv_block->PMV[0][1][1] <<= 1;
227
                    mv_block->PMV[1][1][1] <<= 1;
228 2e7b4c84 Ivan Kalvachev
                }
229
                break;
230
            case  MV_TYPE_DMV:
231
                mv_block->motion_type = XVMC_PREDICTION_DUAL_PRIME;
232 148302e7 Diego Biurrun
                if (s->picture_structure == PICT_FRAME) {
233 2e7b4c84 Ivan Kalvachev
234 ba585726 Diego Biurrun
                    mv_block->PMV[0][0][0] = s->mv[0][0][0];      // top from top
235 81189e4f Diego Biurrun
                    mv_block->PMV[0][0][1] = s->mv[0][0][1] << 1;
236 2e7b4c84 Ivan Kalvachev
237 ba585726 Diego Biurrun
                    mv_block->PMV[0][1][0] = s->mv[0][0][0];      // bottom from bottom
238 81189e4f Diego Biurrun
                    mv_block->PMV[0][1][1] = s->mv[0][0][1] << 1;
239 2e7b4c84 Ivan Kalvachev
240 ba585726 Diego Biurrun
                    mv_block->PMV[1][0][0] = s->mv[0][2][0];      // dmv00, top from bottom
241 81189e4f Diego Biurrun
                    mv_block->PMV[1][0][1] = s->mv[0][2][1] << 1; // dmv01
242 2e7b4c84 Ivan Kalvachev
243 ba585726 Diego Biurrun
                    mv_block->PMV[1][1][0] = s->mv[0][3][0];      // dmv10, bottom from top
244 81189e4f Diego Biurrun
                    mv_block->PMV[1][1][1] = s->mv[0][3][1] << 1; // dmv11
245 2e7b4c84 Ivan Kalvachev
246 4ec58e13 Diego Biurrun
                } else {
247 ba585726 Diego Biurrun
                    mv_block->PMV[0][1][0] = s->mv[0][2][0];      // dmv00
248
                    mv_block->PMV[0][1][1] = s->mv[0][2][1];      // dmv01
249 2e7b4c84 Ivan Kalvachev
                }
250
                break;
251
            default:
252
                assert(0);
253
        }
254
255
        mv_block->motion_vertical_field_select = 0;
256
257 1f7c1d14 Diego Biurrun
        // set correct field references
258 148302e7 Diego Biurrun
        if (s->mv_type == MV_TYPE_FIELD || s->mv_type == MV_TYPE_16X8) {
259 90509ec7 Ivan Kalvachev
            mv_block->motion_vertical_field_select |= s->field_select[0][0];
260 7e2e870e Diego Biurrun
            mv_block->motion_vertical_field_select |= s->field_select[1][0] << 1;
261
            mv_block->motion_vertical_field_select |= s->field_select[0][1] << 2;
262
            mv_block->motion_vertical_field_select |= s->field_select[1][1] << 3;
263 2e7b4c84 Ivan Kalvachev
        }
264 1f7c1d14 Diego Biurrun
    } // !intra
265 a1db3b93 Diego Biurrun
    // time to handle data blocks
266 2e7b4c84 Ivan Kalvachev
    mv_block->index = render->next_free_data_block_num;
267 5e5c247a Ivan Kalvachev
268 2e7b4c84 Ivan Kalvachev
    blocks_per_mb = 6;
269 148302e7 Diego Biurrun
    if (s->chroma_format >= 2) {
270 4742762f Ivan Kalvachev
        blocks_per_mb = 4 + (1 << s->chroma_format);
271 2e7b4c84 Ivan Kalvachev
    }
272 5e5c247a Ivan Kalvachev
273 1f7c1d14 Diego Biurrun
    // calculate cbp
274 715731a3 Ivan Kalvachev
    cbp = 0;
275 148302e7 Diego Biurrun
    for (i = 0; i < blocks_per_mb; i++) {
276 2d7d8cc7 Diego Biurrun
        cbp += cbp;
277 148302e7 Diego Biurrun
        if (s->block_last_index[i] >= 0)
278 715731a3 Ivan Kalvachev
            cbp++;
279
    }
280 115329f1 Diego Biurrun
281 148302e7 Diego Biurrun
    if (s->flags & CODEC_FLAG_GRAY) {
282 ba585726 Diego Biurrun
        if (s->mb_intra) {                                   // intra frames are always full chroma blocks
283 148302e7 Diego Biurrun
            for (i = 4; i < blocks_per_mb; i++) {
284 6e4b41f8 Diego Biurrun
                memset(s->pblocks[i], 0, sizeof(short)*64);  // so we need to clear them
285 148302e7 Diego Biurrun
                if (!render->unsigned_intra)
286
                    s->pblocks[i][0] = 1 << 10;
287 a579db0c Ivan Kalvachev
            }
288 4ec58e13 Diego Biurrun
        } else {
289 148302e7 Diego Biurrun
            cbp &= 0xf << (blocks_per_mb - 4);
290 ba585726 Diego Biurrun
            blocks_per_mb = 4;                               // luminance blocks only
291 715731a3 Ivan Kalvachev
        }
292 a579db0c Ivan Kalvachev
    }
293 2e7b4c84 Ivan Kalvachev
    mv_block->coded_block_pattern = cbp;
294 148302e7 Diego Biurrun
    if (cbp == 0)
295 2e7b4c84 Ivan Kalvachev
        mv_block->macroblock_type &= ~XVMC_MB_TYPE_PATTERN;
296
297 148302e7 Diego Biurrun
    for (i = 0; i < blocks_per_mb; i++) {
298
        if (s->block_last_index[i] >= 0) {
299 7a74e067 Diego Biurrun
            // I do not have unsigned_intra MOCO to test, hope it is OK.
300 eba9cecc Diego Biurrun
            if (s->mb_intra && (render->idct || (!render->idct && !render->unsigned_intra)))
301 148302e7 Diego Biurrun
                s->pblocks[i][0] -= 1 << 10;
302
            if (!render->idct) {
303 a579db0c Ivan Kalvachev
                s->dsp.idct(s->pblocks[i]);
304 a1db3b93 Diego Biurrun
                /* It is unclear if MC hardware requires pixel diff values to be
305
                 * in the range [-255;255]. TODO: Clipping if such hardware is
306
                 * ever found. As of now it would only be an unnecessary
307
                 * slowdown. */
308 2e7b4c84 Ivan Kalvachev
            }
309 1f7c1d14 Diego Biurrun
            // copy blocks only if the codec doesn't support pblocks reordering
310 148302e7 Diego Biurrun
            if (s->avctx->xvmc_acceleration == 1) {
311 4742762f Ivan Kalvachev
                memcpy(&render->data_blocks[render->next_free_data_block_num*64],
312 6e280340 Ivan Kalvachev
                       s->pblocks[i], sizeof(short)*64);
313 a579db0c Ivan Kalvachev
            }
314
            render->next_free_data_block_num++;
315 2e7b4c84 Ivan Kalvachev
        }
316
    }
317
    render->filled_mv_blocks_num++;
318
319 f9d5bf3c Diego Biurrun
320
    if (render->filled_mv_blocks_num > render->allocated_mv_blocks)
321
        av_log(s->avctx, AV_LOG_ERROR,
322
               "Not enough space to store mv blocks allocated.\n");
323
324
    if (render->next_free_data_block_num > render->allocated_data_blocks)
325
        av_log(s->avctx, AV_LOG_ERROR,
326
               "Offset to next data block exceeds number of allocated data blocks.\n");
327 2e7b4c84 Ivan Kalvachev
328
329 cad1928f Diego Biurrun
    if (render->filled_mv_blocks_num == render->allocated_mv_blocks)
330 7e2e870e Diego Biurrun
        ff_draw_horiz_band(s, 0, 0);
331 2e7b4c84 Ivan Kalvachev
}