Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_03 / synfig-core / src / modules / mod_libavcodec / libavcodec / xvmcvideo.c
1 /*
2  * XVideo Motion Compensation
3  * Copyright (c) 2003 Ivan Kalvachev
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <limits.h>
21
22 //avcodec include
23 #include "avcodec.h"
24 #include "dsputil.h"
25 #include "mpegvideo.h"
26
27 #undef NDEBUG
28 #include <assert.h>
29
30 #ifdef USE_FASTMEMCPY
31 #include "fastmemcpy.h"
32 #endif
33
34 #ifdef HAVE_XVMC
35
36 //X11 includes are in the xvmc_render.h
37 //by replacing it with none-X one
38 //XvMC emulation could be performed
39
40 #include "xvmc_render.h"
41
42 //#include "xvmc_debug.h"
43
44 static int calc_cbp(MpegEncContext *s, int blocknum){
45 /* compute cbp */
46 // for I420 bit_offset=5
47 int  i,cbp = 0;
48     for(i=0; i<blocknum; i++) {
49         if(s->block_last_index[i] >= 0)
50             cbp |= 1 << (5 - i);
51     }
52     return cbp;
53 }
54
55
56
57 //these functions should be called on every new field or/and frame
58 //They should be safe if they are called few times for same field!
59 int XVMC_field_start(MpegEncContext*s, AVCodecContext *avctx){
60 xvmc_render_state_t * render,* last, * next;
61
62     assert(avctx != NULL);
63
64     render = (xvmc_render_state_t*)s->current_picture.data[2];
65     assert(render != NULL);
66     if( (render == NULL) || (render->magic != MP_XVMC_RENDER_MAGIC) )
67         return -1;//make sure that this is render packet
68
69     render->picture_structure = s->picture_structure;
70     render->flags = (s->first_field)? 0: XVMC_SECOND_FIELD;
71
72 //make sure that all data is drawn by XVMC_end_frame
73     assert(render->filled_mv_blocks_num==0);
74
75     render->p_future_surface = NULL;
76     render->p_past_surface = NULL;
77
78     switch(s->pict_type){
79         case  I_TYPE:
80             return 0;// no prediction from other frames
81         case  B_TYPE:
82             next = (xvmc_render_state_t*)s->next_picture.data[2];
83             assert(next!=NULL);
84             assert(next->state & MP_XVMC_STATE_PREDICTION);
85             if(next == NULL) return -1;
86             if(next->magic != MP_XVMC_RENDER_MAGIC) return -1;
87             render->p_future_surface = next->p_surface;
88             //no return here, going to set forward prediction
89         case  P_TYPE:
90             last = (xvmc_render_state_t*)s->last_picture.data[2];
91             if(last == NULL)// && !s->first_field)
92                 last = render;//predict second field from the first
93             if(last->magic != MP_XVMC_RENDER_MAGIC) return -1;
94             assert(last->state & MP_XVMC_STATE_PREDICTION);
95             render->p_past_surface = last->p_surface;
96             return 0;
97      }
98
99 return -1;
100 }
101
102 void XVMC_field_end(MpegEncContext *s){
103 xvmc_render_state_t * render;
104     render = (xvmc_render_state_t*)s->current_picture.data[2];
105     assert(render != NULL);
106
107     if(render->filled_mv_blocks_num > 0){
108 //        printf("xvmcvideo.c: rendering %d left blocks after last slice!!!\n",render->filled_mv_blocks_num );
109         ff_draw_horiz_band(s,0,0);
110     }
111 }
112
113 void XVMC_decode_mb(MpegEncContext *s, DCTELEM block[6][64]){
114 XvMCMacroBlock * mv_block;
115 xvmc_render_state_t * render;
116 int i,cbp,blocks_per_mb;
117
118 const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
119
120
121     if(s->encoding){
122         fprintf(stderr,"XVMC doesn't support encoding!!!\n");
123         assert(0);
124         return;
125     }
126
127    //from MPV_decode_mb(),
128     /* update DC predictors for P macroblocks */
129     if (!s->mb_intra) {
130         s->last_dc[0] =
131         s->last_dc[1] =
132         s->last_dc[2] =  128 << s->intra_dc_precision;
133     }
134
135    //MC doesn't skip blocks
136     s->mb_skiped = 0;
137
138
139    // do I need to export quant when I could not perform postprocessing?
140    // anyway, it doesn't hurrt
141     s->current_picture.qscale_table[mb_xy] = s->qscale;
142
143 //START OF XVMC specific code
144     render = (xvmc_render_state_t*)s->current_picture.data[2];
145     assert(render!=NULL);
146     assert(render->magic==MP_XVMC_RENDER_MAGIC);
147     assert(render->mv_blocks);
148
149     //take the next free macroblock
150     mv_block = &render->mv_blocks[render->start_mv_blocks_num + 
151                                    render->filled_mv_blocks_num ];
152
153 // memset(mv_block,0,sizeof(XvMCMacroBlock));
154
155     mv_block->x = s->mb_x;
156     mv_block->y = s->mb_y;
157     mv_block->dct_type = s->interlaced_dct;//XVMC_DCT_TYPE_FRAME/FIELD;
158 //    mv_block->motion_type = 0;  //zero to silense warnings
159     if(s->mb_intra){
160         mv_block->macroblock_type = XVMC_MB_TYPE_INTRA;//no MC, all done
161     }else{
162         mv_block->macroblock_type = XVMC_MB_TYPE_PATTERN;
163
164         if(s->mv_dir & MV_DIR_FORWARD){
165             mv_block->macroblock_type|= XVMC_MB_TYPE_MOTION_FORWARD;
166             //pmv[n][dir][xy]=mv[dir][n][xy]
167             mv_block->PMV[0][0][0] = s->mv[0][0][0];
168             mv_block->PMV[0][0][1] = s->mv[0][0][1];
169             mv_block->PMV[1][0][0] = s->mv[0][1][0];
170             mv_block->PMV[1][0][1] = s->mv[0][1][1];
171         }
172         if(s->mv_dir & MV_DIR_BACKWARD){
173             mv_block->macroblock_type|=XVMC_MB_TYPE_MOTION_BACKWARD;
174             mv_block->PMV[0][1][0] = s->mv[1][0][0];
175             mv_block->PMV[0][1][1] = s->mv[1][0][1];
176             mv_block->PMV[1][1][0] = s->mv[1][1][0];
177             mv_block->PMV[1][1][1] = s->mv[1][1][1];
178         }
179
180         switch(s->mv_type){
181             case  MV_TYPE_16X16:
182                 mv_block->motion_type = XVMC_PREDICTION_FRAME;
183                 break;
184             case  MV_TYPE_16X8:
185                 mv_block->motion_type = XVMC_PREDICTION_16x8;
186                 break;
187             case  MV_TYPE_FIELD:
188                 mv_block->motion_type = XVMC_PREDICTION_FIELD;
189                 if(s->picture_structure == PICT_FRAME){
190                     mv_block->PMV[0][0][1]<<=1;
191                     mv_block->PMV[1][0][1]<<=1;
192                     mv_block->PMV[0][1][1]<<=1;
193                     mv_block->PMV[1][1][1]<<=1;
194                 }
195                 break;
196             case  MV_TYPE_DMV:
197                 mv_block->motion_type = XVMC_PREDICTION_DUAL_PRIME;
198                 if(s->picture_structure == PICT_FRAME){
199
200                     mv_block->PMV[0][0][0] = s->mv[0][0][0];//top from top
201                     mv_block->PMV[0][0][1] = s->mv[0][0][1]<<1;
202
203                     mv_block->PMV[0][1][0] = s->mv[0][0][0];//bottom from bottom
204                     mv_block->PMV[0][1][1] = s->mv[0][0][1]<<1;
205
206                     mv_block->PMV[1][0][0] = s->mv[0][2][0];//dmv00, top from bottom
207                     mv_block->PMV[1][0][1] = s->mv[0][2][1]<<1;//dmv01
208
209                     mv_block->PMV[1][1][0] = s->mv[0][3][0];//dmv10, bottom from top
210                     mv_block->PMV[1][1][1] = s->mv[0][3][1]<<1;//dmv11
211
212                 }else{
213                     mv_block->PMV[0][1][0] = s->mv[0][2][0];//dmv00
214                     mv_block->PMV[0][1][1] = s->mv[0][2][1];//dmv01
215                 }
216                 break;
217             default:
218                 assert(0);
219         }
220
221         mv_block->motion_vertical_field_select = 0;
222
223 //set correct field referenses
224         if(s->mv_type == MV_TYPE_FIELD || s->mv_type == MV_TYPE_16X8){
225             if( s->field_select[0][0] ) mv_block->motion_vertical_field_select|=1;
226             if( s->field_select[1][0] ) mv_block->motion_vertical_field_select|=2;
227             if( s->field_select[0][1] ) mv_block->motion_vertical_field_select|=4;
228             if( s->field_select[1][1] ) mv_block->motion_vertical_field_select|=8;
229         }
230     }//!intra
231 //time to handle data blocks;
232     mv_block->index = render->next_free_data_block_num;
233     blocks_per_mb = 6;
234 /*
235     switch( s->chroma_format){
236         case CHROMA_422:
237             blocks_per_mb = 8;
238             break;
239         case CHROMA_444:
240             blocks_per_mb = 12;
241             break;
242     }
243 */
244     if(s->flags & CODEC_FLAG_GRAY){
245         if(s->mb_intra){//intra frames are alwasy full chroma block
246             memset(block[4],0,sizeof(short)*8*8);//so we need to clear them
247             memset(block[5],0,sizeof(short)*8*8);
248             if(!render->unsigned_intra)
249                 block[4][0] = block[5][0] = 1<<10;
250         }
251         else
252             blocks_per_mb = 4;//Luminance blocks only
253     };
254     cbp = calc_cbp(s,blocks_per_mb);
255     mv_block->coded_block_pattern = cbp;
256     if(cbp == 0)
257         mv_block->macroblock_type &= ~XVMC_MB_TYPE_PATTERN;
258
259     for(i=0; i<blocks_per_mb; i++){
260         if(s->block_last_index[i] >= 0){
261             // i do not have unsigned_intra MOCO to test, hope it is OK
262             if( (s->mb_intra) && ( render->idct || (!render->idct && !render->unsigned_intra)) )
263                 block[i][0]-=1<<10;
264             if(!render->idct){
265                 s->dsp.idct(block[i]);
266                 //!!TODO!clip!!!
267             }
268 //TODO:avoid block copy by modifying s->block pointer
269             memcpy(&render->data_blocks[(render->next_free_data_block_num++)*64],
270                     block[i],sizeof(short)*8*8);
271         }
272     }
273     render->filled_mv_blocks_num++;
274
275     assert(render->filled_mv_blocks_num <= render->total_number_of_mv_blocks);
276     assert(render->next_free_data_block_num <= render->total_number_of_data_blocks);
277
278
279     if(render->filled_mv_blocks_num >= render->total_number_of_mv_blocks)
280         ff_draw_horiz_band(s,0,0);
281
282 // DumpRenderInfo(render);
283 // DumpMBlockInfo(mv_block);
284
285 }
286
287 #endif