Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1671 tullio 1
/* #define DEBUG */
2
/* subspic.c, Frame buffer substitution routines */
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
 
33
#include "config.h"
34
#include "global.h"
35
 
36
/* private prototypes*/
37
static void Read_Frame(char *filename,
38
  unsigned char *frame_buffer[], int framenum);
39
static void Copy_Frame(unsigned char *src, unsigned char *dst,
40
  int width, int height, int parity, int incr);
41
static int Read_Components(char *filename,
42
  unsigned char *frame[3], int framenum);
43
static int Read_Component(char *fname, unsigned char *frame,
44
  int width, int height);
45
static int Extract_Components(char *filename,
46
  unsigned char *frame[3], int framenum);
47
 
48
extern int read(int Infile, void *Rdbfr, int rdsize);
49
 
50
/* substitute frame buffer routine */
51
void Substitute_Frame_Buffer (bitstream_framenum, sequence_framenum)
52
int bitstream_framenum;
53
int sequence_framenum;
54
{
55
  /* static tracking variables */
56
  static int previous_temporal_reference;
57
  static int previous_bitstream_framenum;
58
  static int previous_anchor_temporal_reference;
59
  static int previous_anchor_bitstream_framenum;
60
  static int previous_picture_coding_type;
61
  static int bgate;
62
 
63
  /* local temporary variables */
64
  int substitute_display_framenum;
65
 
66
 
67
#ifdef DEBUG
68
  cprintf("SUB: seq fn(%d) bitfn(%d) tempref(%d) picstr(%d) type(%d)\n",
69
    sequence_framenum, bitstream_framenum, temporal_reference,
70
    picture_structure, picture_coding_type);
71
#endif
72
 
73
  /* we don't substitute at the first picture of a sequence */
74
  if((sequence_framenum!=0)||(Second_Field))
75
  {
76
    /* only at the start of the frame */
77
    if ((picture_structure==FRAME_PICTURE)||(!Second_Field))
78
    {
79
      if(picture_coding_type==P_TYPE)
80
      {
81
        /* the most recently decoded reference frame needs substituting */
82
        substitute_display_framenum = bitstream_framenum - 1;
83
 
84
        Read_Frame(Substitute_Picture_Filename, forward_reference_frame,
85
          substitute_display_framenum);
86
      }
87
      /* only the first B frame in a consequitve set of B pictures
88
         loads a substitute backward_reference_frame since all subsequent
89
         B frames predict from the same reference pictures */
90
      else if((picture_coding_type==B_TYPE)&&(bgate!=1))
91
      {
92
        substitute_display_framenum =
93
          (previous_temporal_reference - temporal_reference)
94
            + bitstream_framenum - 1;
95
 
96
        Read_Frame(Substitute_Picture_Filename, backward_reference_frame,
97
          substitute_display_framenum);
98
      }
99
    } /* P fields can predict from the two most recently decoded fields, even
100
         from the first field of the same frame being decoded */
101
    else if(Second_Field && (picture_coding_type==P_TYPE))
102
    {
103
      /* our favourite case: the IP field picture pair */
104
      if((previous_picture_coding_type==I_TYPE)&&(picture_coding_type==P_TYPE))
105
      {
106
        substitute_display_framenum = bitstream_framenum;
107
      }
108
      else /* our more generic P field picture pair */
109
      {
110
        substitute_display_framenum =
111
          (temporal_reference - previous_anchor_temporal_reference)
112
            + bitstream_framenum - 1;
113
      }
114
 
115
      Read_Frame(Substitute_Picture_Filename, current_frame, substitute_display_framenum);
116
    }
117
#ifdef DEBUG
118
    else if((picture_coding_type!=B_TYPE)||(picture_coding_type!=D_TYPE))
119
    {
120
      cprintf("NO SUBS FOR THIS PICTURE\n");
121
    }
122
#endif
123
  }
124
 
125
 
126
  /* set b gate so we don't redundantly load next time around */
127
  if(picture_coding_type==B_TYPE)
128
    bgate = 1;
129
  else
130
    bgate = 0;
131
 
132
  /* update general tracking variables */
133
  if((picture_structure==FRAME_PICTURE)||(!Second_Field))
134
  {
135
    previous_temporal_reference  = temporal_reference;
136
    previous_bitstream_framenum  = bitstream_framenum;
137
  }
138
 
139
  /* update reference frame tracking variables */
140
  if((picture_coding_type!=B_TYPE) &&
141
    ((picture_structure==FRAME_PICTURE)||Second_Field))
142
  {
143
    previous_anchor_temporal_reference  = temporal_reference;
144
    previous_anchor_bitstream_framenum  = bitstream_framenum;
145
  }
146
 
147
  previous_picture_coding_type = picture_coding_type;
148
 
149
}
150
 
