amvideo.cpp 9.5 KB

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