-
Notifications
You must be signed in to change notification settings - Fork 1
/
sequence.cpp
184 lines (152 loc) · 5.43 KB
/
sequence.cpp
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
#include <typeinfo>
#include "mpegheader.hpp"
#include "mpegtables.hpp"
#include "picture.hpp"
Sequence::Sequence( BitReader &hdr )
{
init();
hdr.reset();
ahabassert ( hdr.readbits( 32 ) == 0x000001b3 );
horizontal_size_value = hdr.readbits( 12 );
vertical_size_value = hdr.readbits( 12 );
uint atmp = hdr.readbits( 4 );
switch ( atmp ) {
case 1: aspect = SAR1x1; break;
case 2: aspect = DAR4x3; break;
case 3: aspect = DAR16x9; break;
case 4: aspect = DAR221x100; break;
default: throw MPEGInvalid(); break;
}
frame_rate_code = hdr.readbits( 4 );
mpegassert( (frame_rate_code != 0) && (frame_rate_code <= 8) );
bit_rate_value = hdr.readbits( 18 );
mpegassert( hdr.readbits( 1 ) == 1 );
vbv_buffer_size_value = hdr.readbits( 10 );
constrained_parameters_flag = hdr.readbits( 1 );
mpegassert( constrained_parameters_flag == 0 );
uint load_intra_quantiser_matrix = hdr.readbits( 1 );
for ( int i = 0; i < 64; i++ ) {
intra_quantiser_matrix[ mpeg2_normal_scan[ i ] ] =
load_intra_quantiser_matrix
? hdr.readbits( 8 )
: default_intra_quantiser_matrix[ i ];
mpegassert( intra_quantiser_matrix[ mpeg2_normal_scan[ i ] ] != 0 );
}
uint load_non_intra_quantiser_matrix = hdr.readbits( 1 );
for ( int i = 0; i < 64; i++ ) {
non_intra_quantiser_matrix[ mpeg2_normal_scan[ i ] ] =
load_non_intra_quantiser_matrix ? hdr.readbits( 8 ) : 16;
mpegassert( intra_quantiser_matrix[ mpeg2_normal_scan[ i ] ] != 0 );
}
}
uint Sequence::get_horizontal_size( void )
{
return (get_extension()->horizontal_size_extension << 12)
| horizontal_size_value;
}
uint Sequence::get_vertical_size( void )
{
return (get_extension()->vertical_size_extension << 12)
| vertical_size_value;
}
uint Sequence::get_mb_width( void )
{
return (get_horizontal_size() + 15) / 16;
}
uint Sequence::get_mb_height( void )
{
if ( get_extension()->progressive_sequence ) {
return (get_vertical_size() + 15)/16;
} else {
return 2*((get_vertical_size() + 31)/32);
}
/* We don't support field pictures. */
}
uint64_t Sequence::get_frame_rate_numerator( void )
{
return sequence_numerators[ frame_rate_code ]
* ( get_extension()->frame_rate_extension_n + 1 );
}
uint64_t Sequence::get_frame_rate_denominator( void )
{
return sequence_denominators[ frame_rate_code ]
* ( get_extension()->frame_rate_extension_d + 1 );
}
double Sequence::get_frame_rate( void )
{
return (double)get_frame_rate_numerator() / (double)get_frame_rate_denominator();
}
void Sequence::set_unknown_quantiser_flags( void )
{
MPEGHeader *hdr = get_next();
while ( hdr && ( typeid( *hdr ) != typeid( Sequence ) ) ) {
if ( typeid( *hdr ) == typeid( Picture ) ) {
Picture *pic = static_cast<Picture *>( hdr );
pic->set_unknown_quantiser_matrix( true );
}
hdr = hdr->get_next();
}
}
void Sequence::link( void )
{
/* We should handle this more gracefully if a stream is truncated
after sequence header but before sequence extension XXX */
/* Find my extension */
SequenceExtension *se = dynamic_cast<SequenceExtension *>( get_next() );
if ( se == NULL ) {
fprintf( stderr, "Sequence extension not found at %ld.\n", (long)get_location() );
throw MPEGInvalid();
}
extension = se;
if ( get_vertical_size() > 2800 ) {
throw ConformanceLimitExceeded();
}
/* Set quantization matrices of pictures until next sequence */
uint8_t *current_intra = intra_quantiser_matrix;
uint8_t *current_non_intra = non_intra_quantiser_matrix;
MPEGHeader *hdr = extension;
while ( hdr && ( typeid( *hdr ) != typeid( Sequence ) ) ) {
if ( typeid( *hdr ) == typeid( QuantMatrixExtension ) ) {
QuantMatrixExtension *tq =
static_cast<QuantMatrixExtension *>( hdr );
uint8_t *new_intra = tq->get_intra_quantiser_matrix();
uint8_t *new_non_intra = tq->get_non_intra_quantiser_matrix();
if ( new_intra ) current_intra = new_intra;
if ( new_non_intra ) current_non_intra = new_non_intra;
} else if ( typeid( *hdr ) == typeid( Picture ) ) {
Picture *tp = static_cast<Picture *>( hdr );
tp->set_sequence( this );
tp->set_intra( current_intra );
tp->set_non_intra( current_non_intra );
}
hdr = hdr->get_next();
}
/* Make sure sequence parameters don't change */
if ( hdr ) {
Sequence *ts = dynamic_cast<Sequence *>( hdr );
ahabassert( ts );
mpegassert( horizontal_size_value == ts->horizontal_size_value );
mpegassert( vertical_size_value == ts->vertical_size_value );
// mpegassert( aspect == ts->aspect );
mpegassert( frame_rate_code == ts->frame_rate_code );
// mpegassert( bit_rate_value == ts->bit_rate_value );
mpegassert( vbv_buffer_size_value == ts->vbv_buffer_size_value );
mpegassert( constrained_parameters_flag == ts->constrained_parameters_flag );
if ( (aspect != ts->aspect) || (bit_rate_value != ts->bit_rate_value) ) {
fprintf( stderr, "Warning, sequence illegally changes parameters (aspect %d=>%d, bit_rate_value %d=>%d).\n",
aspect, ts->aspect, bit_rate_value, ts->bit_rate_value);
}
}
}
double Sequence::get_sar( void )
{
double width = get_horizontal_size(); /* XXX should be display size if present*/
double height = get_vertical_size();
switch ( aspect ) {
case SAR1x1: return 1;
case DAR4x3: return (height/3.0) / (width/4.0);
case DAR16x9: return (height/9.0) / (width/16.0);
case DAR221x100: return (height/100.0) / (width/221.0);
}
throw AhabException();
}