NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
AviExporter.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2011, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "io/AviExporter.hpp"
29 
30 using namespace FW;
31 
32 //------------------------------------------------------------------------
33 
34 #define AVI_HEADER_SIZE 0xe0
35 
36 //------------------------------------------------------------------------
37 
38 AviExporter::AviExporter(const String& fileName, Vec2i size, int fps)
39 : m_file (fileName, File::Create),
40  m_size (size),
41  m_frame (size, ImageFormat::R8_G8_B8),
42  m_fps (fps)
43 {
44  FW_ASSERT(size.min() > 0 && fps > 0);
45  m_lineBytes = (m_size.x * 3 + 3) & -4;
46  m_frameBytes = m_lineBytes * m_size.y;
47  m_numFrames = 0;
48  writeHeader();
49 }
50 
51 //------------------------------------------------------------------------
52 
54 {
55 }
56 
57 //------------------------------------------------------------------------
58 
60 {
61  // Increment frame counter and allocate buffer.
62 
63  m_numFrames++;
64  m_buffer.resize(8 + m_frameBytes + 8 + m_numFrames * 16);
65 
66  // Output frame header.
67 
68  int ofs = 0;
69  setTag(ofs + 0x00, "00db");
70  setS32(ofs + 0x04, m_frameBytes);
71  ofs += 8;
72 
73  // Output each scanline.
74 
75  for (int y = 0; y < m_size.y; y++)
76  {
77  const U8* src = m_frame.getPtr(Vec2i(0, y));
78  U8* dst = m_buffer.getPtr(ofs);
79  for (int x = m_size.x; x > 0; x--)
80  {
81  *dst++ = src[2]; // B
82  *dst++ = src[1]; // G
83  *dst++ = src[0]; // R
84  src += 3;
85  }
86  ofs += m_lineBytes;
87  }
88 
89  // Output index header.
90 
91  setTag(ofs + 0x00, "idx1"); // AVIOLDINDEX.fcc
92  setS32(ofs + 0x04, m_numFrames * 16); // cb
93  ofs += 8;
94 
95  // Output the offset of each frame.
96 
97  for (int i = 0; i < m_numFrames; i++)
98  {
99  S32 dwOffset = 4 + i * (m_frameBytes + 8);
100  setTag(ofs + 0x00, "00db"); // dwChunkId
101  setS32(ofs + 0x04, 0x10); // dwFlags
102  setS32(ofs + 0x08, dwOffset); // dwOffset
103  setS32(ofs + 0x0c, m_frameBytes); // dwSize
104  ofs += 16;
105  }
106 
107  // Write the buffer.
108 
109  FW_ASSERT(ofs == m_buffer.getSize());
110  m_file.write(m_buffer.getPtr(), m_buffer.getSize());
111 
112  // Update the header, since some fields depend on numFrames.
113 
114  m_file.seek(0);
115  writeHeader();
116 
117  // Seek to the beginning of the next frame.
118 
119  m_file.seek(AVI_HEADER_SIZE + (S64)m_numFrames * (m_frameBytes + 8));
120 }
121 
122 //------------------------------------------------------------------------
123 
125 {
126  m_file.flush();
127 }
128 
129 //------------------------------------------------------------------------
130 
131 void AviExporter::setTag(int ofs, const char* tag)
132 {
133  FW_ASSERT(tag);
134  FW_ASSERT(ofs >= 0 && ofs + 4 <= m_buffer.getSize());
135  U8* ptr = m_buffer.getPtr(ofs);
136 
137  ptr[0] = tag[0];
138  ptr[1] = tag[1];
139  ptr[2] = tag[2];
140  ptr[3] = tag[3];
141 }
142 
143 //------------------------------------------------------------------------
144 
145 void AviExporter::setS32(int ofs, S32 value)
146 {
147  FW_ASSERT(ofs >= 0 && ofs + 4 <= m_buffer.getSize());
148  U8* ptr = m_buffer.getPtr(ofs);
149 
150  ptr[0] = (U8)value;
151  ptr[1] = (U8)(value >> 8);
152  ptr[2] = (U8)(value >> 16);
153  ptr[3] = (U8)(value >> 24);
154 }
155 
156 //------------------------------------------------------------------------
157 
158 void AviExporter::writeHeader(void)
159 {
160  S32 dwMicroSecPerFrame = 1000000 / m_fps;
161  S32 dwMaxBytesPerSec = m_frameBytes * m_fps;
162  S32 dwSuggestedBufferSize = m_frameBytes + 8;
163  S32 rcFrame = m_size.x | (m_size.y << 16);
164  S32 riffSize = AVI_HEADER_SIZE + m_numFrames * (m_frameBytes + 24);
165  S32 moviSize = 4 + m_numFrames * (m_frameBytes + 8);
166 
167  // Initialize to zero.
168 
169  m_buffer.resize(AVI_HEADER_SIZE);
170  memset(m_buffer.getPtr(), 0, m_buffer.getSize());
171 
172  // Write non-zero fields.
173 
174  setTag(0x00, "RIFF");
175  setS32(0x04, riffSize);
176  setTag(0x08, "AVI ");
177  setTag(0x0c, "LIST");
178  setS32(0x10, 0xd4 - 0x14);
179  setTag(0x14, "hdrl");
180  setTag(0x18, "avih"); // AVIMAINHEADER
181  setS32(0x1c, 0x58 - 0x20);
182  setS32(0x20, dwMicroSecPerFrame); // dwMicroSecPerFrame
183  setS32(0x24, dwMaxBytesPerSec); // dwMaxBytesPerSec
184  setS32(0x2c, 0x810); // dwFlags
185  setS32(0x30, m_numFrames); // AVIMAINHEADER.dwTotalFrames
186  setS32(0x38, 1); // dwStreams
187  setS32(0x3c, dwSuggestedBufferSize); // dwSuggestedBufferSize
188  setS32(0x40, m_size.x); // dwWidth
189  setS32(0x44, m_size.y); // dwHeight
190 
191  setTag(0x58, "LIST");
192  setS32(0x5c, 0xd4 - 0x60);
193  setTag(0x60, "strl");
194  setTag(0x64, "strh"); // AVISTREAMINFO
195  setS32(0x68, 0xa4 - 0x6c);
196  setTag(0x6c, "vids"); // fccType
197  setTag(0x70, "DIB "); // fccHandler
198  setS32(0x80, 1); // dwScale
199  setS32(0x84, m_fps); // dwRate
200  setS32(0x8c, m_numFrames); // AVISTREAMINFO.dwLength
201  setS32(0x90, dwSuggestedBufferSize); // dwSuggestedBufferSize
202  setS32(0x94, -1); // dwQuality
203  setS32(0x98, m_frameBytes); // dwSampleSize
204  setS32(0xa0, rcFrame); // rcFrame.right/bottom
205 
206  setTag(0xa4, "strf"); // BITMAPINFOHEADER
207  setS32(0xa8, 0xd4 - 0xac);
208  setS32(0xac, 0xd4 - 0xac); // biSize
209  setS32(0xb0, m_size.x); // biWidth
210  setS32(0xb4, -m_size.y); // biHeight
211  setS32(0xb8, 0x00180001); // biPlanes, biBitCount
212  setS32(0xc0, m_frameBytes); // biSizeImage
213 
214  setTag(0xd4, "LIST");
215  setS32(0xd8, moviSize);
216  setTag(0xdc, "movi");
217 
218  // Write.
219 
220  m_file.write(m_buffer.getPtr(), m_buffer.getSize());
221 }
222 
223 //------------------------------------------------------------------------
void seek(S64 ofs)
Definition: File.cpp:229
#define AVI_HEADER_SIZE
Definition: AviExporter.cpp:34
void ** ptr
Definition: DLLImports.cpp:74
const U8 * getPtr(const Vec2i &pos=0) const
Definition: Image.hpp:150
virtual void flush(void)
Definition: File.cpp:285
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type GLsizei const GLuint framebuffers GLsizei const GLuint renderbuffers GLuint v GLuint v GLenum GLenum GLenum GLuint GLint level GLsizei GLuint framebuffers GLuint const GLchar name GLenum GLintptr GLsizeiptr GLvoid data GLuint GLenum GLint param GLuint GLenum GLint param GLhandleARB programObj GLenum GLenum GLsizei GLsizei height GLenum GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid pixels GLint GLsizei const GLfloat value GLint GLfloat GLfloat v1 GLint GLfloat GLfloat GLfloat v2 GLint GLsizei const GLfloat value GLint GLsizei GLboolean const GLfloat value GLuint program GLuint GLfloat x
Definition: DLLImports.inl:363
void flush(void)
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type GLsizei const GLuint framebuffers GLsizei const GLuint renderbuffers GLuint v GLuint v GLenum GLenum GLenum GLuint GLint level GLsizei GLuint framebuffers GLuint const GLchar name GLenum GLintptr GLsizeiptr GLvoid data GLuint GLenum GLint param GLuint GLenum GLint param GLhandleARB programObj GLenum GLenum GLsizei GLsizei height GLenum GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid pixels GLint GLsizei const GLfloat value GLint GLfloat GLfloat v1 GLint GLfloat GLfloat GLfloat v2 GLint GLsizei const GLfloat value GLint GLsizei GLboolean const GLfloat value GLuint program GLuint GLfloat GLfloat y
Definition: DLLImports.inl:363
#define FW_ASSERT(X)
Definition: Defs.hpp:67
signed int S32
Definition: Defs.hpp:88
AviExporter(const String &fileName, Vec2i size, int fps)
Definition: AviExporter.cpp:38
signed __int64 S64
Definition: Defs.hpp:98
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value
Definition: DLLImports.inl:84
void exportFrame(void)
Definition: AviExporter.cpp:59
virtual void write(const void *ptr, int size)
Definition: File.cpp:278
unsigned char U8
Definition: Defs.hpp:83
const T * getPtr(S idx=0) const
Definition: Array.hpp:202
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr size
Definition: DLLImports.inl:319
void resize(S size)
Definition: Array.hpp:366
S getSize(void) const
Definition: Array.hpp:188
FW_CUDA_FUNC T min(void) const
Definition: Math.hpp:146