1 |
/* |
2 |
* Util.cs |
3 |
* Copyright (c) 2007-2009 kbinani |
4 |
* |
5 |
* This file is part of Boare.Lib.Media. |
6 |
* |
7 |
* Boare.Lib.Media is free software; you can redistribute it and/or |
8 |
* modify it under the terms of the BSD License. |
9 |
* |
10 |
* Boare.Lib.Media is distributed in the hope that it will be useful, |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
13 |
*/ |
14 |
using System; |
15 |
using System.IO; |
16 |
|
17 |
using bocoree; |
18 |
|
19 |
namespace Boare.Lib.Media { |
20 |
|
21 |
internal static class Util { |
22 |
public const byte AVI_INDEX_OF_INDEXES = 0x00; //when each entry in aIndex |
23 |
// array points to an index chunk |
24 |
public const byte AVI_INDEX_OF_CHUNKS = 0x01; // when each entry in aIndex |
25 |
// array points to a chunk in the file |
26 |
public const byte AVI_INDEX_IS_DATA = 0x80; // when each entry is aIndex is |
27 |
// really the data |
28 |
public const byte AVI_INDEX_2FIELD = 0x01; // when fields within frames |
29 |
// are also indexed |
30 |
|
31 |
public const int AVIF_HASINDEX = 16; // Indicates the AVI file has an “idx1” chunk. |
32 |
public const int AVIF_MUSTUSEINDEX = 32; // Indicates the index should be used to determine the order of presentation of the data. |
33 |
public const int AVIF_ISINTERLEAVED = 256; // Indicates the AVI file is interleaved. |
34 |
public const int AVIF_WASCAPTUREFILE = 65536; // Indicates the AVI file is a specially allocated file used for capturing real-time video. |
35 |
public const int AVIF_COPYRIGHTED = 131072; // Indicates the AVI file contains copyrighted data. |
36 |
public const int AVIF_TRUSTCKTYPE = 2048; // Use CKType to find key frames |
37 |
public const int BMP_MAGIC_COOKIE = 19778; //ascii string "BM" |
38 |
|
39 |
/*// <summary> |
40 |
/// 指定されたBITMAP型変数の情報ヘッダーをファイルに書き込みます. |
41 |
/// </summary> |
42 |
/// <param name="bmp"></param> |
43 |
/// <param name="fp"></param> |
44 |
public static void bmpWriteInfoHeader( BITMAPINFOHEADER infoHeader, BinaryWriter stream ) { |
45 |
//type(INFO_HEADER), intent(in) :: infoHeader |
46 |
//type(FILE), intent(inout) :: fp |
47 |
Util.WriteDWORD( (uint)infoHeader.biSize, stream ); |
48 |
Util.WriteDWORD( (uint)infoHeader.biWidth, stream ); |
49 |
Util.WriteDWORD( (uint)infoHeader.biHeight, stream ); |
50 |
Util.WriteWORD( (ushort)infoHeader.biPlanes, stream ); |
51 |
Util.WriteWORD( (ushort)(infoHeader.biBitCount), stream ); |
52 |
Util.WriteDWORD( (uint)infoHeader.biCompression, stream ); |
53 |
Util.WriteDWORD( (uint)infoHeader.biSizeImage, stream ); |
54 |
Util.WriteDWORD( (uint)infoHeader.biXPelsPerMeter, stream ); |
55 |
Util.WriteDWORD( (uint)infoHeader.biYPelsPerMeter, stream ); |
56 |
Util.WriteDWORD( (uint)infoHeader.biClrUsed, stream ); |
57 |
Util.WriteDWORD( (uint)infoHeader.biClrImportant, stream ); |
58 |
}*/ |
59 |
|
60 |
|
61 |
/// <summary> |
62 |
/// ファイルにAVIStreamHeader構造体の値を書き込みます |
63 |
/// </summary> |
64 |
public static void aviWriteStreamHeader( AVIStreamHeader streamHeader, MainAVIHeader mainHeader, BinaryWriter stream ) { |
65 |
//type(AVI_CONTAINER), intent(inout) :: avi |
66 |
Util.fwrite( "strh", stream ); |
67 |
Util.WriteDWORD( 56, stream );// call bmpQWordWrite( 56, avi%fp ) !// AVIStreamHeaderのサイズ |
68 |
//fwrite( streamHeader.fccType, fp );// i = fwrite( avi%streamHeader%fccType, 1, 4, avi%fp ) |
69 |
Util.WriteDWORD( (uint)streamHeader.fccType, stream ); |
70 |
//fwrite( streamHeader.fccHandler, fp );// i = fwrite( streamHeader.fccHandler, 1, 4, fp ); |
71 |
Util.WriteDWORD( (uint)streamHeader.fccHandler, stream ); |
72 |
//WriteDWORD( 0, fp ); |
73 |
Util.WriteDWORD( streamHeader.dwFlags, stream ); |
74 |
//WriteDWORD( streamHeader.dwReserved1, fp ); |
75 |
Util.WriteWORD( 0, stream );//wPriority |
76 |
Util.WriteWORD( 0, stream );//wLanghage |
77 |
Util.WriteDWORD( streamHeader.dwInitialFrames, stream ); |
78 |
Util.WriteDWORD( streamHeader.dwScale, stream ); |
79 |
Util.WriteDWORD( streamHeader.dwRate, stream ); |
80 |
Util.WriteDWORD( streamHeader.dwStart, stream ); |
81 |
Util.WriteDWORD( streamHeader.dwLength, stream ); |
82 |
Util.WriteDWORD( streamHeader.dwSuggestedBufferSize, stream ); |
83 |
Util.WriteDWORD( streamHeader.dwQuality, stream ); |
84 |
Util.WriteDWORD( streamHeader.dwSampleSize, stream ); |
85 |
Util.WriteWORD( 0, stream );//left |
86 |
Util.WriteWORD( 0, stream );//top |
87 |
Util.WriteWORD( (ushort)mainHeader.dwWidth, stream );//right |
88 |
Util.WriteWORD( (ushort)mainHeader.dwHeight, stream );//bottom |
89 |
} |
90 |
|
91 |
|
92 |
/// <summary> |
93 |
/// ファイルにMainAviHeader構造体の値を書き込みます |
94 |
/// </summary> |
95 |
public static void aviWriteMainHeader( MainAVIHeader mainHeader, BinaryWriter stream ) { |
96 |
//type(AVI_CONTAINER), intent(inout) :: avi |
97 |
Util.fwrite( "avih", stream );// i = fwrite( 'avih', 1, 4, avi%fp ) |
98 |
Util.WriteDWORD( 56, stream ); // MainAVIHeaderのサイズ |
99 |
Util.WriteDWORD( mainHeader.dwMicroSecPerFrame, stream ); |
100 |
Util.WriteDWORD( 0/*this.mainHeader.dwMaxBytesPerSec*/, stream ); |
101 |
Util.WriteDWORD( mainHeader.dwReserved1, stream ); |
102 |
Util.WriteDWORD( mainHeader.dwFlags, stream ); |
103 |
Util.WriteDWORD( mainHeader.dwTotalFrames, stream ); |
104 |
Util.WriteDWORD( mainHeader.dwInitialFrames, stream ); |
105 |
Util.WriteDWORD( mainHeader.dwStreams, stream ); |
106 |
Util.WriteDWORD( 0/*this.mainHeader.dwSuggestedBufferSize*/, stream ); |
107 |
Util.WriteDWORD( mainHeader.dwWidth, stream ); |
108 |
Util.WriteDWORD( mainHeader.dwHeight, stream ); |
109 |
Util.WriteDWORD( mainHeader.dwScale, stream ); |
110 |
Util.WriteDWORD( mainHeader.dwRate, stream ); |
111 |
Util.WriteDWORD( mainHeader.dwStart, stream ); |
112 |
Util.WriteDWORD( mainHeader.dwLength, stream ); |
113 |
}//end subroutine |
114 |
|
115 |
|
116 |
public static void fwrite( string str, BinaryWriter fp ) { |
117 |
int length = str.Length; |
118 |
if ( length <= 0 ) { |
119 |
return; |
120 |
} |
121 |
foreach ( char ch in str ) { |
122 |
fp.Write( (byte)ch ); |
123 |
} |
124 |
} |
125 |
|
126 |
|
127 |
/// <summary> |
128 |
/// BYTE値を1byte分ファイルに書き込みます. |
129 |
/// </summary> |
130 |
/// <param name="number"></param> |
131 |
/// <param name="fp"></param> |
132 |
public static void WriteBYTE( byte number, BinaryWriter fp ) { |
133 |
fp.Write( number ); |
134 |
} |
135 |
|
136 |
|
137 |
/// <summary> |
138 |
/// integer(2)のDWORD値を2byte分ファイルに書き込みます. |
139 |
/// </summary> |
140 |
/// <param name="number"></param> |
141 |
/// <param name="fp"></param> |
142 |
public static void WriteWORD( ushort number, BinaryWriter fp ) { |
143 |
byte k1, k2; |
144 |
k1 = (byte)(number >> 8); |
145 |
k2 = (byte)(number - (k1 << 8)); |
146 |
fp.Write( k2 ); |
147 |
fp.Write( k1 ); |
148 |
} |
149 |
|
150 |
|
151 |
/// <summary> |
152 |
/// integer(4)のDWORD値を4byte分ファイルに書き込みます |
153 |
/// </summary> |
154 |
/// <param name="number"></param> |
155 |
/// <param name="fp"></param> |
156 |
public static void WriteDWORD( uint number, BinaryWriter fp ) { |
157 |
uint tmp; |
158 |
byte k1, k2, k3, k4; |
159 |
k1 = (byte)(number >> 24); |
160 |
number -= (uint)(k1 << 24); |
161 |
k2 = (byte)(number >> 16); |
162 |
number -= (uint)(k2 << 16); |
163 |
k3 = (byte)(number >> 8); |
164 |
k4 = (byte)(number - (k3 << 8)); |
165 |
fp.Write( k4 ); |
166 |
fp.Write( k3 ); |
167 |
fp.Write( k2 ); |
168 |
fp.Write( k1 ); |
169 |
} |
170 |
|
171 |
|
172 |
/// <summary> |
173 |
/// integer(8)のQWORD値を8byte分ファイルに書き込みます |
174 |
/// </summary> |
175 |
/// <param name="number"></param> |
176 |
/// <param name="fp"></param> |
177 |
public static void WriteQWORD( ulong number, BinaryWriter fp ) { |
178 |
byte k1, k2, k3, k4, k5, k6, k7, k8; |
179 |
k1 = (byte)(number >> 56); |
180 |
number -= (ulong)k1 << 56; |
181 |
k2 = (byte)(number >> 48); |
182 |
number -= (ulong)k2 << 48; |
183 |
k3 = (byte)(number >> 40); |
184 |
number -= (ulong)k3 << 40; |
185 |
k4 = (byte)(number >> 32); |
186 |
number -= (ulong)k4 << 32; |
187 |
k5 = (byte)(number >> 24); |
188 |
number -= (ulong)k5 << 24; |
189 |
k6 = (byte)(number >> 16); |
190 |
number -= (ulong)k6 << 16; |
191 |
k7 = (byte)(number >> 8); |
192 |
k8 = (byte)(number - (ulong)(k7 << 8)); |
193 |
fp.Write( k8 ); |
194 |
fp.Write( k7 ); |
195 |
fp.Write( k6 ); |
196 |
fp.Write( k5 ); |
197 |
fp.Write( k4 ); |
198 |
fp.Write( k3 ); |
199 |
fp.Write( k2 ); |
200 |
fp.Write( k1 ); |
201 |
} |
202 |
|
203 |
|
204 |
public static uint mmioFOURCC( string fcc ) { |
205 |
char[] str = new char[4]; |
206 |
for ( int i = 0; i < 4; i++ ) { |
207 |
if ( i < fcc.Length ) { |
208 |
str[i] = fcc[i]; |
209 |
} else { |
210 |
str[i] = ' '; |
211 |
} |
212 |
} |
213 |
return mmioFOURCC( str[0], str[1], str[2], str[3] ); |
214 |
} |
215 |
|
216 |
|
217 |
public static uint mmioFOURCC( char ch0, char ch1, char ch2, char ch3 ) { |
218 |
return (uint)((byte)(ch0) | ((byte)(ch1) << 8) | ((byte)(ch2) << 16) | ((byte)(ch3) << 24)); |
219 |
} |
220 |
} |
221 |
|
222 |
} |