r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / libmpeg3 / video / headers.c
blob325f6a80bd92fac8288a926bcb40939afdb58d28
1 #include "../mpeg3demux.h"
2 #include "../mpeg3private.h"
3 #include "../mpeg3protos.h"
4 #include "mpeg3video.h"
6 #include <stdio.h>
7 #include <stdlib.h>
9 int mpeg3video_getseqhdr(mpeg3video_t *video)
11 int i;
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);
35 else
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);
47 else
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");
61 return 0;
65 /* decode sequence extension */
67 int mpeg3video_sequence_extension(mpeg3video_t *video)
69 int prof_lev;
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;
73 int pos = 0;
75 video->mpeg2 = 1;
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);
90 return 0;
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;
100 int pos = 0;
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);
114 return 0;
118 /* decode quant matrix entension */
120 int mpeg3video_quant_matrix_extension(mpeg3video_t *video)
122 int i;
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;
126 int pos = 0;
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);
159 return 0;
163 /* decode sequence scalable extension */
165 int mpeg3video_sequence_scalable_extension(mpeg3video_t *video)
167 int layer_id;
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");
185 return 0;
189 /* decode picture display extension */
191 int mpeg3video_picture_display_extension(mpeg3video_t *video)
193 int n, i;
194 short frame_centre_horizontal_offset[3];
195 short frame_centre_vertical_offset[3];
199 if(video->prog_seq || video->pict_struct != FRAME_PICTURE)
200 n = 1;
201 else
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);
214 return 0;
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;
251 if(video->prog_seq)
253 if(video->repeatfirst)
255 if(video->topfirst)
256 video->repeat_count += 200;
257 else
258 video->repeat_count += 100;
261 else
262 if(video->prog_frame)
264 if(video->repeatfirst)
266 video->repeat_count += 50;
270 //printf("mpeg3video_picture_coding_extension %d\n", video->repeat_count);
271 composite_display_flag = mpeg3bits_getbit_noptr(video->vstream);
273 if(composite_display_flag)
275 v_axis = mpeg3bits_getbit_noptr(video->vstream);
276 video->field_sequence = mpeg3bits_getbits(video->vstream, 3);
277 sub_carrier = mpeg3bits_getbit_noptr(video->vstream);
278 burst_amplitude = mpeg3bits_getbits(video->vstream, 7);
279 sub_carrier_phase = mpeg3bits_getbyte_noptr(video->vstream);
281 return 0;
285 /* decode picture spatial scalable extension */
287 int mpeg3video_picture_spatial_scalable_extension(mpeg3video_t *video)
289 video->pict_scal = 1; /* use spatial scalability in this picture */
291 video->lltempref = mpeg3bits_getbits(video->vstream, 10);
292 mpeg3bits_getbit_noptr(video->vstream);
293 video->llx0 = mpeg3bits_getbits(video->vstream, 15);
294 if(video->llx0 >= 16384) video->llx0 -= 32768;
295 mpeg3bits_getbit_noptr(video->vstream);
296 video->lly0 = mpeg3bits_getbits(video->vstream, 15);
297 if(video->lly0 >= 16384) video->lly0 -= 32768;
298 video->stwc_table_index = mpeg3bits_getbits(video->vstream, 2);
299 video->llprog_frame = mpeg3bits_getbit_noptr(video->vstream);
300 video->llfieldsel = mpeg3bits_getbit_noptr(video->vstream);
301 return 0;
305 /* decode picture temporal scalable extension
307 * not implemented
311 int mpeg3video_picture_temporal_scalable_extension(mpeg3video_t *video)
313 fprintf(stderr, "mpeg3video_picture_temporal_scalable_extension: temporal scalability not supported\n");
314 return 0;
318 /* decode extension and user data */
320 int mpeg3video_ext_user_data(mpeg3video_t *video)
322 int code = mpeg3bits_next_startcode(video->vstream);
325 while((code == MPEG3_EXT_START_CODE || code == MPEG3_USER_START_CODE) &&
326 !mpeg3bits_eof(video->vstream))
328 mpeg3bits_refill(video->vstream);
330 if(code == MPEG3_EXT_START_CODE)
332 int ext_id = mpeg3bits_getbits(video->vstream, 4);
333 switch(ext_id)
335 case SEQ_ID:
336 mpeg3video_sequence_extension(video);
337 break;
338 case DISP_ID:
339 mpeg3video_sequence_display_extension(video);
340 break;
341 case QUANT_ID:
342 mpeg3video_quant_matrix_extension(video);
343 break;
344 case SEQSCAL_ID:
345 mpeg3video_sequence_scalable_extension(video);
346 break;
347 case PANSCAN_ID:
348 mpeg3video_picture_display_extension(video);
349 break;
350 case CODING_ID:
351 mpeg3video_picture_coding_extension(video);
352 break;
353 case SPATSCAL_ID:
354 mpeg3video_picture_spatial_scalable_extension(video);
355 break;
356 case TEMPSCAL_ID:
357 mpeg3video_picture_temporal_scalable_extension(video);
358 break;
359 default:
360 fprintf(stderr,"mpeg3video_ext_user_data: reserved extension start code ID %d\n", ext_id);
361 break;
364 code = mpeg3bits_next_startcode(video->vstream);
366 return 0;
370 /* decode group of pictures header */
372 int mpeg3video_getgophdr(mpeg3video_t *video)
374 int drop_flag, closed_gop, broken_link;
376 //printf("mpeg3video_getgophdr 1\n");
377 video->has_gops = 1;
378 drop_flag = mpeg3bits_getbit_noptr(video->vstream);
379 video->gop_timecode.hour = mpeg3bits_getbits(video->vstream, 5);
380 video->gop_timecode.minute = mpeg3bits_getbits(video->vstream, 6);
381 mpeg3bits_getbit_noptr(video->vstream);
382 video->gop_timecode.second = mpeg3bits_getbits(video->vstream, 6);
383 video->gop_timecode.frame = mpeg3bits_getbits(video->vstream, 6);
384 closed_gop = mpeg3bits_getbit_noptr(video->vstream);
385 broken_link = mpeg3bits_getbit_noptr(video->vstream);
387 //printf("mpeg3video_getgophdr 100\n");
389 * 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,
390 * drop_flag, closed_gop, broken_link);
392 return mpeg3bits_error(video->vstream);
395 /* decode picture header */
397 int mpeg3video_getpicturehdr(mpeg3video_t *video)
399 int temp_ref, vbv_delay;
401 video->pict_scal = 0; /* unless overwritten by pict. spat. scal. ext. */
403 temp_ref = mpeg3bits_getbits(video->vstream, 10);
404 video->pict_type = mpeg3bits_getbits(video->vstream, 3);
405 vbv_delay = mpeg3bits_getbits(video->vstream, 16);
407 if(video->pict_type == P_TYPE || video->pict_type == B_TYPE)
409 video->full_forw = mpeg3bits_getbit_noptr(video->vstream);
410 video->forw_r_size = mpeg3bits_getbits(video->vstream, 3) - 1;
413 if(video->pict_type == B_TYPE)
415 video->full_back = mpeg3bits_getbit_noptr(video->vstream);
416 video->back_r_size = mpeg3bits_getbits(video->vstream, 3) - 1;
419 /* get extra bit picture */
420 while(mpeg3bits_getbit_noptr(video->vstream) &&
421 !mpeg3bits_eof(video->vstream))
422 mpeg3bits_getbyte_noptr(video->vstream);
423 return 0;
427 int mpeg3video_get_header(mpeg3video_t *video, int dont_repeat)
429 unsigned int code;
431 //printf("mpeg3video_get_header 1\n");
432 /* a sequence header should be found before returning from `getheader' the */
433 /* first time (this is to set horizontal/vertical size properly) */
435 //printf("mpeg3video_get_header 1 %d %d\n", video->repeat_count, video->current_repeat);
436 /* Repeat the frame until it's less than 1 count from repeat_count */
437 if(video->repeat_count - video->current_repeat >= 100 && !dont_repeat)
439 return 0;
442 if(dont_repeat)
444 video->repeat_count = 0;
445 video->current_repeat = 0;
447 else
448 video->repeat_count -= video->current_repeat;
450 // Case of no picture coding extension
451 if(video->repeat_count < 0) video->repeat_count = 0;
453 * printf("mpeg3video_get_header 2 %lld %lld\n",
454 * video->vstream->demuxer->titles[0]->fs->current_byte,
455 * video->vstream->demuxer->titles[0]->fs->total_bytes);
457 //printf("mpeg3video_get_header 10\n");
459 while(1)
461 //printf("mpeg3video_get_header 10 %llx\n", mpeg3bits_tell(video->vstream));
462 /* look for startcode */
463 code = mpeg3bits_next_startcode(video->vstream);
465 //printf("mpeg3video_get_header 20 %08x\n", code);
468 * printf("mpeg3video_get_header 2 %llx %llx %08x\n",
469 * video->vstream->demuxer->titles[0]->fs->current_byte,
470 * video->vstream->demuxer->titles[0]->fs->total_bytes,
471 * code);
474 //printf("mpeg3video_get_header 2 %p\n", video->vstream->demuxer);
475 if(mpeg3bits_eof(video->vstream)) return 1;
476 //printf("mpeg3video_get_header 1\n");
477 if(code != MPEG3_SEQUENCE_END_CODE) mpeg3bits_refill(video->vstream);
479 //printf("mpeg3video_get_header 1\n");
480 switch(code)
482 case MPEG3_SEQUENCE_START_CODE:
483 video->found_seqhdr = 1;
484 //printf("mpeg3video_get_header 1\n");
485 mpeg3video_getseqhdr(video);
486 //printf("mpeg3video_get_header 1\n");
487 mpeg3video_ext_user_data(video);
488 //printf("mpeg3video_get_header 100\n");
489 break;
491 case MPEG3_GOP_START_CODE:
492 mpeg3video_getgophdr(video);
493 mpeg3video_ext_user_data(video);
494 break;
496 case MPEG3_PICTURE_START_CODE:
497 //printf("%x\n", mpeg3bits_tell(video->vstream));
498 mpeg3video_getpicturehdr(video);
499 mpeg3video_ext_user_data(video);
500 if(video->found_seqhdr) return 0; /* Exit here */
501 break;
503 case MPEG3_SEQUENCE_END_CODE:
504 // Continue until the end
505 mpeg3bits_refill(video->vstream);
506 break;
508 default:
509 break;
512 return 1; /* Shouldn't be reached. */
515 int mpeg3video_ext_bit_info(mpeg3_slice_buffer_t *slice_buffer)
517 while(mpeg3slice_getbit(slice_buffer)) mpeg3slice_getbyte(slice_buffer);
518 return 0;
521 /* decode slice header */
522 int mpeg3video_getslicehdr(mpeg3_slice_t *slice, mpeg3video_t *video)
524 int slice_vertical_position_extension, intra_slice;
525 int qs;
527 slice_vertical_position_extension = (video->mpeg2 && video->vertical_size > 2800) ?
528 mpeg3slice_getbits(slice->slice_buffer, 3) : 0;
530 if(video->scalable_mode == SC_DP) slice->pri_brk = mpeg3slice_getbits(slice->slice_buffer, 7);
532 qs = mpeg3slice_getbits(slice->slice_buffer, 5);
533 slice->quant_scale = video->mpeg2 ? (video->qscale_type ? mpeg3_non_linear_mquant_table[qs] : (qs << 1)) : qs;
535 if(mpeg3slice_getbit(slice->slice_buffer))
537 intra_slice = mpeg3slice_getbit(slice->slice_buffer);
538 mpeg3slice_getbits(slice->slice_buffer, 7);
539 mpeg3video_ext_bit_info(slice->slice_buffer);
541 else
542 intra_slice = 0;
544 return slice_vertical_position_extension;