3 #include <alsa/asoundlib.h>
11 #include "../libmpeg3/libmpeg3.h"
12 #define zmpeg3_t mpeg3_t
18 /* c++ `pkg-config --cflags --libs gtk+-2.0` y.C ./x86_64/libzmpeg3.a -lpthread -lasound -lm */
22 snd_pcm_uframes_t zpcm_ufrm_size;
23 snd_pcm_sframes_t zpcm_sbfr_size;
24 snd_pcm_sframes_t zpcm_sper_size;
25 unsigned int zpcm_rate;
26 static const char *zpcm_device = "plughw:0,0";
27 static unsigned int zpcm_bfr_time_us = 500000;
28 static unsigned int zpcm_per_time_us = 200000;
29 static snd_pcm_channel_area_t *zpcm_areas = 0;
30 static int zpcm_channels = 0;
31 static short **zpcm_buffers = 0;
32 static short *zpcm_samples = 0;
37 if( zpcm_buffers != 0 ) { delete [] zpcm_buffers; zpcm_buffers = 0; }
38 if( zpcm_areas != 0 ) { delete [] zpcm_areas; zpcm_areas = 0; }
39 if( zpcm_samples != 0 ) { delete [] zpcm_samples; zpcm_samples = 0; }
41 if( zpcm_buffers != 0 ) { free(zpcm_buffers); zpcm_buffers = 0; }
42 if( zpcm_areas != 0 ) { free(zpcm_areas); zpcm_areas = 0; }
43 if( zpcm_samples != 0 ) { free(zpcm_samples); zpcm_samples = 0; }
45 if( zpcm != 0 ) { snd_pcm_close(zpcm); zpcm = 0; }
48 void alsa_open(int chs,int rate)
50 int ich, bits, byts, dir, ret;
51 snd_pcm_format_t fmt = SND_PCM_FORMAT_S16;
52 snd_pcm_hw_params_t *phw;
53 snd_pcm_sw_params_t *psw;
54 snd_pcm_hw_params_alloca(&phw);
55 snd_pcm_sw_params_alloca(&psw);
58 ret = snd_pcm_open(&zpcm, zpcm_device,
59 SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
61 ret = snd_pcm_hw_params_any(zpcm, phw);
63 ret = snd_pcm_hw_params_set_rate_resample(zpcm, phw, 1);
65 ret = snd_pcm_hw_params_set_access(zpcm, phw,
66 SND_PCM_ACCESS_RW_NONINTERLEAVED);
68 ret = snd_pcm_hw_params_set_format(zpcm, phw, fmt);
71 ret = snd_pcm_hw_params_set_channels(zpcm, phw, chs);
75 ret = snd_pcm_hw_params_set_rate_near(zpcm, phw, &zpcm_rate, 0);
76 if( (int)zpcm_rate != rate )
77 printf("nearest audio_rate for %d is %u\n",rate,zpcm_rate);
80 ret = snd_pcm_hw_params_set_buffer_time_near(zpcm, phw,
81 &zpcm_bfr_time_us, &dir);
83 ret = snd_pcm_hw_params_get_buffer_size(phw, &zpcm_ufrm_size);
85 zpcm_sbfr_size = zpcm_ufrm_size;
86 ret = snd_pcm_hw_params_set_period_time_near(zpcm, phw,
87 &zpcm_per_time_us, &dir);
90 ret = snd_pcm_hw_params_get_period_size(phw, &zpcm_ufrm_size, &dir);
92 zpcm_sper_size = zpcm_ufrm_size;
93 ret = snd_pcm_hw_params(zpcm, phw);
96 ret = snd_pcm_sw_params_current(zpcm, psw);
98 ret = snd_pcm_sw_params_set_start_threshold(zpcm, psw,
99 (zpcm_sbfr_size / zpcm_sper_size) * zpcm_sper_size);
101 ret = snd_pcm_sw_params_set_avail_min(zpcm, psw, zpcm_sper_size);
103 ret = snd_pcm_sw_params(zpcm, psw);
104 /* snd_pcm_dump(zpcm, stdout); */
108 zpcm_areas = new snd_pcm_channel_area_t[chs];
109 byts = snd_pcm_format_physical_width(fmt) / 8;
110 zpcm_samples = new short[zpcm_sper_size * chs * byts/2];
111 zpcm_buffers = new short *[chs];
113 zpcm_areas = calloc(chs, sizeof(snd_pcm_channel_area_t));
114 bits = snd_pcm_format_physical_width(fmt);
116 zpcm_samples = malloc(zpcm_sper_size * chs * byts);
117 zpcm_buffers = malloc(chs * sizeof(short*));
120 for( ich = 0; ich < chs; ++ich ) {
121 zpcm_areas[ich].addr = zpcm_samples;
122 zpcm_areas[ich].first = ich * zpcm_sper_size * bits;
123 zpcm_areas[ich].step = bits;
124 zpcm_buffers[ich] = zpcm_samples + ich*zpcm_sper_size;
128 fprintf(stderr,"alsa sample buffer allocation failure.\n");
134 printf("audio error: %s\n", snd_strerror(ret));
139 short *alsa_bfr(int ch)
141 return zpcm_buffers[ch];
146 return zpcm_sper_size;
149 int alsa_recovery(int ret)
151 printf("alsa recovery\n");
154 /* wait until the suspend flag is released, then fall through */
155 while( (ret=snd_pcm_resume(zpcm)) == -EAGAIN ) usleep(100000);
157 ret = snd_pcm_prepare(zpcm);
159 printf("underrun, prepare failed: %s\n", snd_strerror(ret));
162 printf("unhandled error: %s\n",snd_strerror(ret));
168 int alsa_write(int len)
172 short *bfrs[zpcm_channels];
174 short **bfrs = alloca(zpcm_channels*sizeof(short *));
177 for( i=0; i<zpcm_channels; ++i ) bfrs[i] = zpcm_buffers[i];
180 ret = snd_pcm_writen(zpcm,(void **)bfrs, len);
181 if( ret == -EAGAIN ) continue;
186 for( i=0; i<zpcm_channels; ++i ) bfrs[i] += ret;
189 return ret < 0 ? ret : 0;
202 void dst_exit(GtkWidget *widget, gpointer data)
210 gettimeofday(&tv,NULL);
211 return tv.tv_sec + tv.tv_usec/1000000.0;
214 int main(int ac, char **av)
216 int ret, more_data, frame, astream, vstream;
217 int has_audio, total_astreams, audio_channels, sample_rate;
218 int has_video, total_vstreams, width, height, colormodel;
220 long audio_samples, video_frames;
225 GtkWidget *panel_hbox;
227 GdkPixbuf *pbuf0, *pbuf1;
228 GdkImage *img0, *img1;
230 double start, zstart, now, znow, delay;
231 unsigned char **rows, **row0, **row1, *cap, *cap0, *cap1;
240 zmpeg3_t* zsrc = mpeg3_open("/tmp/dat",&ret);
241 //zmpeg3_t* zsrc = mpeg3_open("/root/LimeWire/Shared/Britney Spears - Pepsi Commercial with Bob Dole.mpeg",&ret);
242 //zmpeg3_t* zsrc = mpeg3_open("/tmp/dat.mp3",&ret);
243 //zmpeg3_t* zsrc = mpeg3_open("/dvd/VIDEO_TS/VIDEO_TS.IFO",&ret);
244 printf(" ret = %d\n",ret);
245 if( ret != 0 ) exit(1);
246 has_audio = mpeg3_has_audio(zsrc);
247 printf(" has_audio = %d\n", has_audio);
248 total_astreams = mpeg3_total_astreams(zsrc);
249 printf(" total_astreams = %d\n", total_astreams);
250 for( astream=0; astream<total_astreams; ++astream ) {
251 audio_channels = mpeg3_audio_channels(zsrc, astream);
252 printf(" audio_channels = %d\n", audio_channels);
253 sample_rate = mpeg3_sample_rate(zsrc, astream);
254 printf(" sample_rate = %d\n", sample_rate);
255 audio_samples = mpeg3_audio_samples(zsrc, astream);
256 printf(" audio_samples = %ld\n", audio_samples);
259 has_video = mpeg3_has_video(zsrc);
260 printf(" has_video = %d\n", has_video);
261 total_vstreams = mpeg3_total_vstreams(zsrc);
262 printf(" total_vstreams = %d\n", total_vstreams);
263 for( vstream=0; vstream<total_vstreams; ++vstream ) {
264 width = mpeg3_video_width(zsrc, vstream);
265 printf(" video_width = %d\n", width);
266 height = mpeg3_video_height(zsrc, vstream);
267 printf(" video_height = %d\n", height);
268 frame_rate = mpeg3_frame_rate(zsrc, vstream);
269 printf(" frame_rate = %f\n", frame_rate);
270 video_frames = mpeg3_video_frames(zsrc, vstream);
271 printf(" video_frames = %ld\n", video_frames);
272 colormodel = mpeg3_colormodel(zsrc, vstream);
273 printf(" colormodel = %d\n", colormodel);
276 //mpeg3_set_cpus(zsrc,2);
277 mpeg3_seek_byte(zsrc,0);
284 alsa_open(audio_channels,sample_rate);
289 visual = gdk_visual_get_system();
290 /* toplevel window */
291 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
292 gtk_signal_connect(GTK_OBJECT(window),"destroy",
293 GTK_SIGNAL_FUNC(dst_exit),NULL);
294 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_NONE);
295 /* try for shared image bfr, only seems to work with gtk_rgb */
296 img0 = gdk_image_new(GDK_IMAGE_SHARED, visual, owidth, oheight);
297 pbuf0 = gdk_pixbuf_new_from_data((const guchar *)img0->mem,
298 GDK_COLORSPACE_RGB,FALSE,8,owidth,oheight,owidth*3,NULL,NULL);
299 cap0 = gdk_pixbuf_get_pixels(pbuf0);
300 image = gtk_image_new_from_pixbuf(pbuf0);
301 /* double buffered */
302 img1 = gdk_image_new(GDK_IMAGE_SHARED, visual, owidth, oheight);
303 pbuf1 = gdk_pixbuf_new_from_data((const guchar *)img1->mem,
304 GDK_COLORSPACE_RGB,FALSE,8,owidth,oheight,owidth*3,NULL,NULL);
305 cap1 = gdk_pixbuf_get_pixels(pbuf1);
307 panel_hbox = gtk_hbox_new(FALSE,0);
308 gtk_container_add(GTK_CONTAINER(window), panel_hbox);
309 /* pack image into panel */
310 gtk_box_pack_start(GTK_BOX(panel_hbox), image, TRUE, TRUE, 0);
314 row0 = new unsigned char *[oheight];
315 row1 = new unsigned char *[oheight];
316 int cmdl = zmpeg3_t::cmdl_RGB888;
318 row0 = malloc(oheight*sizeof(unsigned char *));
319 row1 = malloc(oheight*sizeof(unsigned char *));
320 int cmdl = MPEG3_RGB888;
322 for( row=0; row<oheight; ++row ) {
323 row0[row] = cap0 + row*owidth*3;
324 row1[row] = cap1 + row*owidth*3;
327 gtk_widget_show_all(window);
332 signal(SIGINT,sigint);
336 while( done == 0 && more_data ) {
338 if( !gtk_events_pending() ) {
343 int n = alsa_bfrsz();
344 for( ich=0; ich<audio_channels; ++ich) {
345 short *bfr = alsa_bfr(ich);
347 mpeg3_read_audio(zsrc, 0, bfr, ich, n, 0) :
348 mpeg3_reread_audio(zsrc, 0, bfr, ich, n, 0);
349 printf("read_audio(stream=%d,channel=%d) = %d\n", 0, ich, ret);
352 more_data |= !mpeg3_end_of_audio(zsrc,0);
355 ret = mpeg3_read_frame(zsrc, rows, 0, 0, width, height,
356 owidth, oheight, cmdl, 0);
357 printf("read_video(stream=%d, frame=%d) = %d\n", 0, frame, ret);
358 GdkGC *blk = image->style->black_gc;
359 gdk_draw_rgb_image(image->window,blk, 0,0,owidth,oheight,
360 GDK_RGB_DITHER_NONE,cap,owidth*3);
361 *(unsigned long *)&cap ^= ((unsigned long)cap0 ^ (unsigned long)cap1);
362 *(unsigned long *)&rows ^= ((unsigned long)row0 ^ (unsigned long)row1);
364 znow = mpeg3_get_time(zsrc);
365 if( zstart < 0. ) zstart = znow;
366 delay = (znow-zstart) - (now-start);
367 if( delay > 0 ) usleep((int)(delay*100000.0));
368 more_data |= !mpeg3_end_of_video(zsrc,0);
374 gtk_main_iteration();