6 static double picture_rates
[9] = { 0., 24000./1001., 24., 25.,
7 30000./1001., 30., 50., 60000./1001., 60. };
9 char *audio_version
[4] =
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}
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}
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}
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,
46 static int frequency
[4][4] =
53 {22050,24000, 16000,0},
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
)
79 if (what
!= get1bit(bs
))
81 printf ("\nError in MPEG stream at offset (bits) %lld: supposed marker bit not found.\n",bitcount(bs
));
88 /*************************************************************************
89 MPEG Verifikation der Inputfiles
91 Check if files are valid MPEG streams
92 *************************************************************************/
94 void check_files (int argc
,
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... */
109 if (open_file(argv
[1],&bytes_1
))
111 else if (argc
== 4) {
112 if (open_file(argv
[1],&bytes_1
) || open_file(argv
[2],&bytes_2
))
115 init_getbits (&bs1
, argv
[1]);
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
;
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
);
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
;
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
;
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
);
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
;
181 printf ("Files %s and %s are not valid MPEG streams.\n",
183 finish_getbits (&bs1
);
184 finish_getbits (&bs2
);
188 printf ("File %s is not a valid MPEG stream.\n", argv
[1]);
189 finish_getbits (&bs1
);
195 finish_getbits (&bs1
);
197 finish_getbits (&bs2
);
199 *multi_file
= argv
[3];
201 *multi_file
= argv
[2];
206 /*************************************************************************
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
,
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
;
235 unsigned int prozent
= 0;
236 unsigned int old_prozent
=0;
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
);
262 printf ("Invalid MPEG Video stream header.\n");
266 empty_vaunit_struc (&access_unit
);
268 if (pict_rate
>0 && pict_rate
<9)
270 frame_rate
= picture_rates
[pict_rate
];
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);
287 case SEQUENCE_HEADER
:
288 video_info
->num_sequence
++;
292 video_info
->num_groups
++;
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;
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
332 access_unit
.porder
= temporal_reference
+ group_start_pic
;
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');
347 old_prozent
= prozent
;
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
++;
364 video_info
->num_pictures
= decoding_order
;
366 video_info
->stream_length
= (unsigned int)(offset_bits
/ 8);
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
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 /*************************************************************************
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
]);
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");
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 /*************************************************************************
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
;
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");
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");
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 /*************************************************************************
510 holt Informationen zu den einzelnen Audio Access Units
511 (Audio frames) and records it.
512 *************************************************************************/
515 void get_info_audio (
517 Audio_struc
*audio_info
,
518 double first_frame_PTS
,
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
;
530 unsigned int decoding_order
=0;
531 unsigned int samples_per_second
;
532 Aaunit_struc access_unit
;
533 unsigned long syncword
;
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
);
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... */
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
;
581 VectorAppend( aaunits
, &access_unit
);
585 fprintf ( stderr
,"Invalid MPEG Audio stream header.\n");
593 skip
=access_unit
.length
-4;
594 if (skip
& 0x1) getbits (&audio_bs
, 8);
595 if (skip
& 0x2) getbits (&audio_bs
, 16);
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... */
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 );
631 /* No catenated stream... finished! */
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');
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
;
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
;