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