00001 #include <pthread.h>
00002 #include <signal.h>
00003
00004 #include "colormodels.h"
00005 #include "libmpeg3.h"
00006 #include "quicktime.h"
00007
00008 #define OUTPUT_PATH "movie.mov"
00009
00010 #define VIDEO_CODEC QUICKTIME_JPEG
00011
00012 #define AUDIO_CODEC QUICKTIME_TWOS
00013
00014
00015
00016
00017
00018 void
00019 g_log (const char *log_domain,
00020 int log_level,
00021 const char *format,
00022 ...)
00023 {
00024 }
00025
00026 void
00027 g_logv (const char *log_domain,
00028 int log_level,
00029 const char *format,
00030 ...)
00031 {
00032 }
00033
00034
00035
00036
00037
00038 int atexit(void (*function)(void))
00039 {
00040 return 0;
00041 }
00042
00043
00044
00045
00046 quicktime_t *output;
00047 mpeg3_t *input;
00048 pthread_mutex_t mutex;
00049 int predicate = 0;
00050
00051 void* trap_interrupt()
00052 {
00053 pthread_mutex_lock(&mutex);
00054 if(!predicate)
00055 {
00056 predicate = 1;
00057
00058 printf("interrupt trapped 1\n");
00059 quicktime_close(output);
00060 printf("interrupt trapped 2\n");
00061 exit(0);
00062
00063
00064
00065 }
00066 }
00067
00068
00069
00070
00071
00072 int main(int argc, char *argv[])
00073 {
00074 int frame_count = -1;
00075 char *row_pointers[3];
00076 int do_audio = 0;
00077 int do_video = 0;
00078 int channels = 0;
00079 long afragment = 65536;
00080 float **audio_output;
00081 int layer = 0;
00082 int astream = 0;
00083 char input_path[1024];
00084 char output_path[1024];
00085 int i;
00086 long current_frame = 0;
00087 long current_sample = 0;
00088
00089 pthread_mutex_init(&mutex, NULL);
00090 signal(SIGINT, trap_interrupt);
00091
00092 input_path[0] = 0;
00093 output_path[0] = 0;
00094
00095 if(argc < 3)
00096 {
00097 printf("Usage: %s [-a] [-v] <mpeg file> <output movie> [frame count]\n", argv[0]);
00098 exit(1);
00099 }
00100
00101 for(i = 1; i < argc; i++)
00102 {
00103 if(!strcmp(argv[i], "-a"))
00104 {
00105 do_audio = 1;
00106 }
00107 else
00108 if(!strcmp(argv[i], "-v"))
00109 {
00110 do_video = 1;
00111 }
00112 else
00113 if(!input_path[0])
00114 {
00115 strcpy(input_path, argv[i]);
00116 }
00117 else
00118 if(!output_path[0])
00119 {
00120 strcpy(output_path, argv[i]);
00121 }
00122 else
00123 frame_count = atol(argv[i]);
00124 }
00125
00126
00127 if(!(input = mpeg3_open(input_path)))
00128 {
00129 exit(1);
00130 }
00131
00132
00133 if(!(output = quicktime_open(output_path, 0, 1)))
00134 {
00135 exit(1);
00136 }
00137
00138
00139 if(do_video)
00140 {
00141 if(!mpeg3_total_vstreams(input))
00142 {
00143 do_video = 0;
00144 }
00145 else
00146 {
00147 quicktime_set_video(output,
00148 1,
00149 mpeg3_video_width(input, layer),
00150 mpeg3_video_height(input, layer),
00151 mpeg3_frame_rate(input, layer),
00152 VIDEO_CODEC);
00153 quicktime_set_jpeg(output, 80, 0);
00154 }
00155 }
00156
00157
00158 if(do_audio)
00159 {
00160 if(!mpeg3_total_astreams(input))
00161 {
00162 do_audio = 0;
00163 }
00164 else
00165 {
00166 int i;
00167 channels = mpeg3_audio_channels(input, astream);
00168
00169 quicktime_set_audio(output,
00170 channels,
00171 mpeg3_sample_rate(input, 0),
00172 24,
00173 AUDIO_CODEC);
00174
00175 audio_output = malloc(sizeof(float*) * channels);
00176 for(i = 0; i < channels; i++)
00177 audio_output[i] = malloc(sizeof(float) * afragment);
00178 }
00179 }
00180
00181
00182
00183 mpeg3_set_mmx(input, 0);
00184
00185 while((!(do_video && mpeg3_end_of_video(input, layer)) ||
00186 !(do_audio && mpeg3_end_of_audio(input, astream))) &&
00187 (current_frame < frame_count || frame_count < 0))
00188 {
00189
00190 if(do_audio)
00191 {
00192 if(!mpeg3_end_of_audio(input, astream))
00193 {
00194 int fragment = afragment;
00195 int i, j, k;
00196
00197 i = astream;
00198 k = 0;
00199 for(j = 0; j < mpeg3_audio_channels(input, i); j++, k++)
00200 {
00201 if(j == 0)
00202 mpeg3_read_audio(input,
00203 audio_output[k],
00204 0,
00205 j,
00206 fragment,
00207 i);
00208 else
00209 mpeg3_reread_audio(input,
00210 audio_output[k],
00211 0,
00212 j,
00213 fragment,
00214 i);
00215 }
00216
00217
00218
00219 quicktime_encode_audio(output,
00220 0,
00221 audio_output,
00222 fragment);
00223
00224 current_sample += fragment;
00225 if(!do_video)
00226 {
00227 printf(" %d samples written\r", current_sample);
00228 fflush(stdout);
00229 }
00230 }
00231 else
00232 current_sample += afragment;
00233 }
00234
00235 if(do_video)
00236 {
00237 if(!mpeg3_end_of_video(input, layer))
00238 {
00239 int fragment;
00240 if(do_audio)
00241 fragment = (long)((double)current_sample /
00242 mpeg3_sample_rate(input, 0) *
00243 mpeg3_frame_rate(input, layer) -
00244 current_frame);
00245 else
00246 fragment = 1;
00247
00248 for(i = 0; i < fragment && !mpeg3_end_of_video(input, layer); i++)
00249 {
00250 mpeg3_read_yuvframe_ptr(input,
00251 &row_pointers[0],
00252 &row_pointers[1],
00253 &row_pointers[2],
00254 layer);
00255
00256 switch(mpeg3_colormodel(input, layer))
00257 {
00258 case MPEG3_YUV420P:
00259 quicktime_set_cmodel(output, BC_YUV420P);
00260 break;
00261 case MPEG3_YUV422P:
00262 quicktime_set_cmodel(output, BC_YUV422P);
00263 break;
00264 }
00265 quicktime_encode_video(output,
00266 (unsigned char **)row_pointers,
00267 0);
00268
00269 current_frame++;
00270 printf(" %d frames written\r", current_frame);
00271 fflush(stdout);
00272 }
00273 }
00274 }
00275 }
00276 printf("main 2\n");
00277
00278 printf("\n");
00279 quicktime_close(output);
00280 mpeg3_close(input);
00281 }
00282
00283
00284
00285
00286
00287
00288
00289