summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/mpegplay/mpegVideoHeader.cpp
blob: e91401a8aa1852ccefe812b1f1223b06e4350972 (plain)
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
/*
  stores sequence header info, for reinit of stream
  Copyright (C) 2000  Martin Vogt

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Library General Public License as published by
  the Free Software Foundation.

  For more information look at the file COPYRIGHT in this package

 */


#include "mpegVideoHeader.h"

#include "mpegExtension.h"
#include "mpegVideoStream.h"

#include <iostream>

using namespace std;

/* Video rates table */
/* Cheat on Vid rates, round to 30, and use 30 if illegal value 
   Except for 9, where Xing means 15, and given their popularity, we'll
   be nice and do it */

static double VidRateNum[16]={29.97, 24, 24, 25, 29.97, 29.97, 50, 59.94, 
			      59.94, 14.985, 29.97, 29.97, 29.97, 29.97,
			      29.97, 29.97};



static const unsigned char default_intra_matrix[64] = {
  8, 16, 19, 22, 26, 27, 29, 34,
  16, 16, 22, 24, 27, 29, 34, 37,
  19, 22, 26, 27, 29, 34, 34, 38,
  22, 22, 26, 27, 29, 34, 37, 40,
  22, 26, 27, 29, 32, 35, 40, 48,
  26, 27, 29, 32, 35, 40, 48, 58,
  26, 27, 29, 34, 38, 46, 56, 69,
  27, 29, 35, 38, 46, 56, 69, 83};
  


/* Set up array for fast conversion from zig zag order to row/column
   coordinates.
*/

const int zigzag[64][2] = {
  {0,0},{1,0},{0,1},{0,2},{1,1},{2,0},{3,0},{2,1},{1,2},{0,3},{0,4},{1,3},
  {2,2},{3,1},{4,0},{5,0},{4,1},{3,2},{2,3},{1,4},{0,5},{0,6},{1,5},{2,4},
  {3,3},{4,2},{5,1},{6,0},{7,0},{6,1},{5,2},{4,3},{3,4},{2,5},{1,6},{0,7},
  {1,7},{2,6},{3,5},{4,4},{5,3},{6,2},{7,1},{7,2},{6,3},{5,4},{4,5},{3,6},
  {2,7},{3,7},{4,6},{5,5},{6,4},{7,3},{7,4},{6,5},{5,6},{4,7},{5,7},{6,6},
  {7,5},{7,6},{6,7},{7,7} };





MpegVideoHeader::MpegVideoHeader() {
  init();

}


void MpegVideoHeader::init() {

  h_size=0;
  v_size=0;
  mb_height=0;
  mb_width=0;
  mb_size=0;
  aspect_ratio=0;
  bit_rate=0;
  vbv_buffer_size=0;
  const_param_flag=0;
  picture_rate=0.0;
  extension=new MpegExtension();
  init_quanttables();

}

void MpegVideoHeader::init_quanttables() {
  int i;
  int j;

  /* Copy default intra matrix. */

  for (i = 0; i < 8; i++) {
    for (j = 0; j < 8; j++) {
      intra_quant_matrix[i][j]=default_intra_matrix[i*8+j];
    }
  }

  /* Initialize non intra quantization matrix. */

  for (i = 0; i < 8; i++) {
    for (j = 0; j < 8; j++) {
      non_intra_quant_matrix[i][j] = 16;
    }
  }
}


MpegVideoHeader::~MpegVideoHeader() {

  delete extension;

}



/*
 *--------------------------------------------------------------
 *
 * ParseSeqHead --
 *
 *      Assumes bit stream is at the END of the sequence
 *      header start code. Parses off the sequence header.
 *
 * Results:
 *      Fills the vid_stream structure with values derived and
 *      decoded from the sequence header. Allocates the pict image
 *      structures based on the dimensions of the image space
 *      found in the sequence header.
 *
 * Side effects:
 *      Bit stream irreversibly parsed off.
 *
 *--------------------------------------------------------------
 */
