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(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
];
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 %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
);
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
)
81 mpeg3_cell_t
*new_table
;
84 new_allocation
= title
->cell_table_allocation
?
85 title
->cell_table_size
* 2 :
87 new_table
= calloc(1, sizeof(mpeg3_cell_t
) * new_allocation
);
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
,
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
,
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
;
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
,
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;
177 int mpeg3demux_print_cells(mpeg3_title_t
*title
, FILE *output
)
180 mpeg3_t
*file
= title
->file
;
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",
200 int mpeg3demux_print_streams(mpeg3_demuxer_t
*demuxer
, FILE *toc
)
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
]);