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

« back to all changes in this revision

Viewing changes to mpeglib/lib/mpegplay/mpegVideoHeader.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
  stores sequence header info, for reinit of stream
 
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 "mpegVideoHeader.h"
 
15
 
 
16
#include "mpegExtension.h"
 
17
#include "mpegVideoStream.h"
 
18
 
 
19
/* Video rates table */
 
20
/* Cheat on Vid rates, round to 30, and use 30 if illegal value 
 
21
   Except for 9, where Xing means 15, and given their popularity, we'll
 
22
   be nice and do it */
 
23
 
 
24
static double VidRateNum[16]={29.97, 24, 24, 25, 29.97, 29.97, 50, 59.94, 
 
25
                              59.94, 14.985, 29.97, 29.97, 29.97, 29.97,
 
26
                              29.97, 29.97};
 
27
 
 
28
 
 
29
 
 
30
static const unsigned char default_intra_matrix[64] = {
 
31
  8, 16, 19, 22, 26, 27, 29, 34,
 
32
  16, 16, 22, 24, 27, 29, 34, 37,
 
33
  19, 22, 26, 27, 29, 34, 34, 38,
 
34
  22, 22, 26, 27, 29, 34, 37, 40,
 
35
  22, 26, 27, 29, 32, 35, 40, 48,
 
36
  26, 27, 29, 32, 35, 40, 48, 58,
 
37
  26, 27, 29, 34, 38, 46, 56, 69,
 
38
  27, 29, 35, 38, 46, 56, 69, 83};
 
39
  
 
40
 
 
41
 
 
42
/* Set up array for fast conversion from zig zag order to row/column
 
43
   coordinates.
 
44
*/
 
45
 
 
46
const int zigzag[64][2] = {
 
47
  {0,0},{1,0},{0,1},{0,2},{1,1},{2,0},{3,0},{2,1},{1,2},{0,3},{0,4},{1,3},
 
48
  {2,2},{3,1},{4,0},{5,0},{4,1},{3,2},{2,3},{1,4},{0,5},{0,6},{1,5},{2,4},
 
49
  {3,3},{4,2},{5,1},{6,0},{7,0},{6,1},{5,2},{4,3},{3,4},{2,5},{1,6},{0,7},
 
50
  {1,7},{2,6},{3,5},{4,4},{5,3},{6,2},{7,1},{7,2},{6,3},{5,4},{4,5},{3,6},
 
51
  {2,7},{3,7},{4,6},{5,5},{6,4},{7,3},{7,4},{6,5},{5,6},{4,7},{5,7},{6,6},
 
52
  {7,5},{7,6},{6,7},{7,7} };
 
53
 
 
54
 
 
55
 
 
56
 
 
57
 
 
58
MpegVideoHeader::MpegVideoHeader() {
 
59
  init();
 
60
 
 
61
}
 
62
 
 
63
 
 
64
void MpegVideoHeader::init() {
 
65
 
 
66
  h_size=0;
 
67
  v_size=0;
 
68
  mb_height=0;
 
69
  mb_width=0;
 
70
  mb_size=0;
 
71
  aspect_ratio=0;
 
72
  bit_rate=0;
 
73
  vbv_buffer_size=0;
 
74
  const_param_flag=0;
 
75
  picture_rate=0.0;
 
76
  extension=new MpegExtension();
 
77
  init_quanttables();
 
78
 
 
79
}
 
80
 
 
81
void MpegVideoHeader::init_quanttables() {
 
82
  int i;
 
83
  int j;
 
84
 
 
85
  /* Copy default intra matrix. */
 
86
 
 
87
  for (i = 0; i < 8; i++) {
 
88
    for (j = 0; j < 8; j++) {
 
89
      intra_quant_matrix[i][j]=default_intra_matrix[i*8+j];
 
90
    }
 
91
  }
 
92
 
 
93
  /* Initialize non intra quantization matrix. */
 
94
 
 
95
  for (i = 0; i < 8; i++) {
 
96
    for (j = 0; j < 8; j++) {
 
97
      non_intra_quant_matrix[i][j] = 16;
 
98
    }
 
99
  }
 
100
}
 
101
 
 
102
 
 
103
MpegVideoHeader::~MpegVideoHeader() {
 
104
 
 
105
  delete extension;
 
106
 
 
107
}
 
108
 
 
109
 
 
110
 
 
111
/*
 
112
 *--------------------------------------------------------------
 
113
 *
 
114
 * ParseSeqHead --
 
115
 *
 
116
 *      Assumes bit stream is at the END of the sequence
 
117
 *      header start code. Parses off the sequence header.
 
118
 *
 
119
 * Results:
 
120
 *      Fills the vid_stream structure with values derived and
 
121
 *      decoded from the sequence header. Allocates the pict image
 
122
 *      structures based on the dimensions of the image space
 
123
 *      found in the sequence header.
 
124
 *
 
125
 * Side effects:
 
126
 *      Bit stream irreversibly parsed off.
 
127
 *
 
128
 *--------------------------------------------------------------
 
129
 */
 
130
int MpegVideoHeader::parseSeq(MpegVideoStream* mpegVideoStream) {
 
131
  unsigned int data;
 
132
  int i ;
 
133
 
 
134
  // seq_start_code already flushed!!!
 
135
 
 
136
  /* Get horizontal size of image space. */
 
137
 
 
138
  h_size=mpegVideoStream->getBits(12);
 
139
 
 
140
  /* Get vertical size of image space. */
 
141
 
 
142
  v_size=mpegVideoStream->getBits(12);
 
143
 
 
144
 
 
145
  /* Calculate macroblock width and height of image space. */
 
146
 
 
147
  mb_width = (h_size + 15) / 16;
 
148
  mb_height = (v_size + 15) / 16;
 
149
  mb_size=mb_height * mb_width-1;
 
150
  
 
151
 
 
152
 
 
153
  /* Parse of aspect ratio code. */
 
154
 
 
155
  data=mpegVideoStream->getBits(4);
 
156
 
 
157
  aspect_ratio = (unsigned char) data;
 
158
 
 
159
  /* Parse off picture rate code. */
 
160
 
 
161
  data=mpegVideoStream->getBits(4);
 
162
  picture_rate=VidRateNum[data];
 
163
 
 
164
  /* Parse off bit rate. */
 
165
 
 
166
  data=mpegVideoStream->getBits(18);
 
167
  bit_rate = data;
 
168
 
 
169
  /* Flush marker bit. */
 
170
 
 
171
  mpegVideoStream->flushBits(1);
 
172
 
 
173
  /* Parse off vbv buffer size. */
 
174
 
 
175
  data=mpegVideoStream->getBits(10);
 
176
  vbv_buffer_size = data;
 
177
 
 
178
  /* Parse off contrained parameter flag. */
 
179
 
 
180
  data=mpegVideoStream->getBits(1);
 
181
  if (data) {
 
182
    const_param_flag = true;
 
183
  } else
 
184
    const_param_flag = false;
 
185
 
 
186
  /*
 
187
   * If intra_quant_matrix_flag set, parse off intra quant matrix values.
 
188
   */
 
189
  data=mpegVideoStream->getBits(1);
 
190
  if (data) {
 
191
    for (i = 0; i < 64; i++) {
 
192
      data=mpegVideoStream->getBits(8);
 
193
      intra_quant_matrix[zigzag[i][1]][zigzag[i][0]]=(unsigned char)data;
 
194
    }
 
195
  }
 
196
  /*
 
197
   * If non intra quant matrix flag set, parse off non intra quant matrix
 
198
   * values.
 
199
   */
 
200
 
 
201
  data=mpegVideoStream->getBits(1);
 
202
  if (data) {
 
203
    for (i = 0; i < 64; i++) {
 
204
      data=mpegVideoStream->getBits(8);
 
205
 
 
206
      non_intra_quant_matrix[zigzag[i&0x3f][1]][zigzag[i&0x3f][0]]=
 
207
        (unsigned char) data;
 
208
    }
 
209
  }
 
210
  /*
 
211
   * If next start code is extension/user start code, 
 
212
   * parse off extension data.
 
213
   */
 
214
  extension->processExtensionData(mpegVideoStream);
 
215
 
 
216
  return true;
 
217
 
 
218
}
 
219
 
 
220
 
 
221
 
 
222
 
 
223
void MpegVideoHeader::copyTo(MpegVideoHeader* dest) {
 
224
 
 
225
 
 
226
  dest->h_size=h_size;
 
227
  dest->v_size=v_size;
 
228
  dest->mb_height=mb_height;
 
229
  dest->mb_width=mb_width;
 
230
  dest->mb_size=mb_size;
 
231
  dest->aspect_ratio=aspect_ratio;
 
232
  dest->bit_rate=bit_rate;
 
233
  dest->vbv_buffer_size=vbv_buffer_size;
 
234
  dest->const_param_flag=const_param_flag;
 
235
  dest->picture_rate=picture_rate;
 
236
 
 
237
  int i;
 
238
  int j;
 
239
 
 
240
 
 
241
   /* Copy default intra matrix. */
 
242
 
 
243
  for (i = 0; i < 8; i++) {
 
244
    for (j = 0; j < 8; j++) {
 
245
      dest->intra_quant_matrix[i][j]=intra_quant_matrix[i][j];
 
246
 
 
247
    }
 
248
  }
 
249
 
 
250
  /* Initialize non intra quantization matrix. */
 
251
 
 
252
  for (i = 0; i < 8; i++) {
 
253
    for (j = 0; j < 8; j++) {
 
254
      dest->non_intra_quant_matrix[i][j] =non_intra_quant_matrix[i][j] ;
 
255
    }
 
256
  }
 
257
  
 
258
}
 
259
 
 
260
 
 
261
 
 
262
 
 
263
double MpegVideoHeader::getPictureTime() {
 
264
  if (picture_rate > 0) {
 
265
    return 1.0/picture_rate;
 
266
  }
 
267
  return 0.0;
 
268
}
 
269
 
 
270
 
 
271
 
 
272
 
 
273
 
 
274
 
 
275
 
 
276
void MpegVideoHeader::print(char* description) {
 
277
  cout << "MpegVideoHeader [START]:"<<description<<endl;
 
278
  cout <<"h_size:"<<h_size<<endl;
 
279
  cout <<"v_size:"<<v_size<<endl;
 
280
  cout <<"mb_height:"<<mb_height<<endl;
 
281
  cout <<"mb_width:"<<mb_width<<endl;
 
282
  cout <<"mb_size:"<<mb_size<<endl;
 
283
  cout <<"aspect_ratio:"<<aspect_ratio<<endl;
 
284
  cout <<"bit_rate:"<<bit_rate<<endl;
 
285
  cout <<"vbv_buffer_size:"<<vbv_buffer_size<<endl;
 
286
  cout <<"const_param_flag:"<<const_param_flag<<endl;
 
287
  cout << "MpegVideoHeader [END]:"<<endl;
 
288
 
 
289
}