r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / libmpeg3 / mpeg3dump.c
blobab3b714277c3eea2df29ea881f4ca11656842f2a
1 #include "libmpeg3.h"
2 #include "mpeg3protos.h"
3 #include <stdlib.h>
5 #define BUFSIZE 65536
7 void test_32bit_overflow(char *outfile, int (*out_counter), FILE *(*out))
9 if(ftell(*out) > 0x7f000000)
11 char string[1024];
12 fclose(*out);
13 sprintf(string, "%s%03d", outfile, ++(*out_counter));
14 (*out) = fopen(string, "wb");
18 int main(int argc, char *argv[])
20 mpeg3_t *file;
21 int i, j, result = 0;
22 unsigned char *output, **output_rows;
23 int out_counter = 0;
24 float *audio_output_f;
25 unsigned char *audio_output_i;
26 long total_samples = 0;
27 FILE *out;
28 char outfile[1024];
29 int decompress_audio = 0, decompress_video = 0;
30 int audio_track = 0;
31 /* Print cell offsets */
32 int print_offsets = 1;
33 int print_pids = 1;
35 outfile[0] = 0;
36 if(argc < 2)
38 printf(
39 "Dump information or extract audio to a 24 bit pcm file.\n"
40 "Example: dump -a0 outputfile.pcm take1.vob\n"
42 exit(1);
45 for(i = 1; i < argc; i++)
47 if(!strncmp(argv[i], "-a", 2))
49 // Check for track number
50 if(strlen(argv[i]) > 2)
52 audio_track = atol(argv[i] + 2);
55 // Check for filename
56 if(i + 1 < argc)
58 strcpy(outfile, argv[++i]);
59 decompress_audio = 1;
61 else
63 fprintf(stderr, "-a must be paired with a filename.\n");
64 exit(1);
67 // Check if file exists
68 if(out = fopen(outfile, "r"))
70 fprintf(stderr, "%s exists.\n", outfile);
71 exit(1);
76 file = mpeg3_open(argv[argc - 1]);
77 if(outfile[0])
79 out = fopen(outfile, "wb");
89 if(file)
92 // Audio streams
93 fprintf(stderr, "have_mmx=%d\n", file->have_mmx);
94 fprintf(stderr, "total_astreams=%d\n", mpeg3_total_astreams(file));
96 for(i = 0; i < mpeg3_total_astreams(file); i++)
98 fprintf(stderr, " Stream 0x%04x: channels=%d rate=%d samples=%ld format=%s\n",
99 file->atrack[i]->demuxer->astream,
100 mpeg3_audio_channels(file, i),
101 mpeg3_sample_rate(file, i),
102 mpeg3_audio_samples(file, i),
103 mpeg3_audio_format(file, i));
105 if(print_offsets)
107 fprintf(stderr, "total_sample_offsets=%d\n", file->atrack[i]->total_sample_offsets);
108 for(j = 0; j < file->atrack[i]->total_sample_offsets; j++)
110 fprintf(stderr, "%llx ", file->atrack[i]->sample_offsets[j]);
111 if(j > 0 && !(j % 8)) fprintf(stderr, "\n");
113 fprintf(stderr, "\n");
119 // Video streams
120 fprintf(stderr, "total_vstreams=%d\n", mpeg3_total_vstreams(file));
122 for(i = 0; i < mpeg3_total_vstreams(file); i++)
124 fprintf(stderr, " Stream 0x%04x: w=%d h=%d framerate=%0.3f frames=%ld coding=%s\n",
125 file->vtrack[i]->demuxer->vstream,
126 mpeg3_video_width(file, i),
127 mpeg3_video_height(file, i),
128 mpeg3_frame_rate(file, i),
129 mpeg3_video_frames(file, i),
130 mpeg3_colormodel(file, i) == MPEG3_YUV420P ? "420" : "422");
132 if(print_offsets)
134 fprintf(stderr, "total_frame_offsets=%d\n", file->vtrack[i]->total_frame_offsets);
135 for(j = 0; j < file->vtrack[i]->total_frame_offsets; j++)
137 fprintf(stderr, "%d=%llx ", j, file->vtrack[i]->frame_offsets[j]);
138 if(j > 0 && !(j % 8)) fprintf(stderr, "\n");
140 fprintf(stderr, "\n");
142 fprintf(stderr, "total_keyframe_numbers=%d\n", file->vtrack[i]->total_keyframe_numbers);
143 for(j = 0; j < file->vtrack[i]->total_keyframe_numbers; j++)
145 fprintf(stderr, "%lld ", file->vtrack[i]->keyframe_numbers[j]);
146 if(j > 0 && !(j % 8)) fprintf(stderr, "\n");
148 fprintf(stderr, "\n");
153 // Titles
154 fprintf(stderr, "total_titles=%d\n", file->demuxer->total_titles);
155 for(i = 0; i < file->demuxer->total_titles; i++)
157 fprintf(stderr, " Title path=%s total_bytes=%llx cell_table_size=%d\n",
158 file->demuxer->titles[i]->fs->path,
159 file->demuxer->titles[i]->total_bytes,
160 file->demuxer->titles[i]->timecode_table_size);
162 if(print_offsets)
164 for(j = 0; j < file->demuxer->titles[i]->timecode_table_size; j++)
165 fprintf(stderr, " Cell: %llx-%llx program=%d\n",
166 file->demuxer->titles[i]->timecode_table[j].start_byte,
167 file->demuxer->titles[i]->timecode_table[j].end_byte,
168 file->demuxer->titles[i]->timecode_table[j].program);
174 // Pids
175 if(print_pids)
177 mpeg3_demuxer_t *demuxer = file->demuxer;
178 printf("Total PIDs=%d\n", demuxer->total_pids);
179 for(i = 0; i < demuxer->total_pids; i++)
181 printf("0x%04x ", demuxer->pid_table[i]);
183 printf("\n");
188 // Write audio
189 if(decompress_audio)
191 mpeg3_set_cpus(file, 2);
192 audio_output_f = malloc(BUFSIZE * sizeof(float));
193 audio_output_i = malloc(BUFSIZE * 3 * mpeg3_audio_channels(file, audio_track));
195 //printf("%d\n", mpeg3_end_of_audio(file, audio_track));
196 while(!mpeg3_end_of_audio(file, audio_track) && !result)
198 test_32bit_overflow(outfile, &out_counter, &out);
200 for(i = 0; i < mpeg3_audio_channels(file, audio_track); i++)
202 if(i == 0)
203 result = mpeg3_read_audio(file,
204 audio_output_f,
207 BUFSIZE,
208 audio_track);
209 else
210 result = mpeg3_reread_audio(file,
211 audio_output_f, /* Pointer to pre-allocated buffer of floats */
212 0, /* Pointer to pre-allocated buffer of int16's */
213 i, /* Channel to decode */
214 BUFSIZE, /* Number of samples to decode */
215 audio_track);
217 for(j = 0; j < BUFSIZE; j++)
219 int sample = audio_output_f[j] * 0x7fffff;
220 unsigned char *output_i = audio_output_i + j * 3 * mpeg3_audio_channels(file, audio_track) + i * 3;
221 if(sample > 0x7fffff)
222 sample = 0x7fffff;
223 else
224 if(sample < -0x7fffff)
225 sample = -0x7fffff;
226 *output_i++ = (sample & 0xff0000) >> 16;
227 *output_i++ = (sample & 0xff00) >> 8;
228 *output_i = sample & 0xff;
233 result = !fwrite(audio_output_i, BUFSIZE * 3 * mpeg3_audio_channels(file, audio_track), 1, out);
238 * audio_output_i = malloc(BUFSIZE * 2 * mpeg3_audio_channels(file, 0));
239 * mpeg3_seek_percentage(file, 0.1);
240 * result = mpeg3_read_audio(file, 0, audio_output_i, 1, BUFSIZE, 0);
244 * output = malloc(mpeg3_video_width(file, 0) * mpeg3_video_height(file, 0) * 3 + 4);
245 * output_rows = malloc(sizeof(unsigned char*) * mpeg3_video_height(file, 0));
246 * for(i = 0; i < mpeg3_video_height(file, 0); i++)
247 * output_rows[i] = &output[i * mpeg3_video_width(file, 0) * 3];
248 * printf("dump 1\n");
249 * mpeg3_seek_percentage(file, 0.375);
250 * result = mpeg3_read_frame(file,
251 * output_rows,
252 * 0,
253 * 0,
254 * mpeg3_video_width(file, 0),
255 * mpeg3_video_height(file, 0),
256 * mpeg3_video_width(file, 0),
257 * mpeg3_video_height(file, 0),
258 * MPEG3_RGB888,
259 * 0);
260 * printf("dump 2\n");
263 mpeg3_close(file);
265 return 0;