2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program 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. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "XBTFWriter.h"
22 #define __STDC_FORMAT_MACROS
24 #include "guilib/XBTF.h"
25 #include "utils/EndianSwap.h"
26 #if defined(TARGET_FREEBSD) || defined(TARGET_DARWIN)
28 #elif !defined(TARGET_DARWIN)
33 #define WRITE_STR(str, size, file) fwrite(str, size, 1, file)
34 #define WRITE_U32(i, file) { uint32_t _n = Endian_SwapLE32(i); fwrite(&_n, 4, 1, file); }
35 #define WRITE_U64(i, file) { uint64_t _n = i; _n = Endian_SwapLE64(i); fwrite(&_n, 8, 1, file); }
37 CXBTFWriter::CXBTFWriter(CXBTF& xbtf, const std::string& outputFile) : m_xbtf(xbtf)
39 m_outputFile = outputFile;
45 bool CXBTFWriter::Create()
47 m_file = fopen(m_outputFile.c_str(), "wb");
56 bool CXBTFWriter::Close()
58 if (m_file == NULL || m_data == NULL)
63 fwrite(m_data, 1, m_size, m_file);
70 void CXBTFWriter::Cleanup()
82 bool CXBTFWriter::AppendContent(unsigned char const* data, size_t length)
84 unsigned char *new_data = (unsigned char *)realloc(m_data, m_size + length);
87 { // OOM - cleanup and fail
94 memcpy(m_data + m_size, data, length);
100 bool CXBTFWriter::UpdateHeader(const std::vector<unsigned int>& dupes)
107 uint64_t offset = m_xbtf.GetHeaderSize();
109 WRITE_STR(XBTF_MAGIC, 4, m_file);
110 WRITE_STR(XBTF_VERSION, 1, m_file);
112 std::vector<CXBTFFile>& files = m_xbtf.GetFiles();
113 WRITE_U32(files.size(), m_file);
114 for (size_t i = 0; i < files.size(); i++)
116 CXBTFFile& file = files[i];
118 // Convert path to lower case
119 char* ch = file.GetPath();
126 WRITE_STR(file.GetPath(), 256, m_file);
127 WRITE_U32(file.GetLoop(), m_file);
129 std::vector<CXBTFFrame>& frames = file.GetFrames();
130 WRITE_U32(frames.size(), m_file);
131 for (size_t j = 0; j < frames.size(); j++)
133 CXBTFFrame& frame = frames[j];
135 frame.SetOffset(files[dupes[i]].GetFrames()[j].GetOffset());
138 frame.SetOffset(offset);
139 offset += frame.GetPackedSize();
142 WRITE_U32(frame.GetWidth(), m_file);
143 WRITE_U32(frame.GetHeight(), m_file);
144 WRITE_U32(frame.GetFormat(true), m_file);
145 WRITE_U64(frame.GetPackedSize(), m_file);
146 WRITE_U64(frame.GetUnpackedSize(), m_file);
147 WRITE_U32(frame.GetDuration(), m_file);
148 WRITE_U64(frame.GetOffset(), m_file);
153 int64_t pos = ftell(m_file);
154 if (pos != (int64_t)m_xbtf.GetHeaderSize())
156 printf("Expected header size (%" PRId64 ") != actual size (%" PRId64 ")\n", m_xbtf.GetHeaderSize(), pos);