Mercurial > mplayer.hg
annotate libmpcodecs/vd_libmpeg2.c @ 7464:baac3cd44794
we don't need the 0x1FF hack any more
| author | arpi |
|---|---|
| date | Sat, 21 Sep 2002 22:38:41 +0000 |
| parents | 28677d779205 |
| children | 77076fb29637 |
| rev | line source |
|---|---|
| 4998 | 1 #include <stdio.h> |
| 2 #include <stdlib.h> | |
| 3 | |
| 4 #include "config.h" | |
| 5 #include "mp_msg.h" | |
| 6 | |
| 7 #include "vd_internal.h" | |
| 8 | |
| 9 static vd_info_t info = | |
| 10 { | |
| 5465 | 11 "MPEG 1/2 Video decoder v2.0", |
| 4998 | 12 "libmpeg2", |
| 13 "A'rpi", | |
| 14 "Aaron & Walken", | |
| 15 "native" | |
| 16 }; | |
| 17 | |
| 18 LIBVD_EXTERN(libmpeg2) | |
| 19 | |
| 5465 | 20 #define USE_SIGJMP_TRICK |
| 4998 | 21 |
| 5465 | 22 #ifdef USE_SIGJMP_TRICK |
| 23 #include <signal.h> | |
| 24 #include <setjmp.h> | |
| 25 #endif | |
| 26 | |
| 27 //#include "libmpdemux/parse_es.h" | |
| 28 | |
| 29 #include "libvo/video_out.h" // FIXME!!! | |
| 30 | |
| 4998 | 31 #include "libmpeg2/mpeg2.h" |
| 32 #include "libmpeg2/mpeg2_internal.h" | |
| 5465 | 33 #include "libmpeg2/mm_accel.h" |
| 4998 | 34 |
| 5465 | 35 #include "../cpudetect.h" |
| 36 | |
| 37 mpeg2_config_t config; // FIXME!!! | |
| 38 static picture_t *picture=NULL; // exported from libmpeg2/decode.c | |
| 39 | |
| 40 static int table_init_state=0; | |
| 41 | |
| 4998 | 42 // to set/get/query special features/parameters |
| 43 static int control(sh_video_t *sh,int cmd,void* arg,...){ | |
| 44 return CONTROL_UNKNOWN; | |
| 45 } | |
| 46 | |
| 5465 | 47 static vo_frame_t frames[3]; |
| 48 | |
| 4998 | 49 // init driver |
| 50 static int init(sh_video_t *sh){ | |
| 5465 | 51 |
| 52 config.flags = 0; | |
| 53 if(gCpuCaps.hasMMX) | |
| 54 config.flags |= MM_ACCEL_X86_MMX; | |
| 55 if(gCpuCaps.hasMMX2) | |
| 56 config.flags |= MM_ACCEL_X86_MMXEXT; | |
| 57 if(gCpuCaps.has3DNow) | |
| 58 config.flags |= MM_ACCEL_X86_3DNOW; | |
| 59 #ifdef HAVE_MLIB | |
| 60 config.flags |= MM_ACCEL_MLIB; | |
| 61 #endif | |
| 62 | |
| 63 picture=malloc(sizeof(picture_t)); // !!! NEW HACK :) !!! | |
| 64 memset(picture,0,sizeof(picture_t)); | |
| 65 header_state_init (picture); | |
| 66 | |
| 67 if(!table_init_state){ | |
| 68 idct_init (); | |
| 69 motion_comp_init (); | |
| 70 table_init_state=1; | |
| 71 } | |
| 72 | |
| 5003 | 73 picture->pp_options=divx_quality; |
| 5465 | 74 |
|
5675
0ff1b9ab7afc
slices+field pictures fixed, initial sig11 workaround
arpi
parents:
5613
diff
changeset
|
75 memset(frames,0,3*sizeof(vo_frame_t)); |
|
0ff1b9ab7afc
slices+field pictures fixed, initial sig11 workaround
arpi
parents:
5613
diff
changeset
|
76 |
| 5465 | 77 picture->forward_reference_frame=&frames[0]; |
| 78 picture->backward_reference_frame=&frames[1]; | |
| 79 picture->temp_frame=&frames[2]; | |
| 80 picture->current_frame=NULL; | |
| 81 | |
| 4998 | 82 // send seq header to the decoder: *** HACK *** |
| 5465 | 83 // mpeg2_decode_data(NULL,videobuffer,videobuffer+videobuf_len,0); |
| 84 // mpeg2_allocate_image_buffers (picture); | |
| 5124 | 85 return mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_YV12); |
| 4998 | 86 } |
| 87 | |
| 88 // uninit driver | |
| 89 static void uninit(sh_video_t *sh){ | |
| 5465 | 90 // mpeg2_free_image_buffers (picture); |
| 91 } | |
| 92 | |
| 93 static void draw_slice (vo_frame_t * frame, uint8_t ** src){ | |
| 94 vo_functions_t * output = frame->vo; | |
| 95 int stride[3]; | |
| 96 int y=picture->slice<<4; | |
| 97 | |
| 98 stride[0]=picture->coded_picture_width; | |
| 99 stride[1]=stride[2]=stride[0]/2; | |
| 100 | |
| 101 output->draw_slice (src, | |
| 102 stride, picture->display_picture_width, | |
| 103 (y+16<=picture->display_picture_height) ? 16 : | |
| 104 picture->display_picture_height-y, | |
| 105 0, y); | |
| 106 | |
| 107 ++picture->slice; | |
| 4998 | 108 } |
| 109 | |
| 5465 | 110 static int in_slice_flag=0; // FIXME! move to picture struct |
| 111 static int drop_frame=0; // FIXME! move to picture struct | |
| 112 | |
| 113 static mp_image_t* parse_chunk (sh_video_t* sh, int code, uint8_t * buffer, int framedrop){ | |
| 114 mp_image_t* mpi=NULL; | |
| 115 | |
| 116 // stats_header (code, buffer); | |
| 117 | |
| 118 if (in_slice_flag && ((!code) || (code >= 0xb0))) { | |
| 119 // ok, we've completed decoding a frame/field! | |
| 120 in_slice_flag = 0; | |
| 121 mpi=picture->display_frame->mpi; | |
| 122 if(picture->picture_structure!=FRAME_PICTURE && !picture->second_field) | |
| 123 mpi=NULL; // we don't draw first fields! | |
| 124 } | |
| 125 | |
| 126 switch (code) { | |
| 127 case 0x00: /* picture_start_code */ | |
| 128 if (header_process_picture_header (picture, buffer)) { | |
| 129 printf ("bad picture header\n"); | |
| 130 } | |
| 131 drop_frame = framedrop && (picture->picture_coding_type == B_TYPE); | |
| 132 drop_frame |= framedrop>=2; // hard drop | |
| 133 break; | |
| 134 | |
| 135 case 0xb3: /* sequence_header_code */ | |
| 136 if (header_process_sequence_header (picture, buffer)) { | |
| 137 printf ("bad sequence header\n"); | |
| 138 } | |
| 139 break; | |
| 140 | |
| 141 case 0xb5: /* extension_start_code */ | |
| 142 if (header_process_extension (picture, buffer)) { | |
| 143 printf ("bad extension\n"); | |
| 144 } | |
| 145 break; | |
| 146 | |
| 147 default: | |
| 148 if (code >= 0xb0) break; | |
| 149 | |
| 150 if (!in_slice_flag) { | |
| 151 in_slice_flag = 1; | |
| 152 | |
| 153 // set current_frame pointer: | |
| 154 if (!picture->second_field){ | |
| 155 mp_image_t* mpi; | |
| 156 int flags; | |
| 157 if (picture->picture_coding_type == B_TYPE){ | |
|
5675
0ff1b9ab7afc
slices+field pictures fixed, initial sig11 workaround
arpi
parents:
5613
diff
changeset
|
158 flags=(!framedrop && vd_use_slices && |
|
0ff1b9ab7afc
slices+field pictures fixed, initial sig11 workaround
arpi
parents:
5613
diff
changeset
|
159 picture->picture_structure==FRAME_PICTURE) ? |
|
0ff1b9ab7afc
slices+field pictures fixed, initial sig11 workaround
arpi
parents:
5613
diff
changeset
|
160 MP_IMGFLAG_DRAW_CALLBACK:0; |
| 5465 | 161 picture->display_frame= |
| 162 picture->current_frame = picture->temp_frame; | |
| 163 } else { | |
| 164 flags=MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE; | |
| 165 picture->current_frame = picture->forward_reference_frame; | |
| 166 picture->display_frame= | |
| 167 picture->forward_reference_frame = picture->backward_reference_frame; | |
| 168 picture->backward_reference_frame = picture->current_frame; | |
| 169 } | |
| 170 mpi=mpcodecs_get_image(sh,MP_IMGTYPE_IPB, flags, | |
| 171 picture->coded_picture_width, | |
| 172 picture->coded_picture_height); | |
| 173 // ok, lets see what did we get: | |
| 174 if(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK && | |
| 175 !(mpi->flags&MP_IMGFLAG_DIRECT)){ | |
| 176 // nice, filter/vo likes draw_callback :) | |
| 177 picture->current_frame->copy=draw_slice; | |
| 178 } else | |
| 179 picture->current_frame->copy=NULL; | |
| 180 // let's, setup pointers! | |
| 181 picture->current_frame->base[0]=mpi->planes[0]; | |
| 182 picture->current_frame->base[1]=mpi->planes[1]; | |
| 183 picture->current_frame->base[2]=mpi->planes[2]; | |
| 184 picture->current_frame->mpi=mpi; // tricky! | |
|
5675
0ff1b9ab7afc
slices+field pictures fixed, initial sig11 workaround
arpi
parents:
5613
diff
changeset
|
185 #if 1 |
|
0ff1b9ab7afc
slices+field pictures fixed, initial sig11 workaround
arpi
parents:
5613
diff
changeset
|
186 if(!picture->forward_reference_frame->base[0]){ |
|
0ff1b9ab7afc
slices+field pictures fixed, initial sig11 workaround
arpi
parents:
5613
diff
changeset
|
187 // workaround for sig11 |
|
0ff1b9ab7afc
slices+field pictures fixed, initial sig11 workaround
arpi
parents:
5613
diff
changeset
|
188 picture->forward_reference_frame->base[0]=mpi->planes[0]; |
|
0ff1b9ab7afc
slices+field pictures fixed, initial sig11 workaround
arpi
parents:
5613
diff
changeset
|
189 picture->forward_reference_frame->base[1]=mpi->planes[1]; |
|
0ff1b9ab7afc
slices+field pictures fixed, initial sig11 workaround
arpi
parents:
5613
diff
changeset
|
190 picture->forward_reference_frame->base[2]=mpi->planes[2]; |
|
0ff1b9ab7afc
slices+field pictures fixed, initial sig11 workaround
arpi
parents:
5613
diff
changeset
|
191 } |
|
0ff1b9ab7afc
slices+field pictures fixed, initial sig11 workaround
arpi
parents:
5613
diff
changeset
|
192 if(!picture->backward_reference_frame->base[0]){ |
|
0ff1b9ab7afc
slices+field pictures fixed, initial sig11 workaround
arpi
parents:
5613
diff
changeset
|
193 // workaround for sig11 |
|
0ff1b9ab7afc
slices+field pictures fixed, initial sig11 workaround
arpi
parents:
5613
diff
changeset
|
194 picture->backward_reference_frame->base[0]=mpi->planes[0]; |
|
0ff1b9ab7afc
slices+field pictures fixed, initial sig11 workaround
arpi
parents:
5613
diff
changeset
|
195 picture->backward_reference_frame->base[1]=mpi->planes[1]; |
|
0ff1b9ab7afc
slices+field pictures fixed, initial sig11 workaround
arpi
parents:
5613
diff
changeset
|
196 picture->backward_reference_frame->base[2]=mpi->planes[2]; |
|
0ff1b9ab7afc
slices+field pictures fixed, initial sig11 workaround
arpi
parents:
5613
diff
changeset
|
197 } |
|
0ff1b9ab7afc
slices+field pictures fixed, initial sig11 workaround
arpi
parents:
5613
diff
changeset
|
198 #endif |
| 5516 | 199 #ifdef MPEG12_POSTPROC |
| 5515 | 200 mpi->qscale=&picture->current_frame->quant_store[1][1]; |
| 201 mpi->qstride=(MPEG2_MBC+1); | |
| 5516 | 202 #endif |
| 5465 | 203 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"mpeg2: [%c] %p %s \n", |
| 204 (picture->picture_coding_type == B_TYPE) ? 'B':'P', | |
| 205 mpi, (mpi->flags&MP_IMGFLAG_DIRECT)?"DR!":""); | |
| 206 } | |
| 207 | |
| 208 picture->current_frame->vo=sh->video_out; | |
| 209 picture->slice=0; | |
| 210 | |
| 211 } | |
| 212 | |
| 213 if (!drop_frame) { | |
| 214 slice_process (picture, code, buffer); | |
| 215 #ifdef ARCH_X86 | |
| 216 if (config.flags & MM_ACCEL_X86_MMX) __asm__ __volatile__ ("emms"); | |
| 217 #endif | |
| 218 } | |
| 219 | |
| 5145 | 220 } |
| 4998 | 221 return mpi; |
| 222 } | |
| 223 | |
| 5465 | 224 #ifdef USE_SIGJMP_TRICK |
| 225 | |
| 226 static jmp_buf mpeg2_jmp_buf; | |
| 227 | |
| 228 static void mpeg2_sighandler(int sig){ | |
| 229 longjmp(mpeg2_jmp_buf,1); | |
| 230 } | |
| 231 #endif | |
| 232 | |
| 233 // decode a frame | |
| 234 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){ | |
| 235 static uint32_t code; | |
| 236 static uint8_t* pos; | |
| 237 static uint8_t* current; | |
| 238 uint8_t* end=data+len; | |
| 239 static mp_image_t* mpi; | |
| 240 mp_image_t* ret=NULL; | |
| 241 int framedrop=flags&3; | |
| 242 void* old_sigh; | |
| 243 | |
| 244 // Note: static is REQUIRED because of longjmp() may destroy stack! | |
| 245 pos=NULL; | |
| 246 current=data; | |
| 247 mpi=NULL; | |
| 248 | |
| 249 #ifdef USE_SIGJMP_TRICK | |
| 250 old_sigh=signal(SIGSEGV,mpeg2_sighandler); | |
| 251 #endif | |
| 252 | |
| 253 while(current<end){ | |
| 254 // FIND NEXT HEAD: | |
| 255 static unsigned int head; | |
| 256 static uint8_t c; | |
| 257 head=-1; | |
| 258 //-------------------- | |
| 259 while(current<end){ | |
| 260 c=current[0]; | |
| 261 ++current; | |
| 262 head<<=8; | |
| 263 if(head==0x100) break; // synced | |
| 264 head|=c; | |
| 265 } | |
| 266 //-------------------- | |
| 267 if(pos){ | |
| 268 #ifdef USE_SIGJMP_TRICK | |
| 269 if(setjmp(mpeg2_jmp_buf)){ | |
| 270 #ifdef ARCH_X86 | |
| 271 if (config.flags & MM_ACCEL_X86_MMX) __asm__ __volatile__ ("emms"); | |
| 272 #endif | |
| 273 printf("@@@ libmpeg2 returned from sig11... (bad file?) @@@\n"); | |
| 274 } else | |
| 275 #endif | |
| 276 { | |
| 277 ret=parse_chunk(sh, code&0xFF, pos, framedrop); | |
| 278 if(ret) mpi=ret; | |
| 279 } | |
| 280 } | |
| 281 //-------------------- | |
| 282 pos=current;code=head|c; | |
| 283 } | |
| 284 | |
| 285 #ifdef USE_SIGJMP_TRICK | |
| 286 signal(SIGSEGV,old_sigh); // restore sighandler | |
| 287 #endif | |
| 288 | |
| 7464 | 289 // if(code==0x1FF){ |
| 5465 | 290 ret=parse_chunk(sh, 0xFF, NULL, framedrop); // send 'end of frame' |
| 291 if(ret) mpi=ret; | |
| 7464 | 292 // } |
| 5465 | 293 |
| 294 return mpi; | |
| 295 } |
