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