Mercurial > mplayer.hg
annotate libmpeg2/decode.c @ 1901:761836b1fe5a
ffodivx: opendivx->mpeg4 (Ivan Kalvatchev)
author | arpi |
---|---|
date | Mon, 17 Sep 2001 01:54:07 +0000 |
parents | 1d741dfa190c |
children | 7de4eceac32f |
rev | line source |
---|---|
1 | 1 /* Copyright (C) Aaron Holtzman <aholtzma@ess.engr.uvic.ca> - Nov 1999 */ |
2 /* Some cleanup & hacking by A'rpi/ESP-team - Oct 2000 */ | |
3 | |
4 /* mpeg2dec version: */ | |
5 #define PACKAGE "mpeg2dec" | |
36 | 6 #define VERSION "0.2.0-release" |
1 | 7 |
8 #include <stdio.h> | |
9 #include <stdlib.h> | |
10 #include <unistd.h> | |
11 #include <fcntl.h> | |
12 #include <errno.h> | |
13 | |
111 | 14 #include <signal.h> |
15 #include <setjmp.h> | |
16 | |
17 | |
1 | 18 #include "config.h" |
19 | |
36 | 20 #include "video_out.h" |
21 #include <inttypes.h> | |
1 | 22 |
23 #include "mpeg2.h" | |
24 #include "mpeg2_internal.h" | |
25 | |
26 #include "../linux/shmem.h" | |
27 | |
28 //#include "motion_comp.h" | |
29 //#include "idct.h" | |
30 //#include "header.h" | |
31 //#include "slice.h" | |
32 //#include "stats.h" | |
33 | |
34 #include "attributes.h" | |
35 #ifdef __i386__ | |
36 #include "mmx.h" | |
37 #endif | |
38 | |
36 | 39 #include "mm_accel.h" |
40 | |
41 | |
1 | 42 //this is where we keep the state of the decoder |
43 //picture_t picture_data; | |
44 //picture_t *picture=&picture_data; | |
45 picture_t *picture=NULL; | |
46 | |
47 //global config struct | |
48 mpeg2_config_t config; | |
49 | |
50 // the maximum chunk size is determined by vbv_buffer_size which is 224K for | |
51 // MP@ML streams. (we make no pretenses ofdecoding anything more than that) | |
52 //static uint8_t chunk_buffer[224 * 1024 + 4]; | |
53 //static uint32_t shift = 0; | |
54 | |
967 | 55 //static int drop_flag = 0; |
1 | 56 static int drop_frame = 0; |
57 | |
41 | 58 #ifdef MPEG12_POSTPROC |
1 | 59 int quant_store[MBR+1][MBC+1]; // [Review] |
36 | 60 #endif |
1 | 61 |
1636 | 62 static table_init_state=0; |
63 | |
1 | 64 void mpeg2_init (void) |
65 { | |
66 | |
499 | 67 printf (PACKAGE"-"VERSION" (C) 2000-2001 Aaron Holtzman & Michel Lespinasse\n"); |
1 | 68 config.flags = 0; |
69 #ifdef HAVE_MMX | |
70 config.flags |= MM_ACCEL_X86_MMX; | |
71 #endif | |
72 #ifdef HAVE_SSE | |
73 config.flags |= MM_ACCEL_X86_MMXEXT; | |
74 #endif | |
75 #ifdef HAVE_3DNOW | |
76 config.flags |= MM_ACCEL_X86_3DNOW; | |
77 #endif | |
78 #ifdef HAVE_MLIB | |
79 config.flags |= MM_ACCEL_MLIB; | |
80 #endif | |
81 | |
499 | 82 // printf("libmpeg2 config flags = 0x%X\n",config.flags); |
1 | 83 |
1652
e78cf3fc992e
shmem_alloc->memalign, adding function to free memory
arpi
parents:
1636
diff
changeset
|
84 picture=malloc(sizeof(picture_t)); // !!! NEW HACK :) !!! |
1655 | 85 memset(picture,0,sizeof(picture_t)); |
1 | 86 |
87 header_state_init (picture); | |
36 | 88 // picture->repeat_count=0; |
1 | 89 |
90 picture->pp_options=0; | |
91 | |
1636 | 92 if(!table_init_state){ |
93 idct_init (); | |
94 motion_comp_init (); | |
95 table_init_state=1; | |
96 } | |
1 | 97 } |
98 | |
41 | 99 static vo_frame_t frames[4]; |
36 | 100 |
1 | 101 void mpeg2_allocate_image_buffers (picture_t * picture) |
102 { | |
103 int frame_size,buff_size; | |
104 unsigned char *base=NULL; | |
36 | 105 int i; |
1 | 106 |
107 // height+1 requires for yuv2rgb_mmx code (it reads next line after last) | |
108 frame_size = picture->coded_picture_width * (1+picture->coded_picture_height); | |
109 frame_size = (frame_size+31)&(~31); // align to 32 byte boundary | |
110 buff_size = frame_size + (frame_size/4)*2; // 4Y + 1U + 1V | |
111 | |
112 // allocate images in YV12 format | |
41 | 113 #ifdef MPEG12_POSTPROC |
114 for(i=0;i<4;i++){ | |
115 #else | |
36 | 116 for(i=0;i<3;i++){ |
41 | 117 #endif |
1652
e78cf3fc992e
shmem_alloc->memalign, adding function to free memory
arpi
parents:
1636
diff
changeset
|
118 base = memalign(64,buff_size); |
36 | 119 frames[i].base[0] = base; |
120 frames[i].base[1] = base + frame_size * 5 / 4; | |
121 frames[i].base[2] = base + frame_size; | |
122 frames[i].copy = NULL; | |
123 frames[i].vo = NULL; | |
124 } | |
125 | |
126 picture->forward_reference_frame=&frames[0]; | |
127 picture->backward_reference_frame=&frames[1]; | |
128 picture->current_frame=&frames[2]; | |
1 | 129 |
130 } | |
131 | |
1652
e78cf3fc992e
shmem_alloc->memalign, adding function to free memory
arpi
parents:
1636
diff
changeset
|
132 void mpeg2_free_image_buffers (picture_t * picture){ |
e78cf3fc992e
shmem_alloc->memalign, adding function to free memory
arpi
parents:
1636
diff
changeset
|
133 int i; |
e78cf3fc992e
shmem_alloc->memalign, adding function to free memory
arpi
parents:
1636
diff
changeset
|
134 |
e78cf3fc992e
shmem_alloc->memalign, adding function to free memory
arpi
parents:
1636
diff
changeset
|
135 #ifdef MPEG12_POSTPROC |
e78cf3fc992e
shmem_alloc->memalign, adding function to free memory
arpi
parents:
1636
diff
changeset
|
136 for(i=0;i<4;i++){ |
e78cf3fc992e
shmem_alloc->memalign, adding function to free memory
arpi
parents:
1636
diff
changeset
|
137 #else |
e78cf3fc992e
shmem_alloc->memalign, adding function to free memory
arpi
parents:
1636
diff
changeset
|
138 for(i=0;i<3;i++){ |
e78cf3fc992e
shmem_alloc->memalign, adding function to free memory
arpi
parents:
1636
diff
changeset
|
139 #endif |
e78cf3fc992e
shmem_alloc->memalign, adding function to free memory
arpi
parents:
1636
diff
changeset
|
140 free(frames[i].base[0]); |
e78cf3fc992e
shmem_alloc->memalign, adding function to free memory
arpi
parents:
1636
diff
changeset
|
141 } |
e78cf3fc992e
shmem_alloc->memalign, adding function to free memory
arpi
parents:
1636
diff
changeset
|
142 |
e78cf3fc992e
shmem_alloc->memalign, adding function to free memory
arpi
parents:
1636
diff
changeset
|
143 } |
e78cf3fc992e
shmem_alloc->memalign, adding function to free memory
arpi
parents:
1636
diff
changeset
|
144 |
36 | 145 static void copy_slice (vo_frame_t * frame, uint8_t ** src){ |
146 vo_functions_t * output = frame->vo; | |
147 int stride[3]; | |
79 | 148 int y=picture->slice<<4; |
149 uint8_t* src_tmp[3]; | |
1 | 150 |
36 | 151 stride[0]=picture->coded_picture_width; |
152 stride[1]=stride[2]=stride[0]/2; | |
79 | 153 |
154 if(frame!=picture->display_frame){ | |
155 uint8_t** base=picture->display_frame->base; | |
156 src_tmp[0]=base[0]+stride[0]*y; | |
157 src_tmp[1]=base[1]+stride[1]*(y>>1); | |
158 src_tmp[2]=base[2]+stride[2]*(y>>1); | |
159 src=src_tmp; | |
160 } | |
161 | |
162 output->draw_slice (src, | |
163 stride, picture->display_picture_width, | |
36 | 164 (y+16<=picture->display_picture_height) ? 16 : |
165 picture->display_picture_height-y, | |
166 0, y); | |
1 | 167 |
79 | 168 ++picture->slice; |
1 | 169 } |
170 | |
171 static int in_slice_flag=0; | |
172 | |
967 | 173 static int parse_chunk (vo_functions_t * output, int code, uint8_t * buffer, int framedrop) |
1 | 174 { |
175 int is_frame_done = 0; | |
176 | |
177 stats_header (code, buffer); | |
178 | |
179 is_frame_done = in_slice_flag && ((!code) || (code >= 0xb0)); | |
180 if (is_frame_done) { | |
181 in_slice_flag = 0; | |
182 | |
36 | 183 // if(picture->picture_structure != FRAME_PICTURE) printf("Field! %d \n",picture->second_field); |
1 | 184 |
967 | 185 if(!framedrop) |
36 | 186 if (((picture->picture_structure == FRAME_PICTURE) || |
1 | 187 (picture->second_field)) |
36 | 188 ) { |
41 | 189 #ifdef MPEG12_POSTPROC |
190 if(picture->pp_options){ | |
191 // apply OpenDivX postprocess filter | |
192 int stride[3]; | |
193 stride[0]=picture->coded_picture_width; | |
194 stride[1]=stride[2]=stride[0]/2; | |
195 postprocess((picture->picture_coding_type == B_TYPE) ? | |
196 picture->current_frame->base : | |
197 picture->forward_reference_frame->base, | |
198 stride[0], frames[3].base, stride[0], | |
199 picture->coded_picture_width, picture->coded_picture_height, | |
200 &quant_store[1][1], (MBC+1), picture->pp_options); | |
201 output->draw_slice (frames[3].base, stride, | |
202 picture->display_picture_width, | |
203 picture->display_picture_height, 0, 0); | |
79 | 204 }// else |
41 | 205 #endif |
79 | 206 #if 0 |
36 | 207 if (picture->picture_coding_type != B_TYPE) { |
208 int stride[3]; | |
209 stride[0]=picture->coded_picture_width; | |
210 stride[1]=stride[2]=stride[0]/2; | |
211 output->draw_slice (picture->forward_reference_frame->base, | |
212 stride, | |
1 | 213 picture->display_picture_width, |
214 picture->display_picture_height, 0, 0); | |
215 } | |
36 | 216 #endif |
217 } | |
1 | 218 #ifdef ARCH_X86 |
36 | 219 if (config.flags & MM_ACCEL_X86_MMX) emms(); |
1 | 220 #endif |
116 | 221 // output->flip_page(); |
1 | 222 } |
223 | |
224 switch (code) { | |
225 case 0x00: /* picture_start_code */ | |
226 if (header_process_picture_header (picture, buffer)) { | |
227 printf ("bad picture header\n"); | |
114 | 228 //exit (1); |
1 | 229 } |
230 | |
967 | 231 drop_frame = framedrop && (picture->picture_coding_type == B_TYPE); |
232 drop_frame |= framedrop>=2; // hard drop | |
1 | 233 //decode_reorder_frames (); |
234 break; | |
235 | |
236 case 0xb3: /* sequence_header_code */ | |
237 if (header_process_sequence_header (picture, buffer)) { | |
238 printf ("bad sequence header\n"); | |
114 | 239 //exit (1); |
1 | 240 } |
241 break; | |
242 | |
243 case 0xb5: /* extension_start_code */ | |
244 if (header_process_extension (picture, buffer)) { | |
245 printf ("bad extension\n"); | |
114 | 246 //exit (1); |
1 | 247 } |
248 break; | |
249 | |
250 default: | |
251 // if (code >= 0xb9) printf ("stream not demultiplexed ?\n"); | |
252 if (code >= 0xb0) break; | |
253 | |
254 if (!(in_slice_flag)) { | |
255 in_slice_flag = 1; | |
256 | |
36 | 257 // if(!(picture->second_field)) decode_reorder_frames (); |
258 | |
259 // set current_frame pointer: | |
260 if (picture->second_field){ | |
261 // vo_field (picture->current_frame, picture->picture_structure); | |
262 } else { | |
263 if (picture->picture_coding_type == B_TYPE){ | |
79 | 264 picture->display_frame= |
36 | 265 picture->current_frame = &frames[2]; |
79 | 266 // picture->current_frame->copy=copy_slice; |
267 } else { | |
268 picture->current_frame = picture->forward_reference_frame; | |
269 picture->display_frame= | |
270 picture->forward_reference_frame = picture->backward_reference_frame; | |
271 picture->backward_reference_frame = picture->current_frame; | |
272 // picture->current_frame->copy=NULL; | |
273 } | |
274 } | |
275 | |
276 #if 1 | |
41 | 277 #ifdef MPEG12_POSTPROC |
278 if(picture->pp_options) | |
279 picture->current_frame->copy=NULL; else | |
280 #endif | |
36 | 281 picture->current_frame->copy=copy_slice; |
79 | 282 #endif |
967 | 283 if(framedrop) picture->current_frame->copy=NULL; |
36 | 284 picture->current_frame->vo=output; |
79 | 285 picture->slice=0; |
36 | 286 |
1 | 287 } |
288 | |
289 if (!drop_frame) { | |
290 | |
291 slice_process (picture, code, buffer); | |
292 | |
293 #ifdef ARCH_X86 | |
294 if (config.flags & MM_ACCEL_X86_MMX) emms (); | |
295 #endif | |
296 | |
297 } | |
298 } | |
299 | |
300 return is_frame_done; | |
301 } | |
302 | |
111 | 303 static jmp_buf mpeg2_jmp_buf; |
304 | |
305 static void mpeg2_sighandler(int sig){ | |
306 longjmp(mpeg2_jmp_buf,1); | |
307 } | |
1 | 308 |
967 | 309 int mpeg2_decode_data (vo_functions_t *output, uint8_t *current, uint8_t *end,int framedrop) |
1 | 310 { |
311 //static uint8_t code = 0xff; | |
312 //static uint8_t chunk_buffer[65536]; | |
313 //static uint8_t *chunk_ptr = chunk_buffer; | |
314 //static uint32_t shift = 0; | |
315 uint8_t code; | |
316 uint8_t *pos=NULL; | |
317 uint8_t *start=current; | |
318 int ret = 0; | |
111 | 319 void* old_sigh; |
320 | |
321 if(setjmp(mpeg2_jmp_buf)!=0){ | |
322 printf("@@@ FATAL!!!??? libmpeg2 returned from sig11 before the actual decoding! @@@\n"); | |
323 return 0; | |
324 } | |
325 | |
326 old_sigh=signal(SIGSEGV,mpeg2_sighandler); | |
1 | 327 |
328 // printf("RCVD %d bytes\n",end-current); | |
329 | |
330 while(current<end){ | |
331 // FIND NEXT HEAD: | |
332 unsigned int head=-1; | |
333 uint8_t c; | |
334 //-------------------- | |
335 while(current<end){ | |
336 c=current[0]; | |
337 ++current; | |
338 head<<=8; | |
339 if(head==0x100) break; // synced | |
340 head|=c; | |
341 } | |
342 //-------------------- | |
343 if(pos){ | |
344 //if((code&0x100)!=0x100) printf("libmpeg2: FATAL! code=%X\n",code); | |
345 //printf("pos=%d chunk %3X size=%d next-code=%X\n",pos-start,code,current-pos,head|c); | |
111 | 346 if(setjmp(mpeg2_jmp_buf)==0){ |
967 | 347 ret+=parse_chunk(output, code&0xFF, pos, framedrop); |
111 | 348 } else { |
204 | 349 #ifdef ARCH_X86 |
350 if (config.flags & MM_ACCEL_X86_MMX) emms (); | |
351 #endif | |
499 | 352 printf("@@@ libmpeg2 returned from sig11... (bad file?) @@@\n"); |
111 | 353 } |
1 | 354 } |
355 //-------------------- | |
356 pos=current;code=head|c; | |
357 } | |
358 | |
111 | 359 signal(SIGSEGV,old_sigh); // restore sighandler |
360 | |
967 | 361 if(code==0x1FF) ret+=parse_chunk(output, 0xFF, NULL, framedrop); // send 'end of frame' |
1 | 362 |
363 return ret; | |
364 } | |
365 | |
967 | 366 //void mpeg2_drop (int flag) |
367 //{ | |
368 // drop_flag = flag; | |
369 //} | |
1 | 370 |