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