Merge branch 'ct' of git.pipapo.org:cinelerra-ct into ct
[cinelerra_cv/ct.git] / libmpeg3 / mpeg3dump.c
blob9e7c72d80228c850773d271aa4b11a6e6807a7e6
1 #include "libmpeg3.h"
2 #include "mpeg3protos.h"
3 #include <stdlib.h>
4 #include <string.h>
6 #define BUFSIZE 65536
8 void test_32bit_overflow(char *outfile, int (*out_counter), FILE *(*out))
10 if(ftell(*out) > 0x7f000000)
12 char string[1024];
13 fclose(*out);
14 sprintf(string, "%s%03d", outfile, ++(*out_counter));
15 (*out) = fopen(string, "wb");
19 int main(int argc, char *argv[])
21 mpeg3_t *file;
22 int i, j, result = 0;
23 unsigned char *output, **output_rows;
24 int out_counter = 0;
25 float *audio_output_f;
26 unsigned char *audio_output_i;
27 long total_samples = 0;
28 FILE *out;
29 char outfile[1024];
30 int decompress_audio = 0, decompress_video = 0;
31 int audio_track = 0;
32 /* Print cell offsets */
33 int print_offsets = 0;
34 int print_pids = 1;
36 outfile[0] = 0;
37 if(argc < 2)
39 printf(
40 "Dump information or extract audio to a 24 bit pcm file.\n"
41 "Example: dump -a0 outputfile.pcm take1.vob\n"
43 exit(1);
46 for(i = 1; i < argc; i++)
48 if(!strncmp(argv[i], "-a", 2))
50 // Check for track number
51 if(strlen(argv[i]) > 2)
53 audio_track = atol(argv[i] + 2);
56 // Check for filename
57 if(i + 1 < argc)
59 strcpy(outfile, argv[++i]);
60 decompress_audio = 1;
62 else
64 fprintf(stderr, "-a must be paired with a filename.\n");
65 exit(1);
68 // Check if file exists
69 if(out = fopen(outfile, "r"))
71 fprintf(stderr, "%s exists.\n", outfile);
72 exit(1);
77 int error = 0;
78 file = mpeg3_open(argv[argc - 1], &error);
79 if(outfile[0])
81 out = fopen(outfile, "wb");
91 if(file)
94 // Audio streams
95 fprintf(stderr, "total_astreams=%d\n", mpeg3_total_astreams(file));
97 for(i = 0; i < mpeg3_total_astreams(file); i++)
99 fprintf(stderr, " Stream 0x%04x: channels=%d rate=%d samples=%ld format=%s\n",
100 file->atrack[i]->demuxer->astream,
101 mpeg3_audio_channels(file, i),
102 mpeg3_sample_rate(file, i),
103 mpeg3_audio_samples(file, i),
104 mpeg3_audio_format(file, i));
106 if(print_offsets)
108 fprintf(stderr, "total_sample_offsets=%d\n", file->atrack[i]->total_sample_offsets);
109 for(j = 0; j < file->atrack[i]->total_sample_offsets; j++)
111 fprintf(stderr, "%llx ", file->atrack[i]->sample_offsets[j]);
112 if(j > 0 && !(j % 8)) fprintf(stderr, "\n");
114 fprintf(stderr, "\n");
120 // Video streams
121 fprintf(stderr, "total_vstreams=%d\n", mpeg3_total_vstreams(file));
123 for(i = 0; i < mpeg3_total_vstreams(file); i++)
125 fprintf(stderr, " Stream 0x%04x: w=%d h=%d framerate=%0.3f frames=%ld coding=%s\n",
126 file->vtrack[i]->demuxer->vstream,
127 mpeg3_video_width(file, i),
128 mpeg3_video_height(file, i),
129 mpeg3_frame_rate(file, i),
130 mpeg3_video_frames(file, i),
131 mpeg3_colormodel(file, i) == MPEG3_YUV420P ? "420" : "422");
133 if(print_offsets)
135 fprintf(stderr, "total_frame_offsets=%d\n", file->vtrack[i]->total_frame_offsets);
136 for(j = 0; j < file->vtrack[i]->total_frame_offsets; j++)
138 fprintf(stderr, "%d=%llx ", j, file->vtrack[i]->frame_offsets[j]);
139 if(j > 0 && !(j % 8)) fprintf(stderr, "\n");
141 fprintf(stderr, "\n");
143 fprintf(stderr, "total_keyframe_numbers=%d\n", file->vtrack[i]->total_keyframe_numbers);
144 for(j = 0; j < file->vtrack[i]->total_keyframe_numbers; j++)
146 fprintf(stderr, "%lld ", file->vtrack[i]->keyframe_numbers[j]);
147 if(j > 0 && !(j % 8)) fprintf(stderr, "\n");
149 fprintf(stderr, "\n");
153 // Subtitle tracks
154 printf("total subtitle tracks: %d\n", mpeg3_subtitle_tracks(file));
155 for(i = 0; i < mpeg3_subtitle_tracks(file); i++)
157 mpeg3_strack_t *strack = file->strack[i];
158 printf(" stream: 0x%02x total_offsets: %d\n",
159 strack->id,
160 strack->total_offsets);
161 if(print_offsets)
163 for(j = 0; j < strack->total_offsets; j++)
165 printf("%llx ", strack->offsets[j]);
167 printf("\n");
171 // Titles
172 fprintf(stderr, "total_titles=%d\n", file->demuxer->total_titles);
173 for(i = 0; i < file->demuxer->total_titles; i++)
175 fprintf(stderr, " Title path=%s total_bytes=%llx cell_table_size=%d\n",
176 file->demuxer->titles[i]->fs->path,
177 file->demuxer->titles[i]->total_bytes,
178 file->demuxer->titles[i]->cell_table_size);
180 if(print_offsets)
182 for(j = 0; j < file->demuxer->titles[i]->cell_table_size; j++)
183 fprintf(stderr, " Cell: %llx-%llx %llx-%llx program=%d\n",
184 file->demuxer->titles[i]->cell_table[j].program_start,
185 file->demuxer->titles[i]->cell_table[j].program_end,
186 file->demuxer->titles[i]->cell_table[j].title_start,
187 file->demuxer->titles[i]->cell_table[j].title_end,
188 file->demuxer->titles[i]->cell_table[j].program);
194 // Pids
195 if(print_pids)
197 mpeg3_demuxer_t *demuxer = file->demuxer;
198 printf("Total PIDs=%d\n", demuxer->total_pids);
199 for(i = 0; i < demuxer->total_pids; i++)
201 printf("0x%04x ", demuxer->pid_table[i]);
203 printf("\n");
208 // Write audio
209 if(decompress_audio)
211 mpeg3_set_cpus(file, 2);
212 audio_output_f = malloc(BUFSIZE * sizeof(float));
213 audio_output_i = malloc(BUFSIZE * 3 * mpeg3_audio_channels(file, audio_track));
215 //printf("%d\n", mpeg3_end_of_audio(file, audio_track));
216 while(!mpeg3_end_of_audio(file, audio_track) && !result)
218 test_32bit_overflow(outfile, &out_counter, &out);
220 for(i = 0; i < mpeg3_audio_channels(file, audio_track); i++)
222 if(i == 0)
223 result = mpeg3_read_audio(file,
224 audio_output_f,
227 BUFSIZE,
228 audio_track);
229 else
230 result = mpeg3_reread_audio(file,
231 audio_output_f, /* Pointer to pre-allocated buffer of floats */
232 0, /* Pointer to pre-allocated buffer of int16's */
233 i, /* Channel to decode */
234 BUFSIZE, /* Number of samples to decode */
235 audio_track);
237 for(j = 0; j < BUFSIZE; j++)
239 int sample = audio_output_f[j] * 0x7fffff;
240 unsigned char *output_i = audio_output_i + j * 3 * mpeg3_audio_channels(file, audio_track) + i * 3;
241 if(sample > 0x7fffff)
242 sample = 0x7fffff;
243 else
244 if(sample < -0x7fffff)
245 sample = -0x7fffff;
246 *output_i++ = (sample & 0xff0000) >> 16;
247 *output_i++ = (sample & 0xff00) >> 8;
248 *output_i = sample & 0xff;
253 result = !fwrite(audio_output_i, BUFSIZE * 3 * mpeg3_audio_channels(file, audio_track), 1, out);
258 * audio_output_i = malloc(BUFSIZE * 2 * mpeg3_audio_channels(file, 0));
259 * mpeg3_seek_percentage(file, 0.1);
260 * result = mpeg3_read_audio(file, 0, audio_output_i, 1, BUFSIZE, 0);
264 * output = malloc(mpeg3_video_width(file, 0) * mpeg3_video_height(file, 0) * 3 + 4);
265 * output_rows = malloc(sizeof(unsigned char*) * mpeg3_video_height(file, 0));
266 * for(i = 0; i < mpeg3_video_height(file, 0); i++)
267 * output_rows[i] = &output[i * mpeg3_video_width(file, 0) * 3];
268 * printf("dump 1\n");
269 * mpeg3_seek_percentage(file, 0.375);
270 * result = mpeg3_read_frame(file,
271 * output_rows,
272 * 0,
273 * 0,
274 * mpeg3_video_width(file, 0),
275 * mpeg3_video_height(file, 0),
276 * mpeg3_video_width(file, 0),
277 * mpeg3_video_height(file, 0),
278 * MPEG3_RGB888,
279 * 0);
280 * printf("dump 2\n");
283 mpeg3_close(file);
285 return 0;