~vcs-imports/smpeg/trunk

« back to all changes in this revision

Viewing changes to video/readfile.cpp

  • Committer: icculus
  • Date: 2021-08-20 16:53:24 UTC
  • Revision ID: svn-v4:d67a7ff0-0d32-0410-8e92-b5b47b70abfd:trunk:414
Moved to GitHub: https://github.com/icculus/smpeg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * readfile.c --
3
 
 *
4
 
 *       Procedures concerned with reading data and parsing 
5
 
 *       start codes from MPEG files.
6
 
 *
7
 
 */
8
 
 
9
 
/*
10
 
 * Copyright (c) 1995 The Regents of the University of California.
11
 
 * All rights reserved.
12
 
 * 
13
 
 * Permission to use, copy, modify, and distribute this software and its
14
 
 * documentation for any purpose, without fee, and without written agreement is
15
 
 * hereby granted, provided that the above copyright notice and the following
16
 
 * two paragraphs appear in all copies of this software.
17
 
 * 
18
 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
19
 
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
20
 
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
21
 
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
 
 * 
23
 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
24
 
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25
 
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
26
 
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
27
 
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
28
 
 */
29
 
 
30
 
/*
31
 
 * Portions of this software Copyright (c) 1995 Brown University.
32
 
 * All rights reserved.
33
 
 * 
34
 
 * Permission to use, copy, modify, and distribute this software and its
35
 
 * documentation for any purpose, without fee, and without written agreement
36
 
 * is hereby granted, provided that the above copyright notice and the
37
 
 * following two paragraphs appear in all copies of this software.
38
 
 * 
39
 
 * IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE TO ANY PARTY FOR
40
 
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
41
 
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF BROWN
42
 
 * UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43
 
 * 
44
 
 * BROWN UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
45
 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
46
 
 * PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
47
 
 * BASIS, AND BROWN UNIVERSITY HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
48
 
 * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
49
 
 */
50
 
 
51
 
#include <sys/types.h>
52
 
#ifdef __STDC__
53
 
#include <stdlib.h>
54
 
#include <string.h>
55
 
#endif
56
 
#include <string.h>
57
 
#include <signal.h>
58
 
 
59
 
#include "SDL_endian.h"
60
 
#include "video.h"
61
 
#include "proto.h"
62
 
#include "util.h"
63
 
#include "dither.h"
64
 
 
65
 
 
66
 
/*
67
 
   Changes to make the code reentrant:
68
 
      deglobalized: totNumFrames, realTimeStart, stream id vars, Prase_done,
69
 
         swap, seekValue, input, EOF_flag, ReadPacket statics, sys_layer,
70
 
         bitOffset, bitLength, bitBuffer, curVidStream
71
 
   removed: [aud,sys,vid]Bytes
72
 
   Additional changes:
73
 
      get rid on ANSI C complaints about shifting
74
 
   -lsh@cs.brown.edu (Loring Holden)
75
 
 */
76
 
 
77
 
/*
78
 
 *--------------------------------------------------------------
79
 
 *
80
 
 * get_more_data --
81
 
 *
82
 
 *      Called by get_more_data to read in more data from
83
 
 *      video MPG files (non-system-layer)
84
 
 *
85
 
 * Results:
86
 
 *      Input buffer updated, buffer length updated.
87
 
 *      Returns 1 if data read, 0 if EOF, -1 if error.
88
 
 *
89
 
 * Side effects:
90
 
 *      None.
91
 
 *
92
 
 *--------------------------------------------------------------
93
 
 */
94
 
 
95
 
int 
96
 
get_more_data( VidStream* vid_stream )
97
 
{
98
 
  unsigned int *buf_start;
99
 
  int length, num_read, i;
100
 
  unsigned int request;
101
 
  unsigned char *buffer, *mark;
102
 
  unsigned int *lmark;
103
 
  Sint32 timestamp_offset;
104
 
  Uint32 data_pos;
105
 
     
106
 
  if (vid_stream->EOF_flag) return 0;
107
 
  
108
 
  buf_start = vid_stream->buf_start;
109
 
  length = vid_stream->buf_length;
110
 
  buffer = (unsigned char *) vid_stream->buffer;
111
 
  
112
 
  if (length > 0) {
113
 
    memcpy((unsigned char *) buf_start, buffer, (unsigned int) (length*4));
114
 
    mark = ((unsigned char *) (buf_start + length));
115
 
  }
116
 
  else {
117
 
    mark = (unsigned char *) buf_start;
118
 
    length = 0;
119
 
  }
120
 
  
121
 
  request = (vid_stream->max_buf_length-length)*4;
122
 
  
123
 
  data_pos = vid_stream->_smpeg->mpeg->pos;  
124
 
  num_read = vid_stream->_smpeg->mpeg->copy_data((Uint8 *)mark, request);
125
 
 
126
 
  vid_stream->timestamp = vid_stream->_smpeg->mpeg->timestamp;
127
 
  timestamp_offset = vid_stream->_smpeg->mpeg->timestamp_pos - data_pos;
128
 
  vid_stream->timestamp_mark = (unsigned int *)(mark+timestamp_offset);
129
 
  vid_stream->timestamp_used = false;
130
 
 
131
 
  /* Paulo Villegas - 26/1/1993: Correction for 4-byte alignment */
132
 
  {
133
 
    int num_read_rounded;
134
 
    unsigned char *index;
135
 
    
136
 
    num_read_rounded = 4*(num_read/4);
137
 
    
138
 
    /* this can happen only if num_read<request; i.e. end of file reached */
139
 
    if ( num_read_rounded < num_read ) { 
140
 
          num_read_rounded = 4*( num_read/4+1 );
141
 
 
142
 
            /* fill in with zeros */
143
 
          for( index=mark+num_read; index<mark+num_read_rounded; *(index++)=0 );
144
 
 
145
 
          /* advance to the next 4-byte boundary */
146
 
          num_read = num_read_rounded;
147
 
    }
148
 
  }
149
 
  
150
 
  if (num_read < 0) {
151
 
    return -1;
152
 
  }
153
 
  if (num_read == 0) {
154
 
    vid_stream->buffer = buf_start;
155
 
    
156
 
    /* Make 32 bits after end equal to 0 and 32
157
 
     * bits after that equal to seq end code
158
 
     * in order to prevent messy data from infinite
159
 
     * recursion.
160
 
     */
161
 
    
162
 
    *(buf_start + length) = 0x0;
163
 
    *(buf_start + length+1) = SEQ_END_CODE;
164
 
    
165
 
    vid_stream->EOF_flag = 1;
166
 
    return 0;
167
 
  }
168
 
  
169
 
  lmark = (unsigned int *) mark;
170
 
  
171
 
  num_read = num_read/4;
172
 
  
173
 
  for (i = 0; i < num_read; i++) {
174
 
    *lmark = SDL_SwapBE32(*lmark);
175
 
    lmark++;
176
 
  }
177
 
  
178
 
  vid_stream->buffer = buf_start;
179
 
  vid_stream->buf_length = length + num_read;
180
 
  
181
 
  return 1;
182
 
}
183
 
 
184
 
/* EOF */