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