File: frametype.c

package info (click to toggle)
ucbmpeg 1r2-6
  • links: PTS
  • area: non-free
  • in suites: hamm, potato, slink
  • size: 9,504 kB
  • ctags: 7,643
  • sloc: ansic: 79,920; tcl: 2,985; perl: 313; asm: 284; makefile: 269; csh: 13
file content (369 lines) | stat: -rw-r--r-- 9,387 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*===========================================================================*
 * frametype.c								     *
 *									     *
 *	procedures to keep track of frame types (I, P, B)		     *
 *									     *
 * EXPORTED PROCEDURES:							     *
 *	FType_Type						             *
 *	FType_FutureRef						             *
 *	FType_PastRef						             *
 *									     *
 * SYNOPSIS								     *
 *	FType_Type	returns the type of the given numbered frame	     *
 *	FType_FutureRef	returns the number of the future reference frame     *
 *	FType_PastRef	returns the number of the past reference frame	     *
 *									     *
 *===========================================================================*/

/*
 * Copyright (c) 1995 The Regents of the University of California.
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice and the following
 * two paragraphs appear in all copies of this software.
 *
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 */


/*==============*
 * HEADER FILES *
 *==============*/

#include "all.h"
#include "prototypes.h"
#include "frames.h"
#include "frame.h"
#include "param.h"


static FrameTable *frameTable=NULL;
static boolean use_cache = FALSE;
static int firstI = 0;

/*==================*
 * GLOBAL VARIABLES *
 *==================*/

boolean	    forceEncodeLast = FALSE;
extern int framePatternLen;
extern char *framePattern;


/*=====================*
 * EXPORTED PROCEDURES *
 *=====================*/

/*===========================================================================*
 *
 * FType_Type
 *
 *	returns the type of the given numbered frame
 *
 * RETURNS:	the type
 *
 * SIDE EFFECTS:    none
 *
 *===========================================================================*/
int
FType_Type(frameNum)
    int frameNum;
{
  if (use_cache) return (int)frameTable[frameNum].typ;
  
  if ( forceEncodeLast && (frameNum+1 == numInputFiles) ) {
    int result;
    
    result = framePattern[frameNum % framePatternLen];
    if ( result == 'b' ) return 'i';
    else return result;
  } else {
    if (specificsOn) {
      static int lastI = -1;
      int newtype;
      
      if (lastI > frameNum) lastI = -1;
      newtype = SpecTypeLookup(frameNum);
      switch (newtype) {
      case 1:
	lastI = frameNum;
	return 'i';
      case 2:
	return 'p';
      case 3:
	return 'b';
      default:
	if (lastI != -1) return framePattern[(frameNum-lastI+firstI) % framePatternLen];
	else return framePattern[frameNum % framePatternLen];
      }
    } else return framePattern[frameNum % framePatternLen];
  }
}


/*===========================================================================*
 *
 * FType_FutureRef
 *
 *	returns the number of the future reference frame
 *
 * RETURNS:	the number; -1 if none
 *
 * SIDE EFFECTS:    none
 *
 *===========================================================================*/
int
FType_FutureRef(currFrameNum)
    int currFrameNum;
{
    int	    index;
    int	    futureIndex;
    int	    result;

    if (use_cache) {
      return frameTable[currFrameNum].next->number;
    } else {
      index = currFrameNum % framePatternLen;
      futureIndex = frameTable[index].next->number;
      
      result = currFrameNum +
	(((futureIndex-index)+framePatternLen) % framePatternLen);
      
      if ( (result >= numInputFiles) && forceEncodeLast ) {
	return numInputFiles-1;
      } else {
	return result;
      }
    }
}


/*===========================================================================*
 *
 * FType_PastRef
 *
 *	returns the number of the past reference frame
 *
 * RETURNS:	the number
 *
 * SIDE EFFECTS:    none
 *
 *===========================================================================*/
int
FType_PastRef(currFrameNum)
    int currFrameNum;
{
    int	    index;
    int	    pastIndex;

    if (use_cache) {
      return frameTable[currFrameNum].prev->number;
    } else {
      index = currFrameNum % framePatternLen;
      pastIndex = frameTable[index].prev->number;
      
      return currFrameNum -
	(((index-pastIndex)+framePatternLen) % framePatternLen);
    }
}


/*===========================================================================*
 *
 * SetFramePattern
 *
 *	set the IPB pattern; calls ComputeFrameTable to set up table
 *
 * RETURNS:	nothing
 *
 * SIDE EFFECTS:    framePattern, framePatternLen, frameTable
 *
 *===========================================================================*/
