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