Fixed initialisation of tf in file_open(). Without setting the memory to 0,
[cinelerra_cv/mob.git] / libmpeg3 / mpeg3toc3.c
blobcfc88ce43aa2deb7bfcf9a0a96f41a52498504c7
1 #include "libmpeg3.h"
2 #include "mpeg3protos.h"
4 #define TOCVERSION 4
5 #define TOCVIDEO 4
7 int mpeg3_generate_toc(FILE *output, char *path, int timecode_search, int print_streams)
9 mpeg3_t *file = mpeg3_open(path);
10 mpeg3_demuxer_t *demuxer;
11 int i;
13 if(file)
15 demuxer = mpeg3_new_demuxer(file, 0, 0, -1);
16 if(file->is_ifo_file)
18 int i;
19 mpeg3io_open_file(file->fs);
20 mpeg3demux_read_ifo(file, demuxer, 1);
21 mpeg3io_close_file(file->fs);
23 for(i = 0; i < demuxer->total_titles; i++)
25 fprintf(output, "TOCVERSION %d\n", TOCVERSION);
27 if(file->is_program_stream)
28 fprintf(output, "PROGRAM_STREAM\n");
29 else
30 if(file->is_transport_stream)
31 fprintf(output, "TRANSPORT_STREAM\n");
33 fprintf(output, "PATH: %s\n"
34 "SIZE: %ld\n"
35 "PACKETSIZE: %ld\n",
36 demuxer->titles[i]->fs->path,
37 demuxer->titles[i]->total_bytes,
38 file->packet_size);
40 /* Just print the first title's streams */
41 if(i == 0)
42 mpeg3demux_print_streams(demuxer, output);
44 mpeg3demux_print_timecodes(demuxer->titles[i], output);
47 return 0;
49 else
51 char complete_path[MPEG3_STRLEN];
52 mpeg3io_complete_path(complete_path, path);
54 mpeg3demux_create_title(demuxer, timecode_search, output);
56 fprintf(output, "TOCVERSION %d\n"
57 "PATH: %s\n", TOCVERSION, complete_path);
59 if(file->is_program_stream)
60 fprintf(output, "PROGRAM_STREAM\n");
61 else
62 if(file->is_transport_stream)
63 fprintf(output, "TRANSPORT_STREAM\n");
64 else
65 if(file->is_video_stream)
66 fprintf(output, "VIDEO_STREAM\n");
67 else
68 if(file->is_audio_stream)
69 fprintf(output, "AUDIO_STREAM\n");
71 /* Just print the first title's streams */
72 if(print_streams) mpeg3demux_print_streams(demuxer, output);
74 if(file->is_transport_stream || file->is_program_stream)
76 fprintf(output, "SIZE: %ld\n", demuxer->titles[demuxer->current_title]->total_bytes);
77 fprintf(output, "PACKETSIZE: %ld\n", file->packet_size);
80 mpeg3demux_print_timecodes(demuxer->titles[demuxer->current_title], output);
81 return 0;
84 mpeg3_delete_demuxer(demuxer);
85 mpeg3_close(file);
87 return 1;
90 /* Read the title information from a toc */
91 static int read_titles(mpeg3_demuxer_t *demuxer, int version)
93 char string1[MPEG3_STRLEN], string2[MPEG3_STRLEN];
94 long start_byte, end_byte;
95 double start_time, end_time;
96 float program;
97 mpeg3_title_t *title = 0;
98 mpeg3_t *file = demuxer->file;
100 // Eventually use IFO file to generate titles
101 while(!feof(file->fs->fd))
103 char string[1024];
104 int i = 0, byte;
106 // Scanf is broken for single word lines
108 byte = fgetc(file->fs->fd);
109 if(byte != 0 &&
110 byte != 0xd &&
111 byte != 0xa) string[i++] = byte;
112 }while(byte != 0xd &&
113 byte != 0xa &&
114 !feof(file->fs->fd) &&
115 i < 1023);
116 string[i] = 0;
118 if(strlen(string))
120 sscanf(string, "%s %s %ld %lf %lf %f",
121 string1,
122 string2,
123 &end_byte,
124 &start_time,
125 &end_time,
126 &program);
128 //printf("read_titles 2 %d %s %s %ld %f %f %f\n", strlen(string), string1, string2, end_byte, start_time, end_time, program);
129 if(!strncasecmp(string1, "PATH:", 5))
131 //printf("read_titles 2\n");
132 title = demuxer->titles[demuxer->total_titles++] = mpeg3_new_title(file, string2);
134 else
135 if(!strcasecmp(string1, "PROGRAM_STREAM"))
137 //printf("read_titles 3\n");
138 file->is_program_stream = 1;
140 else
141 if(!strcasecmp(string1, "TRANSPORT_STREAM"))
143 //printf("read_titles 4\n");
144 file->is_transport_stream = 1;
146 else
147 if(title)
149 mpeg3demux_cell_t *timecode;
150 start_byte = atol(string2);
151 //printf("read_titles 5\n");
152 if(!strcasecmp(string1, "REGION:"))
154 timecode = mpeg3_append_timecode(demuxer,
155 title,
164 //printf("mpeg3toc2 3\n");
165 timecode->start_byte = start_byte;
166 //printf("mpeg3toc2 4\n");
167 timecode->end_byte = end_byte;
168 timecode->start_time = start_time;
169 timecode->end_time = end_time;
170 timecode->program = program;
175 * printf("read_title %p end: %f start: %f\n",
176 * timecode,
177 * timecode->end_time,
178 * timecode->start_time);
183 else
184 if(!strcasecmp(string1, "ASTREAM:"))
185 demuxer->astream_table[start_byte] = end_byte;
186 else
187 if(!strcasecmp(string1, "VSTREAM:"))
188 demuxer->vstream_table[start_byte] = end_byte;
189 else
190 if(!strcasecmp(string1, "SIZE:"))
191 title->total_bytes = start_byte;
192 else
193 if(!strcasecmp(string1, "PACKETSIZE:"))
194 file->packet_size = start_byte;
195 //printf("read_titles 3\n");
200 mpeg3demux_assign_programs(demuxer);
201 mpeg3demux_open_title(demuxer, 0);
202 return 0;
205 int mpeg3_read_toc(mpeg3_t *file)
207 char string[MPEG3_STRLEN];
208 int version;
210 /* Test version number */
211 mpeg3io_seek(file->fs, 0);
212 fscanf(file->fs->fd, "%s %d", string, &version);
213 if(version != TOCVERSION && version != TOCVIDEO) return 1;
214 switch(version)
216 case TOCVIDEO:
217 file->is_video_stream = 1;
218 break;
221 /* Read titles */
222 read_titles(file->demuxer, version);
223 return 0;