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