r581: Pierre's patch adding default interlacing to specific fileformats
[cinelerra_cv/mob.git] / mplexhi / inptstrm.c
blob72efade5079d143327814674717feb4c5475aaec
1 #include "main.h"
3 #include <math.h>
6 static double picture_rates [9] = { 0., 24000./1001., 24., 25.,
7 30000./1001., 30., 50., 60000./1001., 60. };
9 char *audio_version[4] =
11 "2.5",
12 "2.0",
13 "reserved",
14 "1.0"
17 unsigned int bitrate_index [4][3][16] =
19 { /* MPEG audio V2.5 */
20 {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,0},
21 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0},
22 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0}
24 { /*RESERVED*/
25 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
26 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
27 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
29 { /* MPEG audio V2 */
30 {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,0},
31 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0},
32 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0}
34 { /* MPEG audio V1 */
35 {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,0},
36 {0,32,48,56,64,80,96,112,128,160,192,224,256,320,384,0},
37 {0,32,40,48,56,64,80,96,112,128,160,192,224,256,320,0}
42 static double ratio [16] = { 0., 1., 0.6735, 0.7031, 0.7615, 0.8055,
43 0.8437, 0.8935, 0.9157, 0.9815, 1.0255, 1.0695, 1.0950, 1.1575,
44 1.2015, 0.};
46 static int frequency [4][4] =
48 /* MPEG audio V2.5 */
49 {11025,12000,8000,0},
50 /* RESERVED */
51 { 0, 0, 0, 0 },
52 /* MPEG audio V2 */
53 {22050,24000, 16000,0},
54 /* MPEG audio V1 */
55 {44100, 48000, 32000, 0}
57 static char mode [4][15] =
58 { "stereo", "joint stereo", "dual channel", "single channel" };
59 static char copyright [2][20] =
60 { "no copyright","copyright protected" };
61 static char original [2][10] =
62 { "copy","original" };
63 static char emphasis [4][20] =
64 { "none", "50/15 microseconds", "reserved", "CCITT J.17" };
65 static unsigned int slots [4] = {12, 144, 144, 0};
66 static unsigned int samples [4] = {384, 1152, 1152, 0};
69 /*************************************************************************
70 MPEG Streams Kontrolle
72 Basic Checks on MPEG Streams
73 *************************************************************************/
75 void marker_bit (bs, what)
76 Bit_stream_struc *bs;
77 unsigned int what;
79 if (what != get1bit(bs))
81 printf ("\nError in MPEG stream at offset (bits) %lld: supposed marker bit not found.\n",bitcount(bs));
82 exit (1);
88 /*************************************************************************
89 MPEG Verifikation der Inputfiles
91 Check if files are valid MPEG streams
92 *************************************************************************/
94 void check_files (int argc,
95 char* argv[],
96 char** audio_file,
97 char** video_file,
98 char** multi_file,
99 unsigned int *audio_bytes,
100 unsigned int *video_bytes
103 Bit_stream_struc bs1, bs2, undo;
104 unsigned int bytes_1, bytes_2;
106 /* As yet no streams determined... */
107 which_streams = 0;
108 if (argc == 3) {
109 if (open_file(argv[1],&bytes_1))
110 exit (1); }
111 else if (argc == 4) {
112 if (open_file(argv[1],&bytes_1) || open_file(argv[2],&bytes_2))
113 exit (1); }
115 init_getbits (&bs1, argv[1]);
117 if (argc == 4)
118 init_getbits (&bs2, argv[2]);
120 /* Das Bitstreampaket kuemmert sich bei einem look_ahead nicht
121 darum, den Buffer vorzubereiten, weil es davon ausgeht, dass
122 vorher mindestens einmal ein getbits () gemacht wurde. Da das
123 hier nicht zutrifft, muss manuell das erledigt werden, was
124 sonst getbits () macht, d.h. ein Buffer eingelesen werden und
125 bestimmte Werte in der Struktur gesetzt werden. */
127 prepareundo(&bs1, &undo);
128 if (getbits( &bs1, 12 ) == 0xfff)
130 *audio_file = argv[1];
131 *audio_bytes= bytes_1;
132 printf ("File %s is a 11172-3 Audio stream.\n",argv[1]);
133 which_streams |= STREAMS_AUDIO;
134 if (argc == 4 ) {
135 if ( getbits(&bs2, 32) != 0x1b3)
137 printf ("File %s is not a MPEG-1/2 Video stream.\n",argv[2]);
138 finish_getbits (&bs1);
139 finish_getbits (&bs2);
140 exit (1);
142 else
144 printf ("File %s is a MPEG-1/2 Video stream.\n",argv[2]);
145 which_streams |= STREAMS_VIDEO;
146 *video_file = argv[2];
147 *video_bytes= bytes_2;
152 else
154 undochanges( &bs1, &undo);
155 if ( getbits( &bs1, 32) == 0x1b3)
157 *video_file = argv[1];
158 *video_bytes= bytes_1;
159 printf ("File %s is a 11172-2 Video stream.\n",argv[1]);
160 which_streams |= STREAMS_VIDEO;
161 if (argc == 4 ) {
162 if ( getbits( &bs2, 12 ) != 0xfff)
164 printf ("File %s is not a 11172-3 Audio stream.\n",argv[2]);
165 finish_getbits (&bs1);
166 finish_getbits (&bs2);
167 exit (1);
169 else
171 printf ("File %s is a 11172-3 Audio stream.\n",argv[2]);
172 which_streams |= STREAMS_AUDIO;
173 *audio_file = argv[2];
174 *audio_bytes= bytes_2;
178 else
180 if (argc == 4) {
181 printf ("Files %s and %s are not valid MPEG streams.\n",
182 argv[1],argv[2]);
183 finish_getbits (&bs1);
184 finish_getbits (&bs2);
185 exit (1);
187 else {
188 printf ("File %s is not a valid MPEG stream.\n", argv[1]);
189 finish_getbits (&bs1);
190 exit (1);
195 finish_getbits (&bs1);
196 if (argc == 4)
197 finish_getbits (&bs2);
198 if (argc == 4 )
199 *multi_file = argv[3];
200 else
201 *multi_file = argv[2];
206 /*************************************************************************
207 Get_Info_Video
208 holt Informationen zu den einzelnen Access-Units (Frames) des
209 Videofiles ein und speichert sie in einer temporaeren Datei
210 ab. Wird spaeter zum Aufbau der gemultiplexten Datei gebraucht.
212 Gets informations on the single access units (frames) of the
213 video stream and saves them in a tmp file for further
214 processing. We need it for building the multiplex file.
215 *************************************************************************/
217 void get_info_video (char *video_file,
218 Video_struc *video_info,
219 double *ret_first_frame_PTS,
220 unsigned int length,
221 Vector *vid_info_vec)
223 Bit_stream_struc video_bs;
224 bitcount_t offset_bits=0LL;
225 bitcount_t stream_length=0LL;
226 bitcount_t prev_stream_length=0LL;
227 Vaunit_struc access_unit;
228 unsigned int syncword;
229 unsigned int decoding_order=0;
230 unsigned int group_order=0;
231 unsigned int group_start_pic=0;
232 unsigned long temporal_reference=0;
233 unsigned short pict_rate;
234 int i;
235 unsigned int prozent = 0;
236 unsigned int old_prozent=0;
237 double frame_rate;
238 unsigned int max_bits_persec = 0;
239 Vector vaunits = NewVector( sizeof(Vaunit_struc));
240 int first_pic_header;
244 printf ("\nScanning Video stream for access units information.\n");
245 init_getbits (&video_bs, video_file);
247 if (getbits (&video_bs, 32)==SEQUENCE_HEADER)
249 video_info->num_sequence++;
250 video_info->horizontal_size = getbits (&video_bs, 12);
251 video_info->vertical_size = getbits (&video_bs, 12);
252 video_info->aspect_ratio = getbits (&video_bs, 4);
253 pict_rate = getbits (&video_bs, 4);
254 video_info->picture_rate = pict_rate;
255 video_info->bit_rate = getbits (&video_bs, 18);
256 marker_bit (&video_bs, 1);
257 video_info->vbv_buffer_size = getbits (&video_bs, 10);
258 video_info->CSPF = get1bit (&video_bs);
260 } else
262 printf ("Invalid MPEG Video stream header.\n");
263 exit (1);
266 empty_vaunit_struc (&access_unit);
268 if (pict_rate >0 && pict_rate<9)
270 frame_rate = picture_rates[pict_rate];
272 else
274 frame_rate = 25.0;
277 /* Skip to the end of the 1st AU (*2nd* Picture start!)
279 *ret_first_frame_PTS = 0.0;
280 first_pic_header = 1;
281 while(!end_bs(&video_bs) && seek_sync (&video_bs, SYNCWORD_START, 24) &&
282 ( !opt_max_PTS || access_unit.PTS < opt_max_PTS ) )
284 syncword = (SYNCWORD_START<<8) + getbits (&video_bs, 8);
285 switch (syncword)
287 case SEQUENCE_HEADER:
288 video_info->num_sequence++;
289 break;
291 case GROUP_START:
292 video_info->num_groups++;
293 group_order=0;
294 break;
296 case PICTURE_START:
297 /* We only know length once we reach the *next* AU */
298 stream_length = bitcount (&video_bs)-32LL;
299 if( first_pic_header )
301 first_pic_header = 0;
303 else
305 access_unit.length = (int) (stream_length - offset_bits)>>3;
306 offset_bits = stream_length;
307 video_info->avg_frames[access_unit.type-1]+=access_unit.length;
308 VectorAppend( vaunits, &access_unit );
311 temporal_reference = getbits (&video_bs, 10);
312 access_unit.type = getbits (&video_bs, 3);
314 if( access_unit.type == IFRAME )
316 unsigned int bits_persec =
317 (unsigned int) ( ((double)(stream_length - prev_stream_length)) *
318 frame_rate / ((double)(1+decoding_order - group_start_pic)));
320 if( bits_persec > max_bits_persec )
322 max_bits_persec = bits_persec;
324 prev_stream_length = stream_length;
325 group_start_pic = decoding_order;
328 access_unit.DTS = (clockticks) (decoding_order * (double)CLOCKS / frame_rate);
329 access_unit.dorder = decoding_order;
330 access_unit.PTS = (clockticks) ((temporal_reference + group_start_pic) * (double)CLOCKS
331 / frame_rate);
332 access_unit.porder = temporal_reference + group_start_pic;
333 decoding_order++;
334 group_order++;
336 if ((access_unit.type>0) && (access_unit.type<5))
338 video_info->num_frames[access_unit.type-1]++;
341 prozent =(int) (((float)bitcount(&video_bs)/8/(float)length)*100);
342 if ( prozent > old_prozent && verbose > 0 )
344 printf ("Got %d picture headers. %2d%%%c",
345 decoding_order, prozent, verbose > 1 ? '\n' : '\r');
346 fflush (stdout);
347 old_prozent = prozent;
350 break;
352 case SEQUENCE_END:
353 stream_length = bitcount (&video_bs);
354 access_unit.length = (stream_length - offset_bits)>>3;
355 VectorAppend( vaunits, &access_unit );
356 video_info->avg_frames[access_unit.type-1]+=access_unit.length;
357 offset_bits = stream_length;
358 video_info->num_seq_end++;
359 break;
364 video_info->num_pictures = decoding_order;
366 video_info->stream_length = (unsigned int)(offset_bits / 8);
367 for (i=0; i<4; i++)
368 if (video_info->num_frames[i]!=0)
369 video_info->avg_frames[i] /= video_info->num_frames[i];
371 video_info->comp_bit_rate = (unsigned int)
373 (((double)video_info->stream_length) / ((double) video_info->num_pictures))
374 * ((double)frame_rate) + 25.0
375 ) / 50;
377 /* Peak bit rate in 50B/sec units... */
378 video_info->peak_bit_rate = ((max_bits_persec / 8) / 50);
379 finish_getbits (&video_bs);
380 output_info_video (video_info);
382 *vid_info_vec = vaunits;
386 /*************************************************************************
387 Output_Info_Video
388 gibt Ubersicht ueber gesammelte Informationen aus
390 Prints information on video access units
391 *************************************************************************/
393 void output_info_video (video_info)
395 Video_struc *video_info;
397 printf("\n+------------------ VIDEO STREAM INFORMATION -----------------+\n");
399 printf ("\nStream length : %11llu\n",video_info->stream_length);
400 printf ("Sequence start : %8u\n",video_info->num_sequence);
401 printf ("Sequence end : %8u\n",video_info->num_seq_end);
402 printf ("No. Pictures : %8u\n",video_info->num_pictures);
403 printf ("No. Groups : %8u\n",video_info->num_groups);
404 printf ("No. I Frames : %8u avg. size%6u bytes\n",
405 video_info->num_frames[0],video_info->avg_frames[0]);
406 printf ("No. P Frames : %8u avg. size%6u bytes\n",
407 video_info->num_frames[1],video_info->avg_frames[1]);
408 printf ("No. B Frames : %8u avg. size%6u bytes\n",
409 video_info->num_frames[2],video_info->avg_frames[2]);
410 printf ("No. D Frames : %8u avg. size%6u bytes\n",
411 video_info->num_frames[3],video_info->avg_frames[3]);
413 printf ("Horizontal size: %8u\n",video_info->horizontal_size);
414 printf ("Vertical size : %8u\n",video_info->vertical_size);
415 printf ("Aspect ratio : %1.4f ",ratio[video_info->aspect_ratio]);
417 switch (video_info->aspect_ratio)
419 case 0: printf ("forbidden\n"); break;
420 case 1: printf ("VGA etc\n"); break;
421 case 3: printf ("16:9, 625 line\n"); break;
422 case 6: printf ("16:9, 525 line\n"); break;
423 case 8: printf ("CCIR601, 625 line\n"); break;
424 case 12: printf ("CCIR601, 525 line\n"); break;
425 case 15: printf ("reserved\n"); break;
426 default: printf ("\n");
429 if (video_info->picture_rate == 0)
430 printf("Picture rate : forbidden\n");
431 else if (video_info->picture_rate <9)
432 printf("Picture rate : %2.3f frames/sec\n",
433 picture_rates[video_info->picture_rate]);
434 else
435 printf("Picture rate : %x reserved\n",video_info->picture_rate);
437 if (video_info->bit_rate == 0x3ffff) {
438 video_info->bit_rate = 0;
439 printf("Bit rate : variable\n"); }
440 else if (video_info->bit_rate == 0)
441 printf("Bit rate : forbidden\n");
442 else
443 printf("Bit rate : %7u bits/sec\n",
444 video_info->bit_rate*400);
446 printf ("Computed rate : %8u bits/sec\n",video_info->comp_bit_rate*400);
447 printf ("Peak rate : %8u bits/sec\n",video_info->peak_bit_rate*400);
448 printf ("Vbv buffer size: %8u bytes\n",video_info->vbv_buffer_size*2048);
449 printf ("CSPF : %8u\n",video_info->CSPF);
452 /*************************************************************************
453 Output_Info_Audio
454 gibt gesammelte Informationen zu den Audio Access Units aus.
456 Prints information on audio access units
457 *************************************************************************/
459 void output_info_audio (audio_info)
461 Audio_struc *audio_info;
463 unsigned int layer;
464 unsigned int bitrate;
466 layer=3-audio_info->layer;
467 bitrate = bitrate_index[audio_info->version_id][layer][audio_info->bit_rate];
470 printf("\n+------------------ AUDIO STREAM INFORMATION -----------------+\n");
471 printf ("Audio version : %s\n", audio_version[audio_info->version_id]);
472 printf (" Stream length : %11llu\n",audio_info->stream_length);
473 printf ("Syncwords : %8u\n",audio_info->num_syncword);
474 printf ("Frames : %8u size %6u bytes\n",
475 audio_info->num_frames[0],audio_info->size_frames[0]);
476 printf ("Frames : %8u size %6u bytes\n",
477 audio_info->num_frames[1],audio_info->size_frames[1]);
478 printf ("Layer : %8u\n",1+layer);
480 if (audio_info->protection == 0) printf ("CRC checksums : yes\n");
481 else printf ("CRC checksums : no\n");
483 if (audio_info->bit_rate == 0)
484 printf ("Bit rate : free\n");
485 else if (audio_info->bit_rate == 0xf)
486 printf ("Bit rate : reserved\n");
487 else
488 printf ("Bit rate : %8u bytes/sec (%3u kbit/sec)\n",
489 bitrate*128, bitrate);
491 if (audio_info->frequency == 3)
492 printf ("Frequency : reserved\n");
493 else
494 printf ("Frequency : %d Hz\n",
495 frequency[audio_info->version_id][audio_info->frequency]);
497 printf ("Mode : %8u %s\n",
498 audio_info->mode,mode[audio_info->mode]);
499 printf ("Mode extension : %8u\n",audio_info->mode_extension);
500 printf ("Copyright bit : %8u %s\n",
501 audio_info->copyright,copyright[audio_info->copyright]);
502 printf ("Original/Copy : %8u %s\n",
503 audio_info->original_copy,original[audio_info->original_copy]);
504 printf ("Emphasis : %8u %s\n",
505 audio_info->emphasis,emphasis[audio_info->emphasis]);
508 /*************************************************************************
509 Get_Info_Audio
510 holt Informationen zu den einzelnen Audio Access Units
511 (Audio frames) and records it.
512 *************************************************************************/
515 void get_info_audio (
516 char *audio_file,
517 Audio_struc *audio_info,
518 double first_frame_PTS,
519 unsigned int length,
520 Vector *audio_info_vec
524 Bit_stream_struc audio_bs;
525 bitcount_t offset_bits=0;
526 bitcount_t prev_offset;
527 unsigned int framesize;
528 unsigned int padding_bit;
529 unsigned int skip;
530 unsigned int decoding_order=0;
531 unsigned int samples_per_second;
532 Aaunit_struc access_unit;
533 unsigned long syncword;
534 int i;
535 unsigned int prozent;
536 unsigned int old_prozent=0;
537 Vector aaunits = NewVector(sizeof(Aaunit_struc));
539 printf ("\nScanning Audio stream for access units information. \n");
540 init_getbits (&audio_bs, audio_file);
541 empty_aaunit_struc (&access_unit);
543 /* A.Stevens 2000 - update to be compatible up to MPEG2.5
545 if (getbits (&audio_bs, 11)==AUDIO_SYNCWORD)
547 audio_info->num_syncword++;
548 audio_info->version_id = getbits (&audio_bs, 2);
549 audio_info->layer = getbits (&audio_bs, 2);
550 audio_info->protection = get1bit (&audio_bs);
551 audio_info->bit_rate = getbits (&audio_bs, 4);
552 audio_info->frequency = getbits (&audio_bs, 2);
553 padding_bit=get1bit(&audio_bs);
554 get1bit (&audio_bs);
555 audio_info->mode = getbits (&audio_bs, 2);
556 audio_info->mode_extension = getbits (&audio_bs, 2);
557 audio_info->copyright = get1bit (&audio_bs);
558 audio_info->original_copy = get1bit (&audio_bs);
559 audio_info->emphasis = getbits (&audio_bs, 2);
561 /* TODO: I'll be the slots counts have changed in the newer versions too... */
562 framesize =
563 bitrate_index[audio_info->version_id][3-audio_info->layer][audio_info->bit_rate] *
564 slots [3-audio_info->layer] *1000 /
565 frequency[audio_info->version_id][audio_info->frequency];
567 audio_info->size_frames[0] = framesize;
568 audio_info->size_frames[1] = framesize+1;
569 audio_info->num_frames[padding_bit]++;
571 access_unit.length = audio_info->size_frames[padding_bit];
573 samples_per_second = frequency[audio_info->version_id][audio_info->frequency];
575 /* Presentation time-stamping */
576 access_unit.PTS = (clockticks)
577 decoding_order * samples [3-audio_info->layer] * (clockticks)(CLOCKS) /
578 samples_per_second + first_frame_PTS;
579 access_unit.dorder = decoding_order;
580 ++decoding_order;
581 VectorAppend( aaunits, &access_unit );
583 } else
585 fprintf ( stderr,"Invalid MPEG Audio stream header.\n");
586 exit (1);
590 do {
593 skip=access_unit.length-4;
594 if (skip & 0x1) getbits (&audio_bs, 8);
595 if (skip & 0x2) getbits (&audio_bs, 16);
596 skip=skip>>2;
598 for (i=0;i<skip;i++)
600 getbits (&audio_bs, 32);
602 prev_offset = offset_bits;
603 offset_bits = bitcount(&audio_bs);
605 /* Check we have reached the end of have another catenated
606 stream to process before finishing ... */
609 if ( (syncword = getbits (&audio_bs, 11))!=AUDIO_SYNCWORD )
611 int bits_to_end = length*8 - offset_bits;
612 if( bits_to_end > 1024*8 )
614 /* There appears to be another catenated stream... */
615 int next;
616 printf( "\nEnd of component bit-stream ... seeking next\n" );
617 /* Catenated stream must start on byte boundary */
618 syncword = (syncword<<(8-offset_bits % 8));
619 next = getbits( &audio_bs,8-(offset_bits % 8) );
620 syncword = syncword | next;
622 printf( "syncword offset = %d syncword = %03x, next = %08x\n",
623 offset_bits % 8, syncword, next ); */
624 if( syncword != AUDIO_SYNCWORD )
626 printf( "Warning: Failed to find start of next stream at %lld prev %lld !\n", offset_bits/8, prev_offset/8 );
627 break;
630 else
631 /* No catenated stream... finished! */
632 break;
635 getbits( &audio_bs, 11); /* Skip version, layer, protection, bitrate,sampling */
636 prozent =(int) (((float) bitcount(&audio_bs)/8/(float)length)*100);
637 audio_info->num_syncword++;
639 if ((prozent > old_prozent && verbose > 0))
642 printf ("Got %d frame headers. %2d%%%c",
643 audio_info->num_syncword,prozent, verbose > 1? '\n' : '\r');
644 fflush (stdout);
645 old_prozent=prozent;
649 padding_bit=get1bit(&audio_bs);
650 access_unit.length = audio_info->size_frames[padding_bit];
652 access_unit.PTS = (clockticks)(decoding_order) * (clockticks)(samples [3-audio_info->layer])*
653 (clockticks)(CLOCKS) / samples_per_second +first_frame_PTS;
655 decoding_order++;
656 VectorAppend( aaunits, &access_unit );
657 audio_info->num_frames[padding_bit]++;
659 getbits (&audio_bs, 9);
661 } while (!end_bs(&audio_bs) &&
662 (!opt_max_PTS || access_unit.PTS < opt_max_PTS));
664 printf ("\nDone, stream bit offset %lld.\n",offset_bits);
666 audio_info->stream_length = offset_bits >> 3;
667 finish_getbits (&audio_bs);
668 output_info_audio (audio_info);
670 *audio_info_vec = aaunits;