7 mpeg3_css_t
* mpeg3_new_css();
12 int64_t mpeg3io_tell_gcc(mpeg3_fs_t
*fs
);
13 double mpeg3_add_double_gcc(double x
, double y
);
14 double mpeg3_divide_double_gcc(double x
, double y
);
15 int64_t mpeg3_total_bytes_gcc(mpeg3_title_t
*title
);
16 int64_t mpeg3io_path_total_bytes(char *path
);
17 int64_t mpeg3io_get_total_bytes(mpeg3_fs_t
*fs
);
24 mpeg3_title_t
* mpeg3_new_title(mpeg3_t
*file
, char *path
);
25 void mpeg3_new_timecode(mpeg3_title_t
*title
,
31 mpeg3demux_timecode_t
* mpeg3_append_timecode(mpeg3_demuxer_t
*demuxer
,
43 mpeg3_atrack_t
* mpeg3_new_atrack(mpeg3_t
*file
,
46 mpeg3_demuxer_t
*demuxer
,
48 int mpeg3_delete_atrack(mpeg3_t
*file
, mpeg3_atrack_t
*atrack
);
51 /* These return 1 on failure and 0 on success */
52 int mpeg3_next_header();
56 mpeg3_vtrack_t
* mpeg3_new_vtrack(mpeg3_t
*file
,
58 mpeg3_demuxer_t
*demuxer
,
60 int mpeg3_delete_vtrack(mpeg3_t
*file
, mpeg3_vtrack_t
*vtrack
);
63 mpeg3audio_t
* mpeg3audio_new(mpeg3_t
*file
, mpeg3_atrack_t
*track
, int is_ac3
);
64 int mpeg3audio_delete(mpeg3audio_t
*audio
);
65 /* Read header and store common parameters in audio structure */
66 int mpeg3audio_read_header(mpeg3audio_t
*audio
);
69 /* Audio consists of many possible formats, each packetized into frames. */
70 /* Each format has a constructor, header decoder, frame decoder, and destructor. */
71 /* The function set is determined by the audio format. */
73 /* To decode audio, for each frame read the header sized number of bytes. */
74 /* Call the header decoder for the format. */
75 /* Call the frame decoder for the format. */
77 int mpeg3_new_decode_tables(mpeg3_layer_t
*audio
);
78 int mpeg3_init_layer3(mpeg3_layer_t
*audio
);
79 int mpeg3_init_layer2(mpeg3_layer_t
*audio
);
81 /* Create a new layer decoder */
82 mpeg3_layer_t
* mpeg3_new_layer();
84 /* Create a new ac3 decoder */
85 mpeg3_ac3_t
* mpeg3_new_ac3();
87 /* Create a new pcm decoder */
88 mpeg3_pcm_t
* mpeg3_new_pcm();
91 /* Delete a new layer decoder */
92 void mpeg3_delete_layer(mpeg3_layer_t
*audio
);
94 /* Delete a new ac3 decoder */
95 void mpeg3_delete_ac3(mpeg3_ac3_t
*audio
);
97 /* Delete a new pcm decoder */
98 void mpeg3_delete_pcm(mpeg3_pcm_t
*audio
);
101 /* Return 1 if the data isn't a header */
102 int mpeg3_layer_check(unsigned char *data
);
103 int mpeg3_ac3_check(unsigned char *header
);
104 int mpeg3_pcm_check(unsigned char *header
);
106 /* These return the size of the next frame including the header */
107 /* or 0 if it wasn't a header. */
108 /* Decode a layer header */
109 int mpeg3_layer_header(mpeg3_layer_t
*audio
, unsigned char *data
);
112 /* Decode an AC3 header */
113 int mpeg3_ac3_header(mpeg3_ac3_t
*audio
, unsigned char *header
);
115 /* Decode an PCM header */
116 int mpeg3_pcm_header(mpeg3_pcm_t
*audio
, unsigned char *header
);
119 /* Reset after a seek */
120 void mpeg3_layer_reset(mpeg3_layer_t
*audio
);
122 /* Decode a frame of layer 3 audio. */
123 /* The output is linear, one buffer for every channel. */
124 /* The user should get the channel count from one of the header commands */
125 /* The output must be big enough to hold the largest frame of audio */
126 /* These functions return the number of samples rendered */
127 int mpeg3audio_dolayer3(mpeg3_layer_t
*audio
,
133 /* Decode a frame of layer 2 audio */
134 int mpeg3audio_dolayer2(mpeg3_layer_t
*audio
,
140 /* Decode a frame of ac3 audio */
141 int mpeg3audio_doac3(mpeg3_ac3_t
*audio
,
148 /* Decode a frame of ac3 audio */
149 int mpeg3audio_dopcm(mpeg3_pcm_t
*audio
,
157 mpeg3video_t
* mpeg3video_new(mpeg3_t
*file
, mpeg3_vtrack_t
*track
);
158 int mpeg3video_delete(mpeg3video_t
*video
);
159 int mpeg3video_read_frame(mpeg3video_t
*video
,
161 unsigned char **output_rows
,
169 void mpeg3video_dump(mpeg3video_t
*video
);
175 mpeg3_fs_t
* mpeg3_new_fs(char *path
);
176 int mpeg3_delete_fs(mpeg3_fs_t
*fs
);
177 int mpeg3io_open_file(mpeg3_fs_t
*fs
);
178 int mpeg3io_close_file(mpeg3_fs_t
*fs
);
179 int mpeg3io_seek(mpeg3_fs_t
*fs
, int64_t byte
);
180 int mpeg3io_read_data(unsigned char *buffer
, long bytes
, mpeg3_fs_t
*fs
);
181 int mpeg3io_next_code(mpeg3_fs_t
*fs
, uint32_t code
, int count
);
182 int mpeg3io_prev_code(mpeg3_fs_t
*fs
, uint32_t code
, int count
);
202 mpeg3_demuxer_t
* mpeg3_new_demuxer(mpeg3_t
*file
,
206 int mpeg3_delete_demuxer(mpeg3_demuxer_t
*demuxer
);
207 int mpeg3demux_read_data(mpeg3_demuxer_t
*demuxer
,
208 unsigned char *output
,
210 unsigned int mpeg3demux_read_int32(mpeg3_demuxer_t
*demuxer
);
211 unsigned int mpeg3demux_read_int24(mpeg3_demuxer_t
*demuxer
);
212 unsigned int mpeg3demux_read_int16(mpeg3_demuxer_t
*demuxer
);
213 double mpeg3demux_length(mpeg3_demuxer_t
*demuxer
);
214 mpeg3_demuxer_t
* mpeg3_get_demuxer(mpeg3_t
*file
);
216 /* Give byte offset relative to current title */
217 int64_t mpeg3demux_tell(mpeg3_demuxer_t
*demuxer
);
219 /* Give byte offset relative to start of movie */
220 int64_t mpeg3demux_tell_absolute(mpeg3_demuxer_t
*demuxer
);
222 /* Give total number of bytes in all titles */
223 int64_t mpeg3demux_movie_size(mpeg3_demuxer_t
*demuxer
);
225 double mpeg3demux_tell_percentage(mpeg3_demuxer_t
*demuxer
);
227 /* Return the current title */
228 int mpeg3demux_tell_title(mpeg3_demuxer_t
*demuxer
);
230 double mpeg3demux_get_time(mpeg3_demuxer_t
*demuxer
);
231 int mpeg3demux_eof(mpeg3_demuxer_t
*demuxer
);
232 int mpeg3demux_bof(mpeg3_demuxer_t
*demuxer
);
233 void mpeg3demux_start_reverse(mpeg3_demuxer_t
*demuxer
);
234 void mpeg3demux_start_forward(mpeg3_demuxer_t
*demuxer
);
235 int mpeg3demux_open_title(mpeg3_demuxer_t
*demuxer
, int title_number
);
236 int mpeg3demux_seek_byte(mpeg3_demuxer_t
*demuxer
, int64_t byte
);
237 int mpeg3demux_seek_percentage(mpeg3_demuxer_t
*demuxer
, double percentage
);
238 int64_t mpeg3demuxer_total_bytes(mpeg3_demuxer_t
*demuxer
);
239 unsigned char mpeg3demux_read_char_packet(mpeg3_demuxer_t
*demuxer
);
240 unsigned char mpeg3demux_read_prev_char_packet(mpeg3_demuxer_t
*demuxer
);
241 int mpeg3demux_read_program(mpeg3_demuxer_t
*demuxer
);
243 /* Get last pts read */
244 double mpeg3demux_audio_pts(mpeg3_demuxer_t
*demuxer
);
246 double mpeg3demux_video_pts(mpeg3_demuxer_t
*demuxer
);
248 /* Set the last pts read to -1 for audio and video */
249 void mpeg3demux_reset_pts(mpeg3_demuxer_t
*demuxer
);
251 /* scan forward for next pts. Used in percentage seeking to synchronize */
252 double mpeg3demux_scan_pts(mpeg3_demuxer_t
*demuxer
);
254 /* seek using sequential search to the pts given. Used in percentage seeking. */
255 int mpeg3demux_goto_pts(mpeg3_demuxer_t
*demuxer
, double pts
);
258 unsigned char mpeg3demux_read_char_packet(mpeg3_demuxer_t
*demuxer
);
259 unsigned char mpeg3demux_read_prev_char_packet(mpeg3_demuxer_t
*demuxer
);
261 #define mpeg3demux_error(demuxer) (((mpeg3_demuxer_t *)(demuxer))->error_flag)
263 #define mpeg3demux_time_offset(demuxer) (((mpeg3_demuxer_t *)(demuxer))->time_offset)
265 #define mpeg3demux_current_time(demuxer) (((mpeg3_demuxer_t *)(demuxer))->time + ((mpeg3_demuxer_t *)(demuxer))->time_offset)
267 static unsigned char mpeg3demux_read_char(mpeg3_demuxer_t
*demuxer
)
269 //printf("mpeg3demux_read_char %lx %lx\n", demuxer->data_position, demuxer->data_size);
270 if(demuxer
->data_position
< demuxer
->data_size
)
272 return demuxer
->data_buffer
[demuxer
->data_position
++];
276 return mpeg3demux_read_char_packet(demuxer
);
282 static unsigned char mpeg3demux_read_prev_char(mpeg3_demuxer_t
*demuxer
)
284 if(demuxer
->data_position
!= 0)
286 return demuxer
->data_buffer
[demuxer
->data_position
--];
290 return mpeg3demux_read_prev_char_packet(demuxer
);
308 mpeg3_bits_t
* mpeg3bits_new_stream(mpeg3_t
*file
, mpeg3_demuxer_t
*demuxer
);
309 int mpeg3bits_delete_stream(mpeg3_bits_t
* stream
);
310 int mpeg3bits_seek_byte(mpeg3_bits_t
* stream
, int64_t position
);
311 int mpeg3bits_open_title(mpeg3_bits_t
* stream
, int title
);
312 int64_t mpeg3bits_tell(mpeg3_bits_t
* stream
);
319 #define mpeg3bits_tell_percentage(stream) mpeg3demux_tell_percentage((stream)->demuxer)
321 #define mpeg3bits_packet_time(stream) mpeg3demux_current_time((stream)->demuxer)
323 #define mpeg3bits_time_offset(stream) mepg2demux_time_offset((stream)->demuxer)
325 #define mpeg3bits_error(stream) mpeg3demux_error((stream)->demuxer)
327 #define mpeg3bits_eof(stream) mpeg3demux_eof((stream)->demuxer)
329 #define mpeg3bits_bof(stream) mpeg3demux_bof((stream)->demuxer)
331 /* Read bytes backward from the file until the reverse_bits is full. */
332 static void mpeg3bits_fill_reverse_bits(mpeg3_bits_t
* stream
, int bits
)
335 while(stream
->bit_number
> 7)
338 stream
->bfr_size
-= 8;
339 stream
->bit_number
-= 8;
342 // Insert bytes before bfr_size
343 while(stream
->bfr_size
- stream
->bit_number
< bits
)
345 if(stream
->input_ptr
)
346 stream
->bfr
|= (unsigned int)(*--stream
->input_ptr
) << stream
->bfr_size
;
348 stream
->bfr
|= (unsigned int)mpeg3demux_read_prev_char(stream
->demuxer
) << stream
->bfr_size
;
349 stream
->bfr_size
+= 8;
353 /* Read bytes forward from the file until the forward_bits is full. */
354 static void mpeg3bits_fill_bits(mpeg3_bits_t
* stream
, int bits
)
356 while(stream
->bit_number
< bits
)
359 if(stream
->input_ptr
)
361 stream
->bfr
|= *stream
->input_ptr
++;
365 stream
->bfr
|= mpeg3demux_read_char(stream
->demuxer
);
367 stream
->bit_number
+= 8;
368 stream
->bfr_size
+= 8;
369 if(stream
->bfr_size
> 32) stream
->bfr_size
= 32;
373 /* Return 8 bits, advancing the file position. */
374 static unsigned int mpeg3bits_getbyte_noptr(mpeg3_bits_t
* stream
)
376 if(stream
->bit_number
< 8)
379 if(stream
->input_ptr
)
380 stream
->bfr
|= *stream
->input_ptr
++;
382 stream
->bfr
|= mpeg3demux_read_char(stream
->demuxer
);
384 stream
->bfr_size
+= 8;
385 if(stream
->bfr_size
> 32) stream
->bfr_size
= 32;
387 return (stream
->bfr
>> stream
->bit_number
) & 0xff;
389 return (stream
->bfr
>> (stream
->bit_number
-= 8)) & 0xff;
392 static unsigned int mpeg3bits_getbit_noptr(mpeg3_bits_t
* stream
)
394 if(!stream
->bit_number
)
397 stream
->bfr
|= mpeg3demux_read_char(stream
->demuxer
);
399 stream
->bfr_size
+= 8;
400 if(stream
->bfr_size
> 32) stream
->bfr_size
= 32;
402 stream
->bit_number
= 7;
404 return (stream
->bfr
>> 7) & 0x1;
406 return (stream
->bfr
>> (--stream
->bit_number
)) & (0x1);
409 /* Return n number of bits, advancing the file position. */
410 /* Use in place of flushbits */
411 static unsigned int mpeg3bits_getbits(mpeg3_bits_t
* stream
, int bits
)
413 if(bits
<= 0) return 0;
414 mpeg3bits_fill_bits(stream
, bits
);
415 return (stream
->bfr
>> (stream
->bit_number
-= bits
)) & (0xffffffff >> (32 - bits
));
418 static unsigned int mpeg3bits_showbits24_noptr(mpeg3_bits_t
* stream
)
420 while(stream
->bit_number
< 24)
423 stream
->bfr
|= mpeg3demux_read_char(stream
->demuxer
);
424 stream
->bit_number
+= 8;
425 stream
->bfr_size
+= 8;
426 if(stream
->bfr_size
> 32) stream
->bfr_size
= 32;
428 return (stream
->bfr
>> (stream
->bit_number
- 24)) & 0xffffff;
431 static unsigned int mpeg3bits_showbits32_noptr(mpeg3_bits_t
* stream
)
433 while(stream
->bit_number
< 32)
436 stream
->bfr
|= mpeg3demux_read_char(stream
->demuxer
);
437 stream
->bit_number
+= 8;
438 stream
->bfr_size
+= 8;
439 if(stream
->bfr_size
> 32) stream
->bfr_size
= 32;
444 static unsigned int mpeg3bits_showbits(mpeg3_bits_t
* stream
, int bits
)
446 mpeg3bits_fill_bits(stream
, bits
);
447 return (stream
->bfr
>> (stream
->bit_number
- bits
)) & (0xffffffff >> (32 - bits
));
450 static unsigned int mpeg3bits_getbits_reverse(mpeg3_bits_t
* stream
, int bits
)
453 mpeg3bits_fill_reverse_bits(stream
, bits
);
454 result
= (stream
->bfr
>> stream
->bit_number
) & (0xffffffff >> (32 - bits
));
455 stream
->bit_number
+= bits
;
459 static unsigned int mpeg3bits_showbits_reverse(mpeg3_bits_t
* stream
, int bits
)
462 mpeg3bits_fill_reverse_bits(stream
, bits
);
463 result
= (stream
->bfr
>> stream
->bit_number
) & (0xffffffff >> (32 - bits
));
484 void mpeg3io_read_buffer(mpeg3_fs_t
*fs
);
486 #define mpeg3io_tell(fs) (((mpeg3_fs_t *)(fs))->current_byte)
489 #define mpeg3io_eof(fs) (((mpeg3_fs_t *)(fs))->current_byte >= ((mpeg3_fs_t *)(fs))->total_bytes)
492 #define mpeg3io_bof(fs) (((mpeg3_fs_t *)(fs))->current_byte < 0)
494 #define mpeg3io_get_fd(fs) (fileno(((mpeg3_fs_t *)(fs))->fd))
496 #define mpeg3io_total_bytes(fs) (((mpeg3_fs_t *)(fs))->total_bytes)
498 static int mpeg3io_sync_buffer(mpeg3_fs_t
*fs
)
500 if(fs
->buffer_position
+ fs
->buffer_offset
!= fs
->current_byte
)
501 // Reposition buffer offset
503 fs
->buffer_offset
= fs
->current_byte
- fs
->buffer_position
;
507 if(fs
->current_byte
< fs
->buffer_position
||
508 fs
->current_byte
>= fs
->buffer_position
+ fs
->buffer_size
)
510 mpeg3io_read_buffer(fs
);
513 return !fs
->buffer_size
;
516 static unsigned int mpeg3io_read_char(mpeg3_fs_t
*fs
)
519 mpeg3io_sync_buffer(fs
);
520 result
= fs
->buffer
[fs
->buffer_offset
++];
525 static unsigned char mpeg3io_next_char(mpeg3_fs_t
*fs
)
527 unsigned char result
;
528 mpeg3io_sync_buffer(fs
);
529 result
= fs
->buffer
[fs
->buffer_offset
];
533 static uint32_t mpeg3io_read_int32(mpeg3_fs_t
*fs
)
537 /* Do not fread. This breaks byte ordering. */
538 a
= mpeg3io_read_char(fs
);
539 b
= mpeg3io_read_char(fs
);
540 c
= mpeg3io_read_char(fs
);
541 d
= mpeg3io_read_char(fs
);
549 static uint32_t mpeg3io_read_int24(mpeg3_fs_t
*fs
)
553 /* Do not fread. This breaks byte ordering. */
554 b
= mpeg3io_read_char(fs
);
555 c
= mpeg3io_read_char(fs
);
556 d
= mpeg3io_read_char(fs
);
563 static uint16_t mpeg3io_read_int16(mpeg3_fs_t
*fs
)
567 /* Do not fread. This breaks byte ordering. */
568 c
= mpeg3io_read_char(fs
);
569 d
= mpeg3io_read_char(fs
);
593 #define mpeg3slice_fillbits(buffer, nbits) \
594 while(((mpeg3_slice_buffer_t*)(buffer))->bits_size < (nbits)) \
596 if(((mpeg3_slice_buffer_t*)(buffer))->current_position < ((mpeg3_slice_buffer_t*)(buffer))->buffer_size) \
598 ((mpeg3_slice_buffer_t*)(buffer))->bits <<= 8; \
599 ((mpeg3_slice_buffer_t*)(buffer))->bits |= ((mpeg3_slice_buffer_t*)(buffer))->data[((mpeg3_slice_buffer_t*)(buffer))->current_position++]; \
601 ((mpeg3_slice_buffer_t*)(buffer))->bits_size += 8; \
604 #define mpeg3slice_flushbits(buffer, nbits) \
606 mpeg3slice_fillbits((buffer), (nbits)); \
607 ((mpeg3_slice_buffer_t*)(buffer))->bits_size -= (nbits); \
610 #define mpeg3slice_flushbit(buffer) \
612 if(((mpeg3_slice_buffer_t*)(buffer))->bits_size) \
613 ((mpeg3_slice_buffer_t*)(buffer))->bits_size--; \
615 if(((mpeg3_slice_buffer_t*)(buffer))->current_position < ((mpeg3_slice_buffer_t*)(buffer))->buffer_size) \
617 ((mpeg3_slice_buffer_t*)(buffer))->bits = \
618 ((mpeg3_slice_buffer_t*)(buffer))->data[((mpeg3_slice_buffer_t*)(buffer))->current_position++]; \
619 ((mpeg3_slice_buffer_t*)(buffer))->bits_size = 7; \
623 static unsigned int mpeg3slice_getbit(mpeg3_slice_buffer_t
*buffer
)
625 if(buffer
->bits_size
)
626 return (buffer
->bits
>> (--buffer
->bits_size
)) & 0x1;
628 if(buffer
->current_position
< buffer
->buffer_size
)
630 buffer
->bits
= buffer
->data
[buffer
->current_position
++];
631 buffer
->bits_size
= 7;
632 return (buffer
->bits
>> 7) & 0x1;
638 static unsigned int mpeg3slice_getbits2(mpeg3_slice_buffer_t
*buffer
)
640 if(buffer
->bits_size
>= 2)
641 return (buffer
->bits
>> (buffer
->bits_size
-= 2)) & 0x3;
643 if(buffer
->current_position
< buffer
->buffer_size
)
646 buffer
->bits
|= buffer
->data
[buffer
->current_position
++];
647 buffer
->bits_size
+= 6;
648 return (buffer
->bits
>> buffer
->bits_size
) & 0x3;
653 static unsigned int mpeg3slice_getbyte(mpeg3_slice_buffer_t
*buffer
)
655 if(buffer
->bits_size
>= 8)
656 return (buffer
->bits
>> (buffer
->bits_size
-= 8)) & 0xff;
658 if(buffer
->current_position
< buffer
->buffer_size
)
661 buffer
->bits
|= buffer
->data
[buffer
->current_position
++];
662 return (buffer
->bits
>> buffer
->bits_size
) & 0xff;
668 static unsigned int mpeg3slice_getbits(mpeg3_slice_buffer_t
*slice_buffer
, int bits
)
670 if(bits
== 1) return mpeg3slice_getbit(slice_buffer
);
671 mpeg3slice_fillbits(slice_buffer
, bits
);
672 return (slice_buffer
->bits
>> (slice_buffer
->bits_size
-= bits
)) & (0xffffffff >> (32 - bits
));
675 static unsigned int mpeg3slice_showbits16(mpeg3_slice_buffer_t
*buffer
)
677 if(buffer
->bits_size
>= 16)
678 return (buffer
->bits
>> (buffer
->bits_size
- 16)) & 0xffff;
680 if(buffer
->current_position
< buffer
->buffer_size
)
683 buffer
->bits_size
+= 16;
684 buffer
->bits
|= (unsigned int)buffer
->data
[buffer
->current_position
++] << 8;
685 buffer
->bits
|= buffer
->data
[buffer
->current_position
++];
686 return (buffer
->bits
>> (buffer
->bits_size
- 16)) & 0xffff;
691 static unsigned int mpeg3slice_showbits9(mpeg3_slice_buffer_t
*buffer
)
693 if(buffer
->bits_size
>= 9)
694 return (buffer
->bits
>> (buffer
->bits_size
- 9)) & 0x1ff;
696 if(buffer
->current_position
< buffer
->buffer_size
)
699 buffer
->bits_size
+= 16;
700 buffer
->bits
|= (unsigned int)buffer
->data
[buffer
->current_position
++] << 8;
701 buffer
->bits
|= buffer
->data
[buffer
->current_position
++];
702 return (buffer
->bits
>> (buffer
->bits_size
- 9)) & 0x1ff;
707 static unsigned int mpeg3slice_showbits5(mpeg3_slice_buffer_t
*buffer
)
709 if(buffer
->bits_size
>= 5)
710 return (buffer
->bits
>> (buffer
->bits_size
- 5)) & 0x1f;
712 if(buffer
->current_position
< buffer
->buffer_size
)
715 buffer
->bits_size
+= 8;
716 buffer
->bits
|= buffer
->data
[buffer
->current_position
++];
717 return (buffer
->bits
>> (buffer
->bits_size
- 5)) & 0x1f;
722 static unsigned int mpeg3slice_showbits(mpeg3_slice_buffer_t
*slice_buffer
, int bits
)
724 mpeg3slice_fillbits(slice_buffer
, bits
);
725 return (slice_buffer
->bits
>> (slice_buffer
->bits_size
- bits
)) & (0xffffffff >> (32 - bits
));