comparison xvmcvideo.c @ 1381:f07e17427140 libavcodec

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