1 #include "mpeg3private.h"
2 #include "mpeg3protos.h"
3 #include "mpeg3title.h"
9 mpeg3_title_t
* mpeg3_new_title(mpeg3_t
*file
, char *path
)
11 mpeg3_title_t
*title
= calloc(1, sizeof(mpeg3_title_t
));
12 title
->fs
= mpeg3_new_fs(path
);
17 int mpeg3_delete_title(mpeg3_title_t
*title
)
19 mpeg3_delete_fs(title
->fs
);
20 if(title
->timecode_table_size
)
22 free(title
->timecode_table
);
29 int mpeg3_copy_title(mpeg3_title_t
*dst
, mpeg3_title_t
*src
)
33 mpeg3_copy_fs(dst
->fs
, src
->fs
);
34 dst
->total_bytes
= src
->total_bytes
;
36 if(src
->timecode_table_size
)
38 dst
->timecode_table_allocation
= src
->timecode_table_allocation
;
39 dst
->timecode_table_size
= src
->timecode_table_size
;
40 dst
->timecode_table
= calloc(1, sizeof(mpeg3demux_timecode_t
) * dst
->timecode_table_allocation
);
42 for(i
= 0; i
< dst
->timecode_table_size
; i
++)
44 dst
->timecode_table
[i
] = src
->timecode_table
[i
];
50 int mpeg3_dump_title(mpeg3_title_t
*title
)
54 printf("mpeg3_dump_title path %s timecode_table_size %d\n", title
->fs
->path
, title
->timecode_table_size
);
55 for(i
= 0; i
< title
->timecode_table_size
; i
++)
57 printf("%.02f: %x - %x %.02f %.02f %x\n",
58 title
->timecode_table
[i
].absolute_start_time
,
59 title
->timecode_table
[i
].start_byte
,
60 title
->timecode_table
[i
].end_byte
,
61 title
->timecode_table
[i
].start_time
,
62 title
->timecode_table
[i
].end_time
,
63 title
->timecode_table
[i
].program
);
69 // Realloc doesn't work for some reason.
70 static void extend_timecode_table(mpeg3_title_t
*title
)
72 if(!title
->timecode_table
||
73 title
->timecode_table_allocation
<= title
->timecode_table_size
)
76 mpeg3demux_timecode_t
*new_table
;
79 //printf("extend_timecode_table 1\n");
80 new_allocation
= title
->timecode_table_allocation
?
81 title
->timecode_table_size
* 2 :
83 new_table
= calloc(1, sizeof(mpeg3demux_timecode_t
) * new_allocation
);
84 //printf("extend_timecode_table 1\n");
86 title
->timecode_table
,
87 sizeof(mpeg3demux_timecode_t
) * title
->timecode_table_allocation
);
88 //printf("extend_timecode_table 1 %p %d %d\n", title->timecode_table, title->timecode_table_allocation,
89 // (new_allocation - title->timecode_table_allocation));
90 free(title
->timecode_table
);
91 title
->timecode_table
= new_table
;
92 //printf("extend_timecode_table 2\n");
93 title
->timecode_table_allocation
= new_allocation
;
94 //printf("extend_timecode_table 2\n");
98 void mpeg3_new_timecode(mpeg3_title_t
*title
,
105 mpeg3demux_timecode_t
*new_timecode
;
107 extend_timecode_table(title
);
108 new_timecode
= &title
->timecode_table
[title
->timecode_table_size
];
110 new_timecode
->start_byte
= start_byte
;
111 new_timecode
->start_time
= start_time
;
112 new_timecode
->end_byte
= end_byte
;
113 new_timecode
->end_time
= end_time
;
114 new_timecode
->absolute_start_time
= 0;
115 new_timecode
->program
= program
;
116 title
->timecode_table_size
++;
119 mpeg3demux_timecode_t
* mpeg3_append_timecode(mpeg3_demuxer_t
*demuxer
,
120 mpeg3_title_t
*title
,
128 mpeg3demux_timecode_t
*new_timecode
, *old_timecode
;
131 extend_timecode_table(title
);
133 * printf("mpeg3_append_timecode 1 %d %f %d %f %d %d\n", prev_byte,
141 new_timecode
= &title
->timecode_table
[title
->timecode_table_size
];
144 new_timecode
->start_byte
= start_byte
;
145 new_timecode
->start_time
= start_time
;
146 new_timecode
->absolute_start_time
= 0;
148 if(title
->timecode_table_size
> 0)
150 old_timecode
= &title
->timecode_table
[title
->timecode_table_size
- 1];
151 old_timecode
->end_byte
= prev_byte
;
152 old_timecode
->end_time
= prev_time
;
153 new_timecode
->absolute_start_time
=
155 old_timecode
->start_time
+
156 old_timecode
->absolute_start_time
;
157 new_timecode
->absolute_end_time
= start_time
;
161 title
->timecode_table_size
++;
165 /* Create a title. */
166 /* Build a table of timecodes contained in the program stream. */
167 /* If toc is 0 just read the first and last timecode. */
168 int mpeg3demux_create_title(mpeg3_demuxer_t
*demuxer
,
172 int result
= 0, done
= 0, counter_start
, counter
;
173 mpeg3_t
*file
= demuxer
->file
;
174 long next_byte
, prev_byte
;
175 double next_time
, prev_time
, absolute_time
;
177 mpeg3_title_t
*title
;
178 u_int32_t test_header
= 0;
179 mpeg3demux_timecode_t
*timecode
= 0;
181 demuxer
->error_flag
= 0;
182 demuxer
->read_all
= 1;
184 /* Create a single title */
185 if(!demuxer
->total_titles
)
187 demuxer
->titles
[0] = mpeg3_new_title(file
, file
->fs
->path
);
188 demuxer
->total_titles
= 1;
189 mpeg3demux_open_title(demuxer
, 0);
192 title
= demuxer
->titles
[0];
193 title
->total_bytes
= mpeg3io_total_bytes(title
->fs
);
202 /* Get timecodes for the title */
203 if(file
->is_transport_stream
|| file
->is_program_stream
)
205 mpeg3io_seek(title
->fs
, 0);
206 //fprintf(stderr, "mpeg3demux_create_title: 0 %f %d %d\n", demuxer->time, result, mpeg3io_eof(title->fs));
207 while(!done
&& !result
&& !mpeg3io_eof(title
->fs
))
209 next_byte
= mpeg3io_tell(title
->fs
);
210 result
= mpeg3_read_next_packet(demuxer
);
211 //printf("mpeg3demux_create_title: 1 %f %d\n", demuxer->time, result);
215 next_time
= demuxer
->time
;
216 //printf("timecode: %f %f %f\n", (double)next_time, (double)prev_time, (double)demuxer->time);
217 if(next_time
< prev_time
||
218 next_time
- prev_time
> MPEG3_CONTIGUOUS_THRESHOLD
||
219 !title
->timecode_table_size
)
222 timecode
= mpeg3_append_timecode(demuxer
,
231 * printf("timecode: %ld %ld %f %f\n",
232 * timecode->start_byte,
233 * timecode->end_byte,
234 * timecode->start_time,
235 * timecode->end_time);
238 counter_start
= next_time
;
243 // Breaks transport stream decoding
245 // if (prev_time == next_time)
251 prev_time
= next_time
;
252 prev_byte
= next_byte
;
256 /* Just get the first bytes if not building a toc to get the stream ID's. */
257 if(next_byte
> 0x100000 &&
258 (!timecode_search
|| !toc
)) done
= 1;
259 //printf("mpeg3demux_create_title 2 next_byte=%d next_time=%f done=%d result=%d %d\n",
260 // next_byte, next_time, done, result, mpeg3io_eof(title->fs));
263 /* Get the last timecode */
264 if(!toc
|| !timecode_search
)
266 demuxer
->read_all
= 0;
267 result
= mpeg3io_seek(title
->fs
, title
->total_bytes
);
268 // result = mpeg3io_seek(title->fs, title->total_bytes -
269 // (title->total_bytes % demuxer->packet_size));
270 //printf("mpeg3demux_create_title 3 %d\n", result);
271 if(!result
) result
= mpeg3_read_prev_packet(demuxer
);
274 //fprintf(stderr, "mpeg3demux_create_title 4 %d %f\n", result, demuxer->time);
275 if(title
->timecode_table
&& timecode
)
277 timecode
->end_byte
= title
->total_bytes
;
278 // timecode->end_byte = mpeg3io_tell(title->fs)/* + demuxer->packet_size */;
279 timecode
->end_time
= demuxer
->time
;
280 timecode
->absolute_end_time
= timecode
->end_time
- timecode
->start_time
;
283 //fprintf(stderr, "mpeg3demux_create_title 5 %d\n", result);
285 mpeg3io_seek(title
->fs
, 0);
286 demuxer
->read_all
= 0;
290 int mpeg3demux_print_timecodes(mpeg3_title_t
*title
, FILE *output
)
292 mpeg3demux_timecode_t
*timecode
;
293 mpeg3_t
*file
= title
->file
;
296 if(title
->timecode_table
)
298 for(i
= 0; i
< title
->timecode_table_size
; i
++)
300 timecode
= &title
->timecode_table
[i
];
302 fprintf(output
, "REGION: %ld %ld %f %f %d\n",
303 timecode
->start_byte
,
305 timecode
->start_time
,
313 int mpeg3demux_print_streams(mpeg3_demuxer_t
*demuxer
, FILE *toc
)
316 /* Print the stream information */
317 for(i
= 0; i
< MPEG3_MAX_STREAMS
; i
++)
319 if(demuxer
->astream_table
[i
])
320 fprintf(toc
, "ASTREAM: %d %d\n", i
, demuxer
->astream_table
[i
]);
322 if(demuxer
->vstream_table
[i
])
323 fprintf(toc
, "VSTREAM: %d %d\n", i
, demuxer
->vstream_table
[i
]);