1999-02-05 10:41:55 +01:00
|
|
|
/*
|
|
|
|
* Very simple AVIPLAYER
|
2002-06-01 01:06:46 +02:00
|
|
|
*
|
1999-02-05 10:41:55 +01:00
|
|
|
* Copyright 1999 Marcus Meissner
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2002-06-01 01:06:46 +02:00
|
|
|
*
|
1999-02-05 10:41:55 +01:00
|
|
|
* Status:
|
|
|
|
* - plays .avi streams, video only
|
2003-08-18 22:11:29 +02:00
|
|
|
* - requires Microsoft avifil32.dll and builtin msvfw32.dll.
|
1999-02-05 10:41:55 +01:00
|
|
|
*
|
|
|
|
* Todo:
|
|
|
|
* - audio support (including synchronization etc)
|
|
|
|
* - replace DirectDraw by a 'normal' window, including dithering, controls
|
|
|
|
* etc.
|
2002-06-01 01:06:46 +02:00
|
|
|
*
|
1999-02-05 10:41:55 +01:00
|
|
|
* Bugs:
|
2002-06-01 01:06:46 +02:00
|
|
|
* - no time scheduling, video plays too fast using DirectDraw/XF86DGA
|
1999-02-05 10:41:55 +01:00
|
|
|
* - requires DirectDraw with all disadvantages.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <assert.h>
|
1999-03-25 16:52:09 +01:00
|
|
|
#include <string.h>
|
2003-01-08 00:11:12 +01:00
|
|
|
|
|
|
|
#define NONAMELESSUNION
|
|
|
|
#define NONAMELESSSTRUCT
|
1999-02-05 10:41:55 +01:00
|
|
|
#include "windows.h"
|
|
|
|
#include "wingdi.h"
|
|
|
|
#include "mmsystem.h"
|
|
|
|
#include "ddraw.h"
|
|
|
|
#include "vfw.h"
|
|
|
|
|
|
|
|
|
2001-01-05 04:42:54 +01:00
|
|
|
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
|
1999-02-05 10:41:55 +01:00
|
|
|
{
|
1999-02-09 15:08:29 +01:00
|
|
|
int bytesline,i,n,pos;
|
1999-02-05 10:41:55 +01:00
|
|
|
time_t tstart,tend;
|
|
|
|
LONG cnt;
|
|
|
|
BITMAPINFOHEADER *bmi;
|
|
|
|
HRESULT hres;
|
|
|
|
PAVIFILE avif;
|
|
|
|
PAVISTREAM vids=NULL,auds=NULL;
|
|
|
|
AVIFILEINFO afi;
|
|
|
|
AVISTREAMINFO asi;
|
|
|
|
PGETFRAME vidgetframe=NULL;
|
|
|
|
LPDIRECTDRAW ddraw;
|
|
|
|
DDSURFACEDESC dsdesc;
|
|
|
|
LPDIRECTDRAWSURFACE dsurf;
|
1999-02-09 15:08:29 +01:00
|
|
|
LPDIRECTDRAWPALETTE dpal;
|
|
|
|
PALETTEENTRY palent[256];
|
|
|
|
|
2003-12-02 06:33:26 +01:00
|
|
|
AVIFileInit();
|
2003-10-16 21:12:49 +02:00
|
|
|
if (GetFileAttributes(cmdline) == INVALID_FILE_ATTRIBUTES) {
|
1999-02-05 10:41:55 +01:00
|
|
|
fprintf(stderr,"Usage: aviplay <avifilename>\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2003-12-02 06:33:26 +01:00
|
|
|
hres = AVIFileOpen(&avif,cmdline,OF_READ,NULL);
|
1999-02-05 10:41:55 +01:00
|
|
|
if (hres) {
|
|
|
|
fprintf(stderr,"AVIFileOpen: 0x%08lx\n",hres);
|
|
|
|
exit(1);
|
|
|
|
}
|
2003-12-02 06:33:26 +01:00
|
|
|
hres = AVIFileInfo(avif,&afi,sizeof(afi));
|
1999-02-05 10:41:55 +01:00
|
|
|
if (hres) {
|
|
|
|
fprintf(stderr,"AVIFileInfo: 0x%08lx\n",hres);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
for (n=0;n<afi.dwStreams;n++) {
|
|
|
|
char buf[5];
|
|
|
|
PAVISTREAM ast;
|
|
|
|
|
2003-12-02 06:33:26 +01:00
|
|
|
hres = AVIFileGetStream(avif,&ast,0,n);
|
1999-02-05 10:41:55 +01:00
|
|
|
if (hres) {
|
|
|
|
fprintf(stderr,"AVIFileGetStream %d: 0x%08lx\n",n,hres);
|
|
|
|
exit(1);
|
|
|
|
}
|
2003-12-02 06:33:26 +01:00
|
|
|
hres = AVIStreamInfo(ast,&asi,sizeof(asi));
|
1999-02-05 10:41:55 +01:00
|
|
|
if (hres) {
|
|
|
|
fprintf(stderr,"AVIStreamInfo %d: 0x%08lx\n",n,hres);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
fprintf(stderr,"[Stream %d: ",n);
|
|
|
|
buf[4]='\0';memcpy(buf,&(asi.fccType),4);
|
|
|
|
fprintf(stderr,"%s.",buf);
|
|
|
|
buf[4]='\0';memcpy(buf,&(asi.fccHandler),4);
|
|
|
|
fprintf(stderr,"%s, %s]\n",buf,asi.szName);
|
|
|
|
switch (asi.fccType) {
|
|
|
|
case streamtypeVIDEO:
|
|
|
|
vids = ast;
|
|
|
|
break;
|
2002-06-01 01:06:46 +02:00
|
|
|
case streamtypeAUDIO:
|
1999-02-05 10:41:55 +01:00
|
|
|
auds = ast;
|
|
|
|
break;
|
|
|
|
default: {
|
|
|
|
char type[5];
|
|
|
|
type[4]='\0';memcpy(type,&(asi.fccType),4);
|
|
|
|
|
|
|
|
fprintf(stderr,"Unhandled streamtype %s\n",type);
|
2003-12-02 06:33:26 +01:00
|
|
|
AVIStreamRelease(ast);
|
1999-02-05 10:41:55 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/********************* begin video setup ***********************************/
|
|
|
|
if (!vids) {
|
|
|
|
fprintf(stderr,"No video stream found. Good Bye.\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
cnt = sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD);
|
|
|
|
bmi = HeapAlloc(GetProcessHeap(),0,cnt);
|
2003-12-02 06:33:26 +01:00
|
|
|
hres = AVIStreamReadFormat(vids,0,bmi,&cnt);
|
1999-02-05 10:41:55 +01:00
|
|
|
if (hres) {
|
|
|
|
fprintf(stderr,"AVIStreamReadFormat vids: 0x%08lx\n",hres);
|
|
|
|
exit(1);
|
|
|
|
}
|
1999-02-09 15:08:29 +01:00
|
|
|
vidgetframe = NULL;
|
1999-02-05 10:41:55 +01:00
|
|
|
bmi->biCompression = 0; /* we want it in raw form, uncompressed */
|
1999-02-09 15:08:29 +01:00
|
|
|
/* recalculate the image size */
|
|
|
|
bmi->biSizeImage = ((bmi->biWidth*bmi->biBitCount+31)&~0x1f)*bmi->biPlanes*bmi->biHeight/8;
|
|
|
|
bytesline = ((bmi->biWidth*bmi->biBitCount+31)&~0x1f)*bmi->biPlanes/8;
|
2003-12-02 06:33:26 +01:00
|
|
|
vidgetframe = AVIStreamGetFrameOpen(vids,bmi);
|
1999-02-05 10:41:55 +01:00
|
|
|
if (!vidgetframe) {
|
1999-02-09 15:08:29 +01:00
|
|
|
fprintf(stderr,"AVIStreamGetFrameOpen: failed\n");
|
1999-02-05 10:41:55 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
/********************* end video setup ***********************************/
|
|
|
|
|
|
|
|
/********************* begin display setup *******************************/
|
|
|
|
hres = DirectDrawCreate(NULL,&ddraw,NULL);
|
|
|
|
if (hres) {
|
|
|
|
fprintf(stderr,"DirectDrawCreate: 0x%08lx\n",hres);
|
|
|
|
exit(1);
|
|
|
|
}
|
1999-10-31 02:59:23 +01:00
|
|
|
hres = IDirectDraw_SetDisplayMode(ddraw,bmi->biWidth,bmi->biHeight,bmi->biBitCount);
|
1999-02-05 10:41:55 +01:00
|
|
|
if (hres) {
|
|
|
|
fprintf(stderr,"ddraw.SetDisplayMode: 0x%08lx (change resolution!)\n",hres);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
dsdesc.dwSize=sizeof(dsdesc);
|
|
|
|
dsdesc.dwFlags = DDSD_CAPS;
|
|
|
|
dsdesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
|
1999-10-31 02:59:23 +01:00
|
|
|
hres = IDirectDraw_CreateSurface(ddraw,&dsdesc,&dsurf,NULL);
|
1999-02-05 10:41:55 +01:00
|
|
|
if (hres) {
|
|
|
|
fprintf(stderr,"ddraw.CreateSurface: 0x%08lx\n",hres);
|
|
|
|
exit(1);
|
|
|
|
}
|
1999-02-09 15:08:29 +01:00
|
|
|
if (bmi->biBitCount==8) {
|
|
|
|
RGBQUAD *rgb = (RGBQUAD*)(bmi+1);
|
|
|
|
int i;
|
|
|
|
|
1999-10-31 02:59:23 +01:00
|
|
|
hres = IDirectDraw_CreatePalette(ddraw,DDPCAPS_8BIT,NULL,&dpal,NULL);
|
1999-02-09 15:08:29 +01:00
|
|
|
if (hres) {
|
|
|
|
fprintf(stderr,"ddraw.CreateSurface: 0x%08lx\n",hres);
|
|
|
|
exit(1);
|
|
|
|
}
|
1999-10-31 02:59:23 +01:00
|
|
|
IDirectDrawSurface_SetPalette(dsurf,dpal);
|
1999-02-09 15:08:29 +01:00
|
|
|
for (i=0;i<bmi->biClrUsed;i++) {
|
|
|
|
palent[i].peRed = rgb[i].rgbRed;
|
|
|
|
palent[i].peBlue = rgb[i].rgbBlue;
|
|
|
|
palent[i].peGreen = rgb[i].rgbGreen;
|
|
|
|
}
|
1999-10-31 02:59:23 +01:00
|
|
|
IDirectDrawPalette_SetEntries(dpal,0,0,bmi->biClrUsed,palent);
|
1999-02-09 15:08:29 +01:00
|
|
|
} else
|
|
|
|
dpal = NULL;
|
1999-02-05 10:41:55 +01:00
|
|
|
/********************* end display setup *******************************/
|
|
|
|
|
|
|
|
tstart = time(NULL);
|
|
|
|
pos = 0;
|
|
|
|
while (1) {
|
|
|
|
LPVOID decodedframe;
|
|
|
|
LPBITMAPINFOHEADER lpbmi;
|
|
|
|
LPVOID decodedbits;
|
|
|
|
|
|
|
|
/* video stuff */
|
2003-12-02 06:33:26 +01:00
|
|
|
if (!(decodedframe=AVIStreamGetFrame(vidgetframe,pos++)))
|
1999-02-05 10:41:55 +01:00
|
|
|
break;
|
|
|
|
lpbmi = (LPBITMAPINFOHEADER)decodedframe;
|
|
|
|
decodedbits = (LPVOID)(((DWORD)decodedframe)+lpbmi->biSize);
|
1999-02-09 15:08:29 +01:00
|
|
|
if (lpbmi->biBitCount == 8) {
|
2004-03-24 00:20:16 +01:00
|
|
|
/* can't detect palette change that way I think */
|
1999-02-09 15:08:29 +01:00
|
|
|
RGBQUAD *rgb = (RGBQUAD*)(lpbmi+1);
|
|
|
|
int i,palchanged;
|
|
|
|
|
|
|
|
/* skip used colorentries. */
|
2000-03-24 21:40:41 +01:00
|
|
|
decodedbits=(char*)decodedbits+bmi->biClrUsed*sizeof(RGBQUAD);
|
1999-02-09 15:08:29 +01:00
|
|
|
palchanged = 0;
|
|
|
|
for (i=0;i<bmi->biClrUsed;i++) {
|
|
|
|
if ( (palent[i].peRed != rgb[i].rgbRed) ||
|
|
|
|
(palent[i].peBlue != rgb[i].rgbBlue) ||
|
|
|
|
(palent[i].peGreen != rgb[i].rgbGreen)
|
|
|
|
) {
|
|
|
|
palchanged = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (palchanged) {
|
|
|
|
for (i=0;i<bmi->biClrUsed;i++) {
|
|
|
|
palent[i].peRed = rgb[i].rgbRed;
|
|
|
|
palent[i].peBlue = rgb[i].rgbBlue;
|
|
|
|
palent[i].peGreen = rgb[i].rgbGreen;
|
|
|
|
}
|
1999-10-31 02:59:23 +01:00
|
|
|
IDirectDrawPalette_SetEntries(dpal,0,0,bmi->biClrUsed,palent);
|
1999-02-09 15:08:29 +01:00
|
|
|
}
|
|
|
|
}
|
1999-02-05 10:41:55 +01:00
|
|
|
dsdesc.dwSize = sizeof(dsdesc);
|
1999-10-31 02:59:23 +01:00
|
|
|
hres = IDirectDrawSurface_Lock(dsurf,NULL,&dsdesc,DDLOCK_WRITEONLY,0);
|
1999-02-05 10:41:55 +01:00
|
|
|
if (hres) {
|
|
|
|
fprintf(stderr,"dsurf.Lock: 0x%08lx\n",hres);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
/* Argh. AVIs are upside down. */
|
|
|
|
for (i=0;i<dsdesc.dwHeight;i++) {
|
2001-01-11 01:54:38 +01:00
|
|
|
memcpy( (char *)dsdesc.lpSurface+(i*dsdesc.u1.lPitch),
|
2000-03-24 21:40:41 +01:00
|
|
|
(char *)decodedbits+bytesline*(dsdesc.dwHeight-i-1),
|
1999-02-05 10:41:55 +01:00
|
|
|
bytesline
|
|
|
|
);
|
|
|
|
}
|
2001-01-11 01:54:38 +01:00
|
|
|
IDirectDrawSurface_Unlock(dsurf,dsdesc.lpSurface);
|
1999-02-05 10:41:55 +01:00
|
|
|
}
|
|
|
|
tend = time(NULL);
|
2003-12-02 06:33:26 +01:00
|
|
|
AVIStreamGetFrameClose(vidgetframe);
|
1999-02-05 10:41:55 +01:00
|
|
|
|
1999-10-31 02:59:23 +01:00
|
|
|
IDirectDrawSurface_Release(dsurf);
|
|
|
|
IDirectDraw_RestoreDisplayMode(ddraw);
|
|
|
|
IDirectDraw_Release(ddraw);
|
2003-12-02 06:33:26 +01:00
|
|
|
if (vids) AVIStreamRelease(vids);
|
|
|
|
if (auds) AVIStreamRelease(auds);
|
1999-02-05 10:41:55 +01:00
|
|
|
fprintf(stderr,"%d frames at %g frames/s\n",pos,pos*1.0/(tend-tstart));
|
2003-12-02 06:33:26 +01:00
|
|
|
AVIFileRelease(avif);
|
|
|
|
AVIFileExit();
|
2002-06-01 01:06:46 +02:00
|
|
|
return 0;
|
1999-02-05 10:41:55 +01:00
|
|
|
}
|