2 #include "mpeg3private.h"
3 #include "mpeg3protos.h"
28 mpeg3_t
* mpeg3_new(char *path
)
31 mpeg3_t
*file
= calloc(1, sizeof(mpeg3_t
));
33 file
->fs
= mpeg3_new_fs(path
);
34 // Late compilers don't produce usable code.
35 file
->demuxer
= mpeg3_new_demuxer(file
, 0, 0, -1);
37 file
->index_bytes
= 0x300000;
38 file
->subtitle_track
= -1;
42 mpeg3_index_t
* mpeg3_new_index()
44 mpeg3_index_t
*index
= calloc(1, sizeof(mpeg3_index_t
));
45 index
->index_zoom
= 1;
49 void mpeg3_delete_index(mpeg3_index_t
*index
)
52 for(i
= 0;i
< index
->index_channels
; i
++)
53 free(index
->index_data
[i
]);
54 free(index
->index_data
);
58 int mpeg3_delete(mpeg3_t
*file
)
63 if(debug
) printf("mpeg3_delete 1\n");
64 for(i
= 0; i
< file
->total_vstreams
; i
++)
65 mpeg3_delete_vtrack(file
, file
->vtrack
[i
]);
66 if(debug
) printf("mpeg3_delete 2\n");
68 for(i
= 0; i
< file
->total_astreams
; i
++)
69 mpeg3_delete_atrack(file
, file
->atrack
[i
]);
71 for(i
= 0; i
< file
->total_sstreams
; i
++)
72 mpeg3_delete_strack(file
->strack
[i
]);
75 if(debug
) printf("mpeg3_delete 3\n");
76 mpeg3_delete_fs(file
->fs
);
77 mpeg3_delete_demuxer(file
->demuxer
);
79 if(debug
) printf("mpeg3_delete 4\n");
80 if(file
->frame_offsets
)
82 for(i
= 0; i
< file
->total_vstreams
; i
++)
84 free(file
->frame_offsets
[i
]);
85 free(file
->keyframe_numbers
[i
]);
88 if(debug
) printf("mpeg3_delete 5\n");
89 free(file
->frame_offsets
);
90 free(file
->keyframe_numbers
);
91 free(file
->total_frame_offsets
);
92 free(file
->total_keyframe_numbers
);
95 if(debug
) printf("mpeg3_delete 6\n");
96 if(file
->sample_offsets
)
98 for(i
= 0; i
< file
->total_astreams
; i
++)
99 free(file
->sample_offsets
[i
]);
101 free(file
->sample_offsets
);
102 free(file
->total_sample_offsets
);
105 if(debug
) printf("mpeg3_delete 7\n");
107 if(file
->channel_counts
)
108 free(file
->channel_counts
);
111 free(file
->audio_eof
);
113 if(debug
) printf("mpeg3_delete 8\n");
116 for(i
= 0; i
< file
->total_indexes
; i
++)
117 mpeg3_delete_index(file
->indexes
[i
]);
118 if(debug
) printf("mpeg3_delete 9\n");
123 if(debug
) printf("mpeg3_delete 10\n");
125 if(debug
) printf("mpeg3_delete 11\n");
129 int mpeg3_check_sig(char *path
)
132 u_int32_t bits
, bits2
;
136 fs
= mpeg3_new_fs(path
);
137 if(mpeg3io_open_file(fs
))
143 bits
= mpeg3io_read_int32(fs
);
144 bits2
= mpeg3io_read_int32(fs
);
145 ext
= strrchr(path
, '.');
147 if(bits
== MPEG3_TOC_PREFIX
)
152 if(((bits2
>> 24) & 0xff) == MPEG3_SYNC_BYTE
&&
154 !strncasecmp(ext
, ".m2ts", 5))
159 if((((bits
>> 24) & 0xff) == MPEG3_SYNC_BYTE
) ||
160 (bits
== MPEG3_PACK_START_CODE
) ||
161 ((bits
& 0xfff00000) == 0xfff00000) ||
162 ((bits
& 0xffff0000) == 0xffe30000) ||
163 (bits
== MPEG3_SEQUENCE_START_CODE
) ||
164 (bits
== MPEG3_PICTURE_START_CODE
) ||
165 (((bits
& 0xffff0000) >> 16) == MPEG3_AC3_START_CODE
) ||
166 ((bits
>> 8) == MPEG3_ID3_PREFIX
) ||
167 (bits
== MPEG3_RIFF_CODE
) ||
168 (bits
== MPEG3_IFO_PREFIX
))
174 /* Test file extension. */
175 if(strncasecmp(ext
, ".ifo", 4) &&
176 strncasecmp(ext
, ".mp2", 4) &&
177 strncasecmp(ext
, ".mp3", 4) &&
178 strncasecmp(ext
, ".m1v", 4) &&
179 strncasecmp(ext
, ".m2v", 4) &&
180 strncasecmp(ext
, ".m2s", 4) &&
181 strncasecmp(ext
, ".mpg", 4) &&
182 strncasecmp(ext
, ".vob", 4) &&
183 strncasecmp(ext
, ".mpeg", 4) &&
184 strncasecmp(ext
, ".m2t", 4) &&
185 strncasecmp(ext
, ".ac3", 4))
190 mpeg3io_close_file(fs
);
204 static int is_toc(uint32_t bits
)
206 return (bits
== MPEG3_TOC_PREFIX
);
210 static int is_ifo(uint32_t bits
)
212 return (bits
== MPEG3_IFO_PREFIX
);
215 static int is_transport(uint32_t bits
)
217 return (((bits
>> 24) & 0xff) == MPEG3_SYNC_BYTE
);
220 static int is_bd(uint32_t bits1
, uint32_t bits2
)
222 return (((bits2
>> 24) & 0xff) == MPEG3_SYNC_BYTE
);
225 static int is_program(uint32_t bits
)
227 return (bits
== MPEG3_PACK_START_CODE
);
230 static int is_mpeg_audio(uint32_t bits
)
232 return (bits
& 0xfff00000) == 0xfff00000 ||
233 (bits
& 0xffff0000) == 0xffe30000 ||
234 ((bits
>> 8) == MPEG3_ID3_PREFIX
) ||
235 (bits
== MPEG3_RIFF_CODE
);
238 static int is_mpeg_video(uint32_t bits
)
240 return (bits
== MPEG3_SEQUENCE_START_CODE
||
241 bits
== MPEG3_PICTURE_START_CODE
);
244 static int is_ac3(uint32_t bits
)
246 return (((bits
& 0xffff0000) >> 16) == MPEG3_AC3_START_CODE
);
249 static int calculate_packet_size(int is_transport
,
258 return MPEG3_BD_PACKET_SIZE
;
260 return MPEG3_TS_PACKET_SIZE
;
267 return MPEG3_DVD_PACKET_SIZE
;
270 return MPEG3_DVD_PACKET_SIZE
;
275 int mpeg3_get_file_type(mpeg3_t
*file
,
280 uint32_t bits
= mpeg3io_read_int32(file
->fs
);
281 uint32_t bits2
= mpeg3io_read_int32(file
->fs
);
284 if(old_file
) file
->is_bd
= old_file
->is_bd
;
289 /* Table of contents for another title set */
292 if(toc_atracks
&& toc_vtracks
)
294 if((result
= mpeg3_read_toc(file
, toc_atracks
, toc_vtracks
)))
296 mpeg3io_close_file(file
->fs
);
302 mpeg3io_close_file(file
->fs
);
306 mpeg3io_close_file(file
->fs
);
314 file
->is_program_stream
= 1;
315 if(mpeg3_read_ifo(file
, 0))
317 mpeg3io_close_file(file
->fs
);
321 file
->is_ifo_file
= 1;
322 mpeg3io_close_file(file
->fs
);
325 if(is_bd(bits
, bits2
))
328 file
->is_transport_stream
= 1;
331 if(is_transport(bits
))
333 /* Transport stream */
334 file
->is_transport_stream
= 1;
340 /* Determine packet size empirically */
341 file
->is_program_stream
= 1;
344 if(is_mpeg_audio(bits
))
346 /* MPEG Audio only */
347 file
->is_audio_stream
= 1;
350 if(is_mpeg_video(bits
))
353 file
->is_video_stream
= 1;
359 file
->is_audio_stream
= 1;
363 fprintf(stderr
, "mpeg3_get_file_type: not a readable stream.\n");
368 * printf("mpeg3_get_file_type 2 %p %d %d %d %d\n",
370 * file->is_transport_stream,
371 * file->is_program_stream,
372 * file->is_video_stream,
373 * file->is_audio_stream);
377 file
->packet_size
= calculate_packet_size(file
->is_transport_stream
,
378 file
->is_program_stream
,
379 file
->is_audio_stream
,
380 file
->is_video_stream
,
387 static void copy_subtitles(mpeg3_t
*file
, mpeg3_t
*old_file
)
390 file
->total_sstreams
= old_file
->total_sstreams
;
391 for(i
= 0; i
< file
->total_sstreams
; i
++)
393 file
->strack
[i
] = mpeg3_new_strack(old_file
->strack
[i
]->id
);
394 mpeg3_copy_strack(file
->strack
[i
], old_file
->strack
[i
]);
397 memcpy(file
->palette
, old_file
->palette
, 16 * 4);
398 file
->have_palette
= old_file
->have_palette
;
402 mpeg3_t
* mpeg3_open_copy(char *path
, mpeg3_t
*old_file
, int *error_return
)
407 /* The table of contents may have fewer tracks than are in the demuxer */
408 /* This limits the track count */
409 int toc_atracks
= 0x7fffffff;
410 int toc_vtracks
= 0x7fffffff;
413 /* Initialize the file structure */
414 file
= mpeg3_new(path
);
417 /* Need to perform authentication before reading a single byte. */
418 if(mpeg3io_open_file(file
->fs
))
428 /* =============================== Create the title objects ========================= */
429 if((*error_return
= mpeg3_get_file_type(file
,
449 /* Copy data from an old file */
450 if(old_file
&& mpeg3_get_demuxer(old_file
))
452 mpeg3demux_copy_titles(file
->demuxer
, mpeg3_get_demuxer(old_file
));
453 copy_subtitles(file
, old_file
);
455 file
->is_transport_stream
= old_file
->is_transport_stream
;
456 file
->is_program_stream
= old_file
->is_program_stream
;
457 file
->is_bd
= old_file
->is_bd
;
458 file
->source_date
= old_file
->source_date
;
461 /* Start from scratch */
462 if(!file
->demuxer
->total_titles
)
464 mpeg3_create_title(file
->demuxer
, 0);
479 /* Generate tracks */
480 if(file
->is_transport_stream
|| file
->is_program_stream
)
482 /* Create video tracks */
484 i
< MPEG3_MAX_STREAMS
&& file
->total_vstreams
< toc_vtracks
;
487 if(file
->demuxer
->vstream_table
[i
])
489 file
->vtrack
[file
->total_vstreams
] =
490 mpeg3_new_vtrack(file
,
493 file
->total_vstreams
);
494 if(file
->vtrack
[file
->total_vstreams
])
495 file
->total_vstreams
++;
500 /* Create audio tracks */
501 for(i
= 0; i
< MPEG3_MAX_STREAMS
&& file
->total_astreams
< toc_atracks
; i
++)
503 if(file
->demuxer
->astream_table
[i
])
505 file
->atrack
[file
->total_astreams
] = mpeg3_new_atrack(file
,
507 file
->demuxer
->astream_table
[i
],
509 file
->total_astreams
);
510 if(file
->atrack
[file
->total_astreams
]) file
->total_astreams
++;
515 if(file
->is_video_stream
)
517 /* Create video tracks */
518 file
->vtrack
[0] = mpeg3_new_vtrack(file
,
522 if(file
->vtrack
[0]) file
->total_vstreams
++;
525 if(file
->is_audio_stream
)
527 /* Create audio tracks */
529 file
->atrack
[0] = mpeg3_new_atrack(file
,
534 if(file
->atrack
[0]) file
->total_astreams
++;
541 mpeg3io_close_file(file
->fs
);
546 mpeg3_t
* mpeg3_open(char *path
, int *error_return
)
548 return mpeg3_open_copy(path
, 0, error_return
);
551 int mpeg3_close(mpeg3_t
*file
)
553 /* File is closed in the same procedure it is opened in. */
558 int mpeg3_set_cpus(mpeg3_t
*file
, int cpus
)
562 for(i
= 0; i
< file
->total_vstreams
; i
++)
563 mpeg3video_set_cpus(file
->vtrack
[i
]->video
, cpus
);
567 int mpeg3_has_audio(mpeg3_t
*file
)
569 return file
->total_astreams
> 0;
572 int mpeg3_total_astreams(mpeg3_t
*file
)
574 return file
->total_astreams
;
577 int mpeg3_audio_channels(mpeg3_t
*file
,
580 if(file
->total_astreams
)
581 return file
->atrack
[stream
]->channels
;
585 int mpeg3_sample_rate(mpeg3_t
*file
,
588 if(file
->total_astreams
)
589 return file
->atrack
[stream
]->sample_rate
;
593 long mpeg3_get_sample(mpeg3_t
*file
,
596 if(file
->total_astreams
)
597 return file
->atrack
[stream
]->current_position
;
601 int mpeg3_set_sample(mpeg3_t
*file
,
605 if(file
->total_astreams
)
607 //printf(__FUNCTION__ " 1 %d %d\n", file->atrack[stream]->current_position, sample);
608 file
->atrack
[stream
]->current_position
= sample
;
609 mpeg3audio_seek_sample(file
->atrack
[stream
]->audio
, sample
);
615 long mpeg3_audio_samples(mpeg3_t
*file
,
618 if(file
->total_astreams
)
619 return file
->atrack
[stream
]->total_samples
;
623 char* mpeg3_audio_format(mpeg3_t
*file
, int stream
)
625 if(stream
< file
->total_astreams
)
627 switch(file
->atrack
[stream
]->format
)
629 case AUDIO_UNKNOWN
: return "Unknown"; break;
630 case AUDIO_MPEG
: return "MPEG"; break;
631 case AUDIO_AC3
: return "AC3"; break;
632 case AUDIO_PCM
: return "PCM"; break;
633 case AUDIO_AAC
: return "AAC"; break;
634 case AUDIO_JESUS
: return "Vorbis"; break;
640 int mpeg3_has_video(mpeg3_t
*file
)
642 return file
->total_vstreams
> 0;
645 int mpeg3_total_vstreams(mpeg3_t
*file
)
647 return file
->total_vstreams
;
650 int mpeg3_video_width(mpeg3_t
*file
,
653 if(file
->total_vstreams
)
654 return file
->vtrack
[stream
]->width
;
658 int mpeg3_video_height(mpeg3_t
*file
,
661 if(file
->total_vstreams
)
662 return file
->vtrack
[stream
]->height
;
666 float mpeg3_aspect_ratio(mpeg3_t
*file
, int stream
)
668 if(file
->total_vstreams
)
669 return file
->vtrack
[stream
]->aspect_ratio
;
673 double mpeg3_frame_rate(mpeg3_t
*file
,
676 if(file
->total_vstreams
)
677 return file
->vtrack
[stream
]->frame_rate
;
681 long mpeg3_video_frames(mpeg3_t
*file
,
684 if(file
->total_vstreams
)
685 return file
->vtrack
[stream
]->total_frames
;
689 long mpeg3_get_frame(mpeg3_t
*file
,
692 if(file
->total_vstreams
)
693 return file
->vtrack
[stream
]->current_position
;
697 int mpeg3_set_frame(mpeg3_t
*file
,
701 if(file
->total_vstreams
)
703 file
->vtrack
[stream
]->current_position
= frame
;
704 mpeg3video_seek_frame(file
->vtrack
[stream
]->video
, frame
);
710 int mpeg3_seek_byte(mpeg3_t
*file
, int64_t byte
)
714 // file->percentage_pts = -1;
715 for(i
= 0; i
< file
->total_vstreams
; i
++)
717 file
->vtrack
[i
]->current_position
= 0;
718 mpeg3video_seek_byte(file
->vtrack
[i
]->video
, byte
);
721 for(i
= 0; i
< file
->total_astreams
; i
++)
723 file
->atrack
[i
]->current_position
= 0;
724 mpeg3audio_seek_byte(file
->atrack
[i
]->audio
, byte
);
731 * double mpeg3_get_percentage_pts(mpeg3_t *file)
733 * return file->percentage_pts;
736 * void mpeg3_set_percentage_pts(mpeg3_t *file, double pts)
742 int mpeg3_previous_frame(mpeg3_t
*file
, int stream
)
744 file
->last_type_read
= 2;
745 file
->last_stream_read
= stream
;
747 if(file
->total_vstreams
)
748 return mpeg3video_previous_frame(file
->vtrack
[stream
]->video
);
753 int64_t mpeg3_tell_byte(mpeg3_t
*file
)
756 if(file
->last_type_read
== 1)
758 result
= mpeg3demux_tell_byte(file
->atrack
[file
->last_stream_read
]->demuxer
);
761 if(file
->last_type_read
== 2)
763 result
= mpeg3demux_tell_byte(file
->vtrack
[file
->last_stream_read
]->demuxer
);
768 int64_t mpeg3_get_bytes(mpeg3_t
*file
)
770 return mpeg3demux_movie_size(file
->demuxer
);
773 double mpeg3_get_time(mpeg3_t
*file
)
775 double atime
= 0, vtime
= 0;
777 if(file
->is_transport_stream
|| file
->is_program_stream
)
779 /* Timecode only available in transport stream */
780 if(file
->last_type_read
== 1)
782 atime
= mpeg3demux_get_time(file
->atrack
[file
->last_stream_read
]->demuxer
);
785 if(file
->last_type_read
== 2)
787 vtime
= mpeg3demux_get_time(file
->vtrack
[file
->last_stream_read
]->demuxer
);
792 /* Use percentage and total time */
793 if(file
->total_astreams
)
795 atime
= mpeg3demux_tell_byte(file
->atrack
[0]->demuxer
) *
796 mpeg3_audio_samples(file
, 0) /
797 mpeg3_sample_rate(file
, 0) /
798 mpeg3_get_bytes(file
);
801 if(file
->total_vstreams
)
803 vtime
= mpeg3demux_tell_byte(file
->vtrack
[0]->demuxer
) *
804 mpeg3_video_frames(file
, 0) /
805 mpeg3_frame_rate(file
, 0) /
806 mpeg3_get_bytes(file
);
810 return MAX(atime
, vtime
);
813 int mpeg3_end_of_audio(mpeg3_t
*file
, int stream
)
816 if(!file
->atrack
[stream
]->channels
) return 1;
817 result
= mpeg3demux_eof(file
->atrack
[stream
]->demuxer
);
821 int mpeg3_end_of_video(mpeg3_t
*file
, int stream
)
824 result
= mpeg3demux_eof(file
->vtrack
[stream
]->demuxer
);
829 int mpeg3_drop_frames(mpeg3_t
*file
, long frames
, int stream
)
833 if(file
->total_vstreams
)
835 result
= mpeg3video_drop_frames(file
->vtrack
[stream
]->video
,
838 if(frames
> 0) file
->vtrack
[stream
]->current_position
+= frames
;
839 file
->last_type_read
= 2;
840 file
->last_stream_read
= stream
;
845 int mpeg3_colormodel(mpeg3_t
*file
, int stream
)
847 if(file
->total_vstreams
)
849 return mpeg3video_colormodel(file
->vtrack
[stream
]->video
);
854 int mpeg3_set_rowspan(mpeg3_t
*file
, int bytes
, int stream
)
856 if(file
->total_vstreams
)
858 file
->vtrack
[stream
]->video
->row_span
= bytes
;
864 int mpeg3_read_frame(mpeg3_t
*file
,
865 unsigned char **output_rows
,
876 //printf("mpeg3_read_frame 1 %d\n", file->vtrack[stream]->current_position);
878 if(file
->total_vstreams
)
880 result
= mpeg3video_read_frame(file
->vtrack
[stream
]->video
,
889 //printf(__FUNCTION__ " 2\n");
890 file
->last_type_read
= 2;
891 file
->last_stream_read
= stream
;
892 file
->vtrack
[stream
]->current_position
++;
895 //printf("mpeg3_read_frame 2 %d\n", file->vtrack[stream]->current_position);
899 int mpeg3_read_yuvframe(mpeg3_t
*file
,
911 //printf("mpeg3_read_yuvframe 1\n");
912 if(file
->total_vstreams
)
914 result
= mpeg3video_read_yuvframe(file
->vtrack
[stream
]->video
,
922 file
->last_type_read
= 2;
923 file
->last_stream_read
= stream
;
924 file
->vtrack
[stream
]->current_position
++;
926 //printf("mpeg3_read_yuvframe 100\n");
930 int mpeg3_read_yuvframe_ptr(mpeg3_t
*file
,
938 if(file
->total_vstreams
)
940 result
= mpeg3video_read_yuvframe_ptr(file
->vtrack
[stream
]->video
,
944 file
->last_type_read
= 2;
945 file
->last_stream_read
= stream
;
946 file
->vtrack
[stream
]->current_position
++;
951 int mpeg3_read_audio(mpeg3_t
*file
,
960 if(file
->total_astreams
)
962 result
= mpeg3audio_decode_audio(file
->atrack
[stream
]->audio
,
967 file
->last_type_read
= 1;
968 file
->last_stream_read
= stream
;
969 file
->atrack
[stream
]->current_position
+= samples
;
975 int mpeg3_reread_audio(mpeg3_t
*file
,
982 if(file
->total_astreams
)
984 mpeg3_set_sample(file
,
985 file
->atrack
[stream
]->current_position
- samples
,
987 file
->last_type_read
= 1;
988 file
->last_stream_read
= stream
;
989 return mpeg3_read_audio(file
,
999 int mpeg3_read_audio_chunk(mpeg3_t
*file
,
1000 unsigned char *output
,
1006 if(file
->total_astreams
)
1008 result
= mpeg3audio_read_raw(file
->atrack
[stream
]->audio
,
1012 file
->last_type_read
= 1;
1013 file
->last_stream_read
= stream
;
1018 int mpeg3_read_video_chunk(mpeg3_t
*file
,
1019 unsigned char *output
,
1025 if(file
->total_vstreams
)
1027 result
= mpeg3video_read_raw(file
->vtrack
[stream
]->video
,
1031 file
->last_type_read
= 2;
1032 file
->last_stream_read
= stream
;
1039 int64_t mpeg3_memory_usage(mpeg3_t
*file
)
1043 for(i
= 0; i
< file
->total_vstreams
; i
++)
1045 result
+= mpeg3_cache_usage(file
->vtrack
[i
]->frame_cache
);