add lv2 plugin interface
[goodguy/history.git] / cinelerra-5.1 / libzmpeg3 / mpeg2qt.c
1 #include <pthread.h>
2 #include <signal.h>
3
4 #include "colormodels.h"
5 #include "libmpeg3.h"
6 #include "quicktime.h"
7
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
14
15
16 // Hack for libdv to remove glib dependancy
17
18 void
19 g_log (const char    *log_domain,
20        int  log_level,
21        const char    *format,
22        ...)
23 {
24 }
25
26 void
27 g_logv (const char    *log_domain,
28        int  log_level,
29        const char    *format,
30        ...)
31 {
32 }
33
34
35
36 // Hack for XFree86 4.1.0
37
38 int atexit(void (*function)(void))
39 {
40         return 0;
41 }
42
43
44 // Dump mpeg video to a quicktime movie
45
46 quicktime_t *output;
47 mpeg3_t *input;
48 pthread_mutex_t mutex;
49 int predicate = 0;
50
51 void* trap_interrupt()
52 {
53         pthread_mutex_lock(&mutex);
54         if(!predicate)
55         {
56                 predicate = 1;
57
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);
65         }
66 }
67
68
69
70
71
72 int main(int argc, char *argv[])
73 {
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;
88
89         pthread_mutex_init(&mutex, NULL);
90         signal(SIGINT, trap_interrupt);
91
92         input_path[0] = 0;
93         output_path[0] = 0;
94
95         if(argc < 3)
96         {
97                 printf("Usage: %s [-a] [-v] <mpeg file> <output movie> [frame count]\n", argv[0]);
98                 exit(1);
99         }
100
101         for(i = 1; i < argc; i++)
102         {
103                 if(!strcmp(argv[i], "-a"))
104                 {
105                         do_audio = 1;
106                 }
107                 else
108                 if(!strcmp(argv[i], "-v"))
109                 {
110                         do_video = 1;
111                 }
112                 else
113                 if(!input_path[0])
114                 {
115                         strcpy(input_path, argv[i]);
116                 }
117                 else
118                 if(!output_path[0])
119                 {
120                         strcpy(output_path, argv[i]);
121                 }
122                 else
123                         frame_count = atol(argv[i]);
124         }
125
126 //printf("main 1\n");
127         if(!(input = mpeg3_open(input_path)))
128         {
129                 exit(1);
130         }
131 //printf("main 1\n");
132
133         if(!(output = quicktime_open(output_path, 0, 1)))
134         {
135                 exit(1);
136         }
137 //printf("main 1\n");
138
139         if(do_video)
140         {
141                 if(!mpeg3_total_vstreams(input))
142                 {
143                         do_video = 0;
144                 }
145                 else
146                 {
147                         quicktime_set_video(output, 
148                                 1, 
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);
154                 }
155         }
156 //printf("main 1\n");
157
158         if(do_audio)
159         {
160                 if(!mpeg3_total_astreams(input))
161                 {
162                         do_audio = 0;
163                 }
164                 else
165                 {
166                         int i;
167                         channels = mpeg3_audio_channels(input, astream);
168
169                         quicktime_set_audio(output, 
170                                 channels, 
171                                 mpeg3_sample_rate(input, 0), 
172                                 24, 
173                                 AUDIO_CODEC);
174
175                         audio_output = malloc(sizeof(float*) * channels);
176                         for(i = 0; i < channels; i++)
177                                 audio_output[i] = malloc(sizeof(float) * afragment);
178                 }
179         }
180 //printf("main 1\n");
181
182 //      quicktime_set_jpeg(output, 100, 0);
183         mpeg3_set_mmx(input, 0);
184
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))
188         {
189 //printf("%d %d\n", mpeg3_end_of_video(input, layer), mpeg3_end_of_audio(input, astream));
190                 if(do_audio)
191                 {
192                         if(!mpeg3_end_of_audio(input, astream))
193                         {
194                                 int fragment = afragment;
195                                 int i, j, k;
196
197                                 i = astream;
198                                 k = 0;
199                                 for(j = 0; j < mpeg3_audio_channels(input, i); j++, k++)
200                                 {
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 */
207                                                         i);
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 */
214                                                         i);
215                                 }
216
217
218
219                                 quicktime_encode_audio(output, 
220                                         0, 
221                                         audio_output, 
222                                         fragment);
223
224                                 current_sample += fragment;
225                                 if(!do_video)
226                                 {
227                                         printf(" %d samples written\r", current_sample);
228                                         fflush(stdout);
229                                 }
230                         }
231                         else
232                                 current_sample += afragment;
233                 }
234
235                 if(do_video)
236                 {
237                         if(!mpeg3_end_of_video(input, layer))
238                         {
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;
247
248                                 for(i = 0; i < fragment && !mpeg3_end_of_video(input, layer); i++)
249                                 {
250                                         mpeg3_read_yuvframe_ptr(input,
251                                                 &row_pointers[0],
252                                                 &row_pointers[1],
253                                                 &row_pointers[2],
254                                                 layer);
255
256                                         switch(mpeg3_colormodel(input, layer))
257                                         {
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;
264                                         }
265                                         quicktime_encode_video(output, 
266                                                 (unsigned char **)row_pointers, 
267                                                 0);
268
269                                         current_frame++;
270                                         printf(" %d frames written\r", current_frame);
271                                         fflush(stdout);
272                                 }
273                         }
274                 }
275         }
276 printf("main 2\n");
277
278         printf("\n");
279         quicktime_close(output);
280         mpeg3_close(input);
281 }
282
283
284
285
286
287
288
289