#define SIMPLE_ASCII_UPPER(x)  (((x)>='a') ? ((x)-'a'+'A') : (x))
void
SetFramePattern(pattern)
    char *pattern;
{
    int len = strlen(pattern);
    char *buf;
    int index;

    if ( ! pattern ) {
	fprintf(stderr, "pattern cannot be NULL\n");
	exit(1);
    }

    if ( SIMPLE_ASCII_UPPER(pattern[0]) != 'I' ) {
      for (index=0; index < len; index++) {

	if (SIMPLE_ASCII_UPPER(pattern[index]) == 'I') {
	  break;
	} else if (SIMPLE_ASCII_UPPER(pattern[index]) == 'P') {
	  fprintf(stderr, "first reference frame must be 'i'\n");
	  exit(1);
	}
      }
    }

    buf = (char *)malloc(sizeof(char)*(len+1));
    ERRCHK(buf, "malloc");

    firstI = -1;
    for ( index = 0; index < len; index++ ) {
      switch( SIMPLE_ASCII_UPPER(pattern[index]) ) {
      case 'I':	
	buf[index] = 'i';
	if (firstI == -1) firstI = index;
	break;
      case 'P':	
	buf[index] = 'p'; 
	break;
      case 'B':	
	buf[index] = 'b';
	break;
      default:
	fprintf(stderr, "Frame type '%c' not supported.\n", pattern[index]);
	exit(1);
      }
    }
    buf[len] = 0;

    if (firstI == -1) {
      fprintf(stderr, "Must have an I-frame in PATTERN\n");
      exit(1);
    }

    framePattern = buf;
    framePatternLen = len;

    /* Used to ComputeFrameTable(), but now must wait until param parsed. (STDIN or not)*/
}


/*===========================================================================*
 *
 * ComputeFrameTable
 *
 *	compute a table of I, P, B frames to help in determining dependencies
 *
 * RETURNS:	nothing
 *
 * SIDE EFFECTS:    frameTable
 *
 *===========================================================================*/
void
ComputeFrameTable()
{
    register int index;
    FrameTable	*lastI, *lastIP, *firstB, *secondIP;
    FrameTable	*ptr;
    char typ;
    int table_size;

    if (!stdinUsed) {
      table_size = numInputFiles;
    } else {
      table_size = framePatternLen;
    }

    frameTable = (FrameTable *) malloc((1+table_size)*sizeof(FrameTable));
    ERRCHK(frameTable, "malloc");

    lastI = NULL;
    lastIP = NULL;
    firstB = NULL;
    secondIP = NULL;
    for ( index = 0; index < table_size; index++ ) {
	frameTable[index].number = index;
	typ = FType_Type(index);
	frameTable[index].typ = typ;
	switch( typ ) {
	    case 'i':
		ptr = firstB;
		while ( ptr != NULL ) {
		    ptr->next = &(frameTable[index]);
		    ptr = ptr->nextOutput;
		}
		frameTable[index].nextOutput = firstB;
		frameTable[index].prev = lastIP;	/* for freeing */
		if ( lastIP != NULL ) {
		    lastIP->next = &(frameTable[index]);
		    if ( secondIP == NULL ) {
			secondIP = &(frameTable[index]);
		    }
		}
		lastIP = &(frameTable[index]);
		firstB = NULL;
		break;
	    case 'p':
		ptr = firstB;
		while ( ptr != NULL ) {
		    ptr->next = &(frameTable[index]);
		    ptr = ptr->nextOutput;
		}
		frameTable[index].nextOutput = firstB;
		frameTable[index].prev = lastIP;
		if ( lastIP != NULL ) {
		    lastIP->next = &(frameTable[index]);
		    if ( secondIP == NULL ) {
			secondIP = &(frameTable[index]);
		    }
		}
		lastIP = &(frameTable[index]);
		firstB = NULL;
		break;
	    case 'b':
		if ( (index+1 == framePatternLen) ||
		     (FType_Type(index+1) != 'b') ) {
		    frameTable[index].nextOutput = NULL;
		} else {
		    frameTable[index].nextOutput = &(frameTable[index+1]);
		}
		frameTable[index].prev = lastIP;
		if ( firstB == NULL ) {
		    firstB = &(frameTable[index]);
		}
		break;
	    default:
	        fprintf(stderr, "Programmer Error in ComputeFrameTable (%d)\n",
			framePattern[index]);
	        exit(1);
	        break;
	}
    }
    
    /* why? SRS */
    frameTable[table_size].number = framePatternLen;
    ptr = firstB;
    while ( ptr != NULL ) {
	ptr->next = &(frameTable[table_size]);
	ptr = ptr->nextOutput;
    }
    frameTable[table_size].nextOutput = firstB;
    frameTable[table_size].prev = lastIP;
    if ( secondIP == NULL )
	frameTable[table_size].next = &(frameTable[0]);
    else
	frameTable[table_size].next = secondIP;

    frameTable[0].prev = lastIP;
    if ( lastIP != NULL ) {
	lastIP->next = &(frameTable[table_size]);
    }

    if (!stdinUsed) {
      use_cache = TRUE;
    }
}