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