Merge branch 'ct' of git.pipapo.org:cinelerra-ct into ct
[cinelerra_cv/ct.git] / libmpeg3 / mpeg3title.c
blobdb08830a8397cc8ccf191a806c17ba87713c43eb
1 #include "mpeg3private.h"
2 #include "mpeg3protos.h"
3 #include "mpeg3title.h"
6 #include <stdlib.h>
7 #include <string.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);
13 title->file = file;
14 return title;
17 int mpeg3_delete_title(mpeg3_title_t *title)
19 mpeg3_delete_fs(title->fs);
20 if(title->cell_table_size)
22 free(title->cell_table);
24 free(title);
25 return 0;
29 int mpeg3_copy_title(mpeg3_title_t *dst, mpeg3_title_t *src)
31 int i;
33 mpeg3_copy_fs(dst->fs, src->fs);
34 dst->total_bytes = src->total_bytes;
35 dst->start_byte = src->start_byte;
36 dst->end_byte = src->end_byte;
38 if(src->cell_table_size)
40 dst->cell_table_allocation = src->cell_table_allocation;
41 dst->cell_table_size = src->cell_table_size;
42 dst->cell_table = calloc(1, sizeof(mpeg3_cell_t) * dst->cell_table_allocation);
44 for(i = 0; i < dst->cell_table_size; i++)
46 dst->cell_table[i] = src->cell_table[i];
49 return 0;
52 int mpeg3_dump_title(mpeg3_title_t *title)
54 int i;
56 printf("mpeg3_dump_title path %s %llx-%llx cell_table_size %d\n",
57 title->fs->path,
58 title->start_byte,
59 title->end_byte,
60 title->cell_table_size);
61 for(i = 0; i < title->cell_table_size; i++)
63 printf("%llx-%llx %llx-%llx %x\n",
64 title->cell_table[i].title_start,
65 title->cell_table[i].title_end,
66 title->cell_table[i].program_start,
67 title->cell_table[i].program_end,
68 title->cell_table[i].program);
70 return 0;
74 // Realloc doesn't work for some reason.
75 static void extend_cell_table(mpeg3_title_t *title)
77 if(!title->cell_table ||
78 title->cell_table_allocation <= title->cell_table_size)
80 long new_allocation;
81 mpeg3_cell_t *new_table;
82 int i;
84 new_allocation = title->cell_table_allocation ?
85 title->cell_table_size * 2 :
86 64;
87 new_table = calloc(1, sizeof(mpeg3_cell_t) * new_allocation);
89 if(title->cell_table)
91 memcpy(new_table,
92 title->cell_table,
93 sizeof(mpeg3_cell_t) * title->cell_table_allocation);
94 free(title->cell_table);
96 title->cell_table = new_table;
97 title->cell_table_allocation = new_allocation;
101 void mpeg3_new_cell(mpeg3_title_t *title,
102 int64_t program_start,
103 int64_t program_end,
104 int64_t title_start,
105 int64_t title_end,
106 int program)
108 mpeg3_cell_t *new_cell;
110 extend_cell_table(title);
111 new_cell = &title->cell_table[title->cell_table_size];
113 new_cell->program_start = program_start;
114 new_cell->program_end = program_end;
115 new_cell->title_start = title_start;
116 new_cell->title_end = title_end;
117 new_cell->program = program;
118 title->cell_table_size++;
121 /* Create a title and get PID's by scanning first few bytes. */
122 int mpeg3_create_title(mpeg3_demuxer_t *demuxer,
123 FILE *toc)
125 int result = 0, done = 0, counter_start, counter;
126 mpeg3_t *file = demuxer->file;
127 int64_t next_byte, prev_byte;
128 double next_time, prev_time, absolute_time;
129 long i;
130 mpeg3_title_t *title;
131 u_int32_t test_header = 0;
133 demuxer->error_flag = 0;
134 demuxer->read_all = 1;
136 /* Create a single title */
137 if(!demuxer->total_titles)
139 demuxer->titles[0] = mpeg3_new_title(file, file->fs->path);
140 demuxer->total_titles = 1;
141 mpeg3demux_open_title(demuxer, 0);
144 title = demuxer->titles[0];
145 title->total_bytes = mpeg3io_total_bytes(title->fs);
146 title->start_byte = 0;
147 title->end_byte = title->total_bytes;
149 // Create default cell
150 mpeg3_new_cell(title,
152 title->end_byte,
154 title->end_byte,
158 /* Get PID's and tracks */
159 if(file->is_transport_stream || file->is_program_stream)
161 mpeg3io_seek(title->fs, 0);
162 while(!done && !result && !mpeg3io_eof(title->fs))
164 next_byte = mpeg3io_tell(title->fs);
165 result = mpeg3_read_next_packet(demuxer);
167 /* Just get the first bytes if not building a toc to get the stream ID's. */
168 if(next_byte > 0x1000000 && !toc) done = 1;
172 mpeg3io_seek(title->fs, 0);
173 demuxer->read_all = 0;
174 return 0;
177 int mpeg3demux_print_cells(mpeg3_title_t *title, FILE *output)
179 mpeg3_cell_t *cell;
180 mpeg3_t *file = title->file;
181 int i;
183 if(title->cell_table)
185 for(i = 0; i < title->cell_table_size; i++)
187 cell = &title->cell_table[i];
189 fprintf(output, "REGION: %llx-%llx %llx-%llx %f %f %d\n",
190 cell->program_start,
191 cell->program_end,
192 cell->title_start,
193 cell->title_end,
194 cell->program);
197 return 0;
200 int mpeg3demux_print_streams(mpeg3_demuxer_t *demuxer, FILE *toc)
202 int i;
203 /* Print the stream information */
204 for(i = 0; i < MPEG3_MAX_STREAMS; i++)
206 if(demuxer->astream_table[i])
207 fprintf(toc, "ASTREAM: %d %d\n", i, demuxer->astream_table[i]);
209 if(demuxer->vstream_table[i])
210 fprintf(toc, "VSTREAM: %d %d\n", i, demuxer->vstream_table[i]);
212 return 0;