151
 
152
/* Note: fields are only read to serve as the same-frame reference for
153
   a second field */
154
static void Read_Frame(fname,frame,framenum)
155
char *fname;
156
unsigned char *frame[];
157
int framenum;
158
{
159
  int parity;
160
  int rerr = 0;
161
  int field_mode;
162
 
163
  if(framenum<0)
164
    cprintf("ERROR: framenum (%d) is less than zero\n", framenum);
165
 
166
 
167
  if(Big_Picture_Flag)
168
    rerr = Extract_Components(fname, substitute_frame, framenum);
169
  else
170
    rerr = Read_Components(fname, substitute_frame, framenum);
171
 
172
  if(rerr!=0)
173
  {
174
    cprintf("was unable to substitute frame\n");
175
  }
176
 
177
  /* now copy to the appropriate buffer */
178
  /* first field (which we are attempting to substitute) must be
179
     of opposite field parity to the current one */
180
  if((Second_Field)&&(picture_coding_type==P_TYPE))
181
  {
182
    parity      = (picture_structure==TOP_FIELD ? 1:0);      
183
    field_mode  = (picture_structure==FRAME_PICTURE ? 0:1);
184
  }
185
  else
186
  {
187
    /* Like frame structued pictures, B pictures only substitute an entire frame
188
       since both fields always predict from the same frame (with respect
189
       to forward/backwards directions) */
190
    parity = 0;
191
    field_mode = 0;
192
  }
193
 
194
 
195
  Copy_Frame(substitute_frame[0], frame[0], Coded_Picture_Width,
196
    Coded_Picture_Height, parity, field_mode);
197
 
198
  Copy_Frame(substitute_frame[1], frame[1], Chroma_Width, Chroma_Height,
199
    parity, field_mode);
200
 
201
  Copy_Frame(substitute_frame[2], frame[2], Chroma_Width, Chroma_Height,
202
    parity, field_mode);
203
 
204
#ifdef VERBOSE
205
  if(Verbose_Flag > NO_LAYER)
206
    cprintf("substituted %s %d\n",
207
      (field_mode ? (parity?"bottom field":"bottom field"):"frame"), framenum);
208
#endif
209
}
210
 
211
 
212
 
213
 
214
static int Read_Components(filename, frame, framenum)
215
char *filename;
216
unsigned char *frame[3];
217
int framenum;
218
{
219
  int err = 0;
220
  char outname[FILENAME_LENGTH];
221
  char name[FILENAME_LENGTH];
222
 
223
  sprintf(outname,filename,framenum);
224
 
225
 
226
  sprintf(name,"%s.Y",outname);
227
  err += Read_Component(name, frame[0], Coded_Picture_Width,
228
    Coded_Picture_Height);
229
 
230
  sprintf(name,"%s.U",outname);
231
  err += Read_Component(name, frame[1], Chroma_Width, Chroma_Height);
232
 
233
  sprintf(name,"%s.V",outname);
234
  err += Read_Component(name, frame[2], Chroma_Width, Chroma_Height);
235
 
236
  return(err);
237
}
238
 
239
 
