17 #define MPEG3_RELEASE 0
19 #define RENDERFARM_FS_PREFIX "vfs://"
22 #define MPEG3_FLOAT32 float
24 #define MPEG3_TOC_PREFIX 0x544f4320
25 #define MPEG3_TOC_VERSION 0xfd
26 #define MPEG3_ID3_PREFIX 0x494433
27 #define MPEG3_IFO_PREFIX 0x44564456
28 #define MPEG3_IO_SIZE 0x100000 /* Bytes read by mpeg3io at a time */
29 //#define MPEG3_IO_SIZE 0x800 /* Bytes read by mpeg3io at a time */
30 #define MPEG3_RIFF_CODE 0x52494646
31 #define MPEG3_PROC_CPUINFO "/proc/cpuinfo"
32 #define MPEG3_RAW_SIZE 0x100000 /* Largest possible packet */
33 #define MPEG3_TS_PACKET_SIZE 188
34 #define MPEG3_DVD_PACKET_SIZE 0x800
35 #define MPEG3_SYNC_BYTE 0x47
36 #define MPEG3_PACK_START_CODE 0x000001ba
37 #define MPEG3_SEQUENCE_START_CODE 0x000001b3
38 #define MPEG3_SEQUENCE_END_CODE 0x000001b7
39 #define MPEG3_SYSTEM_START_CODE 0x000001bb
40 #define MPEG3_STRLEN 1024
41 #define MPEG3_PIDMAX 256 /* Maximum number of PIDs in one stream */
42 #define MPEG3_PROGRAM_ASSOCIATION_TABLE 0x00
43 #define MPEG3_CONDITIONAL_ACCESS_TABLE 0x01
44 #define MPEG3_PACKET_START_CODE_PREFIX 0x000001
45 #define MPEG3_PRIVATE_STREAM_2 0xbf
46 #define MPEG3_PADDING_STREAM 0xbe
47 #define MPEG3_GOP_START_CODE 0x000001b8
48 #define MPEG3_PICTURE_START_CODE 0x00000100
49 #define MPEG3_EXT_START_CODE 0x000001b5
50 #define MPEG3_USER_START_CODE 0x000001b2
51 #define MPEG3_SLICE_MIN_START 0x00000101
52 #define MPEG3_SLICE_MAX_START 0x000001af
53 #define MPEG3_AC3_START_CODE 0x0b77
54 #define MPEG3_PCM_START_CODE 0x7f7f807f
55 #define MPEG3_MAX_CPUS 256
56 #define MPEG3_MAX_STREAMS 0x10000
57 #define MPEG3_MAX_PACKSIZE 262144
58 /* Positive difference before declaring timecodes discontinuous */
59 #define MPEG3_CONTIGUOUS_THRESHOLD 10
60 /* Minimum number of seconds before interleaving programs */
61 #define MPEG3_PROGRAM_THRESHOLD 5
62 /* Number of frames difference before absolute seeking */
63 #define MPEG3_SEEK_THRESHOLD 16
64 /* Size of chunk of audio in table of contents */
65 #define MPEG3_AUDIO_CHUNKSIZE 0x10000
66 /* Minimum amount of data required to read an audio packet in streaming mode. */
67 #define MPEG3_AUDIO_STREAM_SIZE 0x1000
68 /* Minimum amount of data required to read a video header in streaming mode. */
69 #define MPEG3_VIDEO_STREAM_SIZE 0x1000
70 #define MPEG3_LITTLE_ENDIAN ((*(uint32_t*)"x\0\0\0") & 0x000000ff)
71 /* Number of samples in audio history */
72 #define MPEG3_AUDIO_HISTORY 0x100000
73 /* Range to scan for pts after byte seek */
74 #define MPEG3_PTS_RANGE 0x100000
76 /* Values for audio format */
77 #define AUDIO_UNKNOWN 0
85 /* Table of contents */
86 #define FILE_TYPE_PROGRAM 0x0
87 #define FILE_TYPE_TRANSPORT 0x1
88 #define FILE_TYPE_AUDIO 0x2
89 #define FILE_TYPE_VIDEO 0x3
91 #define STREAM_AUDIO 0x4
92 #define STREAM_VIDEO 0x5
94 #define OFFSETS_AUDIO 0x6
95 #define OFFSETS_VIDEO 0x7
97 #define ATRACK_COUNT 0x8
98 #define VTRACK_COUNT 0x9
100 #define TITLE_PATH 0x2
102 // Combine the pid and the stream id into one unit
103 #define CUSTOM_ID(pid, stream_id) (((pid << 8) | stream_id) & 0xffff)
104 #define CUSTOM_ID_PID(id) (id >> 8)
105 #define CUSTOM_ID_STREAMID(id) (id & 0xff)
108 #define MAX(a, b) ((a) > (b) ? (a) : (b))
112 #define MIN(a, b) ((a) < (b) ? (a) : (b))
132 unsigned char key
[5];
138 char device_path
[MPEG3_STRLEN
]; /* Device the file is located on */
139 unsigned char disk_key
[MPEG3_DVD_PACKET_SIZE
];
140 unsigned char title_key
[5];
142 struct mpeg3_block key1
;
143 struct mpeg3_block key2
;
144 struct mpeg3_block keycheck
;
147 char path
[MPEG3_STRLEN
];
171 /* Filesystem structure */
172 /* We buffer in MPEG3_IO_SIZE buffers. Stream IO would require back */
173 /* buffering a buffer since the stream must be rewound for packet headers, */
174 /* sequence start codes, format parsing, decryption, and mpeg3cat. */
181 mpeg3_css_t
*css
; /* Encryption object */
182 char path
[MPEG3_STRLEN
];
183 unsigned char *buffer
; /* Readahead buffer */
184 int64_t buffer_offset
; /* Current buffer position */
185 int64_t buffer_size
; /* Bytes in buffer */
186 int64_t buffer_position
; /* Byte in file of start of buffer */
188 /* Hypothetical position of file pointer */
189 int64_t current_byte
;
209 // May get rid of time values and rename to a cell offset table.
210 // May also get rid of end byte.
213 /* Starting byte of cell in the title (start_byte) */
215 /* Ending byte of cell in the title (end_byte) */
217 /* Starting byte of the cell in the program */
218 int64_t program_start
;
219 /* Ending byte of the cell in the program */
221 /* Program the cell belongs to */
229 /* Total bytes in title file. Critical for seeking and length. */
231 /* Absolute starting byte of the title in the stream */
233 /* Absolute ending byte of the title in the stream + 1 */
236 mpeg3_cell_t
*cell_table
;
237 int cell_table_size
; /* Number of entries */
238 int cell_table_allocation
; /* Number of available slots */
266 /* One unparsed packet. MPEG3_RAW_SIZE allocated since we don't know the packet size */
267 unsigned char *raw_data
;
268 /* Offset in raw_data of read pointer */
270 /* Amount loaded in last raw_data */
274 /* Elementary stream data when only one stream is to be read. */
275 /* Erased in every call to read a packet. */
276 unsigned char *data_buffer
;
277 /* Allocation of data_buffer */
279 /* Position in data_buffer of write pointer */
281 /* Position in data_buffer of read pointer */
283 /* Start of the next pes packet in the data buffer for reading. */
284 /* Used for decoding PCM. */
287 /* Elementary stream data when all streams are to be read. There is no */
288 /* read pointer since data is expected to be copied directly to a track. */
289 /* Some packets contain audio and video. Further division into */
290 /* stream ID may be needed. */
291 unsigned char *audio_buffer
;
295 unsigned char *video_buffer
;
301 /* What type of data to read. */
306 /* Direction of reads */
308 /* Set to 1 when eof or attempt to read before beginning */
310 /* Temp variables for returning */
311 unsigned char next_char
;
312 /* Info for mpeg3cat */
313 int64_t last_packet_start
;
314 int64_t last_packet_end
;
315 int64_t last_packet_decryption
;
318 mpeg3_title_t
*titles
[MPEG3_MAX_STREAMS
];
320 /* Title currently being used */
324 /* Tables of every stream ID encountered */
325 int astream_table
[MPEG3_MAX_STREAMS
]; /* macro of audio format if audio */
326 int vstream_table
[MPEG3_MAX_STREAMS
]; /* 1 if video */
332 /* Cell in the current title currently used */
335 /* Byte position in current program. */
336 int64_t program_byte
;
337 /* Total bytes in all titles */
339 /* The end of the current stream in the current program */
342 int transport_error_indicator
;
343 int payload_unit_start_indicator
;
344 /* PID of last packet */
346 /* Stream ID of last packet */
347 unsigned int stream_id
;
348 /* Custom ID of last packet */
349 unsigned int custom_id
;
350 int transport_scrambling_control
;
351 int adaptation_field_control
;
352 int continuity_counter
;
354 int pid_table
[MPEG3_PIDMAX
];
355 int continuity_counters
[MPEG3_PIDMAX
];
357 int adaptation_fields
;
358 double time
; /* Time in seconds */
363 /* When only one stream is to be read, these store the stream IDs */
364 /* Video stream ID being decoded. -1 = select first ID in stream */
366 /* Audio stream ID being decoded. -1 = select first ID in stream */
368 /* Multiplexed streams have the audio type */
369 /* Format of the audio derived from multiplexing codes */
371 int program_association_tables
;
374 int transport_stream_id
;
376 double pes_audio_time
; /* Presentation Time stamps */
377 double pes_video_time
; /* Presentation Time stamps */
378 /* Cause the stream parameters to be dumped in human readable format */
400 // next bit in forward direction
401 // next bit in reverse direction |
403 // | | | | | | | | | | | | | | | | | | | | | | | | | | |1|1|1|1|1|1| */
410 uint32_t bfr
; /* bfr = buffer for bits */
411 int bit_number
; /* position of pointer in bfr */
412 int bfr_size
; /* number of bits in bfr. Should always be a multiple of 8 */
413 void *file
; /* Mpeg2 file */
414 mpeg3_demuxer_t
*demuxer
; /* Mpeg2 demuxer */
415 /* If the input ptr is true, data is read from it instead of the demuxer. */
416 unsigned char *input_ptr
;
438 #define MAXFRAMESIZE 4096
439 #define MAXFRAMESAMPLES 65536
440 #define HDRCMPMASK 0xfffffd00
443 #define SCALE_BLOCK 12
444 #define MPEG3AUDIO_PADDING 1024
446 /* Values for mode */
447 #define MPG_MD_STEREO 0
448 #define MPG_MD_JOINT_STEREO 1
449 #define MPG_MD_DUAL_CHANNEL 2
450 #define MPG_MD_MONO 3
453 #define MAX_AC3_FRAMESIZE 1920 * 2 + 512
455 extern int mpeg3_ac3_samplerates
[3];
457 /* Exponent strategy constants */
458 #define MPEG3_EXP_REUSE (0)
459 #define MPEG3_EXP_D15 (1)
460 #define MPEG3_EXP_D25 (2)
461 #define MPEG3_EXP_D45 (3)
463 /* Delta bit allocation constants */
464 #define DELTA_BIT_REUSE (0)
465 #define DELTA_BIT_NEW (1)
466 #define DELTA_BIT_NONE (2)
467 #define DELTA_BIT_RESERVED (3)
477 mpeg3_bits_t
*stream
;
481 unsigned char *bsbuf
, *bsbufold
;
482 unsigned char bsspace
[2][MAXFRAMESIZE
+ 512]; /* MAXFRAMESIZE */
484 long framesize
; /* For mp3 current framesize without header. For AC3 current framesize with header. */
489 int sampling_frequency_code
;
490 int error_protection
;
503 /* Static variable in synthesizer */
505 /* Ignore first frame after a seek */
508 float synth_stereo_buffs
[2][2][0x110];
509 float synth_mono_buff
[64];
510 float mp3_block
[2][2][SBLIMIT
* SSLIMIT
];
517 struct al_table
*alloc
;
520 unsigned int layer2_scfsi_buf
[64];
531 mpeg3_bits_t
*stream
;
536 void *state
; /* a52_state_t */
537 void *output
; /* sample_t */
546 #define PCM_HEADERSIZE 20
560 /* IMDCT variables */
579 mpeg3_ac3_t
*ac3_decoder
;
580 mpeg3_layer_t
*layer_decoder
;
581 mpeg3_pcm_t
*pcm_decoder
;
583 /* In order of importance */
585 /* Number of current frame being decoded */
588 /* Size of frame including header */
590 /* Output from synthesizer in linear floats */
592 /* Number of pcm samples in the buffer */
594 /* Allocated number of samples in output */
595 int output_allocated
;
596 /* Sample position in file of start of output buffer */
599 /* Perform a seek to the sample */
601 /* Perform a seek to the absolute byte */
603 /* +/- number of samples of difference between audio and video */
605 /* Buffer containing current packet */
606 unsigned char packet_buffer
[MAXFRAMESIZE
];
607 /* Position in packet buffer of next byte to read */
617 /* Buffer of frames for index. A frame is a high/low pair. */
619 /* Number of frames allocated in each index channel. */
621 /* Number of index channels allocated */
623 /* Number of high/low pairs in index channel */
625 /* Downsampling of index buffers when constructing index */
635 mpeg3_demuxer_t
*demuxer
;
637 int current_position
;
639 int format
; /* format of audio */
641 /* If we got the header information yet. Used in streaming mode. */
646 /* Pointer to master table of contents when the TOC is read. */
647 /* Pointer to private table when the TOC is being created */
648 /* Stores the absolute byte of each audio chunk */
649 int64_t *sample_offsets
;
650 int total_sample_offsets
;
651 int sample_offsets_allocated
;
652 /* If this sample offset table must be deleted by the track */
654 /* End of stream in table of contents construction */
660 /* Starting byte of previous packet for making TOC */
686 extern unsigned char mpeg3_zig_zag_scan_nommx
[64];
689 extern unsigned char mpeg3_alternate_scan_nommx
[64];
691 /* default intra quantization matrix */
692 extern unsigned char mpeg3_default_intra_quantizer_matrix
[64];
694 /* Frame rate table must agree with the one in the encoder */
695 extern double mpeg3_frame_rate_table
[16];
697 /* non-linear quantization coefficient table */
698 extern unsigned char mpeg3_non_linear_mquant_table
[32];
700 #define CHROMA420 1 /* chroma_format */
704 #define TOP_FIELD 1 /* picture structure */
705 #define BOTTOM_FIELD 2
706 #define FRAME_PICTURE 3
708 #define SEQ_ID 1 /* extension start code IDs */
714 #define SPATSCAL_ID 9
715 #define TEMPSCAL_ID 10
717 #define SC_NONE 0 /* scalable_mode */
723 #define I_TYPE 1 /* picture coding type */
728 #define MB_INTRA 1 /* macroblock type */
730 #define MB_BACKWARD 4
736 #define MC_FIELD 1 /* motion_type */
741 #define MV_FIELD 0 /* mv_format */
754 /* Array of these feeds the slice decoders */
757 unsigned char *data
; /* Buffer for holding the slice data */
758 int buffer_size
; /* Size of buffer */
759 int buffer_allocation
; /* Space allocated for buffer */
760 int current_position
; /* Position in buffer */
763 pthread_mutex_t completion_lock
; /* Lock slice until completion */
764 int done
; /* Signal for slice decoder to skip */
765 } mpeg3_slice_buffer_t
;
767 /* Each slice decoder */
770 void *video
; /* mpeg3video_t */
771 mpeg3_slice_buffer_t
*slice_buffer
;
773 int thread_number
; /* Number of this thread */
774 int current_buffer
; /* Buffer this slice decoder is on */
775 int buffer_step
; /* Number of buffers to skip */
776 int last_buffer
; /* Last buffer this decoder should process */
780 int pri_brk
; /* slice/macroblock */
783 pthread_t tid
; /* ID of thread */
784 pthread_mutex_t input_lock
, output_lock
, completion_lock
;
801 /* ================================= Seeking variables ========================= */
802 mpeg3_bits_t
*vstream
;
804 unsigned char **output_rows
; /* Output frame buffer supplied by user */
805 int in_x
, in_y
, in_w
, in_h
, out_w
, out_h
; /* Output dimensions */
807 int *x_table
, *y_table
; /* Location of every output pixel in the input */
809 int want_yvu
; /* Want to return a YUV frame */
810 char *y_output
, *u_output
, *v_output
; /* Output pointers for a YUV frame */
812 mpeg3_slice_t slice_decoders
[MPEG3_MAX_CPUS
]; /* One slice decoder for every CPU */
813 int total_slice_decoders
; /* Total slice decoders in use */
814 mpeg3_slice_buffer_t slice_buffers
[MPEG3_MAX_CPUS
]; /* Buffers for holding the slice data */
815 int total_slice_buffers
; /* Total buffers in the array to be decompressed */
816 int slice_buffers_initialized
; /* Total buffers initialized in the array */
817 pthread_mutex_t slice_lock
; /* Lock slice array while getting the next buffer */
818 pthread_mutex_t test_lock
;
821 int maxframe
; /* Max value of frame num to read */
822 int64_t byte_seek
; /* Perform absolute byte seek before the next frame is read */
823 int frame_seek
; /* Perform a frame seek before the next frame is read */
824 int framenum
; /* Number of the next frame to be decoded */
825 int last_number
; /* Last framenum rendered */
828 mpeg3_timecode_t gop_timecode
; /* Timecode for the last GOP header read. */
829 int has_gops
; /* Some streams have no GOPs so try sequence start codes instead */
831 /* These are only available from elementary streams. */
832 int frames_per_gop
; /* Frames per GOP after the first GOP. */
833 int first_gop_frames
; /* Frames in the first GOP. */
834 int first_frame
; /* Number of first frame stored in timecode */
835 int last_frame
; /* Last frame in file */
837 /* ================================= Compression variables ===================== */
838 /* Malloced frame buffers. 2 refframes are swapped in and out. */
839 /* while only 1 auxframe is used. */
840 unsigned char *yuv_buffer
[5]; /* Make YVU buffers contiguous for all frames */
841 unsigned char *oldrefframe
[3], *refframe
[3], *auxframe
[3];
842 unsigned char *llframe0
[3], *llframe1
[3];
843 unsigned char *mpeg3_zigzag_scan_table
;
844 unsigned char *mpeg3_alternate_scan_table
;
845 // Source for the next frame presentation
846 unsigned char **output_src
;
847 /* Pointers to frame buffers. */
848 unsigned char *newframe
[3];
849 int horizontal_size
, vertical_size
, mb_width
, mb_height
;
850 int coded_picture_width
, coded_picture_height
;
851 int chroma_format
, chrom_width
, chrom_height
, blk_cnt
;
854 int forw_r_size
, back_r_size
, full_forw
, full_back
;
855 int prog_seq
, prog_frame
;
856 int h_forw_r_size
, v_forw_r_size
, h_back_r_size
, v_back_r_size
;
857 int dc_prec
, pict_struct
, topfirst
, frame_pred_dct
, conceal_mv
;
860 /* Number of times to repeat the current frame * 100 since floating point is impossible in MMX */
862 /* Number of times the current frame has been repeated * 100 */
866 int stwc_table_index
, llw
, llh
, hm
, hn
, vm
, vn
;
867 int lltempref
, llx0
, lly0
, llprog_frame
, llfieldsel
;
868 int matrix_coefficients
;
871 int *cr_to_r
, *cr_to_g
, *cb_to_g
, *cb_to_b
;
872 int *cr_to_r_ptr
, *cr_to_g_ptr
, *cb_to_g_ptr
, *cb_to_b_ptr
;
873 int intra_quantizer_matrix
[64], non_intra_quantizer_matrix
[64];
874 int chroma_intra_quantizer_matrix
[64], chroma_non_intra_quantizer_matrix
[64];
876 int qscale_type
, altscan
; /* picture coding extension */
877 int pict_scal
; /* picture spatial scalable extension */
878 int scalable_mode
; /* sequence scalable extension */
901 mpeg3_demuxer_t
*demuxer
;
903 int current_position
; /* Number of next frame to be played */
904 int total_frames
; /* Total frames in the file */
908 /* Pointer to master table of contents when the TOC is read. */
909 /* Pointer to private table when the TOC is being created */
910 /* Stores the absolute byte of each frame */
911 int64_t *frame_offsets
;
912 int total_frame_offsets
;
913 int frame_offsets_allocated
;
914 int64_t *keyframe_numbers
;
915 int total_keyframe_numbers
;
916 int keyframe_numbers_allocated
;
917 /* Starting byte of previous packet for making TOC */
919 /* End of stream in table of contents construction */
922 /* If these tables must be deleted by the track */
938 /* Store entry path here */
940 /* Master title tables copied to all tracks */
941 mpeg3_demuxer_t
*demuxer
;
946 mpeg3_atrack_t
*atrack
[MPEG3_MAX_STREAMS
];
947 mpeg3_vtrack_t
*vtrack
[MPEG3_MAX_STREAMS
];
950 /* Table of contents storage */
951 int64_t **frame_offsets
;
952 int64_t **sample_offsets
;
953 int64_t **keyframe_numbers
;
956 int *total_frame_offsets
;
957 int *total_sample_offsets
;
958 int *total_keyframe_numbers
;
959 /* Handles changes in channel count after the start of a stream */
961 /* Indexes for audio tracks */
962 mpeg3_index_t
**indexes
;
966 /* Number of bytes to devote to the index of a single track in the index */
967 /* building process. */
970 /* Only one of these is set to 1 to specify what kind of stream we have. */
971 int is_transport_stream
;
972 int is_program_stream
;
974 int is_audio_stream
; /* Elemental stream */
975 int is_video_stream
; /* Elemental stream */
976 /* > 0 if known otherwise determine empirically for every packet */
978 /* Type and stream for getting current absolute byte */
979 int last_type_read
; /* 1 - audio 2 - video */
980 int last_stream_read
;
982 /* Number of program to play */
986 /* Filesystem is seekable. Also means the file isn't a stream. */
989 /* For building TOC, the output file. */
993 * After byte seeking is called, this is set to -1.
994 * The first operation to seek needs to set it to the pts of the byte seek.
995 * Then the next operation to seek needs to match its pts to this value.