1 #include "../libzmpeg3.h"
7 int load_intra_quantizer_matrix, load_non_intra_quantizer_matrix;
10 horizontal_size = vstream->get_bits(12);
11 vertical_size = vstream->get_bits(12);
14 framerate_code = vstream->get_bits(4);
15 bitrate = vstream->get_bits(18);
16 vstream->get_bit_noptr(); /* marker bit (=1) */
17 //int vbv_buffer_size =
18 vstream->get_bits(10);
19 //int constrained_parameters_flag =
20 vstream->get_bit_noptr();
21 frame_rate = frame_rate_table[framerate_code];
23 load_intra_quantizer_matrix = vstream->get_bit_noptr();
24 if( load_intra_quantizer_matrix ) {
26 intra_quantizer_matrix[zigzag_scan_table[i]] = vstream->get_byte_noptr();
30 intra_quantizer_matrix[i] = default_intra_quantizer_matrix[i];
33 load_non_intra_quantizer_matrix = vstream->get_bit_noptr();
34 if( load_non_intra_quantizer_matrix ) {
36 non_intra_quantizer_matrix[zigzag_scan_table[i]] = vstream->get_byte_noptr();
40 non_intra_quantizer_matrix[i] = 16;
43 /* copy luminance to chrominance matrices */
44 for( i=0; i<64; ++i ) {
45 chroma_intra_quantizer_matrix[i] = intra_quantizer_matrix[i];
46 chroma_non_intra_quantizer_matrix[i] = non_intra_quantizer_matrix[i];
54 /* decode sequence extension */
60 scalable_mode = slice_decoder_t::sc_NONE; /* unless overwritten by seq. scal. ext. */
62 vstream->get_byte_noptr();
63 prog_seq = vstream->get_bit_noptr();
64 chroma_format = vstream->get_bits(2);
65 int horizontal_size_extension = vstream->get_bits(2);
66 int vertical_size_extension = vstream->get_bits(2);
67 //int bit_rate_extension =
68 vstream->get_bits(12);
69 vstream->get_bit_noptr();
70 //int vbv_buffer_size_extension =
71 vstream->get_byte_noptr();
73 vstream->get_bit_noptr();
74 //int frame_rate_extension_n =
76 //int frame_rate_extension_d =
78 horizontal_size = (horizontal_size_extension << 12) | (horizontal_size & 0x0fff);
79 vertical_size = (vertical_size_extension << 12) | (vertical_size & 0x0fff);
84 /* decode sequence display extension */
87 sequence_display_extension()
91 int colour_description = vstream->get_bit_noptr();
93 if( colour_description ) {
94 //int colour_primaries =
95 vstream->get_byte_noptr();
96 //int transfer_characteristics =
97 vstream->get_byte_noptr();
98 matrix_coefficients = vstream->get_byte_noptr();
101 //int display_horizontal_size =
102 vstream->get_bits(14);
103 vstream->get_bit_noptr();
104 //int display_vertical_size =
105 vstream->get_bits(14);
110 /* decode quant matrix entension */
113 quant_matrix_extension()
116 int load_intra_quantiser_matrix, load_non_intra_quantiser_matrix;
117 int load_chroma_intra_quantiser_matrix;
118 int load_chroma_non_intra_quantiser_matrix;
120 load_intra_quantiser_matrix = vstream->get_bit_noptr();
121 if( load_intra_quantiser_matrix != 0 ) {
122 for( i=0; i<64; ++i ) {
123 chroma_intra_quantizer_matrix[zigzag_scan_table[i]]
124 = intra_quantizer_matrix[zigzag_scan_table[i]]
125 = vstream->get_byte_noptr();
129 load_non_intra_quantiser_matrix = vstream->get_bit_noptr();
130 if( load_non_intra_quantiser_matrix != 0 ) {
131 for( i=0; i<64; ++i ) {
132 chroma_non_intra_quantizer_matrix[zigzag_scan_table[i]]
133 = non_intra_quantizer_matrix[zigzag_scan_table[i]]
134 = vstream->get_byte_noptr();
138 load_chroma_intra_quantiser_matrix = vstream->get_bit_noptr();
139 if( load_chroma_intra_quantiser_matrix != 0 ) {
140 for( i=0; i<64; ++i )
141 chroma_intra_quantizer_matrix[zigzag_scan_table[i]] = vstream->get_byte_noptr();
144 load_chroma_non_intra_quantiser_matrix = vstream->get_bit_noptr();
145 if( load_chroma_non_intra_quantiser_matrix != 0 ) {
146 for( i=0; i<64; ++i )
147 chroma_non_intra_quantizer_matrix[zigzag_scan_table[i]] = vstream->get_byte_noptr();
154 /* decode sequence scalable extension */
157 sequence_scalable_extension()
159 scalable_mode = vstream->get_bits(2) + 1; /* add 1 to make sc_DP != sc_NONE */
161 vstream->get_bits(4);
163 if( scalable_mode == slice_decoder_t::sc_SPAT ) {
164 llw = vstream->get_bits(14); /* lower_layer_prediction_horizontal_size */
165 vstream->get_bit_noptr();
166 llh = vstream->get_bits(14); /* lower_layer_prediction_vertical_size */
167 hm = vstream->get_bits(5);
168 hn = vstream->get_bits(5);
169 vm = vstream->get_bits(5);
170 vn = vstream->get_bits(5);
173 if( scalable_mode == slice_decoder_t::sc_TEMP )
174 zerr("temporal scalability not implemented\n");
179 /* decode picture display extension */
182 picture_display_extension()
184 // short frame_centre_horizontal_offset[3];
185 // short frame_centre_vertical_offset[3];
187 int n = prog_seq ? (!repeatfirst ? 1 : (!topfirst ? 2 : 3)) :
188 pict_struct != pics_FRAME_PICTURE ? 1 : (!repeatfirst ? 2 : 3) ;
190 for( int i=0; i<n; ++i ) {
191 //frame_centre_horizontal_offset[i] = (short)
192 vstream->get_bits(16);
193 vstream->get_bit_noptr();
194 //frame_centre_vertical_offset[i] = (short)
195 vstream->get_bits(16);
196 vstream->get_bit_noptr();
202 /* decode picture coding extension */
205 picture_coding_extension()
207 h_forw_r_size = vstream->get_bits(4) - 1;
208 v_forw_r_size = vstream->get_bits(4) - 1;
209 h_back_r_size = vstream->get_bits(4) - 1;
210 v_back_r_size = vstream->get_bits(4) - 1;
211 dc_prec = vstream->get_bits(2);
212 pict_struct = vstream->get_bits(2);
213 topfirst = vstream->get_bit_noptr();
214 frame_pred_dct = vstream->get_bit_noptr();
215 conceal_mv = vstream->get_bit_noptr();
216 qscale_type = vstream->get_bit_noptr();
217 intravlc = vstream->get_bit_noptr();
218 altscan = vstream->get_bit_noptr();
219 repeatfirst = vstream->get_bit_noptr();
220 //int chroma_420_type =
221 vstream->get_bit_noptr();
222 prog_frame = vstream->get_bit_noptr();
223 if( repeat_fields > 2 ) repeat_fields = 0;
226 if( prog_seq ) repeat_fields += !topfirst ? 2 : 4;
227 else if( prog_frame ) ++repeat_fields;
231 //static const char *pstruct[] = { "nil", "top", "bot", "fld" };
232 //zmsgs("pstruct %s prog_seq%d prog_frame%d topfirst%d repeatfirst%d repeat_fields%d\n",
233 // pstruct[pict_struct], prog_seq , prog_frame, topfirst, repeatfirst, repeat_fields);
235 int composite_display_flag = vstream->get_bit_noptr();
236 if( composite_display_flag ) {
238 vstream->get_bit_noptr();
239 field_sequence = vstream->get_bits(3);
241 vstream->get_bit_noptr();
242 //int burst_amplitude =
243 vstream->get_bits(7);
244 //int sub_carrier_phase =
245 vstream->get_byte_noptr();
250 /* decode picture spatial scalable extension */
253 picture_spatial_scalable_extension()
255 pict_scal = 1; /* use spatial scalability in this picture */
256 lltempref = vstream->get_bits(10);
257 vstream->get_bit_noptr();
258 llx0 = vstream->get_bits(15);
259 if(llx0 >= 16384) llx0 -= 32768;
260 vstream->get_bit_noptr();
261 lly0 = vstream->get_bits(15);
262 if(lly0 >= 16384) lly0 -= 32768;
263 stwc_table_index = vstream->get_bits(2);
264 llprog_frame = vstream->get_bit_noptr();
265 llfieldsel = vstream->get_bit_noptr();
270 /* decode picture temporal scalable extension
277 picture_temporal_scalable_extension()
279 zerr("temporal scalability not supported\n");
284 /* decode extension and user data */
289 int zcode = vstream->next_start_code();
290 if( zcode < 0 ) return 1;
291 while( zcode == EXT_START_CODE || zcode == USER_START_CODE ) {
293 if( zcode == EXT_START_CODE ) {
294 int ext_id = vstream->get_bits(4);
297 sequence_extension();
300 sequence_display_extension();
303 quant_matrix_extension();
306 sequence_scalable_extension();
309 picture_display_extension();
312 picture_coding_extension();
314 case ext_id_SPATSCAL:
315 picture_spatial_scalable_extension();
317 case ext_id_TEMPSCAL:
318 picture_temporal_scalable_extension();
321 zerrs("reserved extension start code ID %d\n", ext_id);
325 else if( subtitle_track >= 0 ) {
326 int32_t id = vstream->show_bits(32);
327 if( id == 0x47413934 ) { /* "GA94" - atsc user data */
329 int typ = vstream->get_bits(8);
330 if( typ == 3 ) /* mpeg cc */
331 get_cc()->get_atsc_data(vstream);
334 zcode = vstream->next_start_code();
335 if( zcode < 0 ) return 1;
337 //zmsgs("prog_seq=%d prog_frame=%d\n", prog_seq,prog_frame);
342 /* decode group of pictures header */
350 vstream->get_bit_noptr();
351 gop_timecode.hour = vstream->get_bits(5);
352 gop_timecode.minute = vstream->get_bits(6);
353 vstream->get_bit_noptr();
354 gop_timecode.second = vstream->get_bits(6);
355 gop_timecode.frame = vstream->get_bits(6);
357 vstream->get_bit_noptr();
359 vstream->get_bit_noptr();
361 //zmsgs("%d:%d:%d:%d %d %d %d\n",
362 // gop_timecode.hour, gop_timecode.minute, gop_timecode.second,
363 // gop_timecode.frame, drop_flag, closed_gop, broken_link);
364 return vstream->error();
367 /* decode picture header */
372 pict_scal = 0; /* unless overwritten by pict. spat. scal. ext. */
374 vstream->get_bits(10);
375 pict_type = vstream->get_bits(3);
377 vstream->get_bits(16);
378 if( pict_type == pic_type_P || pict_type == pic_type_B ) {
379 full_forw = vstream->get_bit_noptr();
380 forw_r_size = vstream->get_bits(3) - 1;
383 if( pict_type == pic_type_B ) {
384 full_back = vstream->get_bit_noptr();
385 back_r_size = vstream->get_bits(3) - 1;
388 /* get extra bit picture */
389 while( !vstream->eof() && vstream->get_bit_noptr() )
390 vstream->get_bits_noptr(8);
395 /* move to start of header. */
396 /* this updates the demuxer times */
400 for( int count=0x1000; --count>=0; ) {
401 int zcode = vstream->next_start_code();
402 if( zcode < 0 ) break;
404 case SEQUENCE_START_CODE:
406 case PICTURE_START_CODE:
419 // Stop search after this many start codes
420 for( int count=0x1000; --count>=0; ) {
421 /* look for startcode */
422 int zcode = vstream->next_start_code();
423 if( zcode < 0 ) break;
427 case SEQUENCE_START_CODE:
428 /* a sequence header should be found before returning from get_header the */
429 /* first time (this is to set horizontal/vertical size properly) */
440 case PICTURE_START_CODE:
443 if( cc ) cc->reorder();
445 return 0; /* Exit here */
453 int zslice_buffer_t::
456 while( get_bit() ) get_bits(8);
460 /* decode slice header */
461 int zslice_decoder_t::
464 int slice_vertical_position_extension;
466 slice_vertical_position_extension =
467 (video->mpeg2 && video->vertical_size>2800) ?
468 slice_buffer->get_bits(3) : 0;
470 if( video->scalable_mode == sc_DP )
471 pri_brk = slice_buffer->get_bits(7);
473 qs = slice_buffer->get_bits(5);
474 quant_scale = video->mpeg2 ?
475 (video->qscale_type ? non_linear_mquant_table[qs] : (qs << 1)) :
478 //int intra_slice = 0;
479 if( slice_buffer->get_bit() ) {
481 slice_buffer->get_bit();
482 slice_buffer->get_bits(7);
483 slice_buffer->ext_bit_info();
486 return slice_vertical_position_extension;