Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

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