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
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
109 unsigned char key
[5];
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];
119 struct mpeg3_block key1
;
120 struct mpeg3_block key2
;
121 struct mpeg3_block keycheck
;
124 char path
[MPEG3_STRLEN
];
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. */
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
;
186 // May get rid of time values and rename to a cell offset table.
187 // May also get rid of end byte.
190 /* Relative starting byte of cell in the title */
192 /* Relative ending byte of cell in the title */
194 /* Program the cell belongs to */
202 /* Total bytes in title file. Critical for seeking and length. */
204 /* Absolute starting byte of the title in the stream */
206 /* Absolute ending byte of the title in the stream + 1 */
209 mpeg3demux_cell_t
*cell_table
;
210 int cell_table_size
; /* Number of entries */
211 int cell_table_allocation
; /* Number of available slots */
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 */
243 /* Amount loaded in last raw_data */
245 /* One packet payload */
246 unsigned char *data_buffer
;
249 /* Only one is on depending on which track owns the demultiplexer. */
252 /* Direction of reads */
254 /* Set to 1 when eof or attempt to read before beginning */
256 /* Temp variables for returning */
257 unsigned char next_char
;
259 /* Info for mpeg3cat */
260 int64_t last_packet_start
;
261 int64_t last_packet_end
;
262 int64_t last_packet_decryption
;
265 mpeg3_title_t
*titles
[MPEG3_MAX_STREAMS
];
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 */
277 /* Cell in the current title currently used */
280 /* Absolute byte position. */
281 int64_t absolute_byte
;
283 int transport_error_indicator
;
284 int payload_unit_start_indicator
;
286 int transport_scrambling_control
;
287 int adaptation_field_control
;
288 int continuity_counter
;
290 int pid_table
[MPEG3_PIDMAX
];
291 int continuity_counters
[MPEG3_PIDMAX
];
293 int adaptation_fields
;
294 double time
; /* Time in seconds */
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
;
303 int transport_stream_id
;
305 double pes_audio_time
; /* Presentation Time stamps */
306 double pes_video_time
; /* Presentation Time stamps */
327 // next bit in forward direction
328 // next bit in reverse direction |
330 // | | | | | | | | | | | | | | | | | | | | | | | | | | |1|1|1|1|1|1| */
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
;
365 #define MAXFRAMESIZE 4096
366 #define MAXFRAMESAMPLES 65536
367 #define HDRCMPMASK 0xfffffd00
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)
404 mpeg3_bits_t
*stream
;
408 unsigned char *bsbuf
, *bsbufold
;
409 unsigned char bsspace
[2][MAXFRAMESIZE
+ 512]; /* MAXFRAMESIZE */
411 long framesize
; /* For mp3 current framesize without header. For AC3 current framesize with header. */
416 int sampling_frequency_code
;
417 int error_protection
;
430 /* Static variable in synthesizer */
432 /* Ignore first frame after a seek */
435 float synth_stereo_buffs
[2][2][0x110];
436 float synth_mono_buff
[64];
437 float mp3_block
[2][2][SBLIMIT
* SSLIMIT
];
444 struct al_table
*alloc
;
447 unsigned int layer2_scfsi_buf
[64];
458 mpeg3_bits_t
*stream
;
463 void *state
; /* a52_state_t */
464 void *output
; /* sample_t */
473 #define PCM_HEADERSIZE 20
487 /* IMDCT variables */
506 mpeg3_ac3_t
*ac3_decoder
;
507 mpeg3_layer_t
*layer_decoder
;
508 mpeg3_pcm_t
*pcm_decoder
;
510 /* In order of importance */
512 /* Number of current frame being decoded */
515 /* Size of frame including header */
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 */
524 /* Perform a seek to the absolute byte */
526 /* +/- number of samples of difference between audio and video */
528 /* Buffer containing current packet */
529 unsigned char packet_buffer
[MAXFRAMESIZE
];
530 /* Position in packet buffer of next byte to read */
545 mpeg3_demuxer_t
*demuxer
;
547 int current_position
;
549 int format
; /* format of audio */
554 /* Pointer to master table of contents */
555 int64_t *sample_offsets
;
556 int total_sample_offsets
;
580 extern unsigned char mpeg3_zig_zag_scan_nommx
[64];
581 extern unsigned char mpeg3_zig_zag_scan_mmx
[64];
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 */
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 */
610 #define SPATSCAL_ID 9
611 #define TEMPSCAL_ID 10
613 #define SC_NONE 0 /* scalable_mode */
619 #define I_TYPE 1 /* picture coding type */
624 #define MB_INTRA 1 /* macroblock type */
626 #define MB_BACKWARD 4
632 #define MC_FIELD 1 /* motion_type */
637 #define MV_FIELD 0 /* mv_format */
650 /* Array of these feeds the slice decoders */
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 */
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 */
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 */
676 int pri_brk
; /* slice/macroblock */
679 pthread_t tid
; /* ID of thread */
680 pthread_mutex_t input_lock
, output_lock
, completion_lock
;
697 /* ================================= Seeking variables ========================= */
698 mpeg3_bits_t
*vstream
;
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 */
703 int *x_table
, *y_table
; /* Location of every output pixel in the input */
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
;
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 */
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
;
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
;
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 */
760 int stwc_table_index
, llw
, llh
, hm
, hn
, vm
, vn
;
761 int lltempref
, llx0
, lly0
, llprog_frame
, llfieldsel
;
762 int matrix_coefficients
;
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
;
768 int intra_quantizer_matrix
[64], non_intra_quantizer_matrix
[64];
769 int chroma_intra_quantizer_matrix
[64], chroma_non_intra_quantizer_matrix
[64];
771 int qscale_type
, altscan
; /* picture coding extension */
772 int pict_scal
; /* picture spatial scalable extension */
773 int scalable_mode
; /* sequence scalable extension */
796 mpeg3_demuxer_t
*demuxer
;
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
;
830 /* Store entry path here */
832 /* Master title tables copied to all tracks*/
833 mpeg3_demuxer_t
*demuxer
;
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 */
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
;
854 int is_audio_stream
; /* Elemental stream */
855 int is_video_stream
; /* Elemental stream */
856 /* > 0 if known otherwise determine empirically for every packet */
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 */
867 /* Filesystem is 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.