1 #include "main.h"
2 extern int Interactive;
3 /*************************************************************************
4     MPEG Streams Kontrolle
5 
6     Basic Checks on MPEG Streams
7 *************************************************************************/
8 
marker_bit(bs,what)9 void marker_bit (bs, what)
10 Bit_stream_struc *bs;
11 unsigned int what;
12 {
13     if (what != get1bit(bs))
14     {
15         printf ("\nError in MPEG stream at offset (bits) %ul: supposed marker bit not found.\n",sstell(bs));
16         exit (1);
17     }
18 }
19 
20 
21 
22 /*************************************************************************
23     MPEG Verifikation der Inputfiles
24 
25     Check if files are valid MPEG streams
26 *************************************************************************/
27 
check_files(argc,argv,audio_file,video_file,multi_file,audio_bytes,video_bytes,ptr_which_streams)28 void check_files (argc, argv, audio_file, video_file, multi_file,
29 		  audio_bytes, video_bytes, ptr_which_streams)
30 int argc;
31 char* argv[];
32 char** audio_file;
33 char** video_file;
34 char** multi_file;
35 unsigned int *audio_bytes;
36 unsigned int *video_bytes;
37 unsigned int *ptr_which_streams;
38 
39 {
40     Bit_stream_struc bs1, bs2;
41     unsigned int bytes_1, bytes_2;
42 
43     if (argc == 3) {
44 	if (open_file(argv[1],&bytes_1))
45 	    exit (1); }
46     else if (argc == 4) {
47 	if (open_file(argv[1],&bytes_1) || open_file(argv[2],&bytes_2))
48 	    exit (1); }
49 
50     open_bit_stream_r (&bs1, argv[1], BUFFER_SIZE);
51 
52     if (argc == 4)
53 	open_bit_stream_r (&bs2, argv[2], BUFFER_SIZE);
54 
55     /* Das Bitstreampaket kuemmert sich bei einem look_ahead nicht
56        darum, den Buffer vorzubereiten, weil es davon ausgeht, dass
57        vorher mindestens einmal ein getbits () gemacht wurde. Da das
58        hier nicht zutrifft, muss manuell das erledigt werden, was
59        sonst getbits () macht, d.h. ein Buffer eingelesen werden und
60        bestimmte Werte in der Struktur gesetzt werden. */
61 
62     /* The Bitstream package doesn't prepare the buffer when doing
63        a look_ahead, since it thinks that at least ont getbits ()
64        function call was done earlier. This gets done by hand here */
65 
66     bs1.totbit       = 32;
67     bs1.buf_byte_idx = -1;
68     bs1.buf_bit_idx  = 8;
69     refill_buffer (&bs1);
70     bs1.buf_byte_idx = bs1.buf_size-1;
71 
72     if (argc == 4) {
73 	bs2.totbit       = 32;
74 	bs2.buf_byte_idx = -1;
75 	bs2.buf_bit_idx  = 8;
76 	refill_buffer (&bs2);
77 	bs2.buf_byte_idx = bs2.buf_size-1;
78     }
79 
80     if (look_ahead (&bs1, 12) == 0xfff)
81     {
82 	*audio_file = argv[1];
83 	*audio_bytes= bytes_1;
84 	printf ("File %s is a 11172-3 Audio stream.\n",argv[1]);
85 	*ptr_which_streams |= STREAMS_AUDIO;
86 	if (argc == 4 ) {
87 	    if (look_ahead (&bs2, 32) != 0x1b3)
88 		{
89 		    printf ("File %s is not a 11172-2 Video stream.\n",argv[2]);
90 		    close_bit_stream_r (&bs1);
91 		    close_bit_stream_r (&bs2);
92 		    exit (1);
93 		}
94 	    else
95 		{
96 		    printf ("File %s is a 11172-2 Video stream.\n",argv[2]);
97 		    *ptr_which_streams |= STREAMS_VIDEO;
98 		    *video_file = argv[2];
99 		    *video_bytes= bytes_2;
100 		}
101 	}
102 
103     }
104     else if (look_ahead (&bs1, 32) == 0x1b3)
105     {
106 	*video_file = argv[1];
107 	*video_bytes= bytes_1;
108 	printf ("File %s is a 11172-2 Video stream.\n",argv[1]);
109 	*ptr_which_streams |= STREAMS_VIDEO;
110 	if (argc == 4 ) {
111 	    if (look_ahead (&bs2, 12) != 0xfff)
112 		{
113 		    printf ("File %s is not a 11172-3 Audio stream.\n",argv[2]);
114 		    close_bit_stream_r (&bs1);
115 		    close_bit_stream_r (&bs2);
116 		    exit (1);
117 		}
118 	    else
119 		{
120 		    printf ("File %s is a 11172-3 Audio stream.\n",argv[2]);
121 		    *ptr_which_streams |= STREAMS_AUDIO;
122 		    *audio_file = argv[2];
123 		    *audio_bytes= bytes_2;
124 		}
125 	}
126     }
127 
128     else
129     {
130 	if (argc == 4) {
131 	    printf ("Files %s and %s are not valid MPEG streams.\n",
132 		    argv[1],argv[2]);
133 	    close_bit_stream_r (&bs1);
134 	    close_bit_stream_r (&bs2);
135 	    exit (1);
136 	}
137 	else {
138 	    printf ("File %s is not a valid MPEG stream.\n", argv[1]);
139 	    close_bit_stream_r (&bs1);
140 	    exit (1);
141 	}
142     }
143 
144     close_bit_stream_r (&bs1);
145     if (argc == 4) close_bit_stream_r (&bs2);
146 
147     if (argc == 4 )
148 	*multi_file = argv[3];
149     else
150 	*multi_file = argv[2];
151 
152 
153  }
154 
155 /*************************************************************************
156 	Get_Info_Video
157 	holt Informationen zu den einzelnen Access-Units (Frames) des
158 	Videofiles ein und speichert sie in einer temporaeren Datei
159 	ab. Wird spaeter zum Aufbau der gemultiplexten Datei gebraucht.
160 
161 	Gets informations on the single access units (frames) of the
162 	video stream and saves them in a tmp file for further
163 	processing. We need it for building the multiplex file.
164 *************************************************************************/
165 
get_info_video(video_file,video_units,video_info,startup_delay,length)166 void get_info_video (video_file, video_units, video_info, startup_delay, length)
167 
168 char *video_file;
169 char *video_units;
170 Video_struc *video_info;
171 double *startup_delay;
172 unsigned int length;
173 
174 {
175     FILE* info_file;
176     Bit_stream_struc video_bs;
177     unsigned int offset_bits=0;
178     unsigned int stream_length=0;
179     Vaunit_struc access_unit;
180     unsigned long syncword;
181     unsigned long decoding_order=0;
182     unsigned long group_order=0;
183     unsigned long temporal_reference=0;
184     double secs_per_frame=0;
185     unsigned short pict_rate;
186     double DTS;
187     double PTS;
188     int i;
189     unsigned int prozent;
190     unsigned int old_prozent=0;
191 
192     printf ("\nScanning Video stream for access units information.\n");
193     info_file = fopen (video_units, "wb");
194     open_bit_stream_r (&video_bs, video_file, BUFFER_SIZE);
195 
196 
197     if (getbits (&video_bs, 32)==SEQUENCE_HEADER)
198     {
199 	video_info->num_sequence++;
200 	video_info->horizontal_size	= getbits (&video_bs, 12);
201 	video_info->vertical_size	= getbits (&video_bs, 12);
202 	video_info->aspect_ratio	= getbits (&video_bs,  4);
203 	pict_rate 			= getbits (&video_bs,  4);
204 	video_info->picture_rate	= pict_rate;
205 	video_info->bit_rate		= getbits (&video_bs, 18);
206 	marker_bit (&video_bs, 1);
207 	video_info->vbv_buffer_size	= getbits (&video_bs, 10);
208 	video_info->CSPF		= get1bit (&video_bs);
209 
210     } else
211     {
212 	printf ("Invalid MPEG Video stream header.\n");
213 	exit (1);
214     }
215 
216     empty_vaunit_struc (&access_unit);
217     *startup_delay = 2*MAX_FFFFFFFF;
218 
219     if (pict_rate >0 && pict_rate<9)
220 	secs_per_frame = 1. / picture_rates[pict_rate];
221     else
222 	secs_per_frame = 1. / 25.;	/* invalid pict_rate info */
223 
224     do {
225 	if (seek_sync (&video_bs, SYNCWORD_START, 24))
226 	{
227 	    syncword = (SYNCWORD_START<<8) + getbits (&video_bs, 8);
228 	    switch (syncword) {
229 
230 		case SEQUENCE_HEADER:
231 		    video_info->num_sequence++;
232 		    break;
233 
234 		case GROUP_START:
235 		    video_info->num_groups++;
236 		    group_order=0;
237 		    break;
238 
239 		case PICTURE_START:
240 		    /* skip access unit number 0 */
241 		    if (access_unit.type != 0)
242 		    {
243 			stream_length = sstell (&video_bs)-32;
244 			access_unit.length = (stream_length - offset_bits)>>3;
245 			offset_bits = stream_length;
246 		        fwrite (&access_unit, sizeof (Vaunit_struc),
247 			    1, info_file);
248 			video_info->avg_frames[access_unit.type-1]+=
249 			    access_unit.length;
250 		    }
251 
252 		    temporal_reference = getbits (&video_bs, 10);
253 		    access_unit.type   = getbits (&video_bs, 3);
254 
255 		    DTS = decoding_order * secs_per_frame*CLOCKS;
256 		    PTS = (temporal_reference - group_order + 1 +
257 				  decoding_order) * secs_per_frame*CLOCKS;
258 
259 		    *startup_delay=(PTS<*startup_delay ? PTS : *startup_delay);
260 
261 		    make_timecode (DTS,&access_unit.DTS);
262 		    make_timecode (PTS,&access_unit.PTS);
263 		    decoding_order++;
264 		    group_order++;
265 
266 		    if ((access_unit.type>0) && (access_unit.type<5))
267 		        video_info->num_frames[access_unit.type-1]++;
268 
269 		    prozent =(int) (((float)sstell(&video_bs)/8/(float)length)*100);
270 		    video_info->num_pictures++;
271 
272 		    if (prozent > old_prozent)
273 		    {
274 			printf ("Got %d picture headers. %2d%%\r",
275 			    video_info->num_pictures, prozent);
276 			fflush (stdout);
277 			old_prozent = prozent;
278 		    }
279 
280 		    break;
281 
282 		case SEQUENCE_END:
283 		    stream_length = sstell (&video_bs);
284 		    access_unit.length = (stream_length - offset_bits)>>3;
285 	            fwrite (&access_unit, sizeof (Vaunit_struc),
286 			1, info_file);
287 		    video_info->avg_frames[access_unit.type-1]+=
288 			access_unit.length;
289 		    offset_bits = stream_length;
290 		    video_info->num_seq_end++;
291 		    break;
292 
293 	    }
294 	} else break;
295     } while (!end_bs(&video_bs));
296 
297     printf ("\nDone, stream bit offset %ld.\n",offset_bits);
298 
299     video_info->stream_length = offset_bits >> 3;
300     for (i=0; i<4; i++)
301 	if (video_info->num_frames[i]!=0)
302 	   video_info->avg_frames[i] /= video_info->num_frames[i];
303 
304     if (secs_per_frame >0.)
305         video_info->comp_bit_rate = ceil ((double)(video_info->stream_length)/
306 	(double)(video_info->num_pictures)/secs_per_frame/1250.)*25;
307     else
308 	video_info->comp_bit_rate = 0;
309 
310     close_bit_stream_r (&video_bs);
311     fclose (info_file);
312     output_info_video (video_info);
313 
314   if( Interactive )
315     ask_continue ();
316 }
317 
318 /*************************************************************************
319 	Output_Info_Video
320 	gibt Ubersicht ueber gesammelte Informationen aus
321 
322 	Prints information on video access units
323 *************************************************************************/
324 
output_info_video(video_info)325 void output_info_video (video_info)
326 
327 Video_struc *video_info;
328 {
329 printf("\n+------------------ VIDEO STREAM INFORMATION -----------------+\n");
330 
331     printf ("\nStream length  : %8u\n",video_info->stream_length);
332     printf   ("Sequence start : %8u\n",video_info->num_sequence);
333     printf   ("Sequence end   : %8u\n",video_info->num_seq_end);
334     printf   ("No. Pictures   : %8u\n",video_info->num_pictures);
335     printf   ("No. Groups     : %8u\n",video_info->num_groups);
336     printf   ("No. I Frames   : %8u avg. size%6u bytes\n",
337 	video_info->num_frames[0],video_info->avg_frames[0]);
338     printf   ("No. P Frames   : %8u avg. size%6u bytes\n",
339 	video_info->num_frames[1],video_info->avg_frames[1]);
340     printf   ("No. B Frames   : %8u avg. size%6u bytes\n",
341 	video_info->num_frames[2],video_info->avg_frames[2]);
342     printf   ("No. D Frames   : %8u avg. size%6u bytes\n",
343 	video_info->num_frames[3],video_info->avg_frames[3]);
344 
345     printf   ("Horizontal size: %8u\n",video_info->horizontal_size);
346     printf   ("Vertical size  : %8u\n",video_info->vertical_size);
347     printf   ("Aspect ratio   :   %1.4f ",ratio[video_info->aspect_ratio]);
348 
349     switch (video_info->aspect_ratio)
350     {
351 	case  0: printf ("forbidden\n"); break;
352 	case  1: printf ("VGA etc\n"); break;
353 	case  3: printf ("16:9, 625 line\n"); break;
354 	case  6: printf ("16:9, 525 line\n"); break;
355 	case  8: printf ("CCIR601, 625 line\n"); break;
356 	case 12: printf ("CCIR601, 525 line\n"); break;
357 	case 15: printf ("reserved\n"); break;
358 	default: printf ("\n");
359     }
360 
361     if (video_info->picture_rate == 0)
362        printf("Picture rate   : forbidden\n");
363     else if (video_info->picture_rate <9)
364        printf("Picture rate   :   %2.3f frames/sec\n",
365        picture_rates[video_info->picture_rate]);
366     else
367        printf("Picture rate   : %x reserved\n",video_info->picture_rate);
368 
369     if (video_info->bit_rate == 0x3ffff) {
370        video_info->bit_rate = 0;
371        printf("Bit rate       : variable\n"); }
372     else if (video_info->bit_rate == 0)
373 	printf("Bit rate      : forbidden\n");
374     else
375 	printf("Bit rate       : %8u bytes/sec (%7u bits/sec)\n",
376 	       video_info->bit_rate*50,video_info->bit_rate*400);
377 
378     printf   ("Computed rate  : %8u bytes/sec\n",video_info->comp_bit_rate*50);
379     printf   ("Vbv buffer size: %8u bytes\n",video_info->vbv_buffer_size*2048);
380     printf   ("CSPF           : %8u\n",video_info->CSPF);
381 }
382 
383 /*************************************************************************
384 	Output_Info_Audio
385 	gibt gesammelte Informationen zu den Audio Access Units aus.
386 
387 	Prints information on audio access units
388 *************************************************************************/
389 
output_info_audio(audio_info)390 void output_info_audio (audio_info)
391 
392 Audio_struc *audio_info;
393 {
394     unsigned int layer;
395     unsigned long bitrate;
396 
397     layer=3-audio_info->layer;
398     bitrate = bitrate_index[layer][audio_info->bit_rate];
399 
400 
401 printf("\n+------------------ AUDIO STREAM INFORMATION -----------------+\n");
402 
403     printf ("\nStream length  : %8u\n",audio_info->stream_length);
404     printf   ("Syncwords      : %8u\n",audio_info->num_syncword);
405     printf   ("Frames         : %8u size %6u bytes\n",
406 	audio_info->num_frames[0],audio_info->size_frames[0]);
407     printf   ("Frames         : %8u size %6u bytes\n",
408 	audio_info->num_frames[1],audio_info->size_frames[1]);
409     printf   ("Layer          : %8u\n",1+layer);
410 
411     if (audio_info->protection == 0) printf ("CRC checksums  :      yes\n");
412     else  printf ("CRC checksums  :       no\n");
413 
414     if (audio_info->bit_rate == 0)
415 	printf ("Bit rate       :     free\n");
416     else if (audio_info->bit_rate == 0xf)
417 	printf ("Bit rate       : reserved\n");
418     else
419 	printf ("Bit rate       : %8u bytes/sec (%3u kbit/sec)\n",
420 		 bitrate*128, bitrate);
421 
422     if (audio_info->frequency == 3)
423 	printf ("Frequency      : reserved\n");
424     else
425 	printf ("Frequency      :     %2.1f kHz\n",
426 		frequency[audio_info->frequency]);
427 
428     printf   ("Mode           : %8u %s\n",
429 	audio_info->mode,mode[audio_info->mode]);
430     printf   ("Mode extension : %8u\n",audio_info->mode_extension);
431     printf   ("Copyright bit  : %8u %s\n",
432 	audio_info->copyright,copyright[audio_info->copyright]);
433     printf   ("Original/Copy  : %8u %s\n",
434 	audio_info->original_copy,original[audio_info->original_copy]);
435     printf   ("Emphasis       : %8u %s\n",
436 	audio_info->emphasis,emphasis[audio_info->emphasis]);
437 }
438 
439 /*************************************************************************
440 	Get_Info_Audio
441 	holt Informationen zu den einzelnen Audio Access Units
442 	(Audio frames) ein und speichert sie in einer temporaeren
443 	Datei ab.
444 
445 	gets information on the single audio access units (audio frames)
446 	and saves them into a tmp file for further processing.
447 *************************************************************************/
448 
get_info_audio(audio_file,audio_units,audio_info,startup_delay,length)449 void get_info_audio (audio_file, audio_units, audio_info, startup_delay, length)
450 
451 char *audio_file;
452 char *audio_units;
453 Audio_struc *audio_info;
454 double *startup_delay;
455 unsigned int length;
456 
457 {
458     FILE* info_file;
459     Bit_stream_struc audio_bs;
460     unsigned int offset_bits=0;
461     unsigned int stream_length=0;
462     unsigned int framesize;
463     unsigned int padding_bit;
464     unsigned int skip;
465     unsigned int decoding_order=0;
466     double PTS;
467     double samples_per_second;
468     Aaunit_struc access_unit;
469     unsigned long syncword;
470     int i;
471     unsigned int prozent;
472     unsigned int old_prozent=0;
473 
474     printf ("\nScanning Audio stream for access units information.\n");
475     info_file = fopen (audio_units, "wb");
476     open_bit_stream_r (&audio_bs, audio_file, BUFFER_SIZE);
477 
478     empty_aaunit_struc (&access_unit);
479 
480     if (getbits (&audio_bs, 12)==AUDIO_SYNCWORD)
481     {
482 	marker_bit (&audio_bs, 1);
483 	audio_info->num_syncword++;
484 	audio_info->layer 		= getbits (&audio_bs, 2);
485 	audio_info->protection 		= get1bit (&audio_bs);
486 	audio_info->bit_rate 		= getbits (&audio_bs, 4);
487 	audio_info->frequency 		= getbits (&audio_bs, 2);
488 	padding_bit=get1bit(&audio_bs);
489 	get1bit (&audio_bs);
490 	audio_info->mode 		= getbits (&audio_bs, 2);
491 	audio_info->mode_extension 	= getbits (&audio_bs, 2);
492 	audio_info->copyright 		= get1bit (&audio_bs);
493 	audio_info->original_copy 	= get1bit (&audio_bs);
494 	audio_info->emphasis		= getbits (&audio_bs, 2);
495 
496 	framesize =
497 	    bitrate_index[3-audio_info->layer][audio_info->bit_rate] /
498 	    frequency[audio_info->frequency] * slots [3-audio_info->layer];
499 	audio_info->size_frames[0] = framesize;
500 	audio_info->size_frames[1] = framesize+1;
501 
502 	access_unit.length = audio_info->size_frames[padding_bit];
503 
504 	samples_per_second = (double)frequency [audio_info->frequency];
505 
506 	PTS = decoding_order * samples [3-audio_info->layer] /
507 	      samples_per_second * 90. + *startup_delay;
508 	make_timecode (PTS, &access_unit.PTS);
509 	decoding_order++;
510 
511 	fwrite (&access_unit, sizeof (Aaunit_struc),1, info_file);
512 	audio_info->num_frames[padding_bit]++;
513 
514     } else
515     {
516 	printf ("Invalid MPEG Audio stream header.\n");
517 	exit (1);
518     }
519 
520 
521     do {
522 	skip=access_unit.length-4;
523 	if (skip & 0x1) getbits (&audio_bs, 8);
524 	if (skip & 0x2) getbits (&audio_bs, 16);
525 	skip=skip>>2;
526 
527 	for (i=0;i<skip;i++)
528 	{
529 	    getbits (&audio_bs, 32);
530 	}
531 	offset_bits = sstell(&audio_bs);
532 	if (getbits (&audio_bs, 12)==AUDIO_SYNCWORD)
533 	{
534 	    marker_bit (&audio_bs, 1);
535 	    prozent =(int) (((float) sstell(&audio_bs)/8/(float)length)*100);
536 	    audio_info->num_syncword++;
537 	    if (prozent > old_prozent)
538 	    {
539 		printf ("Got %d frame headers. %2d%%\r",
540 			audio_info->num_syncword,prozent);
541 		fflush (stdout);
542 		old_prozent=prozent;
543 
544 	    }
545 	    getbits (&audio_bs, 9);
546 
547 	    padding_bit=get1bit(&audio_bs);
548 	    access_unit.length = audio_info->size_frames[padding_bit];
549 
550 	    PTS = decoding_order * samples [3-audio_info->layer] /
551 		samples_per_second * 90. + *startup_delay;
552 	    make_timecode (PTS, &access_unit.PTS);
553 
554 	    decoding_order++;
555 
556 	    fwrite (&access_unit, sizeof (Aaunit_struc),1, info_file);
557 	    audio_info->num_frames[padding_bit]++;
558 
559 	    getbits (&audio_bs, 9);
560 	}
561 	else break;
562     } while (!end_bs(&audio_bs));
563 
564     printf ("\nDone, stream bit offset %ld.\n",offset_bits);
565 
566     audio_info->stream_length = offset_bits >> 3;
567     close_bit_stream_r (&audio_bs);
568     fclose (info_file);
569     output_info_audio (audio_info);
570   if( Interactive )
571     ask_continue ();
572 
573 }
574 
575