1 | /* The copyright in this software is being made available under the BSD |
---|
2 | * License, included below. This software may be subject to other third party |
---|
3 | * and contributor rights, including patent rights, and no such rights are |
---|
4 | * granted under this license. |
---|
5 | * |
---|
6 | * Copyright (c) 2010-2011, ISO/IEC |
---|
7 | * All rights reserved. |
---|
8 | * |
---|
9 | * Redistribution and use in source and binary forms, with or without |
---|
10 | * modification, are permitted provided that the following conditions are met: |
---|
11 | * |
---|
12 | * * Redistributions of source code must retain the above copyright notice, |
---|
13 | * this list of conditions and the following disclaimer. |
---|
14 | * * Redistributions in binary form must reproduce the above copyright notice, |
---|
15 | * this list of conditions and the following disclaimer in the documentation |
---|
16 | * and/or other materials provided with the distribution. |
---|
17 | * * Neither the name of the ISO/IEC nor the names of its contributors may |
---|
18 | * be used to endorse or promote products derived from this software without |
---|
19 | * specific prior written permission. |
---|
20 | * |
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS |
---|
25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
---|
31 | * THE POSSIBILITY OF SUCH DAMAGE. |
---|
32 | */ |
---|
33 | |
---|
34 | /** \file TAppExtrTop.cpp |
---|
35 | \brief Extractor application class |
---|
36 | */ |
---|
37 | |
---|
38 | #include <list> |
---|
39 | #include <stdio.h> |
---|
40 | #include <fcntl.h> |
---|
41 | #include <assert.h> |
---|
42 | |
---|
43 | #include "TAppExtrTop.h" |
---|
44 | |
---|
45 | // ==================================================================================================================== |
---|
46 | // Local constants |
---|
47 | // ==================================================================================================================== |
---|
48 | |
---|
49 | /// initial bitstream buffer size |
---|
50 | /// should be large enough for parsing SPS |
---|
51 | /// resized as a function of picture size after parsing SPS |
---|
52 | #define BITS_BUF_SIZE 65536 |
---|
53 | |
---|
54 | // ==================================================================================================================== |
---|
55 | // Constructor / destructor / initialization / destroy |
---|
56 | // ==================================================================================================================== |
---|
57 | |
---|
58 | TAppExtrTop::TAppExtrTop() |
---|
59 | { |
---|
60 | } |
---|
61 | |
---|
62 | Void TAppExtrTop::create() |
---|
63 | { |
---|
64 | m_apcInputBitstream = new TComBitstream; |
---|
65 | m_apcInputBitstream->create( BITS_BUF_SIZE ); |
---|
66 | |
---|
67 | if ( m_pchOutputBitstreamFile ) |
---|
68 | { |
---|
69 | m_apcOutputBitstream = new TComBitstream; |
---|
70 | m_apcOutputBitstream->create( BITS_BUF_SIZE ); |
---|
71 | } |
---|
72 | else |
---|
73 | { |
---|
74 | m_apcOutputBitstream = NULL; |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | Void TAppExtrTop::destroy() |
---|
79 | { |
---|
80 | if ( m_apcInputBitstream ) |
---|
81 | { |
---|
82 | m_apcInputBitstream->destroy(); |
---|
83 | delete m_apcInputBitstream; |
---|
84 | m_apcInputBitstream = NULL; |
---|
85 | } |
---|
86 | |
---|
87 | if ( m_apcOutputBitstream ) |
---|
88 | { |
---|
89 | m_apcOutputBitstream->destroy(); |
---|
90 | delete m_apcOutputBitstream; |
---|
91 | m_apcOutputBitstream = NULL; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | // ==================================================================================================================== |
---|
96 | // Public member functions |
---|
97 | // ==================================================================================================================== |
---|
98 | |
---|
99 | /** |
---|
100 | - create internal class |
---|
101 | - initialize internal class |
---|
102 | - until the end of the bitstream, call extraction function in TExtrTop class |
---|
103 | - delete allocated buffers |
---|
104 | - destroy internal class |
---|
105 | . |
---|
106 | */ |
---|
107 | Void TAppExtrTop::extract() |
---|
108 | { |
---|
109 | TComBitstream* pcInputBitstream = m_apcInputBitstream; |
---|
110 | TComBitstream* pcOutputBitstream = m_apcOutputBitstream; |
---|
111 | |
---|
112 | // create & initialize internal classes |
---|
113 | xCreateExtrLib(); |
---|
114 | xInitExtrLib (); |
---|
115 | |
---|
116 | // main extractor loop |
---|
117 | Bool resizedBitstreamBuffer = false; |
---|
118 | |
---|
119 | while ( !m_cTVideoIOInputBitstreamFile.readBits( pcInputBitstream ) ) |
---|
120 | { |
---|
121 | // decide whether to extract packet or not |
---|
122 | if ( m_cTExtrTop.extract( pcInputBitstream, m_suiExtractLayerIds ) && pcOutputBitstream ) |
---|
123 | { |
---|
124 | xCopyInputPacketIntoOutput(); |
---|
125 | m_cTVideoIOOutputBitstreamFile.writeBits( pcOutputBitstream ); |
---|
126 | } |
---|
127 | |
---|
128 | if ( !resizedBitstreamBuffer ) |
---|
129 | { |
---|
130 | TComSPS *sps = m_cTExtrTop.getFirstSPS(); |
---|
131 | if ( sps ) |
---|
132 | { |
---|
133 | pcInputBitstream->destroy(); |
---|
134 | pcInputBitstream->create(sps->getWidth() * sps->getHeight() * 2); |
---|
135 | if ( pcOutputBitstream ) |
---|
136 | { |
---|
137 | pcOutputBitstream->destroy(); |
---|
138 | pcOutputBitstream->create(sps->getWidth() * sps->getHeight() * 2); |
---|
139 | } |
---|
140 | resizedBitstreamBuffer = true; |
---|
141 | } |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | // write SPS info file |
---|
146 | if ( m_pchSpsInfoFile ) |
---|
147 | { |
---|
148 | m_cTExtrTop.dumpSpsInfo( m_cSpsInfoFileHandle ); |
---|
149 | } |
---|
150 | m_cTExtrTop.dumpSpsInfo( std::cout ); |
---|
151 | |
---|
152 | // destroy internal classes |
---|
153 | xDestroyExtrLib(); |
---|
154 | } |
---|
155 | |
---|
156 | // ==================================================================================================================== |
---|
157 | // Protected member functions |
---|
158 | // ==================================================================================================================== |
---|
159 | |
---|
160 | Void TAppExtrTop::xCreateExtrLib() |
---|
161 | { |
---|
162 | // open bitstream files |
---|
163 | m_cTVideoIOInputBitstreamFile.openBits( m_pchInputBitstreamFile, false); // read mode |
---|
164 | if ( m_pchOutputBitstreamFile ) |
---|
165 | { |
---|
166 | m_cTVideoIOOutputBitstreamFile.openBits( m_pchOutputBitstreamFile, true); // write mode |
---|
167 | } |
---|
168 | |
---|
169 | // open text file |
---|
170 | if ( m_pchSpsInfoFile ) |
---|
171 | { |
---|
172 | m_cSpsInfoFileHandle.open( m_pchSpsInfoFile, ios::binary | ios::out ); |
---|
173 | |
---|
174 | if( m_cSpsInfoFileHandle.fail() ) |
---|
175 | { |
---|
176 | printf("\nfailed writing SPS info file\n"); |
---|
177 | exit(0); |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | // create extractor class |
---|
182 | m_cTExtrTop.create(); |
---|
183 | } |
---|
184 | |
---|
185 | Void TAppExtrTop::xDestroyExtrLib() |
---|
186 | { |
---|
187 | // close bitstream files |
---|
188 | m_cTVideoIOInputBitstreamFile.closeBits(); |
---|
189 | if ( m_pchOutputBitstreamFile ) |
---|
190 | { |
---|
191 | m_cTVideoIOOutputBitstreamFile.closeBits(); |
---|
192 | } |
---|
193 | |
---|
194 | // open text file |
---|
195 | if ( m_pchSpsInfoFile ) |
---|
196 | { |
---|
197 | m_cSpsInfoFileHandle.close(); |
---|
198 | } |
---|
199 | |
---|
200 | // destroy extractor class |
---|
201 | m_cTExtrTop.destroy(); |
---|
202 | } |
---|
203 | |
---|
204 | Void TAppExtrTop::xInitExtrLib() |
---|
205 | { |
---|
206 | // initialize extractor class |
---|
207 | m_cTExtrTop.init(); |
---|
208 | } |
---|
209 | |
---|
210 | Void TAppExtrTop::xCopyInputPacketIntoOutput() |
---|
211 | { |
---|
212 | UInt uiNumBytes = m_apcInputBitstream->reinitParsing(); |
---|
213 | UInt uiByteCount; |
---|
214 | UInt uiByte; |
---|
215 | |
---|
216 | assert( m_apcOutputBitstream ); |
---|
217 | |
---|
218 | m_apcOutputBitstream->rewindStreamPacket(); |
---|
219 | m_apcOutputBitstream->resetBits(); |
---|
220 | |
---|
221 | for ( uiByteCount = 0; uiByteCount < uiNumBytes; uiByteCount++ ) |
---|
222 | { |
---|
223 | m_apcInputBitstream->read( 8, uiByte ); |
---|
224 | m_apcOutputBitstream->write( uiByte, 8 ); |
---|
225 | } |
---|
226 | |
---|
227 | m_apcOutputBitstream->flushBuffer(); |
---|
228 | } |
---|