1 #include "../mpeg3demux.h"
2 #include "../mpeg3private.h"
3 #include "../mpeg3protos.h"
4 #include "mpeg3video.h"
9 int mpeg3video_getseqhdr(mpeg3video_t
*video
)
12 mpeg3_t
*file
= video
->file
;
14 int aspect_ratio
, picture_rate
, vbv_buffer_size
;
15 int constrained_parameters_flag
;
16 int load_intra_quantizer_matrix
, load_non_intra_quantizer_matrix
;
18 //printf("mpeg3video_getseqhdr 1\n");
19 video
->horizontal_size
= mpeg3bits_getbits(video
->vstream
, 12);
20 video
->vertical_size
= mpeg3bits_getbits(video
->vstream
, 12);
21 aspect_ratio
= mpeg3bits_getbits(video
->vstream
, 4);
22 video
->framerate_code
= mpeg3bits_getbits(video
->vstream
, 4);
23 video
->bitrate
= mpeg3bits_getbits(video
->vstream
, 18);
24 mpeg3bits_getbit_noptr(video
->vstream
); /* marker bit (=1) */
25 vbv_buffer_size
= mpeg3bits_getbits(video
->vstream
, 10);
26 constrained_parameters_flag
= mpeg3bits_getbit_noptr(video
->vstream
);
27 video
->frame_rate
= mpeg3_frame_rate_table
[video
->framerate_code
];
29 load_intra_quantizer_matrix
= mpeg3bits_getbit_noptr(video
->vstream
);
30 if(load_intra_quantizer_matrix
)
32 for(i
= 0; i
< 64; i
++)
33 video
->intra_quantizer_matrix
[video
->mpeg3_zigzag_scan_table
[i
]] = mpeg3bits_getbyte_noptr(video
->vstream
);
37 for(i
= 0; i
< 64; i
++)
38 video
->intra_quantizer_matrix
[i
] = mpeg3_default_intra_quantizer_matrix
[i
];
41 load_non_intra_quantizer_matrix
= mpeg3bits_getbit_noptr(video
->vstream
);
42 if(load_non_intra_quantizer_matrix
)
44 for(i
= 0; i
< 64; i
++)
45 video
->non_intra_quantizer_matrix
[video
->mpeg3_zigzag_scan_table
[i
]] = mpeg3bits_getbyte_noptr(video
->vstream
);
49 for(i
= 0; i
< 64; i
++)
50 video
->non_intra_quantizer_matrix
[i
] = 16;
53 /* copy luminance to chrominance matrices */
54 for(i
= 0; i
< 64; i
++)
56 video
->chroma_intra_quantizer_matrix
[i
] = video
->intra_quantizer_matrix
[i
];
57 video
->chroma_non_intra_quantizer_matrix
[i
] = video
->non_intra_quantizer_matrix
[i
];
60 //printf("mpeg3video_getseqhdr 100\n");
65 /* decode sequence extension */
67 int mpeg3video_sequence_extension(mpeg3video_t
*video
)
70 int horizontal_size_extension
, vertical_size_extension
;
71 int bit_rate_extension
, vbv_buffer_size_extension
, low_delay
;
72 int frame_rate_extension_n
, frame_rate_extension_d
;
76 video
->scalable_mode
= SC_NONE
; /* unless overwritten by seq. scal. ext. */
77 prof_lev
= mpeg3bits_getbyte_noptr(video
->vstream
);
78 video
->prog_seq
= mpeg3bits_getbit_noptr(video
->vstream
);
79 video
->chroma_format
= mpeg3bits_getbits(video
->vstream
, 2);
80 horizontal_size_extension
= mpeg3bits_getbits(video
->vstream
, 2);
81 vertical_size_extension
= mpeg3bits_getbits(video
->vstream
, 2);
82 bit_rate_extension
= mpeg3bits_getbits(video
->vstream
, 12);
83 mpeg3bits_getbit_noptr(video
->vstream
);
84 vbv_buffer_size_extension
= mpeg3bits_getbyte_noptr(video
->vstream
);
85 low_delay
= mpeg3bits_getbit_noptr(video
->vstream
);
86 frame_rate_extension_n
= mpeg3bits_getbits(video
->vstream
, 2);
87 frame_rate_extension_d
= mpeg3bits_getbits(video
->vstream
, 5);
88 video
->horizontal_size
= (horizontal_size_extension
<< 12) | (video
->horizontal_size
& 0x0fff);
89 video
->vertical_size
= (vertical_size_extension
<< 12) | (video
->vertical_size
& 0x0fff);
94 /* decode sequence display extension */
96 int mpeg3video_sequence_display_extension(mpeg3video_t
*video
)
98 int colour_primaries
= 0, transfer_characteristics
= 0;
99 int display_horizontal_size
, display_vertical_size
;
101 int video_format
= mpeg3bits_getbits(video
->vstream
, 3);
102 int colour_description
= mpeg3bits_getbit_noptr(video
->vstream
);
104 if(colour_description
)
106 colour_primaries
= mpeg3bits_getbyte_noptr(video
->vstream
);
107 transfer_characteristics
= mpeg3bits_getbyte_noptr(video
->vstream
);
108 video
->matrix_coefficients
= mpeg3bits_getbyte_noptr(video
->vstream
);
111 display_horizontal_size
= mpeg3bits_getbits(video
->vstream
, 14);
112 mpeg3bits_getbit_noptr(video
->vstream
);
113 display_vertical_size
= mpeg3bits_getbits(video
->vstream
, 14);
118 /* decode quant matrix entension */
120 int mpeg3video_quant_matrix_extension(mpeg3video_t
*video
)
123 int load_intra_quantiser_matrix
, load_non_intra_quantiser_matrix
;
124 int load_chroma_intra_quantiser_matrix
;
125 int load_chroma_non_intra_quantiser_matrix
;
128 if((load_intra_quantiser_matrix
= mpeg3bits_getbit_noptr(video
->vstream
)) != 0)
130 for(i
= 0; i
< 64; i
++)
132 video
->chroma_intra_quantizer_matrix
[video
->mpeg3_zigzag_scan_table
[i
]]
133 = video
->intra_quantizer_matrix
[video
->mpeg3_zigzag_scan_table
[i
]]
134 = mpeg3bits_getbyte_noptr(video
->vstream
);
138 if((load_non_intra_quantiser_matrix
= mpeg3bits_getbit_noptr(video
->vstream
)) != 0)
140 for (i
= 0; i
< 64; i
++)
142 video
->chroma_non_intra_quantizer_matrix
[video
->mpeg3_zigzag_scan_table
[i
]]
143 = video
->non_intra_quantizer_matrix
[video
->mpeg3_zigzag_scan_table
[i
]]
144 = mpeg3bits_getbyte_noptr(video
->vstream
);
148 if((load_chroma_intra_quantiser_matrix
= mpeg3bits_getbit_noptr(video
->vstream
)) != 0)
150 for(i
= 0; i
< 64; i
++)
151 video
->chroma_intra_quantizer_matrix
[video
->mpeg3_zigzag_scan_table
[i
]] = mpeg3bits_getbyte_noptr(video
->vstream
);
154 if((load_chroma_non_intra_quantiser_matrix
= mpeg3bits_getbit_noptr(video
->vstream
)) != 0)
156 for(i
= 0; i
< 64; i
++)
157 video
->chroma_non_intra_quantizer_matrix
[video
->mpeg3_zigzag_scan_table
[i
]] = mpeg3bits_getbyte_noptr(video
->vstream
);
163 /* decode sequence scalable extension */
165 int mpeg3video_sequence_scalable_extension(mpeg3video_t
*video
)
169 video
->scalable_mode
= mpeg3bits_getbits(video
->vstream
, 2) + 1; /* add 1 to make SC_DP != SC_NONE */
170 layer_id
= mpeg3bits_getbits(video
->vstream
, 4);
172 if(video
->scalable_mode
== SC_SPAT
)
174 video
->llw
= mpeg3bits_getbits(video
->vstream
, 14); /* lower_layer_prediction_horizontal_size */
175 mpeg3bits_getbit_noptr(video
->vstream
);
176 video
->llh
= mpeg3bits_getbits(video
->vstream
, 14); /* lower_layer_prediction_vertical_size */
177 video
->hm
= mpeg3bits_getbits(video
->vstream
, 5);
178 video
->hn
= mpeg3bits_getbits(video
->vstream
, 5);
179 video
->vm
= mpeg3bits_getbits(video
->vstream
, 5);
180 video
->vn
= mpeg3bits_getbits(video
->vstream
, 5);
183 if(video
->scalable_mode
== SC_TEMP
)
184 fprintf(stderr
, "mpeg3video_sequence_scalable_extension: temporal scalability not implemented\n");
189 /* decode picture display extension */
191 int mpeg3video_picture_display_extension(mpeg3video_t
*video
)
194 short frame_centre_horizontal_offset
[3];
195 short frame_centre_vertical_offset
[3];
199 if(video
->prog_seq
|| video
->pict_struct
!= FRAME_PICTURE
)
202 n
= video
->repeatfirst
? 3 : 2;
207 for(i
= 0; i
< n
; i
++)
209 frame_centre_horizontal_offset
[i
] = (short)mpeg3bits_getbits(video
->vstream
, 16);
210 mpeg3bits_getbit_noptr(video
->vstream
);
211 frame_centre_vertical_offset
[i
] = (short)mpeg3bits_getbits(video
->vstream
, 16);
212 mpeg3bits_getbit_noptr(video
->vstream
);
218 /* decode picture coding extension */
220 int mpeg3video_picture_coding_extension(mpeg3video_t
*video
)
222 int chroma_420_type
, composite_display_flag
;
223 int v_axis
= 0, sub_carrier
= 0, burst_amplitude
= 0, sub_carrier_phase
= 0;
225 video
->h_forw_r_size
= mpeg3bits_getbits(video
->vstream
, 4) - 1;
226 video
->v_forw_r_size
= mpeg3bits_getbits(video
->vstream
, 4) - 1;
227 video
->h_back_r_size
= mpeg3bits_getbits(video
->vstream
, 4) - 1;
228 video
->v_back_r_size
= mpeg3bits_getbits(video
->vstream
, 4) - 1;
229 video
->dc_prec
= mpeg3bits_getbits(video
->vstream
, 2);
230 video
->pict_struct
= mpeg3bits_getbits(video
->vstream
, 2);
231 video
->topfirst
= mpeg3bits_getbit_noptr(video
->vstream
);
232 video
->frame_pred_dct
= mpeg3bits_getbit_noptr(video
->vstream
);
233 video
->conceal_mv
= mpeg3bits_getbit_noptr(video
->vstream
);
234 video
->qscale_type
= mpeg3bits_getbit_noptr(video
->vstream
);
235 video
->intravlc
= mpeg3bits_getbit_noptr(video
->vstream
);
236 video
->altscan
= mpeg3bits_getbit_noptr(video
->vstream
);
239 video
->repeatfirst
= mpeg3bits_getbit_noptr(video
->vstream
);
242 chroma_420_type
= mpeg3bits_getbit_noptr(video
->vstream
);
243 video
->prog_frame
= mpeg3bits_getbit_noptr(video
->vstream
);
245 if(video
->repeat_count
> 100)
246 video
->repeat_count
= 0;
247 video
->repeat_count
+= 100;
249 video
->current_repeat
= 0;
252 * printf("%d %d %d %d\n",
253 * video->prog_seq ? 1 : 0,
254 * video->prog_frame ? 1 : 0,
255 * video->topfirst ? 1 : 0,
256 * video->repeatfirst ? 1 : 0);
261 if(video
->repeatfirst
)
266 video
->repeat_count
+= 200;
268 video
->repeat_count
+= 100;
271 if(video
->prog_frame
)
273 video
->repeat_count
+= 50;
277 composite_display_flag
= mpeg3bits_getbit_noptr(video
->vstream
);
279 if(composite_display_flag
)
281 v_axis
= mpeg3bits_getbit_noptr(video
->vstream
);
282 video
->field_sequence
= mpeg3bits_getbits(video
->vstream
, 3);
283 sub_carrier
= mpeg3bits_getbit_noptr(video
->vstream
);
284 burst_amplitude
= mpeg3bits_getbits(video
->vstream
, 7);
285 sub_carrier_phase
= mpeg3bits_getbyte_noptr(video
->vstream
);
291 /* decode picture spatial scalable extension */
293 int mpeg3video_picture_spatial_scalable_extension(mpeg3video_t
*video
)
295 video
->pict_scal
= 1; /* use spatial scalability in this picture */
297 video
->lltempref
= mpeg3bits_getbits(video
->vstream
, 10);
298 mpeg3bits_getbit_noptr(video
->vstream
);
299 video
->llx0
= mpeg3bits_getbits(video
->vstream
, 15);
300 if(video
->llx0
>= 16384) video
->llx0
-= 32768;
301 mpeg3bits_getbit_noptr(video
->vstream
);
302 video
->lly0
= mpeg3bits_getbits(video
->vstream
, 15);
303 if(video
->lly0
>= 16384) video
->lly0
-= 32768;
304 video
->stwc_table_index
= mpeg3bits_getbits(video
->vstream
, 2);
305 video
->llprog_frame
= mpeg3bits_getbit_noptr(video
->vstream
);
306 video
->llfieldsel
= mpeg3bits_getbit_noptr(video
->vstream
);
311 /* decode picture temporal scalable extension
317 int mpeg3video_picture_temporal_scalable_extension(mpeg3video_t
*video
)
319 fprintf(stderr
, "mpeg3video_picture_temporal_scalable_extension: temporal scalability not supported\n");
324 /* decode extension and user data */
326 int mpeg3video_ext_user_data(mpeg3video_t
*video
)
328 int code
= mpeg3bits_next_startcode(video
->vstream
);
331 while((code
== MPEG3_EXT_START_CODE
|| code
== MPEG3_USER_START_CODE
) &&
332 !mpeg3bits_eof(video
->vstream
))
334 mpeg3bits_refill(video
->vstream
);
336 if(code
== MPEG3_EXT_START_CODE
)
338 int ext_id
= mpeg3bits_getbits(video
->vstream
, 4);
342 mpeg3video_sequence_extension(video
);
345 mpeg3video_sequence_display_extension(video
);
348 mpeg3video_quant_matrix_extension(video
);
351 mpeg3video_sequence_scalable_extension(video
);
354 mpeg3video_picture_display_extension(video
);
357 mpeg3video_picture_coding_extension(video
);
360 mpeg3video_picture_spatial_scalable_extension(video
);
363 mpeg3video_picture_temporal_scalable_extension(video
);
366 fprintf(stderr
,"mpeg3video_ext_user_data: reserved extension start code ID %d\n", ext_id
);
370 code
= mpeg3bits_next_startcode(video
->vstream
);
374 * printf("mpeg3video_ext_user_data prog_seq=%d prog_frame=%d\n",
376 * video->prog_frame);
382 /* decode group of pictures header */
384 int mpeg3video_getgophdr(mpeg3video_t
*video
)
386 int drop_flag
, closed_gop
, broken_link
;
388 //printf("mpeg3video_getgophdr 1\n");
390 drop_flag
= mpeg3bits_getbit_noptr(video
->vstream
);
391 video
->gop_timecode
.hour
= mpeg3bits_getbits(video
->vstream
, 5);
392 video
->gop_timecode
.minute
= mpeg3bits_getbits(video
->vstream
, 6);
393 mpeg3bits_getbit_noptr(video
->vstream
);
394 video
->gop_timecode
.second
= mpeg3bits_getbits(video
->vstream
, 6);
395 video
->gop_timecode
.frame
= mpeg3bits_getbits(video
->vstream
, 6);
396 closed_gop
= mpeg3bits_getbit_noptr(video
->vstream
);
397 broken_link
= mpeg3bits_getbit_noptr(video
->vstream
);
399 //printf("mpeg3video_getgophdr 100\n");
401 * printf("%d:%d:%d:%d %d %d %d\n", video->gop_timecode.hour, video->gop_timecode.minute, video->gop_timecode.second, video->gop_timecode.frame,
402 * drop_flag, closed_gop, broken_link);
404 return mpeg3bits_error(video
->vstream
);
407 /* decode picture header */
409 int mpeg3video_getpicturehdr(mpeg3video_t
*video
)
411 int temp_ref
, vbv_delay
;
413 video
->pict_scal
= 0; /* unless overwritten by pict. spat. scal. ext. */
415 temp_ref
= mpeg3bits_getbits(video
->vstream
, 10);
416 video
->pict_type
= mpeg3bits_getbits(video
->vstream
, 3);
417 vbv_delay
= mpeg3bits_getbits(video
->vstream
, 16);
419 if(video
->pict_type
== P_TYPE
|| video
->pict_type
== B_TYPE
)
421 video
->full_forw
= mpeg3bits_getbit_noptr(video
->vstream
);
422 video
->forw_r_size
= mpeg3bits_getbits(video
->vstream
, 3) - 1;
425 if(video
->pict_type
== B_TYPE
)
427 video
->full_back
= mpeg3bits_getbit_noptr(video
->vstream
);
428 video
->back_r_size
= mpeg3bits_getbits(video
->vstream
, 3) - 1;
431 /* get extra bit picture */
432 while(mpeg3bits_getbit_noptr(video
->vstream
) &&
433 !mpeg3bits_eof(video
->vstream
))
434 mpeg3bits_getbyte_noptr(video
->vstream
);
439 int mpeg3video_get_header(mpeg3video_t
*video
, int dont_repeat
)
442 mpeg3_t
*file
= video
->file
;
443 mpeg3_vtrack_t
*track
= video
->track
;
444 mpeg3_bits_t
*vstream
= video
->vstream
;
445 mpeg3_demuxer_t
*demuxer
= track
->demuxer
;
447 /* a sequence header should be found before returning from get_header the */
448 /* first time (this is to set horizontal/vertical size properly) */
450 /* Repeat the frame until it's less than 1 count from repeat_count */
451 if(video
->repeat_count
- video
->current_repeat
>= 100 && !dont_repeat
)
458 video
->repeat_count
= 0;
459 video
->current_repeat
= 0;
462 video
->repeat_count
-= video
->current_repeat
;
464 // Case of no picture coding extension
465 if(video
->repeat_count
< 0) video
->repeat_count
= 0;
469 /* look for startcode */
470 code
= mpeg3bits_next_startcode(vstream
);
473 if(mpeg3bits_eof(vstream
)) return 1;
476 if(code
!= MPEG3_SEQUENCE_END_CODE
) mpeg3bits_refill(vstream
);
480 case MPEG3_SEQUENCE_START_CODE
:
481 video
->found_seqhdr
= 1;
482 mpeg3video_getseqhdr(video
);
483 mpeg3video_ext_user_data(video
);
486 case MPEG3_GOP_START_CODE
:
487 mpeg3video_getgophdr(video
);
488 mpeg3video_ext_user_data(video
);
491 case MPEG3_PICTURE_START_CODE
:
492 mpeg3video_getpicturehdr(video
);
493 mpeg3video_ext_user_data(video
);
494 if(video
->found_seqhdr
) return 0; /* Exit here */
497 case MPEG3_SEQUENCE_END_CODE
:
498 // Continue until the end
499 mpeg3bits_refill(vstream
);
508 return 1; /* Shouldn't be reached. */
511 int mpeg3video_ext_bit_info(mpeg3_slice_buffer_t
*slice_buffer
)
513 while(mpeg3slice_getbit(slice_buffer
)) mpeg3slice_getbyte(slice_buffer
);
517 /* decode slice header */
518 int mpeg3video_getslicehdr(mpeg3_slice_t
*slice
, mpeg3video_t
*video
)
520 int slice_vertical_position_extension
, intra_slice
;
523 slice_vertical_position_extension
= (video
->mpeg2
&& video
->vertical_size
> 2800) ?
524 mpeg3slice_getbits(slice
->slice_buffer
, 3) : 0;
526 if(video
->scalable_mode
== SC_DP
) slice
->pri_brk
= mpeg3slice_getbits(slice
->slice_buffer
, 7);
528 qs
= mpeg3slice_getbits(slice
->slice_buffer
, 5);
529 slice
->quant_scale
= video
->mpeg2
? (video
->qscale_type
? mpeg3_non_linear_mquant_table
[qs
] : (qs
<< 1)) : qs
;
531 if(mpeg3slice_getbit(slice
->slice_buffer
))
533 intra_slice
= mpeg3slice_getbit(slice
->slice_buffer
);
534 mpeg3slice_getbits(slice
->slice_buffer
, 7);
535 mpeg3video_ext_bit_info(slice
->slice_buffer
);
540 return slice_vertical_position_extension
;