[bluray] Fix stream info/language retrieval for blurays in non-nav mode.
[vuplus_xbmc] / xbmc / cdrip / Encoder.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://www.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 // write the stream to our writebuffer, and write the buffer to disk if it's full
93 int CEncoder::WriteStream(const void *pBuffer, uint32_t iBytes)
94 {
95   if ((WRITEBUFFER_SIZE - m_dwWriteBufferPointer) > iBytes)
96   {
97     // writebuffer is big enough to fit data
98     memcpy(m_btWriteBuffer + m_dwWriteBufferPointer, pBuffer, iBytes);
99     m_dwWriteBufferPointer += iBytes;
100     return iBytes;
101   }
102   else
103   {
104     // buffer is not big enough to fit data
105     if (m_dwWriteBufferPointer == 0)
106     {
107       // nothing in our buffer, just write the entire pBuffer to disk
108       return FileWrite(pBuffer, iBytes);
109     }
110
111     uint32_t dwBytesRemaining = iBytes - (WRITEBUFFER_SIZE - m_dwWriteBufferPointer);
112     // fill up our write buffer and write it to disk
113     memcpy(m_btWriteBuffer + m_dwWriteBufferPointer, pBuffer, (WRITEBUFFER_SIZE - m_dwWriteBufferPointer));
114     FileWrite(m_btWriteBuffer, WRITEBUFFER_SIZE);
115     m_dwWriteBufferPointer = 0;
116
117     // pbtRemaining = pBuffer + bytesWritten
118     uint8_t* pbtRemaining = (uint8_t *)pBuffer + (iBytes - dwBytesRemaining);
119     if (dwBytesRemaining > WRITEBUFFER_SIZE)
120     {
121       // data is not going to fit in our buffer, just write it to disk
122       if (FileWrite(pbtRemaining, dwBytesRemaining) == -1) return -1;
123       return iBytes;
124     }
125     else
126     {
127       // copy remaining bytes to our currently empty writebuffer
128       memcpy(m_btWriteBuffer, pbtRemaining, dwBytesRemaining);
129       m_dwWriteBufferPointer = dwBytesRemaining;
130       return iBytes;
131     }
132   }
133 }
134
135 // flush the contents of our writebuffer
136 int CEncoder::FlushStream()
137 {
138   int iResult;
139   if (m_dwWriteBufferPointer == 0) return 0;
140
141   iResult = FileWrite(m_btWriteBuffer, m_dwWriteBufferPointer);
142   m_dwWriteBufferPointer = 0;
143
144   return iResult;
145 }