240
static int Read_Component(Filename, Frame, Width, Height)
241
char *Filename;
242
unsigned char *Frame;
243
int Width;
244
int Height;
245
{
246
  int Size;
247
  int Bytes_Read;
248
  int Infile;
249
 
250
  Size = Width*Height;
251
 
252
#ifdef DEBUG
253
  cprintf("SUBS: reading %s\n", filename);
254
#endif
255
 
256
  if(!(Infile=1)<0)
257
  {
258
    cprintf("ERROR: unable to open reference filename (%s)\n", Filename);
259
        return(-1);
260
  }
261
 
262
  Bytes_Read = read(Infile, Frame, Size);
263
 
264
  if(Bytes_Read!=Size)
265
  {
266
    cprintf("was able to read only %d bytes of %d of file %s\n",
267
      Bytes_Read, Size, Filename);
268
  }
269
 
270
  return(0);
271
}
272
 
273
 
274
/* optimization: do not open the big file each time. Open once at start
275
   of decoder, and close at the very last frame */
276
 
277
/* Note: "big" files were used in E-mail exchanges almost exclusively by the
278
   MPEG Committee's syntax validation and conformance ad-hoc groups from
279
   the year 1993 until 1995 */
280
static int Extract_Components(filename, frame, framenum)
281
char *filename;
282
unsigned char *frame[3];
283
int framenum;
284
{
285
/*  int err = 0; */
286
  int line;
287
  int size, offset;
288
 
289
  cprintf("Extract_Components\n");
290
 
291
  /* compute size of each frame (in bytes) */
292
  size = (Coded_Picture_Width*Coded_Picture_Height);
293
 
294
  if(chroma_format==CHROMA444)
295
    size = (size * 3);
296
  else if(chroma_format==CHROMA422)
297
    size = (size * 2);
298
  else if(chroma_format==CHROMA420)
299
    size = ((size*3)>>1);
300
  else
301
    cprintf("ERROR: chroma_format (%d) not recognized\n", chroma_format);
302
 
303
 
304
  /* compute distance into "big" file */
305
  offset = size*framenum;
306
 
307
#ifdef DEBUG
308
  printf("EXTRACTING: frame(%d) offset(%d), size (%d) from %s\n",
309
    framenum, offset, size, filename);
310
#endif
311
 
312
  /* seek to location in big file where desired frame begins */
313
  /* note: this offset cannot exceed a few billion bytes due to the */
314
  /*       obvious limitations of 32-bit integers */
315
  //fseek(fd, offset, 0);
316
 
317
  /* Y  */
318
  for (line=0; line<Coded_Picture_Height; line++)
319
  {
320
    //fread(frame[0]+(line*Coded_Picture_Width),1,Coded_Picture_Width,fd);
321
  }
322
 
323
  /* Cb */
324
  for (line=0; line<Chroma_Height; line++)
325
  {
326
    //fread(frame[1]+(line*Chroma_Width),1,Chroma_Width,fd);
327
  }
328
 
329
  /* Cr */
330
  for (line=0; line<Chroma_Height; line++)
331
  {
332
    //fread(frame[2]+(line*Chroma_Width),1,Chroma_Width,fd);
333
  }
334
 
335
 
336
  //fclose(fd);
337
  return(0);
338
}
339
 
340
 
341
static void Copy_Frame(src, dst, width, height, parity, field_mode)
342
unsigned char *src;
343
unsigned char *dst;
344
int width;
345
int height;
346
int parity;        /* field parity (top or bottom) to overwrite */
347
int field_mode;    /* 0 = frame, 1 = field                      */
348
{
349
  int row, col;
350
  int s, d;
351
  int incr;
352
 
353
  s = d = 0;
354
 
355
#ifdef DEBUG
356
  cprintf("COPYING (w=%d, h=%d, parity=%d, field_mode=%d)\n",
357
    width,height,parity,field_mode);
358
#endif /* DEBUG */
359
 
360
  if(field_mode)
361
  {
362
    incr = 2;
363
 
364
    if(parity==0)
365
      s += width;
366
  }
367
  else
368
  {
369
    incr = 1;
370
  }
371
 
372
  for(row=0; row<height; row+=incr)
373
  {
374
    for(col=0; col<width; col++)
375
    {
376
      dst[d+col] = src[s+col];
377
    }
378
 
379
    d += (width*incr);
380
    s += (width*incr);
381
  }
382
 
383
}
384