Rbuild changes for include structure change.
[reactos.git] / reactos / dll / win32 / avifil32 / getframe.c
1 /*
2 * Copyright 2002-2003 Michael Günnewig
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 #define COM_NO_WINDOWS_H
20 #include <assert.h>
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winnls.h"
26 #include "windowsx.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29
30 #include "vfw.h"
31 #include "mmreg.h"
32
33 #include "avifile_private.h"
34
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(avifile);
38
39 #ifndef DIBPTR
40 #define DIBPTR(lp) ((LPBYTE)(lp) + (lp)->biSize + \
41 (lp)->biClrUsed * sizeof(RGBQUAD))
42 #endif
43
44 /***********************************************************************/
45
46 static HRESULT WINAPI IGetFrame_fnQueryInterface(IGetFrame *iface,
47 REFIID refiid, LPVOID *obj);
48 static ULONG WINAPI IGetFrame_fnAddRef(IGetFrame *iface);
49 static ULONG WINAPI IGetFrame_fnRelease(IGetFrame *iface);
50 static LPVOID WINAPI IGetFrame_fnGetFrame(IGetFrame *iface, LONG lPos);
51 static HRESULT WINAPI IGetFrame_fnBegin(IGetFrame *iface, LONG lStart,
52 LONG lEnd, LONG lRate);
53 static HRESULT WINAPI IGetFrame_fnEnd(IGetFrame *iface);
54 static HRESULT WINAPI IGetFrame_fnSetFormat(IGetFrame *iface,
55 LPBITMAPINFOHEADER lpbi,
56 LPVOID lpBits, INT x, INT y,
57 INT dx, INT dy);
58
59 static const struct IGetFrameVtbl igetframeVtbl = {
60 IGetFrame_fnQueryInterface,
61 IGetFrame_fnAddRef,
62 IGetFrame_fnRelease,
63 IGetFrame_fnGetFrame,
64 IGetFrame_fnBegin,
65 IGetFrame_fnEnd,
66 IGetFrame_fnSetFormat
67 };
68
69 typedef struct _IGetFrameImpl {
70 /* IUnknown stuff */
71 const IGetFrameVtbl *lpVtbl;
72 LONG ref;
73
74 /* IGetFrame stuff */
75 BOOL bFixedStream;
76 PAVISTREAM pStream;
77
78 LPVOID lpInBuffer;
79 LONG cbInBuffer;
80 LPBITMAPINFOHEADER lpInFormat;
81 LONG cbInFormat;
82
83 LONG lCurrentFrame;
84 LPBITMAPINFOHEADER lpOutFormat;
85 LPVOID lpOutBuffer;
86
87 HIC hic;
88 BOOL bResize;
89 DWORD x;
90 DWORD y;
91 DWORD dx;
92 DWORD dy;
93
94 BOOL bFormatChanges;
95 DWORD dwFormatChangeCount;
96 DWORD dwEditCount;
97 } IGetFrameImpl;
98
99 /***********************************************************************/
100
101 static void AVIFILE_CloseCompressor(IGetFrameImpl *This)
102 {
103 if (This->lpOutFormat != NULL && This->lpInFormat != This->lpOutFormat) {
104 GlobalFreePtr(This->lpOutFormat);
105 This->lpOutFormat = NULL;
106 }
107 if (This->lpInFormat != NULL) {
108 GlobalFreePtr(This->lpInFormat);
109 This->lpInFormat = NULL;
110 }
111 if (This->hic != NULL) {
112 if (This->bResize)
113 ICDecompressExEnd(This->hic);
114 else
115 ICDecompressEnd(This->hic);
116 ICClose(This->hic);
117 This->hic = NULL;
118 }
119 }
120
121 PGETFRAME AVIFILE_CreateGetFrame(PAVISTREAM pStream)
122 {
123 IGetFrameImpl *pg;
124
125 /* check parameter */
126 if (pStream == NULL)
127 return NULL;
128
129 pg = (IGetFrameImpl*)LocalAlloc(LPTR, sizeof(IGetFrameImpl));
130 if (pg != NULL) {
131 pg->lpVtbl = &igetframeVtbl;
132 pg->ref = 1;
133 pg->lCurrentFrame = -1;
134 pg->pStream = pStream;
135 IAVIStream_AddRef(pStream);
136 }
137
138 return (PGETFRAME)pg;
139 }
140
141 static HRESULT WINAPI IGetFrame_fnQueryInterface(IGetFrame *iface,
142 REFIID refiid, LPVOID *obj)
143 {
144 IGetFrameImpl *This = (IGetFrameImpl *)iface;
145
146 TRACE("(%p,%s,%p)\n", This, debugstr_guid(refiid), obj);
147
148 if (IsEqualGUID(&IID_IUnknown, refiid) ||
149 IsEqualGUID(&IID_IGetFrame, refiid)) {
150 *obj = iface;
151 return S_OK;
152 }
153
154 return OLE_E_ENUM_NOMORE;
155 }
156
157 static ULONG WINAPI IGetFrame_fnAddRef(IGetFrame *iface)
158 {
159 IGetFrameImpl *This = (IGetFrameImpl *)iface;
160 ULONG ref = InterlockedIncrement(&This->ref);
161
162 TRACE("(%p)\n", iface);
163
164 return ref;
165 }
166
167 static ULONG WINAPI IGetFrame_fnRelease(IGetFrame *iface)
168 {
169 IGetFrameImpl *This = (IGetFrameImpl *)iface;
170 ULONG ref = InterlockedDecrement(&This->ref);
171
172 TRACE("(%p)\n", iface);
173
174 if (!ref) {
175 AVIFILE_CloseCompressor(This);
176 if (This->pStream != NULL) {
177 IAVIStream_Release(This->pStream);
178 This->pStream = NULL;
179 }
180
181 LocalFree((HLOCAL)iface);
182 return 0;
183 }
184
185 return ref;
186 }
187
188 static LPVOID WINAPI IGetFrame_fnGetFrame(IGetFrame *iface, LONG lPos)
189 {
190 IGetFrameImpl *This = (IGetFrameImpl *)iface;
191
192 LONG readBytes;
193 LONG readSamples;
194
195 TRACE("(%p,%ld)\n", iface, lPos);
196
197 /* We don't want negative start values! -- marks invalid buffer content */
198 if (lPos < 0)
199 return NULL;
200
201 /* check state */
202 if (This->pStream == NULL)
203 return NULL;
204 if (This->lpInFormat == NULL)
205 return NULL;
206
207 /* Could stream have changed? */
208 if (! This->bFixedStream) {
209 AVISTREAMINFOW sInfo;
210
211 IAVIStream_Info(This->pStream, &sInfo, sizeof(sInfo));
212
213 if (sInfo.dwEditCount != This->dwEditCount) {
214 This->dwEditCount = sInfo.dwEditCount;
215 This->lCurrentFrame = -1;
216 }
217
218 if (sInfo.dwFormatChangeCount != This->dwFormatChangeCount) {
219 /* stream has changed */
220 if (This->lpOutFormat != NULL) {
221 BITMAPINFOHEADER bi;
222
223 memcpy(&bi, This->lpOutFormat, sizeof(bi));
224 AVIFILE_CloseCompressor(This);
225
226 if (FAILED(IGetFrame_SetFormat(iface, &bi, NULL, 0, 0, -1, -1))) {
227 if (FAILED(IGetFrame_SetFormat(iface, NULL, NULL, 0, 0, -1, -1)))
228 return NULL;
229 }
230 } else if (FAILED(IGetFrame_SetFormat(iface, NULL, NULL, 0, 0, -1, -1)))
231 return NULL;
232 }
233 }
234
235 if (lPos != This->lCurrentFrame) {
236 LONG lNext = IAVIStream_FindSample(This->pStream,lPos,FIND_KEY|FIND_PREV);
237
238 if (lNext == -1)
239 return NULL; /* frame doesn't exist */
240 if (lNext <= This->lCurrentFrame && This->lCurrentFrame < lPos)
241 lNext = This->lCurrentFrame + 1;
242
243 for (; lNext <= lPos; lNext++) {
244 /* new format for this frame? */
245 if (This->bFormatChanges) {
246 IAVIStream_ReadFormat(This->pStream, lNext,
247 This->lpInFormat, &This->cbInFormat);
248 if (This->lpOutFormat != NULL) {
249 if (This->lpOutFormat->biBitCount <= 8)
250 ICDecompressGetPalette(This->hic, This->lpInFormat,
251 This->lpOutFormat);
252 }
253 }
254
255 /* read input frame */
256 while (FAILED(AVIStreamRead(This->pStream, lNext, 1, This->lpInBuffer,
257 This->cbInBuffer, &readBytes, &readSamples))) {
258 /* not enough memory for input buffer? */
259 readBytes = 0;
260 if (FAILED(AVIStreamSampleSize(This->pStream, lNext, &readBytes)))
261 return NULL; /* bad thing, but bad things will happen */
262 if (readBytes <= 0) {
263 ERR(": IAVIStream::REad doesn't return needed bytes!\n");
264 return NULL;
265 }
266
267 /* IAVIStream::Read failed because of other reasons not buffersize? */
268 if (This->cbInBuffer >= readBytes)
269 break;
270 This->cbInBuffer = This->cbInFormat + readBytes;
271 This->lpInFormat = GlobalReAllocPtr(This->lpInFormat, This->cbInBuffer, 0);
272 if (This->lpInFormat == NULL)
273 return NULL; /* out of memory */
274 This->lpInBuffer = (BYTE*)This->lpInFormat + This->cbInFormat;
275 }
276
277 if (readSamples != 1) {
278 ERR(": no frames read\n");
279 return NULL;
280 }
281 if (readBytes != 0) {
282 This->lpInFormat->biSizeImage = readBytes;
283
284 /* nothing to decompress? */
285 if (This->hic == NULL) {
286 This->lCurrentFrame = lPos;
287 return This->lpInFormat;
288 }
289
290 if (This->bResize) {
291 ICDecompressEx(This->hic,0,This->lpInFormat,This->lpInBuffer,0,0,
292 This->lpInFormat->biWidth,This->lpInFormat->biHeight,
293 This->lpOutFormat,This->lpOutBuffer,This->x,This->y,
294 This->dx,This->dy);
295 } else {
296 ICDecompress(This->hic, 0, This->lpInFormat, This->lpInBuffer,
297 This->lpOutFormat, This->lpOutBuffer);
298 }
299 }
300 } /* for (lNext < lPos) */
301 } /* if (This->lCurrentFrame != lPos) */
302
303 return (This->hic == NULL ? This->lpInFormat : This->lpOutFormat);
304 }
305
306 static HRESULT WINAPI IGetFrame_fnBegin(IGetFrame *iface, LONG lStart,
307 LONG lEnd, LONG lRate)
308 {
309 IGetFrameImpl *This = (IGetFrameImpl *)iface;
310
311 TRACE("(%p,%ld,%ld,%ld)\n", iface, lStart, lEnd, lRate);
312
313 This->bFixedStream = TRUE;
314
315 return (IGetFrame_GetFrame(iface, lStart) ? AVIERR_OK : AVIERR_ERROR);
316 }
317
318 static HRESULT WINAPI IGetFrame_fnEnd(IGetFrame *iface)
319 {
320 IGetFrameImpl *This = (IGetFrameImpl *)iface;
321
322 TRACE("(%p)\n", iface);
323
324 This->bFixedStream = FALSE;
325
326 return AVIERR_OK;
327 }
328
329 static HRESULT WINAPI IGetFrame_fnSetFormat(IGetFrame *iface,
330 LPBITMAPINFOHEADER lpbiWanted,
331 LPVOID lpBits, INT x, INT y,
332 INT dx, INT dy)
333 {
334 IGetFrameImpl *This = (IGetFrameImpl *)iface;
335
336 AVISTREAMINFOW sInfo;
337 LPBITMAPINFOHEADER lpbi = lpbiWanted;
338 BOOL bBestDisplay = FALSE;
339
340 TRACE("(%p,%p,%p,%d,%d,%d,%d)\n", iface, lpbiWanted, lpBits,
341 x, y, dx, dy);
342
343 if (This->pStream == NULL)
344 return AVIERR_ERROR;
345
346 if (lpbiWanted == (LPBITMAPINFOHEADER)AVIGETFRAMEF_BESTDISPLAYFMT) {
347 lpbi = NULL;
348 bBestDisplay = TRUE;
349 }
350
351 IAVIStream_Info(This->pStream, &sInfo, sizeof(sInfo));
352 if (sInfo.fccType != streamtypeVIDEO)
353 return AVIERR_UNSUPPORTED;
354
355 This->bFormatChanges =
356 (sInfo.dwFlags & AVISTREAMINFO_FORMATCHANGES ? TRUE : FALSE );
357 This->dwFormatChangeCount = sInfo.dwFormatChangeCount;
358 This->dwEditCount = sInfo.dwEditCount;
359 This->lCurrentFrame = -1;
360
361 /* get input format from stream */
362 if (This->lpInFormat == NULL) {
363 HRESULT hr;
364
365 This->cbInBuffer = (LONG)sInfo.dwSuggestedBufferSize;
366 if (This->cbInBuffer == 0)
367 This->cbInBuffer = 1024;
368
369 IAVIStream_ReadFormat(This->pStream, sInfo.dwStart,
370 NULL, &This->cbInFormat);
371
372 This->lpInFormat =
373 (LPBITMAPINFOHEADER)GlobalAllocPtr(GHND, This->cbInFormat + This->cbInBuffer);
374 if (This->lpInFormat == NULL) {
375 AVIFILE_CloseCompressor(This);
376 return AVIERR_MEMORY;
377 }
378
379 hr = IAVIStream_ReadFormat(This->pStream, sInfo.dwStart, This->lpInFormat, &This->cbInFormat);
380 if (FAILED(hr)) {
381 AVIFILE_CloseCompressor(This);
382 return hr;
383 }
384
385 This->lpInBuffer = ((LPBYTE)This->lpInFormat) + This->cbInFormat;
386 }
387
388 /* check input format */
389 if (This->lpInFormat->biClrUsed == 0 && This->lpInFormat->biBitCount <= 8)
390 This->lpInFormat->biClrUsed = 1u << This->lpInFormat->biBitCount;
391 if (This->lpInFormat->biSizeImage == 0 &&
392 This->lpInFormat->biCompression == BI_RGB) {
393 This->lpInFormat->biSizeImage =
394 DIBWIDTHBYTES(*This->lpInFormat) * This->lpInFormat->biHeight;
395 }
396
397 /* only to pass through? */
398 if (This->lpInFormat->biCompression == BI_RGB && lpBits == NULL) {
399 if (lpbi == NULL ||
400 (lpbi->biCompression == BI_RGB &&
401 lpbi->biWidth == This->lpInFormat->biWidth &&
402 lpbi->biHeight == This->lpInFormat->biHeight &&
403 lpbi->biBitCount == This->lpInFormat->biBitCount)) {
404 This->lpOutFormat = This->lpInFormat;
405 This->lpOutBuffer = DIBPTR(This->lpInFormat);
406 return AVIERR_OK;
407 }
408 }
409
410 /* need memory for output format? */
411 if (This->lpOutFormat == NULL) {
412 This->lpOutFormat =
413 (LPBITMAPINFOHEADER)GlobalAllocPtr(GHND, sizeof(BITMAPINFOHEADER)
414 + 256 * sizeof(RGBQUAD));
415 if (This->lpOutFormat == NULL) {
416 AVIFILE_CloseCompressor(This);
417 return AVIERR_MEMORY;
418 }
419 }
420
421 /* need handle to video compressor */
422 if (This->hic == NULL) {
423 FOURCC fccHandler;
424
425 if (This->lpInFormat->biCompression == BI_RGB)
426 fccHandler = comptypeDIB;
427 else if (This->lpInFormat->biCompression == BI_RLE8)
428 fccHandler = mmioFOURCC('R','L','E',' ');
429 else
430 fccHandler = sInfo.fccHandler;
431
432 if (lpbi != NULL) {
433 if (lpbi->biWidth == 0)
434 lpbi->biWidth = This->lpInFormat->biWidth;
435 if (lpbi->biHeight == 0)
436 lpbi->biHeight = This->lpInFormat->biHeight;
437 }
438
439 This->hic = ICLocate(ICTYPE_VIDEO, fccHandler, This->lpInFormat, lpbi, ICMODE_DECOMPRESS);
440 if (This->hic == NULL) {
441 AVIFILE_CloseCompressor(This);
442 return AVIERR_NOCOMPRESSOR;
443 }
444 }
445
446 /* output format given? */
447 if (lpbi != NULL) {
448 /* check the given output format ... */
449 if (lpbi->biClrUsed == 0 && lpbi->biBitCount <= 8)
450 lpbi->biClrUsed = 1u << lpbi->biBitCount;
451
452 /* ... and remember it */
453 memcpy(This->lpOutFormat, lpbi,
454 lpbi->biSize + lpbi->biClrUsed * sizeof(RGBQUAD));
455 if (lpbi->biBitCount <= 8)
456 ICDecompressGetPalette(This->hic, This->lpInFormat, This->lpOutFormat);
457
458 return AVIERR_OK;
459 } else {
460 if (bBestDisplay) {
461 ICGetDisplayFormat(This->hic, This->lpInFormat,
462 This->lpOutFormat, 0, dx, dy);
463 } else if (ICDecompressGetFormat(This->hic, This->lpInFormat,
464 This->lpOutFormat) < 0) {
465 AVIFILE_CloseCompressor(This);
466 return AVIERR_NOCOMPRESSOR;
467 }
468
469 /* check output format */
470 if (This->lpOutFormat->biClrUsed == 0 &&
471 This->lpOutFormat->biBitCount <= 8)
472 This->lpOutFormat->biClrUsed = 1u << This->lpOutFormat->biBitCount;
473 if (This->lpOutFormat->biSizeImage == 0 &&
474 This->lpOutFormat->biCompression == BI_RGB) {
475 This->lpOutFormat->biSizeImage =
476 DIBWIDTHBYTES(*This->lpOutFormat) * This->lpOutFormat->biHeight;
477 }
478
479 if (lpBits == NULL) {
480 register DWORD size = This->lpOutFormat->biClrUsed * sizeof(RGBQUAD);
481
482 size += This->lpOutFormat->biSize + This->lpOutFormat->biSizeImage;
483 This->lpOutFormat =
484 (LPBITMAPINFOHEADER)GlobalReAllocPtr(This->lpOutFormat, size, GMEM_MOVEABLE);
485 if (This->lpOutFormat == NULL) {
486 AVIFILE_CloseCompressor(This);
487 return AVIERR_MEMORY;
488 }
489 This->lpOutBuffer = DIBPTR(This->lpOutFormat);
490 } else
491 This->lpOutBuffer = lpBits;
492
493 /* for user size was irrelevant */
494 if (dx == -1)
495 dx = This->lpOutFormat->biWidth;
496 if (dy == -1)
497 dy = This->lpOutFormat->biHeight;
498
499 /* need to resize? */
500 if (x != 0 || y != 0) {
501 if (dy == This->lpOutFormat->biHeight &&
502 dx == This->lpOutFormat->biWidth)
503 This->bResize = FALSE;
504 else
505 This->bResize = TRUE;
506 }
507
508 if (This->bResize) {
509 This->x = x;
510 This->y = y;
511 This->dx = dx;
512 This->dy = dy;
513
514 if (ICDecompressExBegin(This->hic,0,This->lpInFormat,This->lpInBuffer,0,
515 0,This->lpInFormat->biWidth,
516 This->lpInFormat->biHeight,This->lpOutFormat,
517 This->lpOutBuffer, x, y, dx, dy) == ICERR_OK)
518 return AVIERR_OK;
519 } else if (ICDecompressBegin(This->hic, This->lpInFormat,
520 This->lpOutFormat) == ICERR_OK)
521 return AVIERR_OK;
522
523 AVIFILE_CloseCompressor(This);
524
525 return AVIERR_COMPRESSOR;
526 }
527 }
528
529 /***********************************************************************/