Synchronize with trunk r58457.
[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 =
315 (sInfo.dwFlags & AVISTREAMINFO_FORMATCHANGES ? TRUE : FALSE );
316 This->dwFormatChangeCount = sInfo.dwFormatChangeCount;
317 This->dwEditCount = sInfo.dwEditCount;
318 This->lCurrentFrame = -1;
319
320 /* get input format from stream */
321 if (This->lpInFormat == NULL) {
322 HRESULT hr;
323
324 This->cbInBuffer = (LONG)sInfo.dwSuggestedBufferSize;
325 if (This->cbInBuffer == 0)
326 This->cbInBuffer = 1024;
327
328 IAVIStream_ReadFormat(This->pStream, sInfo.dwStart,
329 NULL, &This->cbInFormat);
330
331 This->lpInFormat = HeapAlloc(GetProcessHeap(), 0, This->cbInFormat + This->cbInBuffer);
332 if (This->lpInFormat == NULL) {
333 AVIFILE_CloseCompressor(This);
334 return AVIERR_MEMORY;
335 }
336
337 hr = IAVIStream_ReadFormat(This->pStream, sInfo.dwStart, This->lpInFormat, &This->cbInFormat);
338 if (FAILED(hr)) {
339 AVIFILE_CloseCompressor(This);
340 return hr;
341 }
342
343 This->lpInBuffer = ((LPBYTE)This->lpInFormat) + This->cbInFormat;
344 }
345
346 /* check input format */
347 if (This->lpInFormat->biClrUsed == 0 && This->lpInFormat->biBitCount <= 8)
348 This->lpInFormat->biClrUsed = 1u << This->lpInFormat->biBitCount;
349 if (This->lpInFormat->biSizeImage == 0 &&
350 This->lpInFormat->biCompression == BI_RGB) {
351 This->lpInFormat->biSizeImage =
352 DIBWIDTHBYTES(*This->lpInFormat) * This->lpInFormat->biHeight;
353 }
354
355 /* only to pass through? */
356 if (This->lpInFormat->biCompression == BI_RGB && lpBits == NULL) {
357 if (lpbi == NULL ||
358 (lpbi->biCompression == BI_RGB &&
359 lpbi->biWidth == This->lpInFormat->biWidth &&
360 lpbi->biHeight == This->lpInFormat->biHeight &&
361 lpbi->biBitCount == This->lpInFormat->biBitCount)) {
362 This->lpOutFormat = This->lpInFormat;
363 This->lpOutBuffer = DIBPTR(This->lpInFormat);
364 return AVIERR_OK;
365 }
366 }
367
368 /* need memory for output format? */
369 if (This->lpOutFormat == NULL) {
370 This->lpOutFormat =
371 HeapAlloc(GetProcessHeap(), 0, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
372 if (This->lpOutFormat == NULL) {
373 AVIFILE_CloseCompressor(This);
374 return AVIERR_MEMORY;
375 }
376 }
377
378 /* need handle to video compressor */
379 if (This->hic == NULL) {
380 FOURCC fccHandler;
381
382 if (This->lpInFormat->biCompression == BI_RGB)
383 fccHandler = comptypeDIB;
384 else if (This->lpInFormat->biCompression == BI_RLE8)
385 fccHandler = mmioFOURCC('R','L','E',' ');
386 else
387 fccHandler = sInfo.fccHandler;
388
389 if (lpbi != NULL) {
390 if (lpbi->biWidth == 0)
391 lpbi->biWidth = This->lpInFormat->biWidth;
392 if (lpbi->biHeight == 0)
393 lpbi->biHeight = This->lpInFormat->biHeight;
394 }
395
396 This->hic = ICLocate(ICTYPE_VIDEO, fccHandler, This->lpInFormat, lpbi, ICMODE_DECOMPRESS);
397 if (This->hic == NULL) {
398 AVIFILE_CloseCompressor(This);
399 return AVIERR_NOCOMPRESSOR;
400 }
401 }
402
403 /* output format given? */
404 if (lpbi != NULL) {
405 /* check the given output format ... */
406 if (lpbi->biClrUsed == 0 && lpbi->biBitCount <= 8)
407 lpbi->biClrUsed = 1u << lpbi->biBitCount;
408
409 /* ... and remember it */
410 memcpy(This->lpOutFormat, lpbi,
411 lpbi->biSize + lpbi->biClrUsed * sizeof(RGBQUAD));
412 if (lpbi->biBitCount <= 8)
413 ICDecompressGetPalette(This->hic, This->lpInFormat, This->lpOutFormat);
414
415 return AVIERR_OK;
416 } else {
417 if (bBestDisplay) {
418 ICGetDisplayFormat(This->hic, This->lpInFormat,
419 This->lpOutFormat, 0, dx, dy);
420 } else if (ICDecompressGetFormat(This->hic, This->lpInFormat,
421 This->lpOutFormat) < 0) {
422 AVIFILE_CloseCompressor(This);
423 return AVIERR_NOCOMPRESSOR;
424 }
425
426 /* check output format */
427 if (This->lpOutFormat->biClrUsed == 0 &&
428 This->lpOutFormat->biBitCount <= 8)
429 This->lpOutFormat->biClrUsed = 1u << This->lpOutFormat->biBitCount;
430 if (This->lpOutFormat->biSizeImage == 0 &&
431 This->lpOutFormat->biCompression == BI_RGB) {
432 This->lpOutFormat->biSizeImage =
433 DIBWIDTHBYTES(*This->lpOutFormat) * This->lpOutFormat->biHeight;
434 }
435
436 if (lpBits == NULL) {
437 register DWORD size = This->lpOutFormat->biClrUsed * sizeof(RGBQUAD);
438
439 size += This->lpOutFormat->biSize + This->lpOutFormat->biSizeImage;
440 This->lpOutFormat = HeapReAlloc(GetProcessHeap(), 0, This->lpOutFormat, size);
441 if (This->lpOutFormat == NULL) {
442 AVIFILE_CloseCompressor(This);
443 return AVIERR_MEMORY;
444 }
445 This->lpOutBuffer = DIBPTR(This->lpOutFormat);
446 } else
447 This->lpOutBuffer = lpBits;
448
449 /* for user size was irrelevant */
450 if (dx == -1)
451 dx = This->lpOutFormat->biWidth;
452 if (dy == -1)
453 dy = This->lpOutFormat->biHeight;
454
455 /* need to resize? */
456 if (x != 0 || y != 0) {
457 if (dy == This->lpOutFormat->biHeight &&
458 dx == This->lpOutFormat->biWidth)
459 This->bResize = FALSE;
460 else
461 This->bResize = TRUE;
462 }
463
464 if (This->bResize) {
465 This->x = x;
466 This->y = y;
467 This->dx = dx;
468 This->dy = dy;
469
470 if (ICDecompressExBegin(This->hic,0,This->lpInFormat,This->lpInBuffer,0,
471 0,This->lpInFormat->biWidth,
472 This->lpInFormat->biHeight,This->lpOutFormat,
473 This->lpOutBuffer, x, y, dx, dy) == ICERR_OK)
474 return AVIERR_OK;
475 } else if (ICDecompressBegin(This->hic, This->lpInFormat,
476 This->lpOutFormat) == ICERR_OK)
477 return AVIERR_OK;
478
479 AVIFILE_CloseCompressor(This);
480
481 return AVIERR_COMPRESSOR;
482 }
483 }
484
485 static const struct IGetFrameVtbl igetframeVtbl = {
486 IGetFrame_fnQueryInterface,
487 IGetFrame_fnAddRef,
488 IGetFrame_fnRelease,
489 IGetFrame_fnGetFrame,
490 IGetFrame_fnBegin,
491 IGetFrame_fnEnd,
492 IGetFrame_fnSetFormat
493 };
494
495 PGETFRAME AVIFILE_CreateGetFrame(PAVISTREAM pStream)
496 {
497 IGetFrameImpl *pg;
498
499 /* check parameter */
500 if (pStream == NULL)
501 return NULL;
502
503 pg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IGetFrameImpl));
504 if (pg != NULL) {
505 pg->IGetFrame_iface.lpVtbl = &igetframeVtbl;
506 pg->ref = 1;
507 pg->lCurrentFrame = -1;
508 pg->pStream = pStream;
509 IAVIStream_AddRef(pStream);
510 }
511
512 return (PGETFRAME)pg;
513 }
514
515 /***********************************************************************/