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
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
107 unsigned char key
[5];
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];
117 struct mpeg3_block key1
;
118 struct mpeg3_block key2
;
119 struct mpeg3_block keycheck
;
122 char path
[MPEG3_STRLEN
];
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. */
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
;
184 // May get rid of time values and rename to a cell offset table.
185 // May also get rid of end byte.
190 double absolute_start_time
;
191 double absolute_end_time
;
195 } mpeg3demux_timecode_t
;
201 int64_t total_bytes
; /* Total bytes in file. Critical for seeking and length. */
203 mpeg3demux_timecode_t
*timecode_table
;
204 long timecode_table_size
; /* Number of entries */
205 long timecode_table_allocation
; /* Number of available slots */
233 /* One packet. MPEG3_RAW_SIZE allocated since we don't know the packet size */
234 unsigned char *raw_data
;
236 /* Amount loaded in last raw_data */
238 /* One packet payload */
239 unsigned char *data_buffer
;
242 /* Only one is on depending on which track owns the demultiplexer. */
245 /* Direction of reads */
247 /* Set to 1 when eof or attempt to read before beginning */
249 /* Temp variables for returning */
250 unsigned char next_char
;
251 /* Correction factor for time discontinuity */
254 /* Info for mpeg3cat */
255 int64_t last_packet_start
;
256 int64_t last_packet_end
;
257 int64_t last_packet_decryption
;
260 mpeg3_title_t
*titles
[MPEG3_MAX_STREAMS
];
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 */
272 /* Timecode in the current title */
273 int current_timecode
;
275 /* Byte position in the current title */
278 int transport_error_indicator
;
279 int payload_unit_start_indicator
;
281 int transport_scrambling_control
;
282 int adaptation_field_control
;
283 int continuity_counter
;
285 int pid_table
[MPEG3_PIDMAX
];
286 int continuity_counters
[MPEG3_PIDMAX
];
288 int adaptation_fields
;
289 double time
; /* Time in seconds */
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
;
298 int transport_stream_id
;
300 double pes_audio_time
; /* Presentation Time stamps */
301 double pes_video_time
; /* Presentation Time stamps */
322 // next bit in forward direction
323 // next bit in reverse direction |
325 // | | | | | | | | | | | | | | | | | | | | | | | | | | |1|1|1|1|1|1| */
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
;
360 #define MAXFRAMESIZE 4096
361 #define MAXFRAMESAMPLES 65536
362 #define HDRCMPMASK 0xfffffd00
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)
399 mpeg3_bits_t
*stream
;
403 unsigned char *bsbuf
, *bsbufold
;
404 unsigned char bsspace
[2][MAXFRAMESIZE
+ 512]; /* MAXFRAMESIZE */
406 long framesize
; /* For mp3 current framesize without header. For AC3 current framesize with header. */
411 int sampling_frequency_code
;
412 int error_protection
;
425 /* Static variable in synthesizer */
427 /* Ignore first frame after a seek */
430 float synth_stereo_buffs
[2][2][0x110];
431 float synth_mono_buff
[64];
432 float mp3_block
[2][2][SBLIMIT
* SSLIMIT
];
439 struct al_table
*alloc
;
442 unsigned int layer2_scfsi_buf
[64];
453 mpeg3_bits_t
*stream
;
458 void *state
; /* a52_state_t */
459 void *output
; /* sample_t */
468 #define PCM_HEADERSIZE 20
482 /* IMDCT variables */
501 mpeg3_ac3_t
*ac3_decoder
;
502 mpeg3_layer_t
*layer_decoder
;
503 mpeg3_pcm_t
*pcm_decoder
;
505 /* In order of importance */
508 /* Size of frame including header */
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 */
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 */
538 mpeg3_demuxer_t
*demuxer
;
540 long current_position
;
542 int format
; /* format of audio */
547 /* Pointer to master table of contents */
548 int64_t *sample_offsets
;
549 int total_sample_offsets
;
573 extern unsigned char mpeg3_zig_zag_scan_nommx
[64];
574 extern unsigned char mpeg3_zig_zag_scan_mmx
[64];
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 */
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 */
603 #define SPATSCAL_ID 9
604 #define TEMPSCAL_ID 10
608 #define SC_NONE 0 /* scalable_mode */
614 #define I_TYPE 1 /* picture coding type */
619 #define MB_INTRA 1 /* macroblock type */
621 #define MB_BACKWARD 4
627 #define MC_FIELD 1 /* motion_type */
632 #define MV_FIELD 0 /* mv_format */
645 /* Array of these feeds the slice decoders */
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 */
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 */
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 */
671 int pri_brk
; /* slice/macroblock */
674 pthread_t tid
; /* ID of thread */
675 pthread_mutex_t input_lock
, output_lock
, completion_lock
;
692 /* ================================= Seeking variables ========================= */
693 mpeg3_bits_t
*vstream
;
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 */
698 int *x_table
, *y_table
; /* Location of every output pixel in the input */
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
;
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 */
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
;
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
;
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 */
755 int stwc_table_index
, llw
, llh
, hm
, hn
, vm
, vn
;
756 int lltempref
, llx0
, lly0
, llprog_frame
, llfieldsel
;
757 int matrix_coefficients
;
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
;
763 int intra_quantizer_matrix
[64], non_intra_quantizer_matrix
[64];
764 int chroma_intra_quantizer_matrix
[64], chroma_non_intra_quantizer_matrix
[64];
766 int qscale_type
, altscan
; /* picture coding extension */
767 int pict_scal
; /* picture spatial scalable extension */
768 int scalable_mode
; /* sequence scalable extension */
791 mpeg3_demuxer_t
*demuxer
;
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
;
825 mpeg3_fs_t
*fs
; /* Store entry path here */
826 mpeg3_demuxer_t
*demuxer
; /* Master tables */
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 */
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
;
847 int is_audio_stream
; /* Elemental stream */
848 int is_video_stream
; /* Elemental stream */
849 /* > 0 if known otherwise determine empirically for every packet */
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 */
860 /* Filesystem is 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
;