1
0

amvideo.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. //------------------------------------------------------------------------------
  2. // File: AMVideo.cpp
  3. //
  4. // Desc: DirectShow base classes - implements helper functions for
  5. // bitmap formats.
  6. //
  7. // Copyright (c) 1992-2001 Microsoft Corporation. All rights reserved.
  8. //------------------------------------------------------------------------------
  9. #include <streams.h>
  10. #include <limits.h>
  11. // These are bit field masks for true colour devices
  12. const DWORD bits555[] = {0x007C00,0x0003E0,0x00001F};
  13. const DWORD bits565[] = {0x00F800,0x0007E0,0x00001F};
  14. const DWORD bits888[] = {0xFF0000,0x00FF00,0x0000FF};
  15. // This maps bitmap subtypes into a bits per pixel value and also a
  16. // name. unicode and ansi versions are stored because we have to
  17. // return a pointer to a static string.
  18. const struct {
  19. const GUID *pSubtype;
  20. WORD BitCount;
  21. CHAR *pName;
  22. WCHAR *wszName;
  23. } BitCountMap[] = { &MEDIASUBTYPE_RGB1, 1, "RGB Monochrome", L"RGB Monochrome",
  24. &MEDIASUBTYPE_RGB4, 4, "RGB VGA", L"RGB VGA",
  25. &MEDIASUBTYPE_RGB8, 8, "RGB 8", L"RGB 8",
  26. &MEDIASUBTYPE_RGB565, 16, "RGB 565 (16 bit)", L"RGB 565 (16 bit)",
  27. &MEDIASUBTYPE_RGB555, 16, "RGB 555 (16 bit)", L"RGB 555 (16 bit)",
  28. &MEDIASUBTYPE_RGB24, 24, "RGB 24", L"RGB 24",
  29. &MEDIASUBTYPE_RGB32, 32, "RGB 32", L"RGB 32",
  30. &MEDIASUBTYPE_ARGB32, 32, "ARGB 32", L"ARGB 32",
  31. &MEDIASUBTYPE_Overlay, 0, "Overlay", L"Overlay",
  32. &GUID_NULL, 0, "UNKNOWN", L"UNKNOWN"
  33. };
  34. // Return the size of the bitmap as defined by this header
  35. STDAPI_(DWORD) GetBitmapSize(const BITMAPINFOHEADER *pHeader)
  36. {
  37. return DIBSIZE(*pHeader);
  38. }
  39. // This is called if the header has a 16 bit colour depth and needs to work
  40. // out the detailed type from the bit fields (either RGB 565 or RGB 555)
  41. STDAPI_(const GUID) GetTrueColorType(const BITMAPINFOHEADER *pbmiHeader)
  42. {
  43. BITMAPINFO *pbmInfo = (BITMAPINFO *) pbmiHeader;
  44. ASSERT(pbmiHeader->biBitCount == 16);
  45. // If its BI_RGB then it's RGB 555 by default
  46. if (pbmiHeader->biCompression == BI_RGB) {
  47. return MEDIASUBTYPE_RGB555;
  48. }
  49. // Compare the bit fields with RGB 555
  50. DWORD *pMask = (DWORD *) pbmInfo->bmiColors;
  51. if (pMask[0] == bits555[0]) {
  52. if (pMask[1] == bits555[1]) {
  53. if (pMask[2] == bits555[2]) {
  54. return MEDIASUBTYPE_RGB555;
  55. }
  56. }
  57. }
  58. // Compare the bit fields with RGB 565
  59. pMask = (DWORD *) pbmInfo->bmiColors;
  60. if (pMask[0] == bits565[0]) {
  61. if (pMask[1] == bits565[1]) {
  62. if (pMask[2] == bits565[2]) {
  63. return MEDIASUBTYPE_RGB565;
  64. }
  65. }
  66. }
  67. return GUID_NULL;
  68. }
  69. // Given a BITMAPINFOHEADER structure this returns the GUID sub type that is
  70. // used to describe it in format negotiations. For example a video codec fills
  71. // in the format block with a VIDEOINFO structure, it also fills in the major
  72. // type with MEDIATYPE_VIDEO and the subtype with a GUID that matches the bit
  73. // count, for example if it is an eight bit image then MEDIASUBTYPE_RGB8
  74. STDAPI_(const GUID) GetBitmapSubtype(const BITMAPINFOHEADER *pbmiHeader)
  75. {
  76. ASSERT(pbmiHeader);
  77. // If it's not RGB then create a GUID from the compression type
  78. if (pbmiHeader->biCompression != BI_RGB) {
  79. if (pbmiHeader->biCompression != BI_BITFIELDS) {
  80. FOURCCMap FourCCMap(pbmiHeader->biCompression);
  81. return (const GUID) FourCCMap;
  82. }
  83. }
  84. // Map the RGB DIB bit depth to a image GUID
  85. switch(pbmiHeader->biBitCount) {
  86. case 1 : return MEDIASUBTYPE_RGB1;
  87. case 4 : return MEDIASUBTYPE_RGB4;
  88. case 8 : return MEDIASUBTYPE_RGB8;
  89. case 16 : return GetTrueColorType(pbmiHeader);
  90. case 24 : return MEDIASUBTYPE_RGB24;
  91. case 32 : return MEDIASUBTYPE_RGB32;
  92. }
  93. return GUID_NULL;
  94. }
  95. // Given a video bitmap subtype we return the number of bits per pixel it uses
  96. // We return a WORD bit count as thats what the BITMAPINFOHEADER uses. If the
  97. // GUID subtype is not found in the table we return an invalid USHRT_MAX
  98. STDAPI_(WORD) GetBitCount(const GUID *pSubtype)
  99. {
  100. ASSERT(pSubtype);
  101. const GUID *pMediaSubtype;
  102. INT iPosition = 0;
  103. // Scan the mapping list seeing if the source GUID matches any known
  104. // bitmap subtypes, the list is terminated by a GUID_NULL entry
  105. while (TRUE) {
  106. pMediaSubtype = BitCountMap[iPosition].pSubtype;
  107. if (IsEqualGUID(*pMediaSubtype,GUID_NULL)) {
  108. return USHRT_MAX;
  109. }
  110. if (IsEqualGUID(*pMediaSubtype,*pSubtype)) {
  111. return BitCountMap[iPosition].BitCount;
  112. }
  113. iPosition++;
  114. }
  115. }
  116. // Given a bitmap subtype we return a description name that can be used for
  117. // debug purposes. In a retail build this function still returns the names
  118. // If the subtype isn't found in the lookup table we return string UNKNOWN
  119. int LocateSubtype(const GUID *pSubtype)
  120. {
  121. ASSERT(pSubtype);
  122. const GUID *pMediaSubtype;
  123. INT iPosition = 0;
  124. // Scan the mapping list seeing if the source GUID matches any known
  125. // bitmap subtypes, the list is terminated by a GUID_NULL entry
  126. while (TRUE) {
  127. pMediaSubtype = BitCountMap[iPosition].pSubtype;
  128. if (IsEqualGUID(*pMediaSubtype,*pSubtype) ||
  129. IsEqualGUID(*pMediaSubtype,GUID_NULL)
  130. )
  131. {
  132. break;
  133. }
  134. iPosition++;
  135. }
  136. return iPosition;
  137. }
  138. STDAPI_(WCHAR *) GetSubtypeNameW(const GUID *pSubtype)
  139. {
  140. return BitCountMap[LocateSubtype(pSubtype)].wszName;
  141. }
  142. STDAPI_(CHAR *) GetSubtypeNameA(const GUID *pSubtype)
  143. {
  144. return BitCountMap[LocateSubtype(pSubtype)].pName;
  145. }
  146. #ifndef GetSubtypeName
  147. #error wxutil.h should have defined GetSubtypeName
  148. #endif
  149. #undef GetSubtypeName
  150. // this is here for people that linked to it directly; most people
  151. // would use the header file that picks the A or W version.
  152. STDAPI_(CHAR *) GetSubtypeName(const GUID *pSubtype)
  153. {
  154. return GetSubtypeNameA(pSubtype);
  155. }
  156. // The mechanism for describing a bitmap format is with the BITMAPINFOHEADER
  157. // This is really messy to deal with because it invariably has fields that
  158. // follow it holding bit fields, palettes and the rest. This function gives
  159. // the number of bytes required to hold a VIDEOINFO that represents it. This
  160. // count includes the prefix information (like the rcSource rectangle) the
  161. // BITMAPINFOHEADER field, and any other colour information on the end.
  162. //
  163. // WARNING If you want to copy a BITMAPINFOHEADER into a VIDEOINFO always make
  164. // sure that you use the HEADER macro because the BITMAPINFOHEADER field isn't
  165. // right at the start of the VIDEOINFO (there are a number of other fields),
  166. //
  167. // CopyMemory(HEADER(pVideoInfo),pbmi,sizeof(BITMAPINFOHEADER));
  168. //
  169. STDAPI_(LONG) GetBitmapFormatSize(const BITMAPINFOHEADER *pHeader)
  170. {
  171. // Everyone has this to start with this
  172. LONG Size = SIZE_PREHEADER + pHeader->biSize;
  173. ASSERT(pHeader->biSize >= sizeof(BITMAPINFOHEADER));
  174. // Does this format use a palette, if the number of colours actually used
  175. // is zero then it is set to the maximum that are allowed for that colour
  176. // depth (an example is 256 for eight bits). Truecolour formats may also
  177. // pass a palette with them in which case the used count is non zero
  178. // This would scare me.
  179. ASSERT(pHeader->biBitCount <= iPALETTE || pHeader->biClrUsed == 0);
  180. if (pHeader->biBitCount <= iPALETTE || pHeader->biClrUsed) {
  181. LONG Entries = (DWORD) 1 << pHeader->biBitCount;
  182. if (pHeader->biClrUsed) {
  183. Entries = pHeader->biClrUsed;
  184. }
  185. Size += Entries * sizeof(RGBQUAD);
  186. }
  187. // Truecolour formats may have a BI_BITFIELDS specifier for compression
  188. // type which means that room for three DWORDs should be allocated that
  189. // specify where in each pixel the RGB colour components may be found
  190. if (pHeader->biCompression == BI_BITFIELDS) {
  191. Size += SIZE_MASKS;
  192. }
  193. // A BITMAPINFO for a palettised image may also contain a palette map that
  194. // provides the information to map from a source palette to a destination
  195. // palette during a BitBlt for example, because this information is only
  196. // ever processed during drawing you don't normally store the palette map
  197. // nor have any way of knowing if it is present in the data structure
  198. return Size;
  199. }
  200. // Returns TRUE if the VIDEOINFO contains a palette
  201. STDAPI_(BOOL) ContainsPalette(const VIDEOINFOHEADER *pVideoInfo)
  202. {
  203. if (PALETTISED(pVideoInfo) == FALSE) {
  204. if (pVideoInfo->bmiHeader.biClrUsed == 0) {
  205. return FALSE;
  206. }
  207. }
  208. return TRUE;
  209. }
  210. // Return a pointer to the first entry in a palette
  211. STDAPI_(const RGBQUAD *) GetBitmapPalette(const VIDEOINFOHEADER *pVideoInfo)
  212. {
  213. if (pVideoInfo->bmiHeader.biCompression == BI_BITFIELDS) {
  214. return TRUECOLOR(pVideoInfo)->bmiColors;
  215. }
  216. return COLORS(pVideoInfo);
  217. }