#include <stdio.h>
#include <libmpeg3.h>
#include <sys/soundcard.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/Xv.h>
#include <X11/extensions/Xvlib.h>
#include <X11/extensions/XShm.h>
#define GUID_YUV12_PLANAR 0x32315659
int main(int argc, char **argv){
mpeg3_t *file;
double time;
short *buffer2;
short *buffer3;
short *buffer4;
unsigned char **videodata;
int streams,i,audio_fd;
XvImage *yuv_image;
int depth;
XEvent event;
int r;
int _d, _w, _h,_dw;
int retval=0;
int d;
FILE *dsp;
XSetWindowAttributes attr;
GC vo_gc;
Display *mDisplay;
Window vo_window;
Window mRootWin;
XSetWindowAttributes window_attributes;
unsigned long window_mask;
int shmem_flag=0;
int ret;
XShmSegmentInfo yuv_shminfo;
int xv_port=-1;
int p_num_adaptors;
int j;
int screen;
int CompletionType;
char *y_output,*u_output,*v_output;
XVisualInfo vinfo;
XvAdaptorInfo *ai;
screen=DefaultScreen(mDisplay);
mDisplay=XOpenDisplay((char*)0);
screen=DefaultScreen(mDisplay);
XMatchVisualInfo(mDisplay, screen, 16, TrueColor, &vinfo);
window_attributes.colormap=XCreateColormap(mDisplay, DefaultRootWindow(mDisplay), vinfo.visual, AllocNone);
window_attributes.event_mask = StructureNotifyMask | ExposureMask;
window_attributes.background_pixel=0;
window_attributes.border_pixel=0;
//window_attributes.border_pixel = BlackPixel (mDisplay, screen);
//window_attributes.background_pixel = BlackPixel (mDisplay, screen);
window_attributes.override_redirect = 0;
window_mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
depth = DefaultDepth (mDisplay,DefaultScreen(mDisplay));
vo_window=XCreateWindow(mDisplay,DefaultRootWindow(mDisplay), 0, 0, 640,480, 0, depth,InputOutput, CopyFromParent,window_mask ,&window_attributes);
XStoreName(mDisplay,vo_window,"firstxv");
XSetIconName(mDisplay,vo_window,"firstxv");
vo_gc=XCreateGC(mDisplay,vo_window,0,0);
XMapWindow(mDisplay,vo_window);
do {
XNextEvent(mDisplay, &event);
}
while (event.type != MapNotify || event.xmap.event != vo_window);
if (XShmQueryExtension(mDisplay)) shmem_flag = 1;
if (!shmem_flag) {
printf("no shmem available.\n");
exit (-1);
} else {
printf("shmem available!\n");
}
if (shmem_flag==1) CompletionType = XShmGetEventBase(mDisplay) + ShmCompletion;
ret = XvQueryAdaptors(mDisplay, DefaultRootWindow(mDisplay),&p_num_adaptors, &ai);
printf("return queryadaptor: %d -- adaptors available: %d\n",ret,p_num_adaptors);
xv_port=ai[p_num_adaptors-1].base_id;
yuv_image=XvShmCreateImage(mDisplay,xv_port,GUID_YUV12_PLANAR,0,640,480,&yuv_shminfo);
yuv_shminfo.shmid = shmget(IPC_PRIVATE, yuv_image->data_size, IPC_CREAT | 0777);
yuv_shminfo.shmaddr = yuv_image->data = shmat(yuv_shminfo.shmid, 0, 0);
yuv_shminfo.readOnly = False;
if (!XShmAttach(mDisplay, &yuv_shminfo)) {
printf("XShmAttach failed !\n");
exit (-1);
}
// XFlush(mDisplay);
// vo_gc=XCreateGC(mDisplay, vo_window, 0L, &xgcv);
audio_fd=open("/dev/dsp",O_WRONLY);
// memset(buffer2,0x0,10240*sizeof(float));
printf("\n------------------\n");
file=mpeg3_open("/home/revenger/atlantagrape.mpeg"); //test.mp3"); ///home/revenger/testing/kream204.mpg"); //test.mp3");
r=16;
ioctl(audio_fd,SNDCTL_DSP_SETFMT, &r);
r=16;
ioctl(audio_fd,SNDCTL_DSP_SETFMT, &r);
printf("File has audio: %s\n",mpeg3_has_audio(file)?"Yes":"No");
printf("File has video: %s\n",mpeg3_has_video(file)?"Yes":"No");
streams=1; //1; //mpeg3_total_astreams(file);
printf("Streams: %d\n",streams);
streams=1;
ioctl(audio_fd,SNDCTL_DSP_STEREO,&streams);
printf("Sample rate: %d\n",mpeg3_sample_rate(file,streams-1));
r=mpeg3_sample_rate(file,streams-1); //22050; //44100; //44100; //mpeg3_audio_samples(file,streams-1);
buffer2=(short*)malloc(44100*2);
r=44100;
buffer3=(short*)malloc(44100*2);
buffer4=(short*)malloc(88200*2*2);
retval=ioctl(audio_fd,SNDCTL_DSP_SPEED,&r);
if(retval==-1){
printf("couldn't set speed!\n");
}
printf("Length: %d\n",mpeg3_audio_samples(file,streams-1));
videodata=(char**)malloc(640*3);
for(i=0;i<480*3;i++){
videodata[i]=(char*)malloc(480*3);
}
printf("video width: %d\n",mpeg3_video_width(file,0));
printf("color model: %d\n",mpeg3_colormodel(file,0));
y_output=(char*)malloc(200*300);
u_output=(char*)malloc(200*300/4);
v_output=(char*)malloc(200*300/4);
while(1){
// mpeg3_read_frame(file,output_rows,0,0,320,240,320,240,0,1);
mpeg3_read_audio(file,NULL,buffer2,0,44100,streams-1);
mpeg3_reread_audio(file,NULL,buffer3,1,44100,streams-1);
// mpeg3_read_frame(file,videodata,0,0,200,200,200,200,MPEG3_YUV420P,0);
mpeg3_read_yuvframe(file,y_output,u_output,v_output,0,0,200,200,0);
printf("viddata: %x\n",y_output[30]);
// printf("viddata: %x\n",videodata[1][34]);
// n XvShmPutImage(dpy, xv_port, window, gc, yuv_ima
printf("%ld",mpeg3_tell_byte(file));
d=0;
for(i=0;i<88200*2;i++){
if(i%2==0){
buffer4[i]=buffer2[i/2];
// buffer4[i]=buffer4[i]&0xff;
} else {
buffer4[i]=0; //buffer3[i/2];
}
}
for (i = 0; i < 480; i++) {
for (j = 0; j < 640; j++) {
if(i<=200&&j<200){
yuv_image->data[i + j] = y_output[j*i];
} else {
yuv_image->data[640*i + j] = 0;
}
}
}
XGetGeometry(mDisplay, vo_window, &_dw, &_d, &_d, &_w, &_h, &_d, &_d);
printf("height: %d\n",yuv_image->height);
XvShmPutImage(mDisplay, xv_port, vo_window, vo_gc, yuv_image,
0, 0, yuv_image->width, yuv_image->height,
0, 0, _w, _h, True);
write(audio_fd,buffer4,88200*2);
printf("file takes: %f seconds\n",mpeg3_get_time(file));
}
fclose(dsp);
mpeg3_close(file);
return(0);
} |