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
->cell_table_size
)
22 free(title
->cell_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
;
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(mpeg3demux_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
];
52 int mpeg3_dump_title(mpeg3_title_t
*title
)
56 printf("mpeg3_dump_title path %s %llx-%llx cell_table_size %d\n",
60 title
->cell_table_size
);
61 for(i
= 0; i
< title
->cell_table_size
; i
++)
63 printf("%llx - %llx %x\n",
64 title
->cell_table
[i
].start_byte
,
65 title
->cell_table
[i
].end_byte
,
66 title
->cell_table
[i
].program
);
72 // Realloc doesn't work for some reason.
73 static void extend_cell_table(mpeg3_title_t
*title
)
75 if(!title
->cell_table
||
76 title
->cell_table_allocation
<= title
->cell_table_size
)
79 mpeg3demux_cell_t
*new_table
;
82 //printf("extend_cell_table 1\n");
83 new_allocation
= title
->cell_table_allocation
?
84 title
->cell_table_size
* 2 :
86 new_table
= calloc(1, sizeof(mpeg3demux_cell_t
) * new_allocation
);
87 //printf("extend_cell_table 1\n");
90 sizeof(mpeg3demux_cell_t
) * title
->cell_table_allocation
);
91 //printf("extend_cell_table 1 %p %d %d\n", title->cell_table, title->cell_table_allocation,
92 // (new_allocation - title->cell_table_allocation));
93 free(title
->cell_table
);
94 title
->cell_table
= new_table
;
95 //printf("extend_cell_table 2\n");
96 title
->cell_table_allocation
= new_allocation
;
97 //printf("extend_cell_table 2\n");
101 void mpeg3_new_cell(mpeg3_title_t
*title
,
108 mpeg3demux_cell_t
*new_cell
;
110 extend_cell_table(title
);
111 new_cell
= &title
->cell_table
[title
->cell_table_size
];
113 new_cell
->start_byte
= start_byte
;
114 new_cell
->end_byte
= end_byte
;
115 new_cell
->program
= program
;
116 title
->cell_table_size
++;
119 mpeg3demux_cell_t
* mpeg3_append_cell(mpeg3_demuxer_t
*demuxer
,
120 mpeg3_title_t
*title
,
128 mpeg3demux_cell_t
*new_cell
, *old_cell
;
131 extend_cell_table(title
);
133 * printf("mpeg3_append_cell 1 %d %f %d %f %d %d\n", prev_byte,
141 new_cell
= &title
->cell_table
[title
->cell_table_size
];
144 new_cell
->start_byte
= start_byte
;
146 if(title
->cell_table_size
> 0)
148 old_cell
= &title
->cell_table
[title
->cell_table_size
- 1];
149 old_cell
->end_byte
= prev_byte
;
153 title
->cell_table_size
++;
157 /* Create a title. */
158 /* Build a table of cells contained in the program stream. */
159 /* If toc is 0 just read the first and last cell. */
160 int mpeg3demux_create_title(mpeg3_demuxer_t
*demuxer
,
164 int result
= 0, done
= 0, counter_start
, counter
;
165 mpeg3_t
*file
= demuxer
->file
;
166 int64_t next_byte
, prev_byte
;
167 double next_time
, prev_time
, absolute_time
;
169 mpeg3_title_t
*title
;
170 u_int32_t test_header
= 0;
171 mpeg3demux_cell_t
*cell
= 0;
173 demuxer
->error_flag
= 0;
174 demuxer
->read_all
= 1;
176 /* Create a single title */
177 if(!demuxer
->total_titles
)
179 demuxer
->titles
[0] = mpeg3_new_title(file
, file
->fs
->path
);
180 demuxer
->total_titles
= 1;
181 mpeg3demux_open_title(demuxer
, 0);
184 title
= demuxer
->titles
[0];
185 title
->total_bytes
= mpeg3io_total_bytes(title
->fs
);
186 title
->start_byte
= 0;
187 title
->end_byte
= title
->total_bytes
;
190 //printf("mpeg3demux_create_title 1 %d %d\n", file->is_transport_stream, file->is_program_stream, toc);
192 /* Get information about file */
193 if(file
->is_transport_stream
|| file
->is_program_stream
)
195 mpeg3io_seek(title
->fs
, 0);
196 while(!done
&& !result
&& !mpeg3io_eof(title
->fs
))
198 next_byte
= mpeg3io_tell(title
->fs
);
199 result
= mpeg3_read_next_packet(demuxer
);
201 /* Just get the first bytes if not building a toc to get the stream ID's. */
202 if(next_byte
> 0x1000000 &&
203 (!cell_search
|| !toc
)) done
= 1;
205 * printf("mpeg3demux_create_title 3 %lld %d %p\n", next_byte, cell_search, toc);
206 * printf("mpeg3demux_create_title 4 %d %d %d\n",
207 * done, result, mpeg3io_eof(title->fs));
211 /* Get the last cell */
212 if(!toc
|| !cell_search
)
214 demuxer
->read_all
= 0;
215 result
= mpeg3io_seek(title
->fs
, title
->total_bytes
);
216 if(!result
) result
= mpeg3_read_prev_packet(demuxer
);
219 if(title
->cell_table
&& cell
)
221 cell
->end_byte
= title
->total_bytes
;
225 mpeg3io_seek(title
->fs
, 0);
226 demuxer
->read_all
= 0;
230 int mpeg3demux_print_cells(mpeg3_title_t
*title
, FILE *output
)
232 mpeg3demux_cell_t
*cell
;
233 mpeg3_t
*file
= title
->file
;
236 if(title
->cell_table
)
238 for(i
= 0; i
< title
->cell_table_size
; i
++)
240 cell
= &title
->cell_table
[i
];
242 fprintf(output
, "REGION: %ld %ld %f %f %d\n",
251 int mpeg3demux_print_streams(mpeg3_demuxer_t
*demuxer
, FILE *toc
)
254 /* Print the stream information */
255 for(i
= 0; i
< MPEG3_MAX_STREAMS
; i
++)
257 if(demuxer
->astream_table
[i
])
258 fprintf(toc
, "ASTREAM: %d %d\n", i
, demuxer
->astream_table
[i
]);
260 if(demuxer
->vstream_table
[i
])
261 fprintf(toc
, "VSTREAM: %d %d\n", i
, demuxer
->vstream_table
[i
]);