r551: Fixed references of shared effects when a track is deleted.
[cinelerra_cv.git] / libmpeg3 / mpeg3private.h
blob7b789342945949ca2d3ce2d1e92e35d3183ad93f
1 #ifndef MPEG3PRIVATE_H
2 #define MPEG3PRIVATE_H
4 #include <pthread.h>
5 #include <stdint.h>
6 #include <stdio.h>
11 /* Constants */
13 #define MPEG3_MAJOR 1
14 #define MPEG3_MINOR 5
15 #define MPEG3_RELEASE 4
17 #define RENDERFARM_FS_PREFIX "vfs://"
20 #define MPEG3_FLOAT32 float
22 #define MPEG3_TOC_PREFIX 0x544f4320
23 #define MPEG3_TOC_VERSION 0xff
24 #define MPEG3_ID3_PREFIX 0x494433
25 #define MPEG3_IFO_PREFIX 0x44564456
26 #define MPEG3_IO_SIZE 0x100000 /* Bytes read by mpeg3io at a time */
27 //#define MPEG3_IO_SIZE 0x800 /* Bytes read by mpeg3io at a time */
28 #define MPEG3_RIFF_CODE 0x52494646
29 #define MPEG3_PROC_CPUINFO "/proc/cpuinfo"
30 #define MPEG3_RAW_SIZE 0x100000 /* Largest possible packet */
31 #define MPEG3_TS_PACKET_SIZE 188
32 #define MPEG3_DVD_PACKET_SIZE 0x800
33 #define MPEG3_SYNC_BYTE 0x47
34 #define MPEG3_PACK_START_CODE 0x000001ba
35 #define MPEG3_SEQUENCE_START_CODE 0x000001b3
36 #define MPEG3_SEQUENCE_END_CODE 0x000001b7
37 #define MPEG3_SYSTEM_START_CODE 0x000001bb
38 #define MPEG3_STRLEN 1024
39 #define MPEG3_PIDMAX 256 /* Maximum number of PIDs in one stream */
40 #define MPEG3_PROGRAM_ASSOCIATION_TABLE 0x00
41 #define MPEG3_CONDITIONAL_ACCESS_TABLE 0x01
42 #define MPEG3_PACKET_START_CODE_PREFIX 0x000001
43 #define MPEG3_PRIVATE_STREAM_2 0xbf
44 #define MPEG3_PADDING_STREAM 0xbe
45 #define MPEG3_GOP_START_CODE 0x000001b8
46 #define MPEG3_PICTURE_START_CODE 0x00000100
47 #define MPEG3_EXT_START_CODE 0x000001b5
48 #define MPEG3_USER_START_CODE 0x000001b2
49 #define MPEG3_SLICE_MIN_START 0x00000101
50 #define MPEG3_SLICE_MAX_START 0x000001af
51 #define MPEG3_AC3_START_CODE 0x0b77
52 #define MPEG3_PCM_START_CODE 0x7f7f807f
53 #define MPEG3_MAX_CPUS 256
54 #define MPEG3_MAX_STREAMS 0x10000
55 #define MPEG3_MAX_PACKSIZE 262144
56 #define MPEG3_CONTIGUOUS_THRESHOLD 10 /* Positive difference before declaring timecodes discontinuous */
57 #define MPEG3_PROGRAM_THRESHOLD 5 /* Minimum number of seconds before interleaving programs */
58 #define MPEG3_SEEK_THRESHOLD 16 /* Number of frames difference before absolute seeking */
59 #define MPEG3_AUDIO_CHUNKSIZE 0x10000 /* Size of chunk of audio in table of contents */
60 #define MPEG3_LITTLE_ENDIAN ((*(uint32_t*)"x\0\0\0") & 0x000000ff)
61 #define MPEG3_AUDIO_HISTORY 0x100000 /* Number of samples in audio history */
62 #define MPEG3_PTS_RANGE 0x100000 /* Range to scan for pts after byte seek */
64 /* Values for audio format */
65 #define AUDIO_UNKNOWN 0
66 #define AUDIO_MPEG 1
67 #define AUDIO_AC3 2
68 #define AUDIO_PCM 3
69 #define AUDIO_AAC 4
70 #define AUDIO_JESUS 5
73 /* Table of contents */
74 #define FILE_TYPE_PROGRAM 0x0
75 #define FILE_TYPE_TRANSPORT 0x1
76 #define FILE_TYPE_AUDIO 0x2
77 #define FILE_TYPE_VIDEO 0x3
79 #define STREAM_AUDIO 0x4
80 #define STREAM_VIDEO 0x5
82 #define OFFSETS_AUDIO 0x6
83 #define OFFSETS_VIDEO 0x7
85 #define ATRACK_COUNT 0x8
86 #define VTRACK_COUNT 0x9
88 #define TITLE_PATH 0x2
93 // CSS
101 struct mpeg3_block
103 unsigned char b[5];
106 struct mpeg3_playkey
108 int offset;
109 unsigned char key[5];
112 typedef struct
114 int encrypted;
115 char device_path[MPEG3_STRLEN]; /* Device the file is located on */
116 unsigned char disk_key[MPEG3_DVD_PACKET_SIZE];
117 unsigned char title_key[5];
118 char challenge[10];
119 struct mpeg3_block key1;
120 struct mpeg3_block key2;
121 struct mpeg3_block keycheck;
122 int varient;
123 int fd;
124 char path[MPEG3_STRLEN];
125 } mpeg3_css_t;
136 // I/O
148 /* Filesystem structure */
149 /* We buffer in MPEG3_IO_SIZE buffers. Stream IO would require back */
150 /* buffering a buffer since the stream must be rewound for packet headers, */
151 /* sequence start codes, format parsing, decryption, and mpeg3cat. */
155 typedef struct
157 FILE *fd;
158 mpeg3_css_t *css; /* Encryption object */
159 char path[MPEG3_STRLEN];
160 unsigned char *buffer; /* Readahead buffer */
161 int64_t buffer_offset; /* Current buffer position */
162 int64_t buffer_size; /* Bytes in buffer */
163 int64_t buffer_position; /* Byte in file of start of buffer */
165 /* Hypothetical position of file pointer */
166 int64_t current_byte;
167 int64_t total_bytes;
168 } mpeg3_fs_t;
177 // Table of contents
186 // May get rid of time values and rename to a cell offset table.
187 // May also get rid of end byte.
188 typedef struct
190 /* Relative starting byte of cell in the title */
191 int64_t start_byte;
192 /* Relative ending byte of cell in the title */
193 int64_t end_byte;
194 /* Program the cell belongs to */
195 int program;
196 } mpeg3demux_cell_t;
198 typedef struct
200 void *file;
201 mpeg3_fs_t *fs;
202 /* Total bytes in title file. Critical for seeking and length. */
203 int64_t total_bytes;
204 /* Absolute starting byte of the title in the stream */
205 int64_t start_byte;
206 /* Absolute ending byte of the title in the stream + 1 */
207 int64_t end_byte;
208 /* Timecode table */
209 mpeg3demux_cell_t *cell_table;
210 int cell_table_size; /* Number of entries */
211 int cell_table_allocation; /* Number of available slots */
212 } mpeg3_title_t;
224 // Demuxer
235 typedef struct
237 /* mpeg3_t */
238 void* file;
239 /* One packet. MPEG3_RAW_SIZE allocated since we don't know the packet size */
240 unsigned char *raw_data;
241 /* Offset in raw_data */
242 int raw_offset;
243 /* Amount loaded in last raw_data */
244 int raw_size;
245 /* One packet payload */
246 unsigned char *data_buffer;
247 long data_size;
248 long data_position;
249 /* Only one is on depending on which track owns the demultiplexer. */
250 int do_audio;
251 int do_video;
252 /* Direction of reads */
253 int reverse;
254 /* Set to 1 when eof or attempt to read before beginning */
255 int error_flag;
256 /* Temp variables for returning */
257 unsigned char next_char;
258 int read_all;
259 /* Info for mpeg3cat */
260 int64_t last_packet_start;
261 int64_t last_packet_end;
262 int64_t last_packet_decryption;
264 /* Titles */
265 mpeg3_title_t *titles[MPEG3_MAX_STREAMS];
266 int total_titles;
267 int current_title;
269 /* Tables of every stream ID encountered */
270 int astream_table[MPEG3_MAX_STREAMS]; /* macro of audio format if audio */
271 int vstream_table[MPEG3_MAX_STREAMS]; /* 1 if video */
273 /* Programs */
274 int total_programs;
275 int current_program;
277 /* Cell in the current title currently used */
278 int title_cell;
280 /* Absolute byte position. */
281 int64_t absolute_byte;
283 int transport_error_indicator;
284 int payload_unit_start_indicator;
285 int pid;
286 int transport_scrambling_control;
287 int adaptation_field_control;
288 int continuity_counter;
289 int is_padding;
290 int pid_table[MPEG3_PIDMAX];
291 int continuity_counters[MPEG3_PIDMAX];
292 int total_pids;
293 int adaptation_fields;
294 double time; /* Time in seconds */
295 int audio_pid;
296 int video_pid;
297 int astream; /* Video stream ID being decoded. -1 = select first ID in stream */
298 int vstream; /* Audio stream ID being decoded. -1 = select first ID in stream */
299 int aformat; /* format of the audio derived from multiplexing codes */
300 long program_association_tables;
301 int table_id;
302 int section_length;
303 int transport_stream_id;
304 long pes_packets;
305 double pes_audio_time; /* Presentation Time stamps */
306 double pes_video_time; /* Presentation Time stamps */
307 } mpeg3_demuxer_t;
316 // Bitstream
327 // next bit in forward direction
328 // next bit in reverse direction |
329 // v v
330 // | | | | | | | | | | | | | | | | | | | | | | | | | | |1|1|1|1|1|1| */
331 // ^ ^
332 // | bit_number = 1
333 // bfr_size = 6
335 typedef struct
337 uint32_t bfr; /* bfr = buffer for bits */
338 int bit_number; /* position of pointer in bfr */
339 int bfr_size; /* number of bits in bfr. Should always be a multiple of 8 */
340 void *file; /* Mpeg2 file */
341 mpeg3_demuxer_t *demuxer; /* Mpeg2 demuxer */
342 /* If the input ptr is true, data is read from it instead of the demuxer. */
343 unsigned char *input_ptr;
344 } mpeg3_bits_t;
355 // Audio
363 #define AC3_N 512
365 #define MAXFRAMESIZE 4096
366 #define MAXFRAMESAMPLES 65536
367 #define HDRCMPMASK 0xfffffd00
368 #define SBLIMIT 32
369 #define SSLIMIT 18
370 #define SCALE_BLOCK 12
371 #define MPEG3AUDIO_PADDING 1024
373 /* Values for mode */
374 #define MPG_MD_STEREO 0
375 #define MPG_MD_JOINT_STEREO 1
376 #define MPG_MD_DUAL_CHANNEL 2
377 #define MPG_MD_MONO 3
380 #define MAX_AC3_FRAMESIZE 1920 * 2 + 512
382 extern int mpeg3_ac3_samplerates[3];
384 /* Exponent strategy constants */
385 #define MPEG3_EXP_REUSE (0)
386 #define MPEG3_EXP_D15 (1)
387 #define MPEG3_EXP_D25 (2)
388 #define MPEG3_EXP_D45 (3)
390 /* Delta bit allocation constants */
391 #define DELTA_BIT_REUSE (0)
392 #define DELTA_BIT_NEW (1)
393 #define DELTA_BIT_NONE (2)
394 #define DELTA_BIT_RESERVED (3)
400 // Layered decoder
402 typedef struct
404 mpeg3_bits_t *stream;
407 // Layer 3
408 unsigned char *bsbuf, *bsbufold;
409 unsigned char bsspace[2][MAXFRAMESIZE + 512]; /* MAXFRAMESIZE */
410 int bsnum;
411 long framesize; /* For mp3 current framesize without header. For AC3 current framesize with header. */
412 long prev_framesize;
413 int channels;
414 int samplerate;
415 int single;
416 int sampling_frequency_code;
417 int error_protection;
418 int mode;
419 int mode_ext;
420 int lsf;
421 long ssize;
422 int mpeg35;
423 int padding;
424 int layer;
425 int extension;
426 int copyright;
427 int original;
428 int emphasis;
429 int bitrate;
430 /* Static variable in synthesizer */
431 int bo;
432 /* Ignore first frame after a seek */
433 int first_frame;
435 float synth_stereo_buffs[2][2][0x110];
436 float synth_mono_buff[64];
437 float mp3_block[2][2][SBLIMIT * SSLIMIT];
438 int mp3_blc[2];
442 // Layer 2
443 int bitrate_index;
444 struct al_table *alloc;
445 int jsbound;
446 int II_sblimit;
447 unsigned int layer2_scfsi_buf[64];
448 } mpeg3_layer_t;
454 // AC3 decoder
456 typedef struct
458 mpeg3_bits_t *stream;
459 int samplerate;
460 int bitrate;
461 int flags;
462 int channels;
463 void *state; /* a52_state_t */
464 void *output; /* sample_t */
465 int framesize;
466 } mpeg3_ac3_t;
471 // PCM decoder
473 #define PCM_HEADERSIZE 20
474 typedef struct
476 int samplerate;
477 int bits;
478 int channels;
479 int framesize;
480 } mpeg3_pcm_t;
487 /* IMDCT variables */
488 typedef struct
490 float real;
491 float imag;
492 } mpeg3_complex_t;
494 struct al_table
496 short bits;
497 short d;
501 typedef struct
503 void* file;
504 void* track;
506 mpeg3_ac3_t *ac3_decoder;
507 mpeg3_layer_t *layer_decoder;
508 mpeg3_pcm_t *pcm_decoder;
510 /* In order of importance */
511 long outscale;
512 /* Number of current frame being decoded */
513 int framenum;
515 /* Size of frame including header */
516 int framesize;
517 float **output; /* Output from synthesizer in linear floats */
518 int output_size; /* Number of pcm samples in the buffer */
519 int output_allocated; /* Allocated number of samples in output */
520 int output_position; /* Sample position in file of start of output buffer */
522 /* Perform a seek to the sample */
523 int sample_seek;
524 /* Perform a seek to the absolute byte */
525 int64_t byte_seek;
526 /* +/- number of samples of difference between audio and video */
527 int seek_correction;
528 /* Buffer containing current packet */
529 unsigned char packet_buffer[MAXFRAMESIZE];
530 /* Position in packet buffer of next byte to read */
531 int packet_position;
532 } mpeg3audio_t;
541 typedef struct
543 int channels;
544 int sample_rate;
545 mpeg3_demuxer_t *demuxer;
546 mpeg3audio_t *audio;
547 int current_position;
548 int total_samples;
549 int format; /* format of audio */
554 /* Pointer to master table of contents */
555 int64_t *sample_offsets;
556 int total_sample_offsets;
557 } mpeg3_atrack_t;
568 // Video
579 /* zig-zag scan */
580 extern unsigned char mpeg3_zig_zag_scan_nommx[64];
581 extern unsigned char mpeg3_zig_zag_scan_mmx[64];
583 /* alternate scan */
584 extern unsigned char mpeg3_alternate_scan_nommx[64];
585 extern unsigned char mpeg3_alternate_scan_mmx[64];
587 /* default intra quantization matrix */
588 extern unsigned char mpeg3_default_intra_quantizer_matrix[64];
590 /* Frame rate table must agree with the one in the encoder */
591 extern double mpeg3_frame_rate_table[16];
593 /* non-linear quantization coefficient table */
594 extern unsigned char mpeg3_non_linear_mquant_table[32];
596 #define CHROMA420 1 /* chroma_format */
597 #define CHROMA422 2
598 #define CHROMA444 3
600 #define TOP_FIELD 1 /* picture structure */
601 #define BOTTOM_FIELD 2
602 #define FRAME_PICTURE 3
604 #define SEQ_ID 1 /* extension start code IDs */
605 #define DISP_ID 2
606 #define QUANT_ID 3
607 #define SEQSCAL_ID 5
608 #define PANSCAN_ID 7
609 #define CODING_ID 8
610 #define SPATSCAL_ID 9
611 #define TEMPSCAL_ID 10
613 #define SC_NONE 0 /* scalable_mode */
614 #define SC_DP 1
615 #define SC_SPAT 2
616 #define SC_SNR 3
617 #define SC_TEMP 4
619 #define I_TYPE 1 /* picture coding type */
620 #define P_TYPE 2
621 #define B_TYPE 3
622 #define D_TYPE 4
624 #define MB_INTRA 1 /* macroblock type */
625 #define MB_PATTERN 2
626 #define MB_BACKWARD 4
627 #define MB_FORWARD 8
628 #define MB_QUANT 16
629 #define MB_WEIGHT 32
630 #define MB_CLASS4 64
632 #define MC_FIELD 1 /* motion_type */
633 #define MC_FRAME 2
634 #define MC_16X8 2
635 #define MC_DMV 3
637 #define MV_FIELD 0 /* mv_format */
638 #define MV_FRAME 1
650 /* Array of these feeds the slice decoders */
651 typedef struct
653 unsigned char *data; /* Buffer for holding the slice data */
654 int buffer_size; /* Size of buffer */
655 int buffer_allocation; /* Space allocated for buffer */
656 int current_position; /* Position in buffer */
657 uint32_t bits;
658 int bits_size;
659 pthread_mutex_t completion_lock; /* Lock slice until completion */
660 int done; /* Signal for slice decoder to skip */
661 } mpeg3_slice_buffer_t;
663 /* Each slice decoder */
664 typedef struct
666 void *video; /* mpeg3video_t */
667 mpeg3_slice_buffer_t *slice_buffer;
669 int thread_number; /* Number of this thread */
670 int current_buffer; /* Buffer this slice decoder is on */
671 int buffer_step; /* Number of buffers to skip */
672 int last_buffer; /* Last buffer this decoder should process */
673 int fault;
674 int done;
675 int quant_scale;
676 int pri_brk; /* slice/macroblock */
677 short block[12][64];
678 int sparse[12];
679 pthread_t tid; /* ID of thread */
680 pthread_mutex_t input_lock, output_lock, completion_lock;
681 } mpeg3_slice_t;
683 typedef struct
685 int hour;
686 int minute;
687 int second;
688 int frame;
689 } mpeg3_timecode_t;
692 typedef struct
694 void* file;
695 void* track;
697 /* ================================= Seeking variables ========================= */
698 mpeg3_bits_t *vstream;
699 int decoder_initted;
700 unsigned char **output_rows; /* Output frame buffer supplied by user */
701 int in_x, in_y, in_w, in_h, out_w, out_h; /* Output dimensions */
702 int row_span;
703 int *x_table, *y_table; /* Location of every output pixel in the input */
704 int color_model;
705 int want_yvu; /* Want to return a YUV frame */
706 char *y_output, *u_output, *v_output; /* Output pointers for a YUV frame */
708 mpeg3_slice_t slice_decoders[MPEG3_MAX_CPUS]; /* One slice decoder for every CPU */
709 int total_slice_decoders; /* Total slice decoders in use */
710 mpeg3_slice_buffer_t slice_buffers[MPEG3_MAX_CPUS]; /* Buffers for holding the slice data */
711 int total_slice_buffers; /* Total buffers in the array to be decompressed */
712 int slice_buffers_initialized; /* Total buffers initialized in the array */
713 pthread_mutex_t slice_lock; /* Lock slice array while getting the next buffer */
714 pthread_mutex_t test_lock;
716 int blockreadsize;
717 int maxframe; /* Max value of frame num to read */
718 int64_t byte_seek; /* Perform absolute byte seek before the next frame is read */
719 int frame_seek; /* Perform a frame seek before the next frame is read */
720 int framenum; /* Number of the next frame to be decoded */
721 int last_number; /* Last framenum rendered */
722 int found_seqhdr;
723 int bitrate;
724 mpeg3_timecode_t gop_timecode; /* Timecode for the last GOP header read. */
725 int has_gops; /* Some streams have no GOPs so try sequence start codes instead */
727 /* These are only available from elementary streams. */
728 int frames_per_gop; /* Frames per GOP after the first GOP. */
729 int first_gop_frames; /* Frames in the first GOP. */
730 int first_frame; /* Number of first frame stored in timecode */
731 int last_frame; /* Last frame in file */
733 /* ================================= Compression variables ===================== */
734 /* Malloced frame buffers. 2 refframes are swapped in and out. */
735 /* while only 1 auxframe is used. */
736 unsigned char *yuv_buffer[5]; /* Make YVU buffers contiguous for all frames */
737 unsigned char *oldrefframe[3], *refframe[3], *auxframe[3];
738 unsigned char *llframe0[3], *llframe1[3];
739 unsigned char *mpeg3_zigzag_scan_table;
740 unsigned char *mpeg3_alternate_scan_table;
741 // Source for the next frame presentation
742 unsigned char **output_src;
743 /* Pointers to frame buffers. */
744 unsigned char *newframe[3];
745 int horizontal_size, vertical_size, mb_width, mb_height;
746 int coded_picture_width, coded_picture_height;
747 int chroma_format, chrom_width, chrom_height, blk_cnt;
748 int pict_type;
749 int field_sequence;
750 int forw_r_size, back_r_size, full_forw, full_back;
751 int prog_seq, prog_frame;
752 int h_forw_r_size, v_forw_r_size, h_back_r_size, v_back_r_size;
753 int dc_prec, pict_struct, topfirst, frame_pred_dct, conceal_mv;
754 int intravlc;
755 int repeatfirst;
756 int repeat_count; /* Number of times to repeat the current frame * 100 since floating point is impossible in MMX */
757 int current_repeat; /* Number of times the current frame has been repeated * 100 */
758 int secondfield;
759 int skip_bframes;
760 int stwc_table_index, llw, llh, hm, hn, vm, vn;
761 int lltempref, llx0, lly0, llprog_frame, llfieldsel;
762 int matrix_coefficients;
763 int framerate_code;
764 double frame_rate;
765 int *cr_to_r, *cr_to_g, *cb_to_g, *cb_to_b;
766 int *cr_to_r_ptr, *cr_to_g_ptr, *cb_to_g_ptr, *cb_to_b_ptr;
767 int have_mmx;
768 int intra_quantizer_matrix[64], non_intra_quantizer_matrix[64];
769 int chroma_intra_quantizer_matrix[64], chroma_non_intra_quantizer_matrix[64];
770 int mpeg2;
771 int qscale_type, altscan; /* picture coding extension */
772 int pict_scal; /* picture spatial scalable extension */
773 int scalable_mode; /* sequence scalable extension */
774 } mpeg3video_t;
790 typedef struct
792 int width;
793 int height;
794 double frame_rate;
795 float aspect_ratio;
796 mpeg3_demuxer_t *demuxer;
797 mpeg3video_t *video;
798 int current_position; /* Number of next frame to be played */
799 int total_frames; /* Total frames in the file */
802 /* Pointer to master table of contents */
803 int64_t *frame_offsets;
804 int total_frame_offsets;
805 int64_t *keyframe_numbers;
806 int total_keyframe_numbers;
807 } mpeg3_vtrack_t;
817 // Whole thing
828 typedef struct
830 /* Store entry path here */
831 mpeg3_fs_t *fs;
832 /* Master title tables copied to all tracks*/
833 mpeg3_demuxer_t *demuxer;
835 /* Media specific */
836 int total_astreams;
837 int total_vstreams;
838 mpeg3_atrack_t *atrack[MPEG3_MAX_STREAMS];
839 mpeg3_vtrack_t *vtrack[MPEG3_MAX_STREAMS];
841 uint64_t **frame_offsets;
842 uint64_t **sample_offsets;
843 uint64_t **keyframe_numbers;
844 int *total_frame_offsets;
845 int *total_sample_offsets;
846 int *total_keyframe_numbers;
847 /* Handles changes in channel count after the start of a stream */
848 int *channel_counts;
850 /* Only one of these is set to 1 to specify what kind of stream we have. */
851 int is_transport_stream;
852 int is_program_stream;
853 int is_ifo_file;
854 int is_audio_stream; /* Elemental stream */
855 int is_video_stream; /* Elemental stream */
856 /* > 0 if known otherwise determine empirically for every packet */
857 int packet_size;
858 /* Type and stream for getting current absolute byte */
859 int last_type_read; /* 1 - audio 2 - video */
860 int last_stream_read;
862 /* Number of program to play */
863 int program;
864 int cpus;
865 int have_mmx;
867 /* Filesystem is seekable */
868 int seekable;
871 * After byte seeking is called, this is set to -1.
872 * The first operation to seek needs to set it to the pts of the byte seek.
873 * Then the next operation to seek needs to match its pts to this value.
875 int64_t byte_pts;
876 } mpeg3_t;
881 #endif