Merge pull request #5039 from CEikermann/patch-1
[vuplus_xbmc] / xbmc / cdrip / Encoder.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
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)
8  *  any later version.
9  *
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.
14  *
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/>.
18  *
19  */
20
21 #include "Encoder.h"
22 #include "filesystem/File.h"
23 #include "utils/log.h"
24
25 CEncoder::CEncoder()
26 {
27   m_file = NULL;
28   m_dwWriteBufferPointer = 0;
29   m_iInChannels = 0;
30   m_iInSampleRate = 0;
31   m_iInBitsPerSample = 0;
32 }
33
34 CEncoder::~CEncoder()
35 {
36   FileClose();
37 }
38
39 bool CEncoder::Init(const char* strFile, int iInChannels, int iInRate, int iInBits)
40 {
41   if (strFile == NULL) return false;
42
43   m_dwWriteBufferPointer = 0;
44   m_strFile = strFile;
45
46   m_iInChannels = iInChannels;
47   m_iInSampleRate = iInRate;
48   m_iInBitsPerSample = iInBits;
49
50   if (!FileCreate(strFile))
51   {
52     CLog::Log(LOGERROR, "Error: Cannot open file: %s", strFile);
53     return false;
54   }
55   return true;
56 }
57
58 bool CEncoder::FileCreate(const char* filename)
59 {
60   delete m_file;
61
62   m_file = new XFILE::CFile;
63   if (m_file)
64     return m_file->OpenForWrite(filename, true);
65   return false;
66 }
67
68 bool CEncoder::FileClose()
69 {
70   if (m_file)
71   {
72     m_file->Close();
73     delete m_file;
74     m_file = NULL;
75   }
76   return true;
77 }
78
79 // return total bytes written, or -1 on error
80 int CEncoder::FileWrite(const void *pBuffer, uint32_t iBytes)
81 {
82   if (!m_file)
83     return -1;
84
85   uint32_t dwBytesWritten = m_file->Write(pBuffer, iBytes);
86   if (!dwBytesWritten)
87     return -1;
88
89   return dwBytesWritten;
90 }
91
92 int64_t CEncoder::FileSeek(int64_t iFilePosition, int iWhence)
93 {
94   if (!m_file)
95     return -1;
96   FlushStream();
97   return m_file->Seek(iFilePosition, iWhence);
98 }
99
100 // write the stream to our writebuffer, and write the buffer to disk if it's full
101 int CEncoder::WriteStream(const void *pBuffer, uint32_t iBytes)
102 {
103   if ((WRITEBUFFER_SIZE - m_dwWriteBufferPointer) > iBytes)
104   {
105     // writebuffer is big enough to fit data
106     memcpy(m_btWriteBuffer + m_dwWriteBufferPointer, pBuffer, iBytes);
107     m_dwWriteBufferPointer += iBytes;
108     return iBytes;
109   }
110   else
111   {
112     // buffer is not big enough to fit data
113     if (m_dwWriteBufferPointer == 0)
114     {
115       // nothing in our buffer, just write the entire pBuffer to disk
116       return FileWrite(pBuffer, iBytes);
117     }
118
119     uint32_t dwBytesRemaining = iBytes - (WRITEBUFFER_SIZE - m_dwWriteBufferPointer);
120     // fill up our write buffer and write it to disk
121     memcpy(m_btWriteBuffer + m_dwWriteBufferPointer, pBuffer, (WRITEBUFFER_SIZE - m_dwWriteBufferPointer));
122     FileWrite(m_btWriteBuffer, WRITEBUFFER_SIZE);
123     m_dwWriteBufferPointer = 0;
124
125     // pbtRemaining = pBuffer + bytesWritten
126     uint8_t* pbtRemaining = (uint8_t *)pBuffer + (iBytes - dwBytesRemaining);
127     if (dwBytesRemaining > WRITEBUFFER_SIZE)
128     {
129       // data is not going to fit in our buffer, just write it to disk
130       if (FileWrite(pbtRemaining, dwBytesRemaining) == -1) return -1;
131       return iBytes;
132     }
133     else
134     {
135       // copy remaining bytes to our currently empty writebuffer
136       memcpy(m_btWriteBuffer, pbtRemaining, dwBytesRemaining);
137       m_dwWriteBufferPointer = dwBytesRemaining;
138       return iBytes;
139     }
140   }
141 }
142
143 // flush the contents of our writebuffer
144 int CEncoder::FlushStream()
145 {
146   int iResult;
147   if (m_dwWriteBufferPointer == 0) return 0;
148
149   iResult = FileWrite(m_btWriteBuffer, m_dwWriteBufferPointer);
150   m_dwWriteBufferPointer = 0;
151
152   return iResult;
153 }