1 /* Concatenate elementary streams */
2 /* Mpeg3cat is useful for extracting elementary streams from program streams. */
9 #define BUFFER_SIZE 0x100000
11 unsigned char *output_buffer = 0;
12 int64_t output_buffer_size = 0;
13 int64_t output_scan_offset = 0;
19 // Check for first start code before writing out
20 static int write_output(unsigned char *data, int size, zmpeg3_t *file)
22 // Already got start code so write data directly
23 // Or user doesn't want to extract an elementary stream
24 if( got_start || (!do_audio && !do_video) || file->is_bd() ) {
25 return fwrite(data, size, 1, out);
28 // Buffer until start code
29 uint32_t zcode = 0xffffffff;
30 int new_allocation = output_buffer_size + size;
31 unsigned char *new_output_buffer = new unsigned char[new_allocation];
32 memcpy(new_output_buffer, output_buffer, output_buffer_size);
33 memcpy(new_output_buffer + output_buffer_size, data, size);
34 if( output_buffer ) delete [] output_buffer;
35 output_buffer = new_output_buffer;
36 output_buffer_size = new_allocation;
38 if( output_buffer_size >= 4 ) {
40 while( output_scan_offset < output_buffer_size && !got_start ) {
41 zcode = (zcode << 8) | output_buffer[output_scan_offset++];
42 if( zcode == zmpeg3_t::SEQUENCE_START_CODE ) {
47 output_scan_offset -= 4;
50 // Only scan for AC3 start code since we can't scan for mp2 start codes.
51 // It must occur in the first 2048 bytes or we give up.
52 while( output_scan_offset < output_buffer_size &&
53 output_scan_offset < 2048 && !got_start ) {
54 zcode = ((zcode & 0xff) << 8) | output_buffer[output_scan_offset++];
55 if( zcode == zmpeg3_t::AC3_START_CODE ) {
61 output_scan_offset -= 2;
62 else if( output_scan_offset >= 2048 ) {
63 output_scan_offset = 0;
67 output_scan_offset -= 2;
71 return fwrite(output_buffer + output_scan_offset,
72 output_buffer_size - output_scan_offset, 1, out);
80 int main(int argc, char *argv[])
82 char inpath[1024], outpath[1024], newpath[1024];
84 int total_infiles = 0;
87 int current_file, current_output_file = 0, i;
88 unsigned char buffer[BUFFER_SIZE];
91 int64_t total_frames = 0;
93 int64_t total_written = 0;
96 fprintf(stderr, "Concatenate elementary streams or demultiplex a program stream.\n"
97 "Usage: mpeg3cat -[av0123456789] <infile> [infile...] > <outfile>\n\n"
98 "Example: Concatenate 2 video files: mpeg3cat xena1.m2v xena2.m2v > xena.m2v\n"
99 " Extract audio stream 0: mpeg3cat -a0 xena.vob > war_cry.ac3\n");
104 for( i=1; i < argc; ++i ) {
105 if( argv[i][0] == '-' ) {
106 if( argv[i][1] != 'a' && argv[i][1] != 'v' && argv[i][1] != 'o' ) {
107 fprintf(stderr, "invalid option %s\n", argv[i]);
111 if( argv[i][1] == 'o' ) {
112 // Check for filename
114 strcpy(outpath, argv[++i]);
117 fprintf(stderr, "-o requires an output file\n");
121 // Check if file exists
122 if( (out = fopen(outpath, "r")) ) {
123 fprintf(stderr, "%s exists.\n", outpath);
128 if( argv[i][1] == 'a' ) do_audio = 1;
129 else if( argv[i][1] == 'v' ) do_video = 1;
130 if( argv[i][2] != 0 ) {
131 stream = argv[i][2] - '0';
136 inpaths[total_infiles++] = argv[i];
141 if( !(out = fopen(outpath, "wb")) ) {
142 fprintf(stderr, "Failed to open %s for writing\n", outpath);
149 for( current_file=0; current_file < total_infiles; ++current_file ) {
150 strcpy(inpath, inpaths[current_file]);
152 if( !(in = mpeg3_open(inpath, &error)) ) {
153 fprintf(stderr, "Skipping %s\n", inpath);
157 /* output elementary audio stream */
158 if( (mpeg3_has_audio(in) && in->is_audio_stream() ) ||
159 (do_audio && !in->is_audio_stream() && !in->is_video_stream())) {
161 /* Add audio stream to end */
162 if( stream >= in->total_astreams() ) {
163 fprintf(stderr, "No audio stream %d\n", stream);
167 in->atrack[stream]->demuxer->seek_byte(zmpeg3_t::START_BYTE);
168 // in->atrack[stream]->audio->astream->refill();
169 //printf("mpeg3cat 1\n");
170 while( !mpeg3_read_audio_chunk(in, buffer,
171 &output_size, BUFFER_SIZE, stream) ) {
172 //printf("mpeg3cat 2 0x%x\n", output_size);
173 result = !write_output(buffer, output_size, in);
175 perror("write audio chunk");
179 //printf("mpeg3cat 3\n");
181 /* Output elementary video stream */
182 else if( (mpeg3_has_video(in) && in->is_video_stream()) ||
183 (do_video && !in->is_video_stream() && !in->is_audio_stream())) {
184 /* Add video stream to end */
185 int64_t hour, minute, second, frame;
190 if( stream >= in->total_vstreams() ) {
191 fprintf(stderr, "No video stream %d\n", stream);
195 in->vtrack[stream]->demuxer->seek_byte(zmpeg3_t::START_BYTE);
196 in->vtrack[stream]->video->vstream->refill();
198 while( !mpeg3_read_video_chunk(in, buffer, &output_size,
199 BUFFER_SIZE, stream) && output_size >= 4 ) {
200 zcode = (uint32_t)buffer[output_size - 4] << 24;
201 zcode |= (uint32_t)buffer[output_size - 3] << 16;
202 zcode |= (uint32_t)buffer[output_size - 2] << 8;
203 zcode |= (uint32_t)buffer[output_size - 1];
205 /* Got a frame at the end of this buffer. */
206 if( zcode == zmpeg3_t::PICTURE_START_CODE ) {
209 else if( zcode == zmpeg3_t::SEQUENCE_END_CODE ) {
210 /* Got a sequence end code at the end of this buffer. */
214 zcode = (uint32_t)buffer[0] << 24;
215 zcode |= (uint32_t)buffer[1] << 16;
216 zcode |= (uint32_t)buffer[2] << 8;
221 if( zcode == zmpeg3_t::SEQUENCE_START_CODE && current_output_file > 0 ) {
222 /* Skip the sequence start code */
224 while( i < output_size && zcode != zmpeg3_t::GOP_START_CODE ) {
226 zcode |= buffer[i++];
232 /* Search for GOP header to fix */
233 zcode = (uint32_t)buffer[i++] << 24;
234 zcode |= (uint32_t)buffer[i++] << 16;
235 zcode |= (uint32_t)buffer[i++] << 8;
236 zcode |= buffer[i++];
237 while( i < output_size && zcode != zmpeg3_t::GOP_START_CODE ) {
239 zcode |= buffer[i++];
242 if( zcode == zmpeg3_t::GOP_START_CODE ) {
243 /* Get the time code */
244 zcode = (uint32_t)buffer[i] << 24;
245 zcode |= (uint32_t)buffer[i + 1] << 16;
246 zcode |= (uint32_t)buffer[i + 2] << 8;
247 zcode |= (uint32_t)buffer[i + 3];
249 hour = zcode >> 26 & 0x1f;
250 minute = zcode >> 20 & 0x3f;
251 second = zcode >> 13 & 0x3f;
252 frame = zcode >> 7 & 0x3f;
254 /* int64_t gop_frame = (int64_t)(hour * 3600 * mpeg3_frame_rate(in, stream) +
255 minute * 60 * mpeg3_frame_rate(in, stream) +
256 second * mpeg3_frame_rate(in, stream) +
258 /* fprintf(stderr, "old: %02d:%02d:%02d:%02d ", hour, minute, second, frame); */
259 /* Write a new time code */
260 hour = (int64_t)((float)(total_frames - 1) / mpeg3_frame_rate(in, stream) / 3600);
261 carry = hour * 3600 * mpeg3_frame_rate(in, stream);
262 minute = (int64_t)((float)(total_frames - 1 - carry) / mpeg3_frame_rate(in, stream) / 60);
263 carry += minute * 60 * mpeg3_frame_rate(in, stream);
264 second = (int64_t)((float)(total_frames - 1 - carry) / mpeg3_frame_rate(in, stream));
265 carry += second * mpeg3_frame_rate(in, stream);
266 frame = (total_frames - 1 - carry);
268 buffer[i] = ((zcode >> 24) & 0x80) | (hour << 2) | (minute >> 4);
269 buffer[i + 1] = ((zcode >> 16) & 0x08) | ((minute & 0xf) << 4) | (second >> 3);
270 buffer[i + 2] = ((second & 0x7) << 5) | (frame >> 1);
271 buffer[i + 3] = (zcode & 0x7f) | ((frame & 0x1) << 7);
272 /* fprintf(stderr, "new: %02d:%02d:%02d:%02d\n", hour, minute, second, frame); */
276 /* Test 32 bit overflow */
278 if( ftell(out) > 0x7f000000 ) {
281 sprintf(newpath, "%s%03d", outpath, out_counter);
282 if( !(out = fopen(newpath, "wb")) ) {
283 fprintf(stderr, "Couldn't open %s for writing.\n", newpath);
289 * fprintf(stderr, "mpeg3cat 5 %02x %02x %02x %02x\n",
290 * (buffer + offset)[0],
291 * (buffer + offset)[1],
292 * (buffer + offset)[2],
293 * (buffer + offset)[3]);
296 /* Write the frame */
297 result = !write_output(buffer + offset, output_size - offset, in);
299 perror("write video chunk");
305 /* Output program stream */
306 /* In real life, program streams ended up having discontinuities in time codes */
307 /* so this isn't being maintained anymore. */
308 if( in->is_program_stream() ) {
309 zdemuxer_t *demuxer = in->vtrack[0]->demuxer;
312 /* Append program stream with no changes */
313 demuxer->read_all = 1;
314 demuxer->seek_byte(zmpeg3_t::START_BYTE);
317 result = demuxer->seek_phys();
319 demuxer->zdata.size = 0;
320 demuxer->zvideo.size = 0;
321 demuxer->zaudio.size = 0;
322 result = demuxer->read_program();
324 fprintf(stderr, "Hit end of data in %s\n", inpath);
328 // Read again and decrypt it
329 unsigned char raw_data[0x10000];
332 ztitle_t *title = demuxer->titles[demuxer->current_title];
333 int64_t temp_offset = title->fs->tell();
334 int64_t decryption_offset =
335 demuxer->last_packet_decryption - demuxer->last_packet_start;
336 raw_size = demuxer->last_packet_end - demuxer->last_packet_start;
338 title->fs->seek(demuxer->last_packet_start);
339 title->fs->read_data(raw_data, raw_size);
340 title->fs->seek(temp_offset);
342 if( decryption_offset > 0 &&
343 decryption_offset < raw_size &&
344 raw_data[decryption_offset] & 0x30 ) {
345 if( title->fs->css.decrypt_packet(raw_data, 0) ) {
346 fprintf(stderr, "get_ps_pes_packet: Decryption not available\n");
349 raw_data[decryption_offset] &= 0xcf;
355 result = !write_output(raw_data, raw_size, in);
356 total_written += raw_size;
357 if( result) fprintf(stderr, "write program stream: %s\n", strerror(errno) );
362 /* No transport stream support, since these can be catted */
363 fprintf(stderr, "No catting of transport streams.\n");
371 current_output_file++;
375 /* Terminate output */
376 if( current_output_file > 0 && do_video ) {
377 /* Write new end of sequence */
378 /* Not very useful */
379 buffer[0] = (zmpeg3_t::SEQUENCE_END_CODE >> 24) & 0xff;
380 buffer[1] = (zmpeg3_t::SEQUENCE_END_CODE >> 16) & 0xff;
381 buffer[2] = (zmpeg3_t::SEQUENCE_END_CODE >> 8) & 0xff;
382 buffer[3] = (zmpeg3_t::SEQUENCE_END_CODE >> 0) & 0xff;
383 result = !write_output(buffer, 4, in);
386 if( outpath[0]) fclose(out );