Added regedit unit test, a couple minor changes to regedit.
[wine] / programs / avitools / aviplay.c
1 /*
2  * Very simple AVIPLAYER
3  *
4  * Copyright 1999 Marcus Meissner
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * Status:
21  *      - plays .avi streams, video only
22  *      - requires MicroSoft avifil32.dll and builtin msvfw32.dll.
23  *
24  * Todo:
25  *      - audio support (including synchronization etc)
26  *      - replace DirectDraw by a 'normal' window, including dithering, controls
27  *        etc.
28  *
29  * Bugs:
30  *      - no time scheduling, video plays too fast using DirectDraw/XF86DGA
31  *      - requires DirectDraw with all disadvantages.
32  */
33
34 #include <stdio.h>
35 #include <time.h>
36 #include <assert.h>
37 #include <string.h>
38 #include "windows.h"
39 #include "wingdi.h"
40 #include "mmsystem.h"
41 #include "ddraw.h"
42 #include "vfw.h"
43
44
45 int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
46 {
47     int                 bytesline,i,n,pos;
48     time_t              tstart,tend;
49     LONG                cnt;
50     BITMAPINFOHEADER    *bmi;
51     HRESULT             hres;
52     HMODULE             avifil32 = LoadLibrary("avifil32.dll");
53     PAVIFILE            avif;
54     PAVISTREAM          vids=NULL,auds=NULL;
55     AVIFILEINFO         afi;
56     AVISTREAMINFO       asi;
57     PGETFRAME           vidgetframe=NULL;
58     LPDIRECTDRAW        ddraw;
59     DDSURFACEDESC       dsdesc;
60     LPDIRECTDRAWSURFACE dsurf;
61     LPDIRECTDRAWPALETTE dpal;
62     PALETTEENTRY        palent[256];
63
64
65 void    (WINAPI *fnAVIFileInit)(void);
66 void    (WINAPI *fnAVIFileExit)(void);
67 ULONG   (WINAPI *fnAVIFileRelease)(PAVIFILE);
68 ULONG   (WINAPI *fnAVIStreamRelease)(PAVISTREAM);
69 HRESULT (WINAPI *fnAVIFileOpen)(PAVIFILE * ppfile,LPCTSTR szFile,UINT uMode,LPCLSID lpHandler);
70 HRESULT (WINAPI *fnAVIFileInfo)(PAVIFILE ppfile,AVIFILEINFO *afi,LONG size);
71 HRESULT (WINAPI *fnAVIFileGetStream)(PAVIFILE ppfile,PAVISTREAM *afi,DWORD fccType,LONG lParam);
72 HRESULT (WINAPI *fnAVIStreamInfo)(PAVISTREAM iface,AVISTREAMINFO *afi,LONG size);
73 HRESULT (WINAPI *fnAVIStreamReadFormat)(PAVISTREAM iface,LONG pos,LPVOID format,LPLONG size);
74 PGETFRAME (WINAPI *fnAVIStreamGetFrameOpen)(PAVISTREAM iface,LPBITMAPINFOHEADER wanted);
75 LPVOID (WINAPI *fnAVIStreamGetFrame)(PGETFRAME pg,LONG pos);
76 HRESULT (WINAPI *fnAVIStreamGetFrameClose)(PGETFRAME pg);
77
78 #define XX(x) fn##x = (void*)GetProcAddress(avifil32,#x);assert(fn##x);
79 #ifdef UNICODE
80 # define XXT(x) fn##x = (void*)GetProcAddress(avifil32,#x"W");assert(fn##x);
81 #else
82 # define XXT(x) fn##x = (void*)GetProcAddress(avifil32,#x"A");assert(fn##x);
83 #endif
84         /* non character dependend routines: */
85         XX (AVIFileInit);
86         XX (AVIFileExit);
87         XX (AVIFileRelease);
88         XX (AVIFileGetStream);
89         XX (AVIStreamRelease);
90         XX (AVIStreamReadFormat);
91         XX (AVIStreamGetFrameOpen);
92         XX (AVIStreamGetFrame);
93         XX (AVIStreamGetFrameClose);
94         /* A/W routines: */
95         XXT(AVIFileOpen);
96         XXT(AVIFileInfo);
97         XXT(AVIStreamInfo);
98 #undef XX
99 #undef XXT
100
101
102     fnAVIFileInit();
103     if (-1==GetFileAttributes(cmdline)) {
104         fprintf(stderr,"Usage: aviplay <avifilename>\n");
105         exit(1);
106     }
107     hres = fnAVIFileOpen(&avif,cmdline,OF_READ,NULL);
108     if (hres) {
109         fprintf(stderr,"AVIFileOpen: 0x%08lx\n",hres);
110         exit(1);
111     }
112     hres = fnAVIFileInfo(avif,&afi,sizeof(afi));
113     if (hres) {
114         fprintf(stderr,"AVIFileInfo: 0x%08lx\n",hres);
115         exit(1);
116     }
117     for (n=0;n<afi.dwStreams;n++) {
118             char buf[5];
119             PAVISTREAM  ast;
120
121             hres = fnAVIFileGetStream(avif,&ast,0,n);
122             if (hres) {
123                 fprintf(stderr,"AVIFileGetStream %d: 0x%08lx\n",n,hres);
124                 exit(1);
125             }
126             hres = fnAVIStreamInfo(ast,&asi,sizeof(asi));
127             if (hres) {
128                 fprintf(stderr,"AVIStreamInfo %d: 0x%08lx\n",n,hres);
129                 exit(1);
130             }
131             fprintf(stderr,"[Stream %d: ",n);
132             buf[4]='\0';memcpy(buf,&(asi.fccType),4);
133             fprintf(stderr,"%s.",buf);
134             buf[4]='\0';memcpy(buf,&(asi.fccHandler),4);
135             fprintf(stderr,"%s, %s]\n",buf,asi.szName);
136             switch (asi.fccType) {
137             case streamtypeVIDEO:
138                 vids = ast;
139                 break;
140             case streamtypeAUDIO:
141                 auds = ast;
142                 break;
143             default:  {
144                 char type[5];
145                 type[4]='\0';memcpy(type,&(asi.fccType),4);
146
147                 fprintf(stderr,"Unhandled streamtype %s\n",type);
148                 fnAVIStreamRelease(ast);
149                 break;
150             }
151             }
152     }
153 /********************* begin video setup ***********************************/
154     if (!vids) {
155         fprintf(stderr,"No video stream found. Good Bye.\n");
156         exit(0);
157     }
158     cnt = sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD);
159     bmi = HeapAlloc(GetProcessHeap(),0,cnt);
160     hres = fnAVIStreamReadFormat(vids,0,bmi,&cnt);
161     if (hres) {
162         fprintf(stderr,"AVIStreamReadFormat vids: 0x%08lx\n",hres);
163         exit(1);
164     }
165     vidgetframe = NULL;
166     bmi->biCompression = 0; /* we want it in raw form, uncompressed */
167     /* recalculate the image size */
168     bmi->biSizeImage = ((bmi->biWidth*bmi->biBitCount+31)&~0x1f)*bmi->biPlanes*bmi->biHeight/8;
169     bytesline = ((bmi->biWidth*bmi->biBitCount+31)&~0x1f)*bmi->biPlanes/8;
170     vidgetframe = fnAVIStreamGetFrameOpen(vids,bmi);
171     if (!vidgetframe) {
172         fprintf(stderr,"AVIStreamGetFrameOpen: failed\n");
173         exit(1);
174     }
175 /********************* end video setup ***********************************/
176
177 /********************* begin display setup *******************************/
178     hres = DirectDrawCreate(NULL,&ddraw,NULL);
179     if (hres) {
180         fprintf(stderr,"DirectDrawCreate: 0x%08lx\n",hres);
181         exit(1);
182     }
183     hres = IDirectDraw_SetDisplayMode(ddraw,bmi->biWidth,bmi->biHeight,bmi->biBitCount);
184     if (hres) {
185         fprintf(stderr,"ddraw.SetDisplayMode: 0x%08lx (change resolution!)\n",hres);
186         exit(1);
187     }
188     dsdesc.dwSize=sizeof(dsdesc);
189     dsdesc.dwFlags = DDSD_CAPS;
190     dsdesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
191     hres = IDirectDraw_CreateSurface(ddraw,&dsdesc,&dsurf,NULL);
192     if (hres) {
193         fprintf(stderr,"ddraw.CreateSurface: 0x%08lx\n",hres);
194         exit(1);
195     }
196     if (bmi->biBitCount==8) {
197         RGBQUAD         *rgb = (RGBQUAD*)(bmi+1);
198         int             i;
199
200         hres = IDirectDraw_CreatePalette(ddraw,DDPCAPS_8BIT,NULL,&dpal,NULL);
201         if (hres) {
202             fprintf(stderr,"ddraw.CreateSurface: 0x%08lx\n",hres);
203             exit(1);
204         }
205         IDirectDrawSurface_SetPalette(dsurf,dpal);
206         for (i=0;i<bmi->biClrUsed;i++) {
207             palent[i].peRed = rgb[i].rgbRed;
208             palent[i].peBlue = rgb[i].rgbBlue;
209             palent[i].peGreen = rgb[i].rgbGreen;
210         }
211         IDirectDrawPalette_SetEntries(dpal,0,0,bmi->biClrUsed,palent);
212     } else
213         dpal = NULL;
214 /********************* end display setup *******************************/
215
216     tstart = time(NULL);
217     pos = 0;
218     while (1) {
219         LPVOID          decodedframe;
220         LPBITMAPINFOHEADER lpbmi;
221         LPVOID          decodedbits;
222
223 /* video stuff */
224         if (!(decodedframe=fnAVIStreamGetFrame(vidgetframe,pos++)))
225             break;
226         lpbmi = (LPBITMAPINFOHEADER)decodedframe;
227         decodedbits = (LPVOID)(((DWORD)decodedframe)+lpbmi->biSize);
228         if (lpbmi->biBitCount == 8) {
229         /* cant detect palette change that way I think */
230             RGBQUAD     *rgb = (RGBQUAD*)(lpbmi+1);
231             int         i,palchanged;
232
233             /* skip used colorentries. */
234             decodedbits=(char*)decodedbits+bmi->biClrUsed*sizeof(RGBQUAD);
235             palchanged = 0;
236             for (i=0;i<bmi->biClrUsed;i++) {
237                 if (    (palent[i].peRed != rgb[i].rgbRed) ||
238                         (palent[i].peBlue != rgb[i].rgbBlue) ||
239                         (palent[i].peGreen != rgb[i].rgbGreen)
240                 ) {
241                         palchanged = 1;
242                         break;
243                 }
244             }
245             if (palchanged) {
246                 for (i=0;i<bmi->biClrUsed;i++) {
247                     palent[i].peRed = rgb[i].rgbRed;
248                     palent[i].peBlue = rgb[i].rgbBlue;
249                     palent[i].peGreen = rgb[i].rgbGreen;
250                 }
251                 IDirectDrawPalette_SetEntries(dpal,0,0,bmi->biClrUsed,palent);
252             }
253         }
254         dsdesc.dwSize = sizeof(dsdesc);
255         hres = IDirectDrawSurface_Lock(dsurf,NULL,&dsdesc,DDLOCK_WRITEONLY,0);
256         if (hres) {
257             fprintf(stderr,"dsurf.Lock: 0x%08lx\n",hres);
258             exit(1);
259         }
260         /* Argh. AVIs are upside down. */
261         for (i=0;i<dsdesc.dwHeight;i++) {
262             memcpy( (char *)dsdesc.lpSurface+(i*dsdesc.u1.lPitch),
263                     (char *)decodedbits+bytesline*(dsdesc.dwHeight-i-1),
264                     bytesline
265             );
266         }
267         IDirectDrawSurface_Unlock(dsurf,dsdesc.lpSurface);
268     }
269     tend = time(NULL);
270     fnAVIStreamGetFrameClose(vidgetframe);
271
272     IDirectDrawSurface_Release(dsurf);
273     IDirectDraw_RestoreDisplayMode(ddraw);
274     IDirectDraw_Release(ddraw);
275     if (vids) fnAVIStreamRelease(vids);
276     if (auds) fnAVIStreamRelease(auds);
277     fprintf(stderr,"%d frames at %g frames/s\n",pos,pos*1.0/(tend-tstart));
278     fnAVIFileRelease(avif);
279     fnAVIFileExit();
280     return 0;
281 }