int MpegVideoHeader::parseSeq(MpegVideoStream* mpegVideoStream) {
  unsigned int data;
  int i ;

  // seq_start_code already flushed!!!

  /* Get horizontal size of image space. */

  h_size=mpegVideoStream->getBits(12);

  /* Get vertical size of image space. */

  v_size=mpegVideoStream->getBits(12);


  /* Calculate macroblock width and height of image space. */

  mb_width = (h_size + 15) / 16;
  mb_height = (v_size + 15) / 16;
  mb_size=mb_height * mb_width-1;
  


  /* Parse of aspect ratio code. */

  data=mpegVideoStream->getBits(4);

  aspect_ratio = (unsigned char) data;

  /* Parse off picture rate code. */

  data=mpegVideoStream->getBits(4);
  picture_rate=VidRateNum[data];

  /* Parse off bit rate. */

  data=mpegVideoStream->getBits(18);
  bit_rate = data;

  /* Flush marker bit. */

  mpegVideoStream->flushBits(1);

  /* Parse off vbv buffer size. */

  data=mpegVideoStream->getBits(10);
  vbv_buffer_size = data;

  /* Parse off contrained parameter flag. */

  data=mpegVideoStream->getBits(1);
  if (data) {
    const_param_flag = true;
  } else
    const_param_flag = false;

  /*
   * If intra_quant_matrix_flag set, parse off intra quant matrix values.
   */
  data=mpegVideoStream->getBits(1);
  if (data) {
    for (i = 0; i < 64; i++) {
      data=mpegVideoStream->getBits(8);
      intra_quant_matrix[zigzag[i][1]][zigzag[i][0]]=(unsigned char)data;
    }
  }
  /*
   * If non intra quant matrix flag set, parse off non intra quant matrix
   * values.
   */

  data=mpegVideoStream->getBits(1);
  if (data) {
    for (i = 0; i < 64; i++) {
      data=mpegVideoStream->getBits(8);

      non_intra_quant_matrix[zigzag[i&0x3f][1]][zigzag[i&0x3f][0]]=
	(unsigned char) data;
    }
  }
  /*
   * If next start code is extension/user start code, 
   * parse off extension data.
   */
  extension->processExtensionData(mpegVideoStream);

  return true;

}




void MpegVideoHeader::copyTo(MpegVideoHeader* dest) {


  dest->h_size=h_size;
  dest->v_size=v_size;
  dest->mb_height=mb_height;
  dest->mb_width=mb_width;
  dest->mb_size=mb_size;
  dest->aspect_ratio=aspect_ratio;
  dest->bit_rate=bit_rate;
  dest->vbv_buffer_size=vbv_buffer_size;
  dest->const_param_flag=const_param_flag;
  dest->picture_rate=picture_rate;

  int i;
  int j;


   /* Copy default intra matrix. */

  for (i = 0; i < 8; i++) {
    for (j = 0; j < 8; j++) {
      dest->intra_quant_matrix[i][j]=intra_quant_matrix[i][j];

    }
  }

  /* Initialize non intra quantization matrix. */

  for (i = 0; i < 8; i++) {
    for (j = 0; j < 8; j++) {
      dest->non_intra_quant_matrix[i][j] =non_intra_quant_matrix[i][j] ;
    }
  }
  
}




double MpegVideoHeader::getPictureTime() {
  if (picture_rate > 0) {
    return 1.0/picture_rate;
  }
  return 0.0;
}







void MpegVideoHeader::print(char* description) {
  cout << "MpegVideoHeader [START]:"<<description<<endl;
  cout <<"h_size:"<<h_size<<endl;
  cout <<"v_size:"<<v_size<<endl;
  cout <<"mb_height:"<<mb_height<<endl;
  cout <<"mb_width:"<<mb_width<<endl;
  cout <<"mb_size:"<<mb_size<<endl;
  cout <<"aspect_ratio:"<<aspect_ratio<<endl;
  cout <<"bit_rate:"<<bit_rate<<endl;
  cout <<"vbv_buffer_size:"<<vbv_buffer_size<<endl;
  cout <<"const_param_flag:"<<const_param_flag<<endl;
  cout << "MpegVideoHeader [END]:"<<endl;

}