r663: This commit was generated by cvs2svn to compensate for changes in r662,
[cinelerra_cv.git] / mplexhi / main.h
blob2062e59b901030e359a8579974c918dc54656861
1 /*************************************************************************
2 * MPEG SYSTEMS MULTIPLEXER *
3 * Erzeugen einer MPEG/SYSTEMS *
4 * MULTIPLEXED VIDEO/AUDIO DATEI *
5 * aus zwei MPEG Basis Streams *
6 * Christoph Moar *
7 * SIEMENS ZFE ST SN 11 / T SN 6 *
8 * (C) 1994 1995 *
9 **************************************************************************
10 * Generating a MPEG/SYSTEMS *
11 * MULTIPLEXED VIDEO/AUDIO STREAM *
12 * from two MPEG source streams *
13 * Christoph Moar *
14 * SIEMENS CORPORATE RESEARCH AND DEVELOPMENT ST SN 11 / T SN 6 *
15 * (C) 1994 1995 *
16 **************************************************************************
17 * Einschraenkungen vorhanden. Unterstuetzt nicht gesamten MPEG/SYSTEMS *
18 * Standard. Haelt sich i.d.R. an den CSPF-Werten, zusaetzlich (noch) *
19 * nur fuer ein Audio- und/oder ein Video- Stream. Evtl. erweiterbar. *
20 **************************************************************************
21 * Restrictions apply. Will not support the whole MPEG/SYSTEM Standard. *
22 * Basically, will generate Constrained System Parameter Files. *
23 * Mixes only one audio and/or one video stream. Might be expanded. *
24 *************************************************************************/
26 /*************************************************************************
27 * mplex - MPEG/SYSTEMS multiplexer *
28 * Copyright (C) 1994 1995 Christoph Moar *
29 * Siemens ZFE ST SN 11 / T SN 6 *
30 * *
31 * moar@informatik.tu-muenchen.de *
32 * (Christoph Moar) *
33 * *
34 * This program is free software; you can redistribute it and/or modify *
35 * it under the terms of the GNU General Public License as published by *
36 * the Free Software Foundation; either version 2 of the License, or *
37 * (at your option) any later version. *
38 * *
39 * This program is distributed in the hope that it will be useful, *
40 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
41 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
42 * GNU General Public License for more details. *
43 * *
44 * You should have received a copy of the GNU General Public License *
45 * along with this program; if not, write to the Free Software *
46 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
47 *************************************************************************/
49 #include <config.h>
50 #include <stdio.h>
51 #include <inttypes.h>
52 #include "bits.h"
53 #ifdef TIMER
54 #include <sys/time.h>
55 #endif
58 #include "vector.h"
60 typedef uint64_t clockticks;
62 /*************************************************************************
63 Definitionen
64 *************************************************************************/
66 #define MPLEX_VER " 1.4.1"
67 #define MPLEX_DATE "7.12.2000"
69 /* Buffer size parameters */
71 #define MAX_SECTOR_SIZE 16384
72 #define MAX_PACK_HEADER_SIZE 255
73 #define MAX_SYS_HEADER_SIZE 255
76 #define SEQUENCE_HEADER 0x000001b3
77 #define SEQUENCE_END 0x000001b7
78 #define PICTURE_START 0x00000100
79 #define GROUP_START 0x000001b8
80 #define SYNCWORD_START 0x000001
81 #define OLDFRAME 0
82 #define IFRAME 1
83 #define PFRAME 2
84 #define BFRAME 3
85 #define DFRAME 4
87 #define AUDIO_SYNCWORD 0x7ff
90 #define PACK_START 0x000001ba
91 #define SYS_HEADER_START 0x000001bb
92 #define ISO11172_END 0x000001b9
93 #define PACKET_START 0x000001
95 #define MAX_FFFFFFFF 4294967295.0 /* = 0xffffffff in dec. */
97 #define CLOCKS (300 *90000) /* MPEG-2 System Clock Hertz - we divide down by 300.0 for MPEG-1*/
99 /* Range of sizes of the fields following the packet length field in packet header:
100 used to calculate if recieve buffers will have enough space... */
102 #define BUFFERINFO_LENGTH 2
103 #define DTS_PTS_TIMESTAMP_LENGTH 5
104 #define MPEG2_AFTER_PACKET_LENGTH_MIN 3
105 #define MPEG1_AFTER_PACKET_LENGTH_MIN (0+1)
107 /* Sector under-size below which header stuffing rather than padding packets
108 or post-packet zero stuffing is used. *Must* be less than 20 for VCD
109 multiplexing to work correctly!
112 #define MINIMUM_PADDING_PACKET_SIZE 10
115 A "safety margin" for the audio buffer just in case the
116 player doesn't *quite* clear it as quickly as we guess.
117 We don't send a new audio packet until there ought to be at least this
118 much space.
120 #define AUDIO_BUFFER_FILL_MARGIN 300
122 /* The following values for sys_header_length & size are only valid for */
123 /* System streams consisting of two basic streams. When wrapping around */
124 /* the system layer on a single video or a single audio stream, those */
125 /* values get decreased by 3. */
128 #define PACKET_HEADER_SIZE 6
130 #define VIDEOCD_SECTOR_SIZE 2324 /* VideoCD sector size */
131 #define SVCD_SECTOR_SIZE 2324 /* Super VideoCD sector size */
132 #define DVD_SECTOR_SIZE 2048 /* DVD sector size */
135 #define STREAMS_VIDEO 1
136 #define STREAMS_AUDIO 2
137 #define STREAMS_BOTH 3
139 #define AUDIO_STREAMS 0xb8 /* Marker Audio Streams */
140 #define VIDEO_STREAMS 0xb9 /* Marker Video Streams */
141 #define AUDIO_STR_0 0xc0 /* Marker Audio Stream0 */
142 #define VIDEO_STR_0 0xe0 /* Marker Video Stream0 */
143 #define PADDING_STR 0xbe /* Marker Padding Stream*/
145 #define ZERO_STUFFING_BYTE 0
146 #define STUFFING_BYTE 0xff
147 #define RESERVED_BYTE 0xff
148 #define TIMESTAMPBITS_NO 0 /* Flag NO timestamps */
149 #define TIMESTAMPBITS_PTS 2 /* Flag PTS timestamp */
150 #define TIMESTAMPBITS_DTS 1 /* Flag PTS timestamp */
151 #define TIMESTAMPBITS_PTS_DTS (TIMESTAMPBITS_DTS|TIMESTAMPBITS_PTS) /* Flag BOTH timestamps */
153 #define MARKER_MPEG1_SCR 2 /* Marker SCR */
154 #define MARKER_MPEG2_SCR 1 /* These don't need to be distinct! */
155 #define MARKER_JUST_PTS 2 /* Marker only PTS */
156 #define MARKER_PTS 3 /* Marker PTS */
157 #define MARKER_DTS 1 /* Marker DTS */
158 #define MARKER_NO_TIMESTAMPS 0x0f /* Marker NO timestamps */
160 #define STATUS_AUDIO_END 0 /* Statusmessage A end */
161 #define STATUS_VIDEO_END 1 /* Statusmessage V end */
162 #define STATUS_AUDIO_TIME_OUT 2 /* Statusmessage A out */
163 #define STATUS_VIDEO_TIME_OUT 3 /* Statusmessage V out */
165 /*************************************************************************
166 Typ- und Strukturdefinitionen
167 *************************************************************************/
171 typedef struct vaunit_struc /* Informationen ueber Video AU's */
172 { unsigned int length ;
173 unsigned int type ;
174 clockticks DTS ;
175 clockticks PTS ;
176 int dorder;
177 int porder;
178 } Vaunit_struc;
180 typedef struct aaunit_struc /* Informationen ueber Audio AU's */
181 { unsigned long length ;
182 clockticks PTS ;
183 int dorder;
184 } Aaunit_struc;
186 typedef struct video_struc /* Informationen ueber Video Stream */
187 { bitcount_t stream_length ;
188 unsigned int num_sequence ;
189 unsigned int num_seq_end ;
190 unsigned int num_pictures ;
191 unsigned int num_groups ;
192 unsigned int num_frames[4] ;
193 unsigned int avg_frames[4] ;
195 unsigned int horizontal_size;
196 unsigned int vertical_size ;
197 unsigned int aspect_ratio ;
198 unsigned int picture_rate ;
199 unsigned int bit_rate ;
200 unsigned int comp_bit_rate ;
201 unsigned int peak_bit_rate ;
202 unsigned int vbv_buffer_size;
203 unsigned int CSPF ;
204 double secs_per_frame;
205 } Video_struc;
207 typedef struct audio_struc /* Informationen ueber Audio Stream */
208 { bitcount_t stream_length ;
209 unsigned int num_syncword ;
210 unsigned int num_frames [2] ;
211 unsigned int size_frames[2] ;
212 unsigned int version_id ;
213 unsigned int layer ;
214 unsigned int protection ;
215 unsigned int bit_rate ;
216 unsigned int frequency ;
217 unsigned int mode ;
218 unsigned int mode_extension ;
219 unsigned int copyright ;
220 unsigned int original_copy ;
221 unsigned int emphasis ;
222 } Audio_struc;
224 typedef struct sector_struc /* Ein Sektor, kann Pack, Sys Header */
225 /* und Packet enthalten. */
226 { unsigned char buf [MAX_SECTOR_SIZE] ;
227 unsigned int length_of_packet_data ;
228 clockticks TS ;
229 } Sector_struc;
231 typedef struct pack_struc /* Pack Info */
232 { unsigned char buf [MAX_PACK_HEADER_SIZE];
233 int length;
234 clockticks SCR;
235 } Pack_struc;
237 typedef struct sys_header_struc /* System Header Info */
238 { unsigned char buf [MAX_SYS_HEADER_SIZE];
239 int length;
240 } Sys_header_struc;
242 typedef struct buffer_queue /* FIFO-Queue fuer STD Buffer */
243 { unsigned int size ; /* als verkettete Liste implementiert */
244 clockticks DTS ;
245 struct buffer_queue *next ;
246 } Buffer_queue;
249 typedef struct buffer_struc /* Simuliert STD Decoder Buffer */
250 { unsigned int max_size; /* enthaelt Anker auf verkettete Liste */
251 Buffer_queue *first;
252 } Buffer_struc;
255 /*************************************************************************
256 Funktionsprototypen, keine Argumente, K&R Style
257 *************************************************************************/
259 int intro_and_options( int, char **); /* Anzeigen des Introbildschirmes und */
260 /* Ueberpruefen der Argumente */
261 void init_stream_syntax_parameters(Video_struc *video_info,
262 Audio_struc *audio_info );
263 /* Initialisation of syntax paramters */
264 /* based on (checked) options */
266 void check_files (); /* Kontrolliert ob Files vorhanden und */
267 /* weist sie Audio/Video Pointern zu */
268 int open_file (); /* File vorhanden? */
269 void get_info_video (char *video_file,
270 Video_struc *video_info,
271 double *first_frame_PTS,
272 unsigned int length,
273 Vector *vid_info_vec);
274 void get_info_audio (char *audio_file,
275 Audio_struc *audio_info,
276 double first_frame_PTS,
277 unsigned int length,
278 Vector *audio_info_vec
281 void output_info_video (); /* Ausgabe Information Access Units */
282 void output_info_audio (); /* Ausgabe Information Access Units */
283 void marker_bit (); /* Checks for marker bit */
284 void empty_video_struc (); /* Initialisiert Struktur fuer SUN cc */
285 void empty_audio_struc (); /* Initialisiert Struktur fuer SUN cc */
286 void empty_vaunit_struc (); /* Initialisiert Struktur fuer SUN cc */
287 void empty_aaunit_struc (); /* Initialisiert Struktur fuer SUN cc */
288 void empty_sector_struc (); /* Initialisiert Struktur fuer SUN cc */
289 void empty_clockticks (); /* Initialisiert Struktur fuer SUN cc */
290 void init_buffer_struc (); /* Initialisiert Struktur fuer SUN cc */
292 void offset_timecode (clockticks *time1,clockticks *time2,clockticks *offset); /* Rechnet Offset zwischen zwei TimeC. */
293 void copy_timecode (clockticks *,clockticks *); /* setzt 2tes TimeC. dem 1ten gleich */
294 void bytepos_timecode(long long bytepos, clockticks *ts);
295 void make_timecode (double, clockticks *); /* rechnet aus double einen TimeC. */
296 /* und schreibt ihn in clockticks */
297 void add_to_timecode (clockticks *,clockticks *); /* addiert 1tes TimeC. zum 2ten */
298 void buffer_dtspts_mpeg1scr_timecode (clockticks timecode,
299 unsigned char marker,
300 unsigned char **buffer);
301 void buffer_mpeg2scr_timecode( clockticks timecode,
302 unsigned char **buffer
305 int comp_timecode (clockticks *,clockticks *); /* 1tes TimeC. <= 2tes TimeC. ? */
306 int packet_payload( Sys_header_struc *sys_header, Pack_struc *pack_header, int buffers, int PTSstamp, int DTSstamp );
307 /* Compute available packet payload in a sector... */
308 void create_sector (Sector_struc *sector,
309 Pack_struc *pack,
310 Sys_header_struc *sys_header,
311 unsigned int max_packet_data_size,
312 FILE *inputstream,
314 unsigned char type,
315 unsigned char buffer_scale,
316 unsigned int buffer_size,
317 unsigned char buffers,
318 clockticks PTS,
319 clockticks DTS,
320 unsigned char timestamps
323 void create_sys_header (
324 Sys_header_struc *sys_header,
325 unsigned int rate_bound,
326 unsigned char audio_bound,
327 unsigned char fixed,
328 unsigned char CSPS,
329 unsigned char audio_lock,
330 unsigned char video_lock,
331 unsigned char video_bound,
333 unsigned char stream1,
334 unsigned char buffer1_scale,
335 unsigned int buffer1_size,
336 unsigned char stream2,
337 unsigned char buffer2_scale,
338 unsigned int buffer2_size,
339 unsigned int which_streams
340 ); /* erstellt einen System Header */
341 void create_pack (
342 Pack_struc *pack,
343 clockticks SCR,
344 unsigned int mux_rate
345 ); /* erstellt einen Pack Header */
347 void output_video ( clockticks SCR,
348 clockticks SCR_delay,
349 FILE *istream_v,
350 FILE *ostream,
351 Buffer_struc *buffer,
352 Vaunit_struc *video_au,
353 Vector vaunit_info_vec,
354 unsigned int *new_picture_type,
355 unsigned char marker_pack,
356 unsigned char include_sys_header,
357 unsigned char end_of_segment
359 void output_audio ( clockticks SCR,
360 clockticks SCR_delay,
361 FILE *istream_a,
362 FILE *ostream,
363 Buffer_struc *buffer,
364 Aaunit_struc *audio_au,
365 Vector aaunit_info_vec,
366 unsigned char *audio_frame_start,
367 unsigned char marker_pack,
368 unsigned char include_sys_header,
369 unsigned char end_of_segment);
371 void output_padding (
372 clockticks SCR,
373 FILE *ostream,
374 unsigned char start_of_new_pack,
375 unsigned char include_sys_header,
376 unsigned char pseudo_VBR
377 ); /* erstellt und schreibt Padding packet */
379 void buffer_clean (Buffer_struc *buffer, clockticks timenow);
380 void buffer_flush (Buffer_struc *buffer);
381 int buffer_space (Buffer_struc *buffer); /* Anzahl freier Bytes in Buffer */
382 void queue_buffer (Buffer_struc *buffer,
383 unsigned int bytes,
384 clockticks removaltime); /* An Bufferliste anhaengen */
386 void outputstream ( char *video_file,
387 char *audio_file,
388 char *multi_file,
389 Vector vaunit_info_vec,
390 Vector aaunit_info_vec
392 FILE *system_open_init( const char *filename_pat );
393 int system_file_lim_reached( FILE *cur_system_strm );
394 FILE *system_next_file( FILE *cur_system_strm, const char *filename_pat );
396 void status_info ( unsigned int nsectors_a,
397 unsigned int nsectors_v,
398 unsigned int nsectors_p,
399 unsigned long long nbytes,
400 unsigned int buf_v,
401 unsigned int buf_a,
402 int verbose
403 ); /* Status line update */
405 void status_header (void); /* Titelzeilen Statusblock */
406 void status_message (int what, int decode_number); /* Event (end, time_out) mitteilen */
407 void status_footer (void); /* Endzeile */
413 /*************************************************************************
414 Statische Arrays
415 *************************************************************************/
417 extern unsigned int bitrate_index [4][3][16];
419 /*************************************************************************
420 Command line options and derived parameters
421 *************************************************************************/
423 #define MPEG_MPEG1 0
424 #define MPEG_VCD 1
425 #define MPEG_MPEG2 2
426 #define MPEG_SVCD 3
427 #define MPEG_DVD 4
430 extern int opt_quiet_mode;
431 extern int opt_interactive_mode;
432 extern int opt_buffer_size;
433 extern int opt_data_rate;
434 extern int opt_video_offset;
435 extern int opt_audio_offset;
436 extern int opt_sector_size;
437 extern int opt_VBR;
438 extern int opt_mpeg;
439 extern int opt_mux_format;
440 extern int opt_multifile_segment;
441 extern int opt_always_system_headers;
442 extern int opt_packets_per_pack;
443 extern clockticks opt_max_PTS;
444 extern int opt_emul_vcdmplex;
446 extern int verbose;
447 extern unsigned int which_streams;
449 extern int packet_overhead;
450 /* extern int rate_restriction_flag; */
451 /* extern int pack_header_size; */
452 extern int sector_size;
453 extern int mux_rate;
454 extern int dmux_rate;
455 extern int zero_stuffing;
457 extern intmax_t max_system_segment_size;