~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to mpeglib/lib/mpegplay/mpegVideoBitWindow.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  bitwindow mpeg video
 
3
  Copyright (C) 2000  Martin Vogt
 
4
 
 
5
  This program is free software; you can redistribute it and/or modify
 
6
  it under the terms of the GNU Library General Public License as published by
 
7
  the Free Software Foundation.
 
8
 
 
9
  For more information look at the file COPYRIGHT in this package
 
10
 
 
11
 */
 
12
 
 
13
 
 
14
#include "mpegVideoBitWindow.h"
 
15
 
 
16
 
 
17
 
 
18
 
 
19
MpegVideoBitWindow::MpegVideoBitWindow() {
 
20
  // Make buffer length multiple of 4. 
 
21
  
 
22
  this->size=80000;
 
23
 
 
24
 
 
25
 
 
26
  if ((size % 4) != 0) {
 
27
    cout << "MpegVideoStream not multiple of 4"<<endl;
 
28
    exit(-1);
 
29
  }
 
30
 
 
31
  /* Create MpegVideoStream. */
 
32
  buf_start = (unsigned int*) malloc (sizeof(unsigned int)*(size*4));
 
33
  
 
34
  /*
 
35
   * Set max_buf_length to one less than actual length to deal with messy
 
36
   * data without proper seq. end codes.
 
37
   */
 
38
  max_buf_length=size-1;
 
39
 
 
40
  
 
41
  /* Initialize bitstream i/o fields. */
 
42
 
 
43
  bit_offset = 0;
 
44
  buffer = buf_start;
 
45
  buf_length = 0;
 
46
  num_left=0;
 
47
  leftover_bytes=0;
 
48
  curBits = 0;
 
49
 
 
50
 
 
51
 
 
52
 
 
53
  nBitMask[0] = 0x00000000;
 
54
  nBitMask[1] = 0x80000000;
 
55
  nBitMask[2] = 0xc0000000;
 
56
  nBitMask[3] = 0xe0000000;
 
57
  nBitMask[4] = 0xf0000000;
 
58
  nBitMask[5] = 0xf8000000;
 
59
  nBitMask[6] = 0xfc000000;
 
60
  nBitMask[7] = 0xfe000000;
 
61
  nBitMask[8] = 0xff000000;
 
62
  nBitMask[9] = 0xff800000;
 
63
  nBitMask[10] = 0xffc00000;
 
64
  nBitMask[11] = 0xffe00000;
 
65
  nBitMask[12] = 0xfff00000;
 
66
  nBitMask[13] = 0xfff80000;
 
67
  nBitMask[14] = 0xfffc0000;
 
68
  nBitMask[15] = 0xfffe0000;
 
69
  nBitMask[16] = 0xffff0000;
 
70
  nBitMask[17] = 0xffff8000;
 
71
  nBitMask[18] = 0xffffc000;
 
72
  nBitMask[19] = 0xffffe000;
 
73
  nBitMask[20] = 0xfffff000;
 
74
  nBitMask[21] = 0xfffff800;
 
75
  nBitMask[22] = 0xfffffc00;
 
76
  nBitMask[23] = 0xfffffe00;
 
77
  nBitMask[24] = 0xffffff00;
 
78
  nBitMask[25] = 0xffffff80;
 
79
  nBitMask[26] = 0xffffffc0;
 
80
  nBitMask[27] = 0xffffffe0;
 
81
  nBitMask[28] = 0xfffffff0;
 
82
  nBitMask[29] = 0xfffffff8;
 
83
  nBitMask[30] = 0xfffffffc;
 
84
  nBitMask[31] = 0xfffffffe;
 
85
  nBitMask[32] = 0xffffffff;
 
86
 
 
87
 
 
88
}
 
89
 
 
90
 
 
91
MpegVideoBitWindow::~MpegVideoBitWindow() {
 
92
  delete buf_start;
 
93
}
 
94
 
 
95
 
 
96
 
 
97
int MpegVideoBitWindow::appendToBuffer(unsigned char* ptr,int len) {
 
98
  int byte_length = getLength()*4;
 
99
 
 
100
 
 
101
  resizeBuffer(len);
 
102
  
 
103
  if (num_left != 0) {
 
104
    byte_length += num_left;
 
105
    *(buffer+buf_length)=leftover_bytes;
 
106
  }
 
107
  memcpy(((unsigned char *)buffer)+byte_length,ptr,len);
 
108
  
 
109
  if ((unsigned long int)1 != ntohl(1)) {
 
110
    unsigned int *mark = buffer+buf_length;
 
111
    int i;
 
112
    int n=(len+num_left)&0xfffffffc;
 
113
    for (i=0; i < n; i+=4) {
 
114
      *mark=ntohl(*mark);
 
115
      mark++;
 
116
    }
 
117
  }
 
118
  byte_length = byte_length + len;
 
119
  num_left = byte_length % 4;
 
120
  buf_length = byte_length / 4;
 
121
  updateCurBits();
 
122
  
 
123
  leftover_bytes = *(buffer +buf_length);
 
124
  return true;
 
125
}
 
126
 
 
127
int MpegVideoBitWindow::getLinearFree() {
 
128
  unsigned int* endPos=buf_start+size;
 
129
  unsigned int* currPos=buffer+buf_length;
 
130
  
 
131
  int back=endPos-currPos;
 
132
  return back;
 
133
}
 
134
 
 
135
 
 
136
 
 
137
 
 
138
 
 
139
void MpegVideoBitWindow::flushByteOffset() {
 
140
  int byteoff;
 
141
 
 
142
  byteoff = bit_offset % 8;
 
143
 
 
144
  if (byteoff != 0) {
 
145
    flushBitsDirect((8-byteoff));
 
146
  }
 
147
}
 
148
 
 
149
 
 
150
void MpegVideoBitWindow::appendToBuffer(unsigned int startCode) {
 
151
  unsigned int startCodeRaw=htonl(startCode);
 
152
  resizeBuffer(4);
 
153
  appendToBuffer((unsigned char*)&startCodeRaw,4);
 
154
}
 
155
 
 
156
 
 
157
void MpegVideoBitWindow::clear() {
 
158
  buffer = buf_start;
 
159
  buf_length = 0;
 
160
  bit_offset = 0;
 
161
  curBits = 0;
 
162
}
 
163
 
 
164
 
 
165
int MpegVideoBitWindow::getLength() {
 
166
  return buf_length;
 
167
}
 
168
 
 
169
 
 
170
 
 
171
 
 
172
 
 
173
 
 
174
 
 
175
void MpegVideoBitWindow::printChar(int bytes) {
 
176
  int i;
 
177
  unsigned char* mark;
 
178
 
 
179
  mark=(unsigned char *)buffer;
 
180
 
 
181
  for(i=0;i<bytes;i++) {
 
182
    printf("i:%d read=%x\n",i,mark[i]);
 
183
  }
 
184
  printf("*********\n");
 
185
 
 
186
}
 
187
 
 
188
 
 
189
void MpegVideoBitWindow::printInt(int bytes) {
 
190
  int i;
 
191
  int n;
 
192
  unsigned int* mark;
 
193
  
 
194
  mark=(unsigned int*)buf_start;
 
195
 
 
196
  n=bytes/sizeof(int);
 
197
  for(i=0;i<n;i++) {
 
198
    printf("i:%d read=%x\n",i,mark[i]);
 
199
  }
 
200
  printf("*********\n");
 
201
 
 
202
}
 
203
 
 
204
 
 
205
void MpegVideoBitWindow::print() {
 
206
  int byte_length = getLength()*4;
 
207
 
 
208
  printf("bit_offset:%x\n",bit_offset);
 
209
  printf("num_left:%x\n",num_left);
 
210
  printf("leftover_bytes:%x\n",leftover_bytes);
 
211
  printf("buf_length:%x\n",buf_length);
 
212
  printf("curBits:%x\n",curBits);
 
213
  printf("pos:%8x\n",byte_length);
 
214
  printChar(8);
 
215
}
 
216
 
 
217
 
 
218
void MpegVideoBitWindow::resizeBuffer(int insertBytes) {
 
219
  /* Read all the headers, now make room for packet */
 
220
  if (buf_start+max_buf_length < buffer+insertBytes/4+buf_length) {
 
221
 
 
222
    if (max_buf_length - buf_length < (int) insertBytes/4) {
 
223
 
 
224
      /* Buffer too small for a packet (plus whats there), 
 
225
       * time to enlarge it! 
 
226
       */
 
227
 
 
228
      unsigned int *old = buf_start;
 
229
      max_buf_length=buf_length+insertBytes/4+1;
 
230
 
 
231
      buf_start=(unsigned int*) malloc(sizeof(unsigned int)*max_buf_length);
 
232
      if (buf_start == NULL) {
 
233
        cout << "allocation of:"<<max_buf_length<<" bytes failed"<<endl;
 
234
        exit(0);
 
235
      }
 
236
      memcpy((unsigned char *)buf_start,buffer,(unsigned int)buf_length*4);
 
237
      delete old;
 
238
      buffer = buf_start;
 
239
      cout << "enlarge buffer-1 end***********"<<endl;
 
240
 
 
241
    } else {
 
242
      memcpy((unsigned char *)buf_start,
 
243
             buffer, (unsigned int) buf_length*4);
 
244
      buffer = buf_start;
 
245
    }
 
246
  }
 
247
}
 
248
 
 
249
 
 
250
void MpegVideoBitWindow::fillWithIsoEndCode(int bytes) {
 
251
  int i;
 
252
  int n=bytes/4;
 
253
  for (i=0;i<n;i++) {
 
254
    appendToBuffer(ISO_11172_END_CODE);
 
255
  }
 
256
}