4 #include <alsa/asoundlib.h>
9 const double nudge = 3.5;
10 const double ahead = .5;
13 #include "libzmpeg3.h"
15 #include "../libmpeg3/libmpeg3.h"
16 #define zmpeg3_t mpeg3_t
21 /* alpha currently is broken in gdk */
22 /* would render faster if alpha worked correctly */
28 #define COLOR_MODEL zmpeg3_t::cmdl_RGBA8888
30 #define COLOR_MODEL MPEG3_RGBA8888
36 #define COLOR_MODEL zmpeg3_t::cmdl_RGB888
38 #define COLOR_MODEL MPEG3_RGB888
45 static int verbose = 0;
47 /* c++ `pkg-config --cflags --libs gtk+-2.0` y.C ./x86_64/libzmpeg3.a -lpthread -lasound -lm */
51 snd_pcm_uframes_t zpcm_ufrm_size = 0;
52 snd_pcm_sframes_t zpcm_sbfr_size = 0;
53 snd_pcm_sframes_t zpcm_sper_size = 0;
54 snd_pcm_sframes_t zpcm_total_samples = 0;
55 double zpcm_play_time = 0.;
56 pthread_mutex_t zpcm_lock;
57 unsigned int zpcm_rate = 0;
58 static const char *zpcm_device = "plughw:0,0";
59 static unsigned int zpcm_bfr_time_us = 500000;
60 static unsigned int zpcm_per_time_us = 200000;
61 static snd_pcm_channel_area_t *zpcm_areas = 0;
62 static int zpcm_channels = 0;
63 static short **zpcm_buffers = 0;
64 static short *zpcm_samples = 0;
69 if( zpcm_buffers != 0 ) { delete [] zpcm_buffers; zpcm_buffers = 0; }
70 if( zpcm_areas != 0 ) { delete [] zpcm_areas; zpcm_areas = 0; }
71 if( zpcm_samples != 0 ) { delete [] zpcm_samples; zpcm_samples = 0; }
73 if( zpcm_buffers != 0 ) { free(zpcm_buffers); zpcm_buffers = 0; }
74 if( zpcm_areas != 0 ) { free(zpcm_areas); zpcm_areas = 0; }
75 if( zpcm_samples != 0 ) { free(zpcm_samples); zpcm_samples = 0; }
77 if( zpcm != 0 ) { snd_pcm_close(zpcm); zpcm = 0; }
78 pthread_mutex_destroy(&zpcm_lock);
81 void alsa_open(int chs,int rate)
83 int ich, bits, byts, dir, ret;
84 pthread_mutex_init(&zpcm_lock,0);
85 snd_pcm_format_t fmt = SND_PCM_FORMAT_S16;
86 snd_pcm_hw_params_t *phw;
87 snd_pcm_sw_params_t *psw;
88 snd_pcm_hw_params_alloca(&phw);
89 snd_pcm_sw_params_alloca(&psw);
92 zpcm_total_samples = 0;
94 ret = snd_pcm_open(&zpcm, zpcm_device,
95 SND_PCM_STREAM_PLAYBACK, 0 /* SND_PCM_NONBLOCK */);
97 ret = snd_pcm_hw_params_any(zpcm, phw);
99 ret = snd_pcm_hw_params_set_rate_resample(zpcm, phw, 1);
101 ret = snd_pcm_hw_params_set_access(zpcm, phw,
102 SND_PCM_ACCESS_RW_NONINTERLEAVED);
104 ret = snd_pcm_hw_params_set_format(zpcm, phw, fmt);
107 ret = snd_pcm_hw_params_set_channels(zpcm, phw, chs);
111 ret = snd_pcm_hw_params_set_rate_near(zpcm, phw, &zpcm_rate, 0);
112 if( (int)zpcm_rate != rate )
113 printf("nearest audio_rate for %d is %u\n",rate,zpcm_rate);
116 ret = snd_pcm_hw_params_set_buffer_time_near(zpcm, phw,
117 &zpcm_bfr_time_us, &dir);
119 ret = snd_pcm_hw_params_get_buffer_size(phw, &zpcm_ufrm_size);
121 zpcm_sbfr_size = zpcm_ufrm_size;
122 ret = snd_pcm_hw_params_set_period_time_near(zpcm, phw,
123 &zpcm_per_time_us, &dir);
126 ret = snd_pcm_hw_params_get_period_size(phw, &zpcm_ufrm_size, &dir);
128 zpcm_sper_size = zpcm_ufrm_size;
129 ret = snd_pcm_hw_params(zpcm, phw);
132 ret = snd_pcm_sw_params_current(zpcm, psw);
134 ret = snd_pcm_sw_params_set_start_threshold(zpcm, psw,
135 (zpcm_sbfr_size / zpcm_sper_size) * zpcm_sper_size);
137 ret = snd_pcm_sw_params_set_avail_min(zpcm, psw, zpcm_sper_size);
139 ret = snd_pcm_sw_params(zpcm, psw);
140 /* snd_pcm_dump(zpcm, stdout); */
144 zpcm_areas = new snd_pcm_channel_area_t[chs];
145 byts = snd_pcm_format_physical_width(fmt) / 8;
146 zpcm_samples = new short[zpcm_sper_size * chs * byts/2];
147 zpcm_buffers = new short *[chs];
149 zpcm_areas = calloc(chs, sizeof(snd_pcm_channel_area_t));
150 bits = snd_pcm_format_physical_width(fmt);
152 zpcm_samples = malloc(zpcm_sper_size * chs * byts);
153 zpcm_buffers = malloc(chs * sizeof(short*));
156 for( ich = 0; ich < chs; ++ich ) {
157 zpcm_areas[ich].addr = zpcm_samples;
158 zpcm_areas[ich].first = ich * zpcm_sper_size * bits;
159 zpcm_areas[ich].step = bits;
160 zpcm_buffers[ich] = zpcm_samples + ich*zpcm_sper_size;
164 fprintf(stderr,"alsa sample buffer allocation failure.\n");
170 printf("audio error: %s\n", snd_strerror(ret));
175 short *alsa_bfr(int ch)
177 return zpcm_buffers[ch];
182 return zpcm_sper_size;
185 int alsa_recovery(int ret)
187 printf("alsa recovery\n");
190 /* wait until the suspend flag is released, then fall through */
191 while( (ret=snd_pcm_resume(zpcm)) == -EAGAIN ) usleep(100000);
193 ret = snd_pcm_prepare(zpcm);
195 printf("underrun, prepare failed: %s\n", snd_strerror(ret));
198 printf("unhandled error: %s\n",snd_strerror(ret));
204 int alsa_write(int length)
207 int i, ret, count, retry;
208 snd_pcm_sframes_t sample_delay;
209 double play_time, time;
211 short *bfrs[zpcm_channels];
213 short **bfrs = alloca(zpcm_channels*sizeof(short *));
217 for( i=0; i<zpcm_channels; ++i ) bfrs[i] = zpcm_buffers[i];
219 while( count < length ) {
220 ret = snd_pcm_writen(zpcm,(void **)bfrs, length-count);
221 if( ret == -EAGAIN ) continue;
223 if( --retry < 0 ) return ret;
228 for( i=0; i<zpcm_channels; ++i ) bfrs[i] += ret;
231 pthread_mutex_lock(&zpcm_lock);
232 zpcm_total_samples += count;
233 snd_pcm_delay(zpcm,&sample_delay);
234 if( sample_delay > zpcm_total_samples )
235 sample_delay = zpcm_total_samples;
236 gettimeofday(&tv,NULL);
237 time = tv.tv_sec + tv.tv_usec/1000000.0;
238 play_time = (zpcm_total_samples - sample_delay) / (double)zpcm_rate;
239 zpcm_play_time = time - play_time;
240 pthread_mutex_unlock(&zpcm_lock);
241 return ret < 0 ? ret : 0;
246 double time, play_time;
248 pthread_mutex_lock(&zpcm_lock);
249 gettimeofday(&tv,NULL);
250 time = tv.tv_sec + tv.tv_usec/1000000.0;
251 play_time = time - zpcm_play_time;
252 pthread_mutex_unlock(&zpcm_lock);
266 void dst_exit(GtkWidget *widget, gpointer data)
277 gettimeofday(&tv,NULL);
278 time = tv.tv_sec + tv.tv_usec/1000000.0;
279 if( tstart < 0. ) tstart = time;
280 return time - tstart;
284 void mpeg3_stats(zmpeg3_t *zsrc)
286 int astream, vstream;
287 int has_audio, total_astreams, audio_channels, sample_rate;
288 int has_video, total_vstreams, width, height, colormodel;
289 long audio_samples, video_frames;
292 has_audio = mpeg3_has_audio(zsrc);
293 printf(" has_audio = %d\n", has_audio);
294 total_astreams = mpeg3_total_astreams(zsrc);
295 printf(" total_astreams = %d\n", total_astreams);
296 for( astream=0; astream<total_astreams; ++astream ) {
297 audio_channels = mpeg3_audio_channels(zsrc, astream);
298 printf(" audio_channels = %d\n", audio_channels);
299 sample_rate = mpeg3_sample_rate(zsrc, astream);
300 printf(" sample_rate = %d\n", sample_rate);
301 audio_samples = mpeg3_audio_samples(zsrc, astream);
302 printf(" audio_samples = %ld\n", audio_samples);
305 has_video = mpeg3_has_video(zsrc);
306 printf(" has_video = %d\n", has_video);
307 total_vstreams = mpeg3_total_vstreams(zsrc);
308 printf(" total_vstreams = %d\n", total_vstreams);
309 for( vstream=0; vstream<total_vstreams; ++vstream ) {
310 width = mpeg3_video_width(zsrc, vstream);
311 printf(" video_width = %d\n", width);
312 height = mpeg3_video_height(zsrc, vstream);
313 printf(" video_height = %d\n", height);
314 frame_rate = mpeg3_frame_rate(zsrc, vstream);
315 printf(" frame_rate = %f\n", frame_rate);
316 video_frames = mpeg3_video_frames(zsrc, vstream);
317 printf(" video_frames = %ld\n", video_frames);
318 colormodel = mpeg3_colormodel(zsrc, vstream);
319 printf(" colormodel = %d\n", colormodel);
327 double video_time(zmpeg3_t *zsrc,int stream)
330 double vtime = mpeg3_get_video_time(zsrc,stream);
332 double vtime = mpeg3_get_time(zsrc);
335 if( vstart == 0. ) vstart = vnow;
336 if( vtime < last_vtime )
337 vstart -= last_vtime;
339 return vnow - vstart + nudge;
344 void *video_thread(void *the_zsrc)
347 GtkWidget *panel_hbox;
349 GdkPixbuf *pbuf0, *pbuf1;
350 GdkImage *img0, *img1;
353 float frame_rate, frame_delay;
354 unsigned char **rows, **row0, **row1, *cap, *cap0, *cap1;
355 int ret, row, frame, dropped, more_data;
356 int width, height, owidth, oheight;
357 const int frame_drop = 1;
358 zmpeg3_t *zsrc = (zmpeg3_t *)the_zsrc;
360 frame_rate = mpeg3_frame_rate(zsrc, 0);
361 frame_delay = 2.0 / frame_rate;
362 height = mpeg3_video_height(zsrc, 0);
363 width = mpeg3_video_width(zsrc, 0);
364 //oheight = 2*height / 3;
365 //owidth = 2*width / 3;
373 visual = gdk_visual_get_system();
374 /* toplevel window */
375 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
376 gtk_signal_connect(GTK_OBJECT(window),"destroy",
377 GTK_SIGNAL_FUNC(dst_exit),NULL);
378 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_NONE);
379 /* try for shared image bfr, only seems to work with gtk_rgb */
380 img0 = gdk_image_new(GDK_IMAGE_SHARED, visual, owidth, oheight);
381 pbuf0 = gdk_pixbuf_new_from_data((const guchar *)img0->mem,
382 GDK_COLORSPACE_RGB,RGBA,8,owidth,oheight,owidth*BPP,NULL,NULL);
383 cap0 = gdk_pixbuf_get_pixels(pbuf0);
384 image = gtk_image_new_from_pixbuf(pbuf0);
385 /* double buffered */
386 img1 = gdk_image_new(GDK_IMAGE_SHARED, visual, owidth, oheight);
387 pbuf1 = gdk_pixbuf_new_from_data((const guchar *)img1->mem,
388 GDK_COLORSPACE_RGB,RGBA,8,owidth,oheight,owidth*BPP,NULL,NULL);
389 cap1 = gdk_pixbuf_get_pixels(pbuf1);
391 panel_hbox = gtk_hbox_new(FALSE,0);
392 gtk_container_add(GTK_CONTAINER(window), panel_hbox);
393 /* pack image into panel */
394 gtk_box_pack_start(GTK_BOX(panel_hbox), image, TRUE, TRUE, 0);
398 row0 = new unsigned char *[oheight];
399 row1 = new unsigned char *[oheight];
400 int cmdl = COLOR_MODEL;
402 row0 = malloc(oheight*sizeof(unsigned char *));
403 row1 = malloc(oheight*sizeof(unsigned char *));
404 int cmdl = COLOR_MODEL;
406 for( row=0; row<oheight; ++row ) {
407 row0[row] = cap0 + row*owidth*BPP;
408 row1[row] = cap1 + row*owidth*BPP;
411 gtk_widget_show_all(window);
415 vstart = last_vtime = 0.;
418 while( done == 0 && more_data ) {
419 if( !gtk_events_pending() ) {
421 // delay = (frame+dropped)/frame_rate - alsa_time();
422 // delay = the_time() - video_time(zsrc,0);
424 if( -delay >= frame_delay ) {
425 int nframes = (long)ceil(-delay/frame_rate);
426 if( nframes > 2 ) nframes = 2;
427 mpeg3_drop_frames(zsrc,nframes,0);
432 // usleep((int)(delay*1000000.0));
433 ret = mpeg3_read_frame(zsrc, rows, 0, 0, width, height,
434 owidth, oheight, cmdl, 0);
435 //printf("%d %ld\n",frame,zsrc->vtrack[0]->demuxer->titles[0]->fs->current_byte);
437 printf("read_video(stream=%d, frame=%d) = %d\n", 0, frame, ret);
438 //if( frame > 500 && frame < 800 ) {
439 // mpeg3_previous_frame(zsrc,0);
440 // mpeg3_previous_frame(zsrc,0);
442 GdkGC *blk = image->style->black_gc;
443 gdk_draw_rgb_image(image->window,blk, 0,0,owidth,oheight,
444 GDK_RGB_DITHER_NONE,cap,owidth*BPP);
447 { static FILE *fp = 0;
448 int sz = owidth*oheight*BPP;
449 if( fp == 0 ) fp = fopen("/tmp/dat.raw","w");
453 { static FILE *fp = 0;
454 int z, sz = owidth*oheight*BPP;
456 if( fp == 0 ) fp = fopen("/tmp/dat.raw","r");
458 for( z=0; z<sz && abs(zbfr[z]-cap[z])<=1; ++z );
459 //for( z=0; z<sz && zbfr[z]==cap[z]; ++z );
461 printf("bug %d\n",z);
464 if( frame > 250 ) exit(0);
466 *(unsigned long *)&cap ^= ((unsigned long)cap0 ^ (unsigned long)cap1);
467 *(unsigned long *)&rows ^= ((unsigned long)row0 ^ (unsigned long)row1);
468 //printf(" %d/%d %f %f %f\n",frame,dropped,(frame+dropped)/frame_rate,
469 // video_time(zsrc,0),mpeg3_get_video_time(zsrc,0));
471 more_data |= !mpeg3_end_of_video(zsrc,0);
474 gtk_main_iteration();
485 double audio_time(zmpeg3_t *zsrc, int stream)
488 double anow = mpeg3_get_audio_time(zsrc,stream);
490 double anow = mpeg3_get_time(zsrc);
492 if( astart < 0. ) astart = anow;
493 return anow - astart;
498 void *audio_thread(void *the_zsrc)
501 int audio_channels, sample_rate, more_data;
502 zmpeg3_t *zsrc = (zmpeg3_t *)the_zsrc;
505 audio_channels = mpeg3_audio_channels(zsrc, stream);
506 sample_rate = mpeg3_sample_rate(zsrc, stream);
507 alsa_open(audio_channels,sample_rate);
511 while( done == 0 && more_data ) {
514 int n = alsa_bfrsz();
515 for( ich=0; ich<audio_channels; ++ich) {
516 short *bfr = alsa_bfr(ich);
518 mpeg3_read_audio(zsrc, 0, bfr, ich, n, stream) :
519 mpeg3_reread_audio(zsrc, 0, bfr, ich, n, stream);
521 printf("read_audio(stream=%d,channel=%d) = %d\n", stream, ich, ret);
525 delay = audio_time(zsrc,stream) - the_time();
526 if( (delay-ahead) > 0. )
527 usleep((int)(delay*1000000.0/2.));
528 more_data |= !mpeg3_end_of_audio(zsrc,0);
538 int main(int ac, char **av)
549 //zmpeg3_t* zsrc = mpeg3_open("/tmp/dat",&ret);
550 //zmpeg3_t* zsrc = mpeg3_zopen(0,"/tmp/dat",&ret,ZIO_UNBUFFERED+ZIO_SINGLE_ACCESS+ZIO_SEQUENTIAL);
551 //zmpeg3_t* zsrc = mpeg3_open("/tmp/dat.toc",&ret);
552 //zmpeg3_t* zsrc = mpeg3_open("/root/LimeWire/Shared/Britney Spears - Pepsi Commercial with Bob Dole.mpeg",&ret);
553 //zmpeg3_t* zsrc = mpeg3_open("/tmp/dat.mp3",&ret);
554 //zmpeg3_t* zsrc = mpeg3_open("/dvd/VIDEO_TS/VTS_01_0.IFO",&ret);
555 //zmpeg3_t* zsrc = mpeg3_open("/home/dat2.vts",&ret);
556 //zmpeg3_t* zsrc = mpeg3_open("/dvd/BDMV/STREAM/00000.m2ts",&ret);
557 zmpeg3_t* zsrc = mpeg3_open(av[1],&ret);
559 printf(" ret = %d\n",ret);
560 if( ret != 0 ) exit(1);
565 zmpeg3_t* zsrc1 = zsrc;
567 zmpeg3_t* zsrc1 = mpeg3_open_copy(zsrc->fs->path, zsrc, &ret);
571 mpeg3_set_cpus(zsrc,3);
572 mpeg3_seek_byte(zsrc,0);
573 mpeg3_show_subtitle(zsrc, 0);
574 signal(SIGINT,sigint);
577 pthread_create(&the_audio, NULL, audio_thread, (void*)zsrc1);
580 pthread_create(&the_video, NULL, video_thread, (void*)zsrc);
583 pthread_join(the_video,NULL);
586 pthread_join(the_audio,NULL);