r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / libmpeg3 / mpeg3private.h
blob7729c3cf8f4e8bc376487948b7789a52cdb487ef
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 2
17 #define RENDERFARM_FS_PREFIX "vfs://"
20 #define MPEG3_FLOAT32 float
22 #define MPEG3_TOC_PREFIX 0x544f4320
23 #define MPEG3_ID3_PREFIX 0x494433
24 #define MPEG3_IFO_PREFIX 0x44564456
25 #define MPEG3_IO_SIZE 0x100000 /* Bytes read by mpeg3io at a time */
26 #define MPEG3_RIFF_CODE 0x52494646
27 #define MPEG3_PROC_CPUINFO "/proc/cpuinfo"
28 #define MPEG3_RAW_SIZE 0x100000 /* Largest possible packet */
29 #define MPEG3_TS_PACKET_SIZE 188
30 #define MPEG3_DVD_PACKET_SIZE 0x800
31 #define MPEG3_SYNC_BYTE 0x47
32 #define MPEG3_PACK_START_CODE 0x000001ba
33 #define MPEG3_SEQUENCE_START_CODE 0x000001b3
34 #define MPEG3_SEQUENCE_END_CODE 0x000001b7
35 #define MPEG3_SYSTEM_START_CODE 0x000001bb
36 #define MPEG3_STRLEN 1024
37 #define MPEG3_PIDMAX 256 /* Maximum number of PIDs in one stream */
38 #define MPEG3_PROGRAM_ASSOCIATION_TABLE 0x00
39 #define MPEG3_CONDITIONAL_ACCESS_TABLE 0x01
40 #define MPEG3_PACKET_START_CODE_PREFIX 0x000001
41 #define MPEG3_PRIVATE_STREAM_2 0xbf
42 #define MPEG3_PADDING_STREAM 0xbe
43 #define MPEG3_GOP_START_CODE 0x000001b8
44 #define MPEG3_PICTURE_START_CODE 0x00000100
45 #define MPEG3_EXT_START_CODE 0x000001b5
46 #define MPEG3_USER_START_CODE 0x000001b2
47 #define MPEG3_SLICE_MIN_START 0x00000101
48 #define MPEG3_SLICE_MAX_START 0x000001af
49 #define MPEG3_AC3_START_CODE 0x0b77
50 #define MPEG3_PCM_START_CODE 0x7f7f807f
51 #define MPEG3_MAX_CPUS 256
52 #define MPEG3_MAX_STREAMS 0x10000
53 #define MPEG3_MAX_PACKSIZE 262144
54 #define MPEG3_CONTIGUOUS_THRESHOLD 10 /* Positive difference before declaring timecodes discontinuous */
55 #define MPEG3_PROGRAM_THRESHOLD 5 /* Minimum number of seconds before interleaving programs */
56 #define MPEG3_SEEK_THRESHOLD 16 /* Number of frames difference before absolute seeking */
57 #define MPEG3_AUDIO_CHUNKSIZE 0x10000 /* Size of chunk of audio in table of contents */
58 #define MPEG3_LITTLE_ENDIAN ((*(uint32_t*)"x\0\0\0") & 0x000000ff)
59 #define MPEG3_AUDIO_HISTORY 0x100000 /* Number of samples in audio history */
60 #define MPEG3_PTS_RANGE 0x100000 /* Range to scan for pts after percentage seek */
62 /* Values for audio format */
63 #define AUDIO_UNKNOWN 0
64 #define AUDIO_MPEG 1
65 #define AUDIO_AC3 2
66 #define AUDIO_PCM 3
67 #define AUDIO_AAC 4
68 #define AUDIO_JESUS 5
71 /* Table of contents */
72 #define FILE_TYPE_PROGRAM 0x0
73 #define FILE_TYPE_TRANSPORT 0x1
74 #define FILE_TYPE_AUDIO 0x2
75 #define FILE_TYPE_VIDEO 0x3
77 #define STREAM_AUDIO 0x4
78 #define STREAM_VIDEO 0x5
80 #define OFFSETS_AUDIO 0x6
81 #define OFFSETS_VIDEO 0x7
83 #define ATRACK_COUNT 0x8
84 #define VTRACK_COUNT 0x9
86 #define TITLE_PATH 0x2
91 // CSS
99 struct mpeg3_block
101 unsigned char b[5];
104 struct mpeg3_playkey
106 int offset;
107 unsigned char key[5];
110 typedef struct
112 int encrypted;
113 char device_path[MPEG3_STRLEN]; /* Device the file is located on */
114 unsigned char disk_key[MPEG3_DVD_PACKET_SIZE];
115 unsigned char title_key[5];
116 char challenge[10];
117 struct mpeg3_block key1;
118 struct mpeg3_block key2;
119 struct mpeg3_block keycheck;
120 int varient;
121 int fd;
122 char path[MPEG3_STRLEN];
123 } mpeg3_css_t;
134 // I/O
146 /* Filesystem structure */
147 /* We buffer in MPEG3_IO_SIZE buffers. Stream IO would require back */
148 /* buffering a buffer since the stream must be rewound for packet headers, */
149 /* sequence start codes, format parsing, decryption, and mpeg3cat. */
153 typedef struct
155 FILE *fd;
156 mpeg3_css_t *css; /* Encryption object */
157 char path[MPEG3_STRLEN];
158 unsigned char *buffer; /* Readahead buffer */
159 int64_t buffer_offset; /* Current buffer position */
160 int64_t buffer_size; /* Bytes in buffer */
161 int64_t buffer_position; /* Byte in file of start of buffer */
163 /* Hypothetical position of file pointer */
164 int64_t current_byte;
165 int64_t total_bytes;
166 } mpeg3_fs_t;
175 // Table of contents
184 // May get rid of time values and rename to a cell offset table.
185 // May also get rid of end byte.
186 typedef struct
188 int64_t start_byte;
189 double start_time;
190 double absolute_start_time;
191 double absolute_end_time;
192 int64_t end_byte;
193 double end_time;
194 int program;
195 } mpeg3demux_timecode_t;
197 typedef struct
199 void *file;
200 mpeg3_fs_t *fs;
201 int64_t total_bytes; /* Total bytes in file. Critical for seeking and length. */
202 /* Timecode table */
203 mpeg3demux_timecode_t *timecode_table;
204 long timecode_table_size; /* Number of entries */
205 long timecode_table_allocation; /* Number of available slots */
206 } mpeg3_title_t;
218 // Demuxer
229 typedef struct
231 /* mpeg3_t */
232 void* file;
233 /* One packet. MPEG3_RAW_SIZE allocated since we don't know the packet size */
234 unsigned char *raw_data;
235 long raw_offset;
236 /* Amount loaded in last raw_data */
237 int raw_size;
238 /* One packet payload */
239 unsigned char *data_buffer;
240 long data_size;
241 long data_position;
242 /* Only one is on depending on which track owns the demultiplexer. */
243 int do_audio;
244 int do_video;
245 /* Direction of reads */
246 int reverse;
247 /* Set to 1 when eof or attempt to read before beginning */
248 int error_flag;
249 /* Temp variables for returning */
250 unsigned char next_char;
251 /* Correction factor for time discontinuity */
252 double time_offset;
253 int read_all;
254 /* Info for mpeg3cat */
255 int64_t last_packet_start;
256 int64_t last_packet_end;
257 int64_t last_packet_decryption;
259 /* Titles */
260 mpeg3_title_t *titles[MPEG3_MAX_STREAMS];
261 int total_titles;
262 int current_title;
264 /* Tables of every stream ID encountered */
265 int astream_table[MPEG3_MAX_STREAMS]; /* macro of audio format if audio */
266 int vstream_table[MPEG3_MAX_STREAMS]; /* 1 if video */
268 /* Programs */
269 int total_programs;
270 int current_program;
272 /* Timecode in the current title */
273 int current_timecode;
275 /* Byte position in the current title */
276 long current_byte;
278 int transport_error_indicator;
279 int payload_unit_start_indicator;
280 int pid;
281 int transport_scrambling_control;
282 int adaptation_field_control;
283 int continuity_counter;
284 int is_padding;
285 int pid_table[MPEG3_PIDMAX];
286 int continuity_counters[MPEG3_PIDMAX];
287 int total_pids;
288 int adaptation_fields;
289 double time; /* Time in seconds */
290 int audio_pid;
291 int video_pid;
292 int astream; /* Video stream ID being decoded. -1 = select first ID in stream */
293 int vstream; /* Audio stream ID being decoded. -1 = select first ID in stream */
294 int aformat; /* format of the audio derived from multiplexing codes */
295 long program_association_tables;
296 int table_id;
297 int section_length;
298 int transport_stream_id;
299 long pes_packets;
300 double pes_audio_time; /* Presentation Time stamps */
301 double pes_video_time; /* Presentation Time stamps */
302 } mpeg3_demuxer_t;
311 // Bitstream
322 // next bit in forward direction
323 // next bit in reverse direction |
324 // v v
325 // | | | | | | | | | | | | | | | | | | | | | | | | | | |1|1|1|1|1|1| */
326 // ^ ^
327 // | bit_number = 1
328 // bfr_size = 6
330 typedef struct
332 uint32_t bfr; /* bfr = buffer for bits */
333 int bit_number; /* position of pointer in bfr */
334 int bfr_size; /* number of bits in bfr. Should always be a multiple of 8 */
335 void *file; /* Mpeg2 file */
336 mpeg3_demuxer_t *demuxer; /* Mpeg2 demuxer */
337 /* If the input ptr is true, data is read from it instead of the demuxer. */
338 unsigned char *input_ptr;
339 } mpeg3_bits_t;
350 // Audio
358 #define AC3_N 512
360 #define MAXFRAMESIZE 4096
361 #define MAXFRAMESAMPLES 65536
362 #define HDRCMPMASK 0xfffffd00
363 #define SBLIMIT 32
364 #define SSLIMIT 18
365 #define SCALE_BLOCK 12
366 #define MPEG3AUDIO_PADDING 1024
368 /* Values for mode */
369 #define MPG_MD_STEREO 0
370 #define MPG_MD_JOINT_STEREO 1
371 #define MPG_MD_DUAL_CHANNEL 2
372 #define MPG_MD_MONO 3
375 #define MAX_AC3_FRAMESIZE 1920 * 2 + 512
377 extern int mpeg3_ac3_samplerates[3];
379 /* Exponent strategy constants */
380 #define MPEG3_EXP_REUSE (0)
381 #define MPEG3_EXP_D15 (1)
382 #define MPEG3_EXP_D25 (2)
383 #define MPEG3_EXP_D45 (3)
385 /* Delta bit allocation constants */
386 #define DELTA_BIT_REUSE (0)
387 #define DELTA_BIT_NEW (1)
388 #define DELTA_BIT_NONE (2)
389 #define DELTA_BIT_RESERVED (3)
395 // Layered decoder
397 typedef struct
399 mpeg3_bits_t *stream;
402 // Layer 3
403 unsigned char *bsbuf, *bsbufold;
404 unsigned char bsspace[2][MAXFRAMESIZE + 512]; /* MAXFRAMESIZE */
405 int bsnum;
406 long framesize; /* For mp3 current framesize without header. For AC3 current framesize with header. */
407 long prev_framesize;
408 int channels;
409 int samplerate;
410 int single;
411 int sampling_frequency_code;
412 int error_protection;
413 int mode;
414 int mode_ext;
415 int lsf;
416 long ssize;
417 int mpeg35;
418 int padding;
419 int layer;
420 int extension;
421 int copyright;
422 int original;
423 int emphasis;
424 int bitrate;
425 /* Static variable in synthesizer */
426 int bo;
427 /* Ignore first frame after a seek */
428 int first_frame;
430 float synth_stereo_buffs[2][2][0x110];
431 float synth_mono_buff[64];
432 float mp3_block[2][2][SBLIMIT * SSLIMIT];
433 int mp3_blc[2];
437 // Layer 2
438 int bitrate_index;
439 struct al_table *alloc;
440 int jsbound;
441 int II_sblimit;
442 unsigned int layer2_scfsi_buf[64];
443 } mpeg3_layer_t;
449 // AC3 decoder
451 typedef struct
453 mpeg3_bits_t *stream;
454 int samplerate;
455 int bitrate;
456 int flags;
457 int channels;
458 void *state; /* a52_state_t */
459 void *output; /* sample_t */
460 int framesize;
461 } mpeg3_ac3_t;
466 // PCM decoder
468 #define PCM_HEADERSIZE 20
469 typedef struct
471 int samplerate;
472 int bits;
473 int channels;
474 int framesize;
475 } mpeg3_pcm_t;
482 /* IMDCT variables */
483 typedef struct
485 float real;
486 float imag;
487 } mpeg3_complex_t;
489 struct al_table
491 short bits;
492 short d;
496 typedef struct
498 void* file;
499 void* track;
501 mpeg3_ac3_t *ac3_decoder;
502 mpeg3_layer_t *layer_decoder;
503 mpeg3_pcm_t *pcm_decoder;
505 /* In order of importance */
506 long outscale;
507 long framenum;
508 /* Size of frame including header */
509 int framesize;
510 float **output; /* Output from synthesizer in linear floats */
511 long output_size; /* Number of pcm samples in the buffer */
512 long output_allocated; /* Allocated number of samples in output */
513 long output_position; /* Sample position in file of start of output buffer */
515 /* Perform a seek to the sample */
516 int sample_seek;
517 /* Perform a seek to the percentage */
518 double percentage_seek;
519 /* +/- number of samples of difference between audio and video */
520 long seek_correction;
521 /* Buffer containing current packet */
522 unsigned char packet_buffer[MAXFRAMESIZE];
523 /* Position in packet buffer of next byte to read */
524 int packet_position;
525 } mpeg3audio_t;
534 typedef struct
536 int channels;
537 int sample_rate;
538 mpeg3_demuxer_t *demuxer;
539 mpeg3audio_t *audio;
540 long current_position;
541 long total_samples;
542 int format; /* format of audio */
547 /* Pointer to master table of contents */
548 int64_t *sample_offsets;
549 int total_sample_offsets;
550 } mpeg3_atrack_t;
561 // Video
572 /* zig-zag scan */
573 extern unsigned char mpeg3_zig_zag_scan_nommx[64];
574 extern unsigned char mpeg3_zig_zag_scan_mmx[64];
576 /* alternate scan */
577 extern unsigned char mpeg3_alternate_scan_nommx[64];
578 extern unsigned char mpeg3_alternate_scan_mmx[64];
580 /* default intra quantization matrix */
581 extern unsigned char mpeg3_default_intra_quantizer_matrix[64];
583 /* Frame rate table must agree with the one in the encoder */
584 extern double mpeg3_frame_rate_table[16];
586 /* non-linear quantization coefficient table */
587 extern unsigned char mpeg3_non_linear_mquant_table[32];
589 #define CHROMA420 1 /* chroma_format */
590 #define CHROMA422 2
591 #define CHROMA444 3
593 #define TOP_FIELD 1 /* picture structure */
594 #define BOTTOM_FIELD 2
595 #define FRAME_PICTURE 3
597 #define SEQ_ID 1 /* extension start code IDs */
598 #define DISP_ID 2
599 #define QUANT_ID 3
600 #define SEQSCAL_ID 5
601 #define PANSCAN_ID 7
602 #define CODING_ID 8
603 #define SPATSCAL_ID 9
604 #define TEMPSCAL_ID 10
606 #define ERROR (-1)
608 #define SC_NONE 0 /* scalable_mode */
609 #define SC_DP 1
610 #define SC_SPAT 2
611 #define SC_SNR 3
612 #define SC_TEMP 4
614 #define I_TYPE 1 /* picture coding type */
615 #define P_TYPE 2
616 #define B_TYPE 3
617 #define D_TYPE 4
619 #define MB_INTRA 1 /* macroblock type */
620 #define MB_PATTERN 2
621 #define MB_BACKWARD 4
622 #define MB_FORWARD 8
623 #define MB_QUANT 16
624 #define MB_WEIGHT 32
625 #define MB_CLASS4 64
627 #define MC_FIELD 1 /* motion_type */
628 #define MC_FRAME 2
629 #define MC_16X8 2
630 #define MC_DMV 3
632 #define MV_FIELD 0 /* mv_format */
633 #define MV_FRAME 1
645 /* Array of these feeds the slice decoders */
646 typedef struct
648 unsigned char *data; /* Buffer for holding the slice data */
649 int buffer_size; /* Size of buffer */
650 int buffer_allocation; /* Space allocated for buffer */
651 int current_position; /* Position in buffer */
652 uint32_t bits;
653 int bits_size;
654 pthread_mutex_t completion_lock; /* Lock slice until completion */
655 int done; /* Signal for slice decoder to skip */
656 } mpeg3_slice_buffer_t;
658 /* Each slice decoder */
659 typedef struct
661 void *video; /* mpeg3video_t */
662 mpeg3_slice_buffer_t *slice_buffer;
664 int thread_number; /* Number of this thread */
665 int current_buffer; /* Buffer this slice decoder is on */
666 int buffer_step; /* Number of buffers to skip */
667 int last_buffer; /* Last buffer this decoder should process */
668 int fault;
669 int done;
670 int quant_scale;
671 int pri_brk; /* slice/macroblock */
672 short block[12][64];
673 int sparse[12];
674 pthread_t tid; /* ID of thread */
675 pthread_mutex_t input_lock, output_lock, completion_lock;
676 } mpeg3_slice_t;
678 typedef struct
680 long hour;
681 long minute;
682 long second;
683 long frame;
684 } mpeg3_timecode_t;
687 typedef struct
689 void* file;
690 void* track;
692 /* ================================= Seeking variables ========================= */
693 mpeg3_bits_t *vstream;
694 int decoder_initted;
695 unsigned char **output_rows; /* Output frame buffer supplied by user */
696 int in_x, in_y, in_w, in_h, out_w, out_h; /* Output dimensions */
697 int row_span;
698 int *x_table, *y_table; /* Location of every output pixel in the input */
699 int color_model;
700 int want_yvu; /* Want to return a YUV frame */
701 char *y_output, *u_output, *v_output; /* Output pointers for a YUV frame */
703 mpeg3_slice_t slice_decoders[MPEG3_MAX_CPUS]; /* One slice decoder for every CPU */
704 int total_slice_decoders; /* Total slice decoders in use */
705 mpeg3_slice_buffer_t slice_buffers[MPEG3_MAX_CPUS]; /* Buffers for holding the slice data */
706 int total_slice_buffers; /* Total buffers in the array to be decompressed */
707 int slice_buffers_initialized; /* Total buffers initialized in the array */
708 pthread_mutex_t slice_lock; /* Lock slice array while getting the next buffer */
709 pthread_mutex_t test_lock;
711 int blockreadsize;
712 long maxframe; /* Max value of frame num to read */
713 double percentage_seek; /* Perform a percentage seek before the next frame is read */
714 int frame_seek; /* Perform a frame seek before the next frame is read */
715 long framenum; /* Number of the next frame to be decoded */
716 long last_number; /* Last framenum rendered */
717 int found_seqhdr;
718 long bitrate;
719 mpeg3_timecode_t gop_timecode; /* Timecode for the last GOP header read. */
720 int has_gops; /* Some streams have no GOPs so try sequence start codes instead */
722 /* These are only available from elementary streams. */
723 long frames_per_gop; /* Frames per GOP after the first GOP. */
724 long first_gop_frames; /* Frames in the first GOP. */
725 long first_frame; /* Number of first frame stored in timecode */
726 long last_frame; /* Last frame in file */
728 /* ================================= Compression variables ===================== */
729 /* Malloced frame buffers. 2 refframes are swapped in and out. */
730 /* while only 1 auxframe is used. */
731 unsigned char *yuv_buffer[5]; /* Make YVU buffers contiguous for all frames */
732 unsigned char *oldrefframe[3], *refframe[3], *auxframe[3];
733 unsigned char *llframe0[3], *llframe1[3];
734 unsigned char *mpeg3_zigzag_scan_table;
735 unsigned char *mpeg3_alternate_scan_table;
736 // Source for the next frame presentation
737 unsigned char **output_src;
738 /* Pointers to frame buffers. */
739 unsigned char *newframe[3];
740 int horizontal_size, vertical_size, mb_width, mb_height;
741 int coded_picture_width, coded_picture_height;
742 int chroma_format, chrom_width, chrom_height, blk_cnt;
743 int pict_type;
744 int field_sequence;
745 int forw_r_size, back_r_size, full_forw, full_back;
746 int prog_seq, prog_frame;
747 int h_forw_r_size, v_forw_r_size, h_back_r_size, v_back_r_size;
748 int dc_prec, pict_struct, topfirst, frame_pred_dct, conceal_mv;
749 int intravlc;
750 int repeatfirst;
751 int repeat_count; /* Number of times to repeat the current frame * 100 since floating point is impossible in MMX */
752 int current_repeat; /* Number of times the current frame has been repeated * 100 */
753 int secondfield;
754 int skip_bframes;
755 int stwc_table_index, llw, llh, hm, hn, vm, vn;
756 int lltempref, llx0, lly0, llprog_frame, llfieldsel;
757 int matrix_coefficients;
758 int framerate_code;
759 double frame_rate;
760 long *cr_to_r, *cr_to_g, *cb_to_g, *cb_to_b;
761 long *cr_to_r_ptr, *cr_to_g_ptr, *cb_to_g_ptr, *cb_to_b_ptr;
762 int have_mmx;
763 int intra_quantizer_matrix[64], non_intra_quantizer_matrix[64];
764 int chroma_intra_quantizer_matrix[64], chroma_non_intra_quantizer_matrix[64];
765 int mpeg2;
766 int qscale_type, altscan; /* picture coding extension */
767 int pict_scal; /* picture spatial scalable extension */
768 int scalable_mode; /* sequence scalable extension */
769 } mpeg3video_t;
785 typedef struct
787 int width;
788 int height;
789 double frame_rate;
790 float aspect_ratio;
791 mpeg3_demuxer_t *demuxer;
792 mpeg3video_t *video;
793 long current_position; /* Number of next frame to be played */
794 long total_frames; /* Total frames in the file */
797 /* Pointer to master table of contents */
798 int64_t *frame_offsets;
799 int total_frame_offsets;
800 int64_t *keyframe_numbers;
801 int total_keyframe_numbers;
802 } mpeg3_vtrack_t;
812 // Whole thing
823 typedef struct
825 mpeg3_fs_t *fs; /* Store entry path here */
826 mpeg3_demuxer_t *demuxer; /* Master tables */
828 /* Media specific */
829 int total_astreams;
830 int total_vstreams;
831 mpeg3_atrack_t *atrack[MPEG3_MAX_STREAMS];
832 mpeg3_vtrack_t *vtrack[MPEG3_MAX_STREAMS];
834 uint64_t **frame_offsets;
835 uint64_t **sample_offsets;
836 uint64_t **keyframe_numbers;
837 int *total_frame_offsets;
838 int *total_sample_offsets;
839 int *total_keyframe_numbers;
840 /* Handles changes in channel count after the start of a stream */
841 int *channel_counts;
843 /* Only one of these is set to 1 to specify what kind of stream we have. */
844 int is_transport_stream;
845 int is_program_stream;
846 int is_ifo_file;
847 int is_audio_stream; /* Elemental stream */
848 int is_video_stream; /* Elemental stream */
849 /* > 0 if known otherwise determine empirically for every packet */
850 long packet_size;
851 /* Type and stream for getting current percentage */
852 int last_type_read; /* 1 - audio 2 - video */
853 int last_stream_read;
855 /* Number of program to play */
856 int program;
857 int cpus;
858 int have_mmx;
860 /* Filesystem is seekable */
861 int seekable;
864 * After percentage seeking is called, this is set to -1.
865 * The first operation to seek needs to set it to the pts of the percentage seek.
866 * Then the next operation to seek needs to match its pts to this value.
868 double percentage_pts;
869 } mpeg3_t;
874 #endif