r783: Add check if sound/audio configuration is supported in dv format
[cinelerra_cv/mob.git] / libmpeg3 / mpeg2qt.c
blob141237e64de8f13fc75835ebcb309f0bf37e230a
1 #include <pthread.h>
2 #include <signal.h>
4 #include "colormodels.h"
5 #include "libmpeg3.h"
6 #include "quicktime.h"
8 #define OUTPUT_PATH "movie.mov"
9 //#define VIDEO_CODEC QUICKTIME_DIVX
10 #define VIDEO_CODEC QUICKTIME_JPEG
11 //#define VIDEO_CODEC QUICKTIME_YUV420
12 #define AUDIO_CODEC QUICKTIME_TWOS
13 //#define AUDIO_CODEC QUICKTIME_VORBIS
16 // Hack for libdv to remove glib dependancy
18 void
19 g_log (const char *log_domain,
20 int log_level,
21 const char *format,
22 ...)
26 void
27 g_logv (const char *log_domain,
28 int log_level,
29 const char *format,
30 ...)
36 // Hack for XFree86 4.1.0
38 int atexit(void (*function)(void))
40 return 0;
44 // Dump mpeg video to a quicktime movie
46 quicktime_t *output;
47 mpeg3_t *input;
48 pthread_mutex_t mutex;
49 int predicate = 0;
51 void* trap_interrupt()
53 pthread_mutex_lock(&mutex);
54 if(!predicate)
56 predicate = 1;
58 printf("interrupt trapped 1\n");
59 quicktime_close(output);
60 printf("interrupt trapped 2\n");
61 exit(0);
62 // Can't join any threads in the mpeg library which may have been interrupted
63 // before unblocking.
64 // mpeg3_close(input);
72 int main(int argc, char *argv[])
74 int frame_count = -1;
75 char *row_pointers[3];
76 int do_audio = 0;
77 int do_video = 0;
78 int channels = 0;
79 long afragment = 65536;
80 float **audio_output;
81 int layer = 0;
82 int astream = 0;
83 char input_path[1024];
84 char output_path[1024];
85 int i;
86 long current_frame = 0;
87 long current_sample = 0;
89 pthread_mutex_init(&mutex, NULL);
90 signal(SIGINT, trap_interrupt);
92 input_path[0] = 0;
93 output_path[0] = 0;
95 if(argc < 3)
97 printf("Usage: %s [-a] [-v] <mpeg file> <output movie> [frame count]\n", argv[0]);
98 exit(1);
101 for(i = 1; i < argc; i++)
103 if(!strcmp(argv[i], "-a"))
105 do_audio = 1;
107 else
108 if(!strcmp(argv[i], "-v"))
110 do_video = 1;
112 else
113 if(!input_path[0])
115 strcpy(input_path, argv[i]);
117 else
118 if(!output_path[0])
120 strcpy(output_path, argv[i]);
122 else
123 frame_count = atol(argv[i]);
126 //printf("main 1\n");
127 if(!(input = mpeg3_open(input_path)))
129 exit(1);
131 //printf("main 1\n");
133 if(!(output = quicktime_open(output_path, 0, 1)))
135 exit(1);
137 //printf("main 1\n");
139 if(do_video)
141 if(!mpeg3_total_vstreams(input))
143 do_video = 0;
145 else
147 quicktime_set_video(output,
149 mpeg3_video_width(input, layer),
150 mpeg3_video_height(input, layer),
151 mpeg3_frame_rate(input, layer),
152 VIDEO_CODEC);
153 quicktime_set_jpeg(output, 80, 0);
156 //printf("main 1\n");
158 if(do_audio)
160 if(!mpeg3_total_astreams(input))
162 do_audio = 0;
164 else
166 int i;
167 channels = mpeg3_audio_channels(input, astream);
169 quicktime_set_audio(output,
170 channels,
171 mpeg3_sample_rate(input, 0),
172 24,
173 AUDIO_CODEC);
175 audio_output = malloc(sizeof(float*) * channels);
176 for(i = 0; i < channels; i++)
177 audio_output[i] = malloc(sizeof(float) * afragment);
180 //printf("main 1\n");
182 // quicktime_set_jpeg(output, 100, 0);
183 mpeg3_set_mmx(input, 0);
185 while((!(do_video && mpeg3_end_of_video(input, layer)) ||
186 !(do_audio && mpeg3_end_of_audio(input, astream))) &&
187 (current_frame < frame_count || frame_count < 0))
189 //printf("%d %d\n", mpeg3_end_of_video(input, layer), mpeg3_end_of_audio(input, astream));
190 if(do_audio)
192 if(!mpeg3_end_of_audio(input, astream))
194 int fragment = afragment;
195 int i, j, k;
197 i = astream;
198 k = 0;
199 for(j = 0; j < mpeg3_audio_channels(input, i); j++, k++)
201 if(j == 0)
202 mpeg3_read_audio(input,
203 audio_output[k], /* Pointer to pre-allocated buffer of floats */
204 0, /* Pointer to pre-allocated buffer of int16's */
205 j, /* Channel to decode */
206 fragment, /* Number of samples to decode */
208 else
209 mpeg3_reread_audio(input,
210 audio_output[k], /* Pointer to pre-allocated buffer of floats */
211 0, /* Pointer to pre-allocated buffer of int16's */
212 j, /* Channel to decode */
213 fragment, /* Number of samples to decode */
219 quicktime_encode_audio(output,
221 audio_output,
222 fragment);
224 current_sample += fragment;
225 if(!do_video)
227 printf(" %d samples written\r", current_sample);
228 fflush(stdout);
231 else
232 current_sample += afragment;
235 if(do_video)
237 if(!mpeg3_end_of_video(input, layer))
239 int fragment;
240 if(do_audio)
241 fragment = (long)((double)current_sample /
242 mpeg3_sample_rate(input, 0) *
243 mpeg3_frame_rate(input, layer) -
244 current_frame);
245 else
246 fragment = 1;
248 for(i = 0; i < fragment && !mpeg3_end_of_video(input, layer); i++)
250 mpeg3_read_yuvframe_ptr(input,
251 &row_pointers[0],
252 &row_pointers[1],
253 &row_pointers[2],
254 layer);
256 switch(mpeg3_colormodel(input, layer))
258 case MPEG3_YUV420P:
259 quicktime_set_cmodel(output, BC_YUV420P);
260 break;
261 case MPEG3_YUV422P:
262 quicktime_set_cmodel(output, BC_YUV422P);
263 break;
265 quicktime_encode_video(output,
266 (unsigned char **)row_pointers,
269 current_frame++;
270 printf(" %d frames written\r", current_frame);
271 fflush(stdout);
276 printf("main 2\n");
278 printf("\n");
279 quicktime_close(output);
280 mpeg3_close(input);