Rev 1624 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1624 | giacomo | 1 | |
2 | /* mpeg2dec.c, main(), initialization, option processing */ |
||
3 | |||
4 | /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */ |
||
5 | |||
6 | /* |
||
7 | * Disclaimer of Warranty |
||
8 | * |
||
9 | * These software programs are available to the user without any license fee or |
||
10 | * royalty on an "as is" basis. The MPEG Software Simulation Group disclaims |
||
11 | * any and all warranties, whether express, implied, or statuary, including any |
||
12 | * implied warranties or merchantability or of fitness for a particular |
||
13 | * purpose. In no event shall the copyright-holder be liable for any |
||
14 | * incidental, punitive, or consequential damages of any kind whatsoever |
||
15 | * arising from the use of these programs. |
||
16 | * |
||
17 | * This disclaimer of warranty extends to the user of these programs and user's |
||
18 | * customers, employees, agents, transferees, successors, and assigns. |
||
19 | * |
||
20 | * The MPEG Software Simulation Group does not represent or warrant that the |
||
21 | * programs furnished hereunder are free of infringement of any third-party |
||
22 | * patents. |
||
23 | * |
||
24 | * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware, |
||
25 | * are subject to royalty fees to patent holders. Many of these patents are |
||
26 | * general enough such that they are unavoidable regardless of implementation |
||
27 | * design. |
||
28 | * |
||
29 | */ |
||
30 | |||
31 | #include "stdlib.h" |
||
32 | #include "drivers/glib.h" |
||
33 | |||
34 | #include "fsf_contract.h" |
||
35 | |||
36 | #define GLOBAL |
||
37 | #include "config.h" |
||
38 | #include "global.h" |
||
39 | |||
40 | /* private prototypes */ |
||
41 | static int video_sequence _ANSI_ARGS_((int *framenum)); |
||
42 | static int Headers _ANSI_ARGS_((void)); |
||
43 | static void Initialize_Sequence _ANSI_ARGS_((void)); |
||
44 | static void Initialize_Decoder _ANSI_ARGS_((void)); |
||
45 | static void Deinitialize_Sequence _ANSI_ARGS_((void)); |
||
46 | |||
47 | /* #define DEBUG */ |
||
48 | |||
49 | static void Clear_Options(); |
||
50 | #ifdef DEBUG |
||
51 | static void Print_Options(); |
||
52 | #endif |
||
53 | |||
54 | #define MAX_M_QOS 0 |
||
55 | #define MIN_M_QOS 0 |
||
56 | |||
57 | int dos_video_preload(char *file_name, long max_size, void **start, void **end) |
||
58 | { |
||
59 | DOS_FILE* file; |
||
60 | void *buf; |
||
61 | long rd; |
||
62 | |||
63 | |||
64 | file = DOS_fopen(file_name,"r"); |
||
65 | if (file == NULL) return -1; |
||
66 | |||
67 | buf = malloc(max_size); |
||
68 | *start = buf; |
||
69 | |||
70 | while(((rd = DOS_fread(buf, sizeof(BYTE), 2048, file)) == 2048) && |
||
71 | ((buf - *start + rd) < (max_size-2048))) { |
||
72 | buf += rd; |
||
73 | } |
||
74 | |||
75 | *end = buf + rd; |
||
76 | |||
77 | DOS_fclose(file); |
||
78 | return(0); |
||
79 | |||
80 | } |
||
81 | |||
82 | int Init_Mpeg_Decoder(void *start, void *end) |
||
83 | { |
||
84 | int code; |
||
85 | |||
86 | Clear_Options(); |
||
87 | |||
88 | #ifdef DEBUG |
||
89 | Print_Options(); |
||
90 | #endif |
||
91 | |||
92 | ld = &base; /* select base layer context */ |
||
93 | ld->start_file_ptr = start; |
||
94 | ld->end_file_ptr = end; |
||
95 | ld->actual_file_ptr = ld->start_file_ptr; |
||
96 | |||
97 | /* open MPEG base layer bitstream file(s) */ |
||
98 | /* NOTE: this is either a base layer stream or a spatial enhancement stream */ |
||
99 | if ((base.Infile=1)<0) |
||
100 | { |
||
101 | cprintf("Base layer input file %s not found\n", Main_Bitstream_Filename); |
||
102 | } |
||
103 | |||
104 | |||
105 | if(base.Infile != 0) |
||
106 | { |
||
107 | Initialize_Buffer(); |
||
108 | |||
109 | if(Show_Bits(8)==0x47) |
||
110 | { |
||
111 | sprintf(Error_Text,"Decoder currently does not parse transport streams\n"); |
||
112 | Error(Error_Text); |
||
113 | } |
||
114 | |||
115 | next_start_code(); |
||
116 | code = Show_Bits(32); |
||
117 | |||
118 | switch(code) |
||
119 | { |
||
120 | case SEQUENCE_HEADER_CODE: |
||
121 | break; |
||
122 | case PACK_START_CODE: |
||
123 | System_Stream_Flag = 1; |
||
124 | case VIDEO_ELEMENTARY_STREAM: |
||
125 | System_Stream_Flag = 1; |
||
126 | break; |
||
127 | default: |
||
128 | sprintf(Error_Text,"Unable to recognize stream type\n"); |
||
129 | Error(Error_Text); |
||
130 | break; |
||
131 | } |
||
132 | |||
133 | ld->actual_file_ptr = ld->start_file_ptr; |
||
134 | Initialize_Buffer(); |
||
135 | } |
||
136 | |||
137 | if(base.Infile!=0) |
||
138 | { |
||
139 | ld->actual_file_ptr = ld->start_file_ptr; |
||
140 | } |
||
141 | |||
142 | Initialize_Buffer(); |
||
143 | |||
144 | if(Two_Streams) |
||
145 | { |
||
146 | ld = &enhan; /* select enhancement layer context */ |
||
147 | |||
148 | if ((enhan.Infile = 1)<0) |
||
149 | { |
||
150 | sprintf(Error_Text,"enhancment layer bitstream file %s not found\n", |
||
151 | Enhancement_Layer_Bitstream_Filename); |
||
152 | |||
153 | Error(Error_Text); |
||
154 | } |
||
155 | |||
156 | Initialize_Buffer(); |
||
157 | ld = &base; |
||
158 | } |
||
159 | |||
160 | Initialize_Decoder(); |
||
161 | |||
162 | //Decode_Bitstream(); |
||
163 | |||
164 | return 0; |
||
165 | } |
||
166 | |||
167 | /* IMPLEMENTAION specific rouintes */ |
||
168 | static void Initialize_Decoder() |
||
169 | { |
||
170 | int i; |
||
171 | |||
172 | /* Clip table */ |
||
173 | if (!(Clip=(unsigned char *)malloc(1024))) |
||
174 | Error("Clip[] malloc failed\n"); |
||
175 | |||
176 | Clip += 384; |
||
177 | |||
178 | for (i=-384; i<640; i++) |
||
179 | Clip[i] = (i<0) ? 0 : ((i>255) ? 255 : i); |
||
180 | |||
181 | /* IDCT */ |
||
182 | if (Reference_IDCT_Flag) |
||
183 | Initialize_Reference_IDCT(); |
||
184 | else |
||
185 | Initialize_Fast_IDCT(); |
||
186 | |||
187 | } |
||
188 | |||
189 | /* mostly IMPLEMENTAION specific rouintes */ |
||
190 | static void Initialize_Sequence() |
||
191 | { |
||
192 | int cc, size; |
||
193 | static int Table_6_20[3] = {6,8,12}; |
||
194 | |||
195 | /* check scalability mode of enhancement layer */ |
||
196 | if (Two_Streams && (enhan.scalable_mode!=SC_SNR) && (base.scalable_mode!=SC_DP)) |
||
197 | Error("unsupported scalability mode\n"); |
||
198 | |||
199 | /* force MPEG-1 parameters for proper decoder behavior */ |
||
200 | /* see ISO/IEC 13818-2 section D.9.14 */ |
||
201 | if (!base.MPEG2_Flag) |
||
202 | { |
||
203 | progressive_sequence = 1; |
||
204 | progressive_frame = 1; |
||
205 | picture_structure = FRAME_PICTURE; |
||
206 | frame_pred_frame_dct = 1; |
||
207 | chroma_format = CHROMA420; |
||
208 | matrix_coefficients = 5; |
||
209 | } |
||
210 | |||
211 | /* round to nearest multiple of coded macroblocks */ |
||
212 | /* ISO/IEC 13818-2 section 6.3.3 sequence_header() */ |
||
213 | mb_width = (horizontal_size+15)/16; |
||
214 | mb_height = (base.MPEG2_Flag && !progressive_sequence) ? 2*((vertical_size+31)/32) |
||
215 | : (vertical_size+15)/16; |
||
216 | |||
217 | Coded_Picture_Width = 16*mb_width; |
||
218 | Coded_Picture_Height = 16*mb_height; |
||
219 | |||
220 | /* ISO/IEC 13818-2 sections 6.1.1.8, 6.1.1.9, and 6.1.1.10 */ |
||
221 | Chroma_Width = (chroma_format==CHROMA444) ? Coded_Picture_Width |
||
222 | : Coded_Picture_Width>>1; |
||
223 | Chroma_Height = (chroma_format!=CHROMA420) ? Coded_Picture_Height |
||
224 | : Coded_Picture_Height>>1; |
||
225 | |||
226 | /* derived based on Table 6-20 in ISO/IEC 13818-2 section 6.3.17 */ |
||
227 | block_count = Table_6_20[chroma_format-1]; |
||
228 | |||
229 | for (cc=0; cc<3; cc++) |
||
230 | { |
||
231 | if (cc==0) |
||
232 | size = Coded_Picture_Width*Coded_Picture_Height; |
||
233 | else |
||
234 | size = Chroma_Width*Chroma_Height; |
||
235 | |||
236 | if (!(backward_reference_frame[cc] = (unsigned char *)malloc(size))) |
||
237 | Error("backward_reference_frame[] malloc failed\n"); |
||
238 | |||
239 | if (!(forward_reference_frame[cc] = (unsigned char *)malloc(size))) |
||
240 | Error("forward_reference_frame[] malloc failed\n"); |
||
241 | |||
242 | if (!(auxframe[cc] = (unsigned char *)malloc(size))) |
||
243 | Error("auxframe[] malloc failed\n"); |
||
244 | |||
245 | if(Ersatz_Flag) |
||
246 | if (!(substitute_frame[cc] = (unsigned char *)malloc(size))) |
||
247 | Error("substitute_frame[] malloc failed\n"); |
||
248 | |||
249 | |||
250 | if (base.scalable_mode==SC_SPAT) |
||
251 | { |
||
252 | /* this assumes lower layer is 4:2:0 */ |
||
253 | if (!(llframe0[cc] = (unsigned char *)malloc((lower_layer_prediction_horizontal_size*lower_layer_prediction_vertical_size)/(cc?4:1)))) |
||
254 | Error("llframe0 malloc failed\n"); |
||
255 | if (!(llframe1[cc] = (unsigned char *)malloc((lower_layer_prediction_horizontal_size*lower_layer_prediction_vertical_size)/(cc?4:1)))) |
||
256 | Error("llframe1 malloc failed\n"); |
||
257 | } |
||
258 | } |
||
259 | |||
260 | /* SCALABILITY: Spatial */ |
||
261 | if (base.scalable_mode==SC_SPAT) |
||
262 | { |
||
263 | if (!(lltmp = (short *)malloc(lower_layer_prediction_horizontal_size*((lower_layer_prediction_vertical_size*vertical_subsampling_factor_n)/vertical_subsampling_factor_m)*sizeof(short)))) |
||
264 | Error("lltmp malloc failed\n"); |
||
265 | } |
||
266 | |||
267 | #ifdef DISPLAY |
||
268 | if (Output_Type==T_X11) |
||
269 | { |
||
270 | Initialize_Display_Process(""); |
||
271 | Initialize_Dither_Matrix(); |
||
272 | } |
||
273 | #endif /* DISPLAY */ |
||
274 | |||
275 | } |
||
276 | |||
277 | void Error(text) |
||
278 | char *text; |
||
279 | { |
||
280 | cprintf(text); |
||
281 | } |
||
282 | |||
283 | /* Trace_Flag output */ |
||
284 | void Print_Bits(code,bits,len) |
||
285 | int code,bits,len; |
||
286 | { |
||
287 | int i; |
||
288 | for (i=0; i<len; i++) |
||
289 | cprintf("%d",(code>>(bits-1-i))&1); |
||
290 | } |
||
291 | |||
292 | static int Headers() |
||
293 | { |
||
294 | int ret; |
||
295 | |||
296 | ld = &base; |
||
297 | |||
298 | |||
299 | /* return when end of sequence (0) or picture |
||
300 | header has been parsed (1) */ |
||
301 | |||
302 | ret = Get_Hdr(); |
||
303 | |||
304 | |||
305 | if (Two_Streams) |
||
306 | { |
||
307 | ld = &enhan; |
||
308 | if (Get_Hdr()!=ret && !Quiet_Flag) |
||
309 | cprintf("streams out of sync\n"); |
||
310 | ld = &base; |
||
311 | } |
||
312 | |||
313 | return ret; |
||
314 | } |
||
315 | |||
316 | int Decode_Bitstream() |
||
317 | { |
||
318 | int ret; |
||
319 | int Bitstream_Framenum; |
||
320 | |||
321 | Bitstream_Framenum = 0; |
||
322 | |||
323 | for(;;) |
||
324 | { |
||
325 | |||
326 | #ifdef VERIFY |
||
327 | Clear_Verify_Headers(); |
||
328 | #endif /* VERIFY */ |
||
329 | |||
330 | ld->actual_file_ptr = ld->start_file_ptr; |
||
331 | |||
332 | ret = Headers(); |
||
333 | |||
334 | if(ret==1) |
||
335 | { |
||
336 | ret = video_sequence(&Bitstream_Framenum); |
||
337 | } |
||
338 | else |
||
339 | return(ret); |
||
340 | } |
||
341 | |||
342 | } |
||
343 | |||
344 | |||
345 | static void Deinitialize_Sequence() |
||
346 | { |
||
347 | int i; |
||
348 | |||
349 | /* clear flags */ |
||
350 | base.MPEG2_Flag=0; |
||
351 | |||
352 | for(i=0;i<3;i++) |
||
353 | { |
||
354 | free(backward_reference_frame[i]); |
||
355 | free(forward_reference_frame[i]); |
||
356 | free(auxframe[i]); |
||
357 | |||
358 | if (base.scalable_mode==SC_SPAT) |
||
359 | { |
||
360 | free(llframe0[i]); |
||
361 | free(llframe1[i]); |
||
362 | } |
||
363 | } |
||
364 | |||
365 | if (base.scalable_mode==SC_SPAT) |
||
366 | free(lltmp); |
||
367 | |||
368 | #ifdef DISPLAY |
||
369 | if (Output_Type==T_X11) |
||
370 | Terminate_Display_Process(); |
||
371 | #endif |
||
372 | } |
||
373 | |||
374 | |||
375 | static int video_sequence(Bitstream_Framenumber) |
||
376 | int *Bitstream_Framenumber; |
||
377 | { |
||
378 | int Bitstream_Framenum; |
||
379 | int Sequence_Framenum; |
||
380 | int Return_Value; |
||
381 | |||
382 | char tmp[100]; |
||
383 | |||
384 | Bitstream_Framenum = *Bitstream_Framenumber; |
||
385 | Sequence_Framenum=0; |
||
386 | |||
387 | Initialize_Sequence(); |
||
388 | |||
389 | /* decode picture whose header has already been parsed in |
||
390 | Decode_Bitstream() */ |
||
391 | |||
392 | ld->px = 310+(rand()%(500-Coded_Picture_Width)); |
||
393 | ld->py = 100+(rand()%(400-Coded_Picture_Height)); |
||
394 | |||
395 | sprintf(tmp,"Wx = %3d Wy = %3d",Coded_Picture_Width,Coded_Picture_Height); |
||
396 | grx_text(tmp,ld->px,ld->py-10,rgb16(255,255,255),0); |
||
397 | |||
398 | Decode_Picture(Bitstream_Framenum, Sequence_Framenum); |
||
399 | |||
400 | /* update picture numbers */ |
||
401 | if (!Second_Field) |
||
402 | { |
||
403 | Bitstream_Framenum++; |
||
404 | Sequence_Framenum++; |
||
405 | } |
||
406 | |||
407 | fsf_schedule_next_timed_job(NULL, NULL, NULL, NULL, NULL); |
||
408 | |||
409 | /* loop through the rest of the pictures in the sequence */ |
||
410 | while ((Return_Value=Headers())) |
||
411 | { |
||
412 | Decode_Picture(Bitstream_Framenum, Sequence_Framenum); |
||
413 | |||
414 | if (!Second_Field) |
||
415 | { |
||
416 | Bitstream_Framenum++; |
||
417 | Sequence_Framenum++; |
||
418 | } |
||
419 | |||
420 | fsf_schedule_next_timed_job(NULL, NULL, NULL, NULL, NULL); |
||
421 | |||
422 | } |
||
423 | |||
424 | /* put last frame */ |
||
425 | if (Sequence_Framenum!=0) |
||
426 | { |
||
427 | Output_Last_Frame_of_Sequence(Bitstream_Framenum); |
||
428 | } |
||
429 | |||
430 | Deinitialize_Sequence(); |
||
431 | |||
432 | #ifdef VERIFY |
||
433 | Clear_Verify_Headers(); |
||
434 | #endif /* VERIFY */ |
||
435 | |||
436 | *Bitstream_Framenumber = Bitstream_Framenum; |
||
437 | return(Return_Value); |
||
438 | } |
||
439 | |||
440 | |||
441 | |||
442 | static void Clear_Options() |
||
443 | { |
||
444 | Verbose_Flag = 0; |
||
445 | Output_Type = 0; |
||
446 | Output_Picture_Filename = " "; |
||
447 | hiQdither = 0; |
||
448 | Output_Type = 0; |
||
449 | Frame_Store_Flag = 0; |
||
450 | Spatial_Flag = 0; |
||
451 | Lower_Layer_Picture_Filename = " "; |
||
452 | Reference_IDCT_Flag = 0; |
||
453 | Trace_Flag = 0; |
||
454 | Quiet_Flag = 0; |
||
455 | Ersatz_Flag = 0; |
||
456 | Substitute_Picture_Filename = " "; |
||
457 | Two_Streams = 0; |
||
458 | Enhancement_Layer_Bitstream_Filename = " "; |
||
459 | Big_Picture_Flag = 0; |
||
460 | Main_Bitstream_Flag = 0; |
||
461 | Main_Bitstream_Filename = " "; |
||
462 | Verify_Flag = 0; |
||
463 | Stats_Flag = 0; |
||
464 | User_Data_Flag = 0; |
||
465 | } |
||
466 | |||
467 | |||
468 | #ifdef DEBUG |
||
469 | static void Print_Options() |
||
470 | { |
||
471 | |||
472 | printf("Verbose_Flag = %d\n", Verbose_Flag); |
||
473 | printf("Output_Type = %d\n", Output_Type); |
||
474 | printf("Output_Picture_Filename = %s\n", Output_Picture_Filename); |
||
475 | printf("hiQdither = %d\n", hiQdither); |
||
476 | printf("Output_Type = %d\n", Output_Type); |
||
477 | printf("Frame_Store_Flag = %d\n", Frame_Store_Flag); |
||
478 | printf("Spatial_Flag = %d\n", Spatial_Flag); |
||
479 | printf("Lower_Layer_Picture_Filename = %s\n", Lower_Layer_Picture_Filename); |
||
480 | printf("Reference_IDCT_Flag = %d\n", Reference_IDCT_Flag); |
||
481 | printf("Trace_Flag = %d\n", Trace_Flag); |
||
482 | printf("Quiet_Flag = %d\n", Quiet_Flag); |
||
483 | printf("Ersatz_Flag = %d\n", Ersatz_Flag); |
||
484 | printf("Substitute_Picture_Filename = %s\n", Substitute_Picture_Filename); |
||
485 | printf("Two_Streams = %d\n", Two_Streams); |
||
486 | printf("Enhancement_Layer_Bitstream_Filename = %s\n", Enhancement_Layer_Bitstream_Filename); |
||
487 | printf("Big_Picture_Flag = %d\n", Big_Picture_Flag); |
||
488 | printf("Main_Bitstream_Flag = %d\n", Main_Bitstream_Flag); |
||
489 | printf("Main_Bitstream_Filename = %s\n", Main_Bitstream_Filename); |
||
490 | printf("Verify_Flag = %d\n", Verify_Flag); |
||
491 | printf("Stats_Flag = %d\n", Stats_Flag); |
||
492 | printf("User_Data_Flag = %d\n", User_Data_Flag); |
||
493 | |||
494 | } |
||
495 | #endif |