1 /* ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License
8 * Version 1.1 (the "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14 * the specific language governing rights and limitations under the License.
16 * The Original Code is BBC Research and Development code.
18 * The Initial Developer of the Original Code is the British Broadcasting
20 * Portions created by the Initial Developer are Copyright (C) 2004.
21 * All Rights Reserved.
23 * Contributor(s): Thomas Davies (Original Author),
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU General Public License Version 2 (the "GPL"), or the GNU Lesser
30 * Public License Version 2.1 (the "LGPL"), in which case the provisions of
31 * the GPL or the LGPL are applicable instead of those above. If you wish to
32 * allow use of your version of this file only under the terms of the either
33 * the GPL or LGPL and not to allow others to use your version of this file
34 * under the MPL, indicate your decision by deleting the provisions above
35 * and replace them with the notice and other provisions required by the GPL
36 * or LGPL. If you do not delete the provisions above, a recipient may use
37 * your version of this file under the terms of any one of the MPL, the GPL
39 * ***** END LICENSE BLOCK ***** */
41 #ifndef _SEQ_COMPRESS_H_
42 #define _SEQ_COMPRESS_H_
44 /////////////////////////////////////////
45 //-------------------------------------//
46 //Class to manage compressing sequences//
47 //-------------------------------------//
48 /////////////////////////////////////////
50 #include <libdirac_byteio/dirac_byte_stream.h>
51 #include <libdirac_common/common.h>
52 #include <libdirac_encoder/enc_queue.h>
53 #include <libdirac_common/pic_io.h>
54 #include <libdirac_common/dirac_assertions.h>
55 #include <libdirac_encoder/quality_monitor.h>
56 #include <libdirac_encoder/picture_compress.h>
57 #include <libdirac_encoder/rate_control.h>
64 //! Compresses a sequence of frames/fields from a stream.
66 This class compresses a sequence of frames/fields, frame by frame.
67 or field by field. It currently uses GOP parameters set in the encoder
68 parameters in order to define the temporal prediction structure.
69 A version to incorporate non-GOP structures is TBC.
71 This is an abstract class.
73 class SequenceCompressor
{
77 Creates a sequence compressor, and prepares to begin compressing
78 with the first picture.Sets up picture padding in the picture input if
80 \param pin an input stream containing a sequence of frames
81 \param encp parameters for the encoding process
82 \param dirac_byte_stream Output destination for compressed data
84 SequenceCompressor(StreamPicInput
* pin
,
86 DiracByteStream
& dirac_byte_stream
);
90 Destructor. Must delete IO objects created by constructor.
92 virtual ~SequenceCompressor();
96 Load one picture of data into the Sequence Compressor. Sets
97 m_all_done to true if no more data is available to be loaded.
98 Input can be frame or field. So the child class will have to
99 implement this function.
100 \return true - if frame load succeeded.
103 virtual bool LoadNextFrame() = 0;
105 //! Compress the next picture in sequence
107 This function codes the next picture in coding order and returns the
108 next picture in display order. In general these will differ, and
109 because of re-ordering there is a delay which needs to be imposed.
110 This creates problems at the start and at the end of the sequence
111 which must be dealt with. At the start we just keep outputting
112 picture 0. At the end you will need to loop for longer to get all
113 the pictures out. It's up to the calling function to do something
114 with the decoded pictures as they come out -- write them to screen
115 or to file, for example. .
116 If coding is fast enough the compressed version could be watched
117 real-time (with suitable buffering in the calling function to
118 account for encode-time variations).
120 NOTE: LoadNextFrame must be called atleast once before invoking this
123 \return pointer to the next locally decoded picture available for display
125 const EncPicture
*CompressNextPicture();
127 //! Set up the appropriate prediction parameters for a picture
128 virtual void SetPicTypeAndRefs( PictureParams
& pparams
) = 0;
130 //! Return a pointer to the most recent picture encoded
131 const EncPicture
*GetPictureEncoded();
133 DiracByteStats
EndSequence();
135 //! Determine if compression is complete.
137 Indicates whether or not the last picture in the sequence has been
139 \return true if last picture has been compressed; false if not
141 bool Finished(){return m_all_done
;}
143 //! Signal end of sequence
144 void SignalEOS() { m_eos_signalled
= true; }
146 //! The delay required for correct timestamps
147 int PTSOffset(){return m_delay
;}
151 //! Set up the motion block parameters
152 void SetMotionParameters();
154 //! Uses the GOP parameters to convert picture numbers in coded order to display order.
156 Uses the GOP parameters to convert picture numbers in coded order
157 to display order. Pure virtual function. The child class will
159 \param pnum the picture number in coded order
161 virtual int CodedToDisplay(const int pnum
) = 0;
163 //! Make a report to screen on the coding results for the whole sequence
164 void MakeSequenceReport();
166 //! Remove unwanted pictures from picture buffers
167 virtual void CleanBuffers();
169 //! Update the CBR model based on the data we've compressed.
170 //Purely virtual. The child class will have to define it.
171 virtual void UpdateCBRModel(EncPicture
& my_picture
, const PictureByteIO
* picture_byteio
) = 0;
173 //! Update the parameters to be used in advance of coding an intra frame
174 void UpdateIntraPicCBRModel( const PictureParams
& , const bool is_a_cut
);
176 //! Returns true if the encoder can encode a picture
179 //! Completion flag, returned via the Finished method.
182 //! Flag indicating whether we've just finished.
184 Flag which is false if we've been all-done for more than one
185 picture, true otherwise (so that we can take actions on finishing
188 bool m_just_finished
;
190 //! A class to hold the basic block parameters
191 OLBParams
* m_basic_olb_params0
;
193 //! A class to hold the basic block parameters
194 OLBParams
* m_basic_olb_params1
;
196 //! A class to hold the basic block parameters
197 const OLBParams
* m_basic_olb_params2
;
199 //! A class to hold block parameters to use when there are lots of intra blocks
200 OLBParams
* m_intra_olbp
;
202 //! The parameters of the input source
203 SourceParams
& m_srcparams
;
205 //! The parameters used for encoding.
206 EncoderParams
& m_encparams
;
208 //! The parameters used for ME/MC
209 PicturePredParams
& m_predparams
;
211 //! The L1 separation currently in use
214 //! Generic picture parameters for initialising pictures
215 PictureParams m_pparams
;
217 //! Pointer pointing at the picture input.
218 StreamPicInput
* m_pic_in
;
220 //! A picture buffer used for local storage of pictures whilst pending re-ordering or being used for reference.
221 EncQueue m_enc_pbuffer
;
223 //state variables for CompressNextPicture
225 //! The number of the current picture to be coded, in display order
226 int m_current_display_pnum
;
228 //! The number of the current picture to be coded, in coded order
229 int m_current_code_pnum
;
231 //! The number of the picture which should be output for concurrent display or storage
234 //! The index, in display order, of the last picture read
235 int m_last_picture_read
;
237 //! The picture number of the last GOP start
240 //! A delay so that we don't display what we haven't coded
243 //! A class for monitoring the quality of pictures and adjusting parameters appropriately
244 QualityMonitor m_qmonitor
;
246 //! A class for monitoring and controlling bit rate
247 RateController
* m_ratecontrol
;
249 //! A class to hold the picture compressor object
250 PictureCompressor m_pcoder
;
252 //! Output destination for compressed data in bitstream format
253 DiracByteStream
& m_dirac_byte_stream
;
255 //! Flag to check if End of Sequence has been signalled by the end user
256 bool m_eos_signalled
;
259 //! Copy constructor is private and body-less
261 Copy constructor is private and body-less. This class should not
264 SequenceCompressor(const SequenceCompressor
& cpy
);
266 //! Assignment = is private and body-less
268 Assignment = is private and body-less. This class should not be
271 SequenceCompressor
& operator=(const SequenceCompressor
& rhs
);
276 //! Compresses a sequence of frames from a stream.
278 This class compresses a sequence of frames, frame by frame. It
279 currently uses GOP parameters set in the encoder parameters in order
280 to define the temporal prediction structure. A version to incorporate
281 non-GOP structures is TBC.
283 class FrameSequenceCompressor
: public SequenceCompressor
288 Creates a sequence compressor that compresses frames i.e.
289 progressive data, and prepares to begin compressing
290 with the first frame.Sets up frame padding in the picture input if
292 \param pin an input stream containing a sequence of frames
293 \param encp parameters for the encoding process
294 \param dirac_byte_stream Output destination for compressed data
296 FrameSequenceCompressor(StreamPicInput
* pin
,
298 DiracByteStream
& dirac_byte_stream
);
302 Destructor. Must delete IO objects created by constructor.
304 virtual ~FrameSequenceCompressor(){};
308 Load one frame of data into the Sequence Compressor. Sets
309 m_all_done to true if no more data is available to be loaded.
310 \return true - if frame load succeeded.
313 virtual bool LoadNextFrame();
315 //! Set up the appropriate prediction parameters for a picture
316 virtual void SetPicTypeAndRefs( PictureParams
& pparams
);
319 virtual int CodedToDisplay(const int pnum
);
320 virtual void UpdateCBRModel(EncPicture
& my_picture
, const PictureByteIO
* picture_byteio
);
324 //! Compresses a sequence of fields from a stream.
326 This class compresses a sequence of fields, field by field. It
327 currently uses GOP parameters set in the encoder parameters in order
328 to define the temporal prediction structure. A version to incorporate
329 non-GOP structures is TBC.
331 class FieldSequenceCompressor
: public SequenceCompressor
336 Creates a sequence compressor that compresses fields i.e.
337 interlaced data, and prepares to begin compressing
338 with the first field.
339 \param pin an input stream containing a sequence of frames
340 \param encp parameters for the encoding process
341 \param dirac_byte_stream Output destination for compressed data
343 FieldSequenceCompressor(StreamPicInput
* pin
,
345 DiracByteStream
& dirac_byte_stream
);
349 Destructor. Must delete IO objects created by constructor.
351 virtual ~FieldSequenceCompressor();
355 Load one frame i.e. two fields of data into the Sequence
356 Compressor. Sets m_all_done to true if no more data is available
358 \return true - if both fields load succeeded.
361 virtual bool LoadNextFrame();
364 //! Set up the appropriate prediction parameters for a picture
365 virtual void SetPicTypeAndRefs( PictureParams
& pparams
);
369 virtual int CodedToDisplay(const int pnum
);
371 virtual void UpdateCBRModel(EncPicture
& my_picture
, const PictureByteIO
* picture_byteio
);
375 Low pass filter the components in the fields used in Motion
376 Estimation so that ME works better. Using a 1/4 1/2 1/4 filter
378 void PreMotionEstmationFilter (PicArray
& comp
);