1 /* ***** BEGIN LICENSE BLOCK *****
2 *
3 * $Id: common.h,v 1.79 2008/10/01 01:26:47 asuraparaju Exp $ $Name: Dirac_1_0_2 $
4 *
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 *
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/
11 *
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.
15 *
16 * The Original Code is BBC Research and Development code.
17 *
18 * The Initial Developer of the Original Code is the British Broadcasting
19 * Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2004.
21 * All Rights Reserved.
22 *
23 * Contributor(s): Thomas Davies (Original Author),
24 *                 Scott R Ladd,
25 *                 Tim Borer,
26 *                 Anuradha Suraparaju,
27 *                 Andrew Kennedy
28 *                 Myo Tun (Brunel University, myo.tun@brunel.ac.uk)
29 *
30 * Alternatively, the contents of this file may be used under the terms of
31 * the GNU General Public License Version 2 (the "GPL"), or the GNU Lesser
32 * Public License Version 2.1 (the "LGPL"), in which case the provisions of
33 * the GPL or the LGPL are applicable instead of those above. If you wish to
34 * allow use of your version of this file only under the terms of the either
35 * the GPL or LGPL and not to allow others to use your version of this file
36 * under the MPL, indicate your decision by deleting the provisions above
37 * and replace them with the notice and other provisions required by the GPL
38 * or LGPL. If you do not delete the provisions above, a recipient may use
39 * your version of this file under the terms of any one of the MPL, the GPL
40 * or the LGPL.
41 * ***** END LICENSE BLOCK ***** */
42 
43 #ifndef _COMMON_H_
44 #define _COMMON_H_
45 
46 #ifdef  _MSC_VER
47 #define  _CRT_SECURE_NO_DEPRECATE
48 #endif // _MSC_VER
49 
50 #include <libdirac_common/arrays.h>
51 #include <libdirac_common/common_types.h>
52 #include <libdirac_common/dirac_assertions.h>
53 #include <vector>
54 #include <cmath>
55 namespace dirac
56 {
57     /*! \file
58         This file contains common classes used throughout the encoder and
59         decoder.  The main classes are the encoder and decoder parameters for
60         controlling the encode and decode processes. These are passed
61         throughout the codec.  There are also parameter classes for sequences
62         and Pictures.
63     */
64 
65 
66     //Some basic types used throughout the codec ...//
67     //////////////////////////////////////////////////////////////
68 
69     //! Type of picture data (including motion compensated residuals)
70     typedef short ValueType;
71 
72 #if !defined(HAVE_MMX)
73     //! Type of wavelet coefficient data (should be larger than ValueType)
74     typedef int CoeffType;
75 #else
76     //! Type of wavelet coefficient data (should be larger than ValueType)
77     typedef short CoeffType;
78 #endif
79 
80     //! Type for performing calculations on ValueType and CoeffType. Should be >ValueType, >=CoeffType
81     typedef int CalcValueType;
82 
83     //! Prediction modes for blocks
84     enum PredMode{ INTRA , REF1_ONLY , REF2_ONLY , REF1AND2, UNDEFINED };
85 
86     //! Types of picture component
87     enum CompSort{ Y_COMP , U_COMP , V_COMP };
88 
89     //! Addition or subtraction
90     enum AddOrSub{ ADD , SUBTRACT };
91 
92     //! Forward or backward
93     enum Direction { FORWARD , BACKWARD };
94 
95     //! Contexts used for coefficient coding
96     enum CtxAliases
97     {//used for residual coding
98         SIGN0_CTX,          // -sign, previous symbol is 0
99         SIGN_POS_CTX,       // -sign, previous symbol is +ve
100         SIGN_NEG_CTX,       // -sign, previous symbol is -ve
101 
102         // Follow bit contexts
103         Z_FBIN1z_CTX,       // -bin 1, parent is zero, neighbours zero
104         Z_FBIN1nz_CTX,      // -bin 1, parent is zero, neighbours non-zero
105         Z_FBIN2_CTX,        // -bin 2, parent is zero
106         Z_FBIN3_CTX,        // -bin 3, parent is zero
107         Z_FBIN4_CTX,        // -bin 4, parent is zero
108         Z_FBIN5_CTX,        // -bin 5, parent is zero
109         Z_FBIN6plus_CTX,    // -bins 6 plus, parent is zero
110 
111         NZ_FBIN1z_CTX,      // -bin 1, parent is non-zero, neighbours zero
112         NZ_FBIN1nz_CTX,     // -bin 1, parent is non-zero, neighbours non-zero
113         NZ_FBIN2_CTX,       // -bin 2, parent is non-zero
114         NZ_FBIN3_CTX,       // -bin 3, parent is non-zero
115         NZ_FBIN4_CTX,       // -bin 4, parent is non-zero
116         NZ_FBIN5_CTX,       // -bin 5, parent is non-zero
117         NZ_FBIN6plus_CTX,   // -bins 6 plus, parent is non-zero
118 
119         // Information bit contexts
120         INFO_CTX,
121 
122         BLOCK_SKIP_CTX,     // - blocks are skipped
123         Q_OFFSET_FOLLOW_CTX,   // - code block quantiser offset magnitude
124         Q_OFFSET_INFO_CTX,  // - code block quantiser offset info context
125         Q_OFFSET_SIGN_CTX,   // - code block quantiser offset sign
126         TOTAL_COEFF_CTXS   // The total number of coefficient contexts
127     };
128 
129     //! Contexts used for MV data coding
130     enum MvCtxAliases
131     {
132         // DC value contexts //
133         ///////////////////////
134 
135         DC_FBIN1_CTX,
136         DC_FBIN2plus_CTX,
137         DC_INFO_CTX,
138         DC_SIGN_CTX,
139 
140         // Motion vector contexts //
141         ////////////////////////////
142 
143 
144         MV_FBIN1_CTX,
145         MV_FBIN2_CTX,
146         MV_FBIN3_CTX,
147         MV_FBIN4_CTX,
148         MV_FBIN5plus_CTX,
149 
150         MV_INFO_CTX,
151 
152         MV_SIGN_CTX,
153 
154 
155         // Prediction mode contexts
156 
157         PMODE_BIT0_CTX,     // -bit 0, prediction mode value
158         PMODE_BIT1_CTX,     // -bin 1, prediction mode value
159 
160 
161         // Macroblock contexts
162 
163         SB_SPLIT_BIN1_CTX,  // bin 1, SB split mode vals
164         SB_SPLIT_BIN2_CTX,  // bin 2, SB split mode vals. Bin 3 not required
165 
166         SB_SPLIT_INFO_CTX,  // info context for SB split mode
167 
168         TOTAL_MV_CTXS       // The total number of motion vector contexts
169     };
170 
171 
172     /**
173     * Function to convert an integer to a valid VideoFormat
174     *@param video_format Integer corresponding to a format
175     *@return Valid video-format (returns VIDEO_FORMAT_UNDEFINED if no valid format found)
176     */
177     VideoFormat IntToVideoFormat(int video_format);
178 
179     /**
180     * Function to convert an integer to a valid VideoFormat
181     *@param chroma_format Integer corresponding to a format
182     *@return Valid chroma-format (returns formatNK if no valid format found)
183     */
184     ChromaFormat IntToChromaFormat(int chroma_format);
185 
186      /**
187     * Function to convert an integer to a valid FrameRate type
188     *@param frame_rate_idx Integer corresponding to a frame-rate in the spec table
189     *@return Valid FrameRateType (returns FRAMERATE_UNDEFINED if no valid frame-rate found)
190     */
191     FrameRateType IntToFrameRateType(int frame_rate_idx);
192 
193     /**
194     * Function to convert an integer to a valid PixelAspectRatio type
195     *@param pix_asr_idx Integer corresponding to a pixel aspect ratio in the spec table
196     *@return Valid PixelAspectRatioType (returns PIXEL_ASPECT_RATIO_UNDEFINED if no valid pixel aspect ratio found)
197     */
198     PixelAspectRatioType IntToPixelAspectRatioType(int pix_asr_idx);
199 
200     /**
201     * Function to convert an integer to a valid SignalRange type
202     *@param signal_range_idx Integer corresponding to a signal-range in the spec table
203     *@return Valid SignalRangeType (returns SIGNAL_RANGE_UNDEFINED if no valid signal-ratio found)
204     */
205     SignalRangeType IntToSignalRangeType(int signal_range_idx);
206 
207      /**
208     * Function to convert an integer to a valid motion-vector precision type
209     *@param mv_prec Integer corresponding to a valid motion-vector precision
210     *@return Valid MVPrecisionType (returns SIGNAL_RANGE_UNDEFINED if no valid precision found)
211     */
212     MVPrecisionType IntToMVPrecisionType(int mv_prec);
213 
214     //Classes used throughout the codec//
215     /////////////////////////////////////
216 
217     //! Class defining a rational number
218     class Rational
219     {
220     public:
221         //! Numerator
222         unsigned int m_num;
223         //! Denominator
224         unsigned int m_denom;
225     };
226 
227    //! Picture type Class
228     class PictureSort
229     {
230     public:
PictureSort()231         PictureSort() { fs = 0x00; } // default intra non-ref
232 
SetIntra()233         void SetIntra() { fs &= 0xfe; }
SetInter()234         void SetInter() { fs |= 0x01; }
SetNonRef()235         void SetNonRef() { fs &= 0xfd; }
SetRef()236         void SetRef() { fs |= 0x02; }
237 
IsInter()238         bool IsInter () const { return fs & 0x01; }
IsIntra()239         bool IsIntra () const { return !IsInter(); }
IsRef()240         bool IsRef() const { return fs & 0x02; };
IsNonRef()241         bool IsNonRef() const { return !IsRef(); }
242 
SetIntraNonRef()243         void SetIntraNonRef() { SetIntra(); SetNonRef(); }
SetIntraRef()244         void SetIntraRef() { SetIntra(); SetRef(); }
SetInterNonRef()245         void SetInterNonRef() { SetInter(); SetNonRef(); }
SetInterRef()246         void SetInterRef() { SetInter(); SetRef(); }
247 
IsIntraNonRef()248         bool IsIntraNonRef() const { return (fs & 0x03) == 0x00; }
IsIntraRef()249         bool IsIntraRef() const { return (fs & 0x03) == 0x02; }
IsInterNonRef()250         bool IsInterNonRef() const { return (fs & 0x03) == 0x01; }
IsInterRef()251         bool IsInterRef() const { return (fs & 0x03) == 0x03; }
252 
Clear()253         void Clear() { fs=0x00; }
254 
IntraRefPictureSort()255         static PictureSort IntraRefPictureSort()
256         {
257             PictureSort fs;
258             fs.SetIntraRef();
259             return fs;
260         }
261 
InterRefPictureSort()262         static PictureSort InterRefPictureSort()
263         {
264             PictureSort fs;
265             fs.SetInterRef();
266             return fs;
267         }
268 
IntraNonRefPictureSort()269         static PictureSort IntraNonRefPictureSort()
270         {
271             PictureSort fs;
272             fs.SetIntraNonRef();
273             return fs;
274         }
275 
InterNonRefPictureSort()276         static PictureSort InterNonRefPictureSort()
277         {
278             PictureSort fs;
279             fs.SetInterNonRef();
280             return fs;
281         }
282 
283     private:
284         unsigned char fs;
285     };
286 
287     //! Parameters relating to the source material being encoded/decoded
288     class SourceParams
289     {
290     public:
291         //! default constructor
292         SourceParams (const VideoFormat &vf = VIDEO_FORMAT_CUSTOM,
293                       bool set_defaults=true);
294 
295         ////////////////////////////////////////////////////////////////////
296         //NB: Assume default copy constructor, assignment = and destructor//
297         ////////////////////////////////////////////////////////////////////
298 
299         // Gets
300         //! Returns video-format
GetVideoFormat()301         VideoFormat GetVideoFormat() const { return m_video_format;}
302 
303         //! Returns the picture width
Xl()304         unsigned int Xl() const {return m_xl;}
305 
306         //! Returns the picture height
Yl()307         unsigned int Yl() const {return m_yl;}
308 
309         //! Returns the chroma format of the sequence (420, 422, 444)
CFormat()310         ChromaFormat CFormat() const {return m_cformat;}
311 
312         //! Returns the chroma width
313         int ChromaWidth() const;
314 
315         //! Returns the chroma height
316         int ChromaHeight() const;
317 
318         //! Returns the source sampling field of the source scan format
SourceSampling()319         unsigned int SourceSampling() const { return m_source_sampling; }
320 
321         //! Returns true if top field comes first in time
TopFieldFirst()322         bool TopFieldFirst() const { return m_topfieldfirst; }
323 
324            //! Return the number for frames per second
FrameRate()325         Rational FrameRate() const { return m_framerate; }
326 
327         //! Return the type from the frame rate table
FrameRateIndex()328         FrameRateType FrameRateIndex() const { return m_fr_idx; }
329 
330            //! Return the pixel aspect ratio
PixelAspectRatio()331         Rational PixelAspectRatio() const { return m_pixel_aspect_ratio; }
332 
333          //! Return the type from the pixel aspect ratio table
PixelAspectRatioIndex()334         PixelAspectRatioType PixelAspectRatioIndex() const { return m_pix_asr_idx; }
335 
336         // Clean area parameters
337         //! Return the Clean area width
CleanWidth()338         unsigned int CleanWidth() const { return m_clean_width; }
339         //! Return the Clean area height
CleanHeight()340         unsigned int CleanHeight() const { return m_clean_height; }
341         //! Return the Clean area left offset
LeftOffset()342         unsigned int LeftOffset() const { return m_left_offset; }
343         //! Return the Clean area top offset
TopOffset()344         unsigned int TopOffset() const { return m_top_offset; }
345 
346         // Signal Range parameters
347 
348         //! Return the type from the signal range table
SignalRangeIndex()349         SignalRangeType SignalRangeIndex() const { return m_sr_idx; }
350 
351         //! Return the luma offset
LumaOffset()352         unsigned int LumaOffset() const { return m_luma_offset; }
353         //! Return the luma excursion
LumaExcursion()354         unsigned int LumaExcursion() const { return m_luma_excursion; }
355         //! Return the chroma offset
ChromaOffset()356         unsigned int ChromaOffset() const { return m_chroma_offset; }
357         //! Return the chroma excursion
ChromaExcursion()358         unsigned int ChromaExcursion() const { return m_chroma_excursion; }
359 
360         //! Return the index into the colour specification table
ColourSpecificationIndex()361         unsigned int ColourSpecificationIndex() const { return m_cs_idx; }
362 
363         //! Return the colour primaries index
ColourPrimariesIndex()364         ColourPrimaries ColourPrimariesIndex() const { return m_col_primary; }
365         //! Return the colour matrix index
ColourMatrixIndex()366         ColourMatrix ColourMatrixIndex() const { return m_col_matrix; }
367         //! Return the transfer function index
TransferFunctionIndex()368         TransferFunction TransferFunctionIndex() const { return m_transfer_func; }
369 
370         // Sets
371 
372         //! Sets the picture width
SetXl(unsigned int xlen)373         void SetXl(unsigned int xlen) {m_xl = xlen;}
374 
375         //! Sets the picture height
SetYl(unsigned int ylen)376         void SetYl(unsigned int ylen) {m_yl = ylen;}
377 
378         //! Sets the chroma format (Y only, 420, 422 etc)
SetCFormat(ChromaFormat cf)379         void SetCFormat(ChromaFormat cf) {m_cformat=cf;}
380 
381         //! Set if the source sampling field of the scan format
SetSourceSampling(unsigned int source_sampling)382         void SetSourceSampling(unsigned int source_sampling)
383         { m_source_sampling = source_sampling; }
384 
385         //! Set Topfield first. True if top field comes first in time
SetTopFieldFirst(bool tff)386         void SetTopFieldFirst(bool tff) { m_topfieldfirst = tff; }
387 
388         //! Sets the video format
SetVideoFormat(VideoFormat vf)389         void SetVideoFormat(VideoFormat vf){ m_video_format=vf;}
390 
391            //! Set the frame rate
SetFrameRate(const Rational & frate)392         void SetFrameRate(const Rational &frate )
393         {
394             m_fr_idx = FRAMERATE_CUSTOM; m_framerate = frate;
395         }
396 
397         //! Set the frame rate
SetFrameRate(unsigned int fr_num,unsigned int fr_denom)398         void SetFrameRate(unsigned int fr_num, unsigned int fr_denom )
399         {
400             m_fr_idx = FRAMERATE_CUSTOM;
401             m_framerate.m_num = fr_num;
402             m_framerate.m_denom = fr_denom;
403         }
404 
405         //! Set the frame rate
406         void SetFrameRate(FrameRateType fr);
407 
408            //! Set the pixel aspect ratio
SetPixelAspectRatio(const Rational & pix_asr)409         void SetPixelAspectRatio(const Rational &pix_asr)
410         {
411             m_pix_asr_idx = PIXEL_ASPECT_RATIO_CUSTOM;
412             m_pixel_aspect_ratio = pix_asr;
413         }
414 
415         //! Set the pixel aspect ratio
SetPixelAspectRatio(unsigned int pix_as_num,unsigned int pix_as_denom)416         void SetPixelAspectRatio(unsigned int pix_as_num, unsigned int pix_as_denom )
417         {
418             m_pix_asr_idx = PIXEL_ASPECT_RATIO_CUSTOM;
419             m_pixel_aspect_ratio.m_num = pix_as_num;
420             m_pixel_aspect_ratio.m_denom = pix_as_denom;
421         }
422 
423         //! Set the Pixel Aspect Ratio
424         void SetPixelAspectRatio(PixelAspectRatioType pixel_aspect_ratio);
425 
426         // Clean area parameters
427         //! Set the Clean area width
SetCleanWidth(unsigned int clean_width)428         void SetCleanWidth(unsigned int clean_width) { m_clean_width = clean_width; }
429         //! Set the Clean area height
SetCleanHeight(unsigned int clean_height)430         void SetCleanHeight(unsigned int clean_height) { m_clean_height = clean_height; }
431         //! Set the Clean area left offset
SetLeftOffset(unsigned int left_offset)432         void SetLeftOffset(unsigned int left_offset) { m_left_offset = left_offset; }
433         //! Set the Clean area top offset
SetTopOffset(unsigned int top_offset)434         void SetTopOffset(unsigned int top_offset) { m_top_offset = top_offset; }
435 
436         // Signal Range parameters
437         //! Set the Signal Range parameters
438         void SetSignalRange(SignalRangeType sr);
439 
440         //! Set the luma offset
SetLumaOffset(unsigned int luma_offset)441         void SetLumaOffset(unsigned int luma_offset) { m_sr_idx = SIGNAL_RANGE_CUSTOM; m_luma_offset = luma_offset; }
442         //! Set the luma excursion
SetLumaExcursion(unsigned int luma_exc)443         void SetLumaExcursion(unsigned int luma_exc) { m_sr_idx = SIGNAL_RANGE_CUSTOM; m_luma_excursion = luma_exc; }
444         //! Set the chroma offset
SetChromaOffset(unsigned int chroma_off)445         void SetChromaOffset(unsigned int chroma_off) { m_sr_idx = SIGNAL_RANGE_CUSTOM; m_chroma_offset = chroma_off; }
446         //! Set the chroma excursion
SetChromaExcursion(unsigned int chroma_exc)447         void SetChromaExcursion(unsigned int chroma_exc) { m_sr_idx = SIGNAL_RANGE_CUSTOM; m_chroma_excursion = chroma_exc; }
448 
449         //! Set the Colour specification
450         void SetColourSpecification(unsigned int cs_idx);
451         //! Set the colour primaries index
452         void SetColourPrimariesIndex(unsigned int cp);
453         //! Set the colour matrix index
454         void SetColourMatrixIndex(unsigned int cm);
455         //! Set the transfer function index
456         void SetTransferFunctionIndex(unsigned int tf);
457 
458     private:
459         //!Video-format
460         VideoFormat m_video_format;
461 
462         //! Width of video
463         unsigned int m_xl;
464 
465         //! Height of video
466         unsigned int m_yl;
467 
468         //! Presence of chroma and/or chroma sampling structure
469         ChromaFormat m_cformat;
470 
471         //! Source sampling field : 0 - progressive, 1 - interlaced
472         unsigned int m_source_sampling;
473 
474         //! If m_source_sampling=1, true if the top field is first in temporal order
475         bool m_topfieldfirst;
476 
477         //! Index into frame rate table
478         FrameRateType m_fr_idx;
479 
480         //! Frame Rate i.e number of frames per second
481         Rational m_framerate;
482 
483         //! Index into pixel aspect ratio table
484         PixelAspectRatioType m_pix_asr_idx;
485 
486         //! Pixel Aspect Ratio
487         Rational m_pixel_aspect_ratio;
488 
489         // Clean area parameters
490 
491         //! Clean area width
492         unsigned int m_clean_width;
493 
494         //! Clean area height
495         unsigned int m_clean_height;
496 
497         //! Clean area left offset
498         unsigned int m_left_offset;
499 
500         //! Clean area top offset
501         unsigned int m_top_offset;
502 
503         // signal range parameters
504 
505         //! Index into signal range table
506         SignalRangeType m_sr_idx;
507 
508         //! Luma offset
509         unsigned int m_luma_offset;
510         //! Luma excursion
511         unsigned int m_luma_excursion;
512         //! Chroma offset
513         unsigned int m_chroma_offset;
514         //! Chroma excursion
515         unsigned int m_chroma_excursion;
516 
517         //! Index into colour spec table
518         unsigned int m_cs_idx;
519 
520         //! Colour Primaries Index
521         ColourPrimaries m_col_primary;
522 
523         // Colour Matrix index
524         ColourMatrix m_col_matrix;
525 
526         // Transfer function index
527         TransferFunction m_transfer_func;
528     };
529 
530 
531     //! Parameters for initialising picture class objects
532     class PictureParams
533     {
534 
535     public:
536         //! Default constructor
537         PictureParams();
538 
539         //! Constructor
540         /*!
541            Picture chroma format is set Picture sort defaults to I picture.
542         */
543         PictureParams(const ChromaFormat& cf, int xlen, int ylen,
544                     unsigned int luma_depth, unsigned int chroma_depth);
545 
546         //! Constructor
547         /*!
548            Picture chroma format and picture sort are set.
549         */
550         PictureParams(const ChromaFormat& cf, const PictureSort& fs);
551 
552         //! Constructor
553         /*!
554            Constructor. Parameters are derived from the source parameters
555         */
556         PictureParams(const SourceParams& sparams);
557 
558         ////////////////////////////////////////////////////////////////////
559         //NB: Assume default copy constructor, assignment = and destructor//
560         ////////////////////////////////////////////////////////////////////
561 
562         // Gets ...
563 
564         //! Returns the chroma format of the picture
CFormat()565         const ChromaFormat& CFormat() const{return m_cformat;}
566 
567         //! Returns the picture width
Xl()568         int Xl() const {return m_xl;}
569 
570         //! Returns the picture height
Yl()571         int Yl() const {return m_yl;}
572 
573         //! Returns the chroma width of the picture
ChromaXl()574         int ChromaXl() const{return m_cxl;}
575 
576         //! Returns the chroma height of the picture
ChromaYl()577         int ChromaYl() const{return m_cyl;}
578 
579         //! Returns the luma depth
LumaDepth()580         unsigned int LumaDepth() const { return m_luma_depth; }
581 
582         //! Returns the chroma depth
ChromaDepth()583         unsigned int ChromaDepth() const { return m_chroma_depth; }
584 
585         //! Returns the type of the picture
PicSort()586         const PictureSort& PicSort() const {return m_psort;}
587 
588         //! Returns the number of the picture (in time order)
PictureNum()589         int PictureNum() const {return m_fnum;}
590 
591         //! Returns the retired reference picture number
RetiredPictureNum()592         int RetiredPictureNum() const {return m_retd_fnum;}
593 
594         //! Returns whether the picture is bi-directionally predicted by checking references
595         bool IsBPicture() const;
596 
597         //! Returns the number of pictures after the current picture number after which the picture can be discarded
ExpiryTime()598         int ExpiryTime() const {return m_expiry_time;}
599 
600         //! Returns an indication of whether the picture has been output yet
Output()601         bool Output() const {return m_output;}
602 
603         //! Returns a const C++ reference to the set of reference picture numbers (will be empty if the picture is an I picture)
Refs()604         const std::vector<int>& Refs() const {return m_refs;}
605 
606         //! Returns non-const C++ referece to the vector of reference pictures, to allow them to be set
Refs()607         std::vector<int>& Refs(){return m_refs;}
608 
609         //! Return the number of reference pictures
NumRefs()610         unsigned int NumRefs()const {return m_refs.size();}
611 
612         //! Returns type of picture (see enum)
GetPictureType()613         PictureType GetPictureType () const { return m_picture_type; }
614 
615         //! Returns reference picture type (see enum)
GetReferenceType()616         ReferenceType GetReferenceType() const { return m_reference_type;}
617 
618         //! Returns true is entropy coding using Arithmetic coding
UsingAC()619         bool UsingAC() const { return m_using_ac; }
620 
621         // ... Sets
622 
623         //! Sets the type of picture
624         void SetPicSort( const PictureSort& ps );
625 
626         //! Sets the picture to be Intra/Inter
627         void SetPictureType(const PictureType ftype);
628 
629         //! Sets the picture to be a reference or not
630         void SetReferenceType(const ReferenceType rtype);
631 
632         //! Sets the picture number
SetPictureNum(const int fn)633         void SetPictureNum( const int fn ){ m_fnum=fn; }
634 
635         //! Sets how long the picture will stay in the buffer (encoder only)
SetExpiryTime(const int expt)636         void SetExpiryTime( const int expt ){ m_expiry_time=expt; }
637 
638         //! Sets a flag to indicate that the picture has been output
SetAsOutput()639         void SetAsOutput(){m_output=true;}
640 
641         //! Sets the chroma format
SetCFormat(ChromaFormat cf)642         void SetCFormat(ChromaFormat cf){ m_cformat = cf; }
643 
644         //! Sets the picture width
645         void SetXl(int xlen);
646 
647         //! Sets the picture height
648         void SetYl(int ylen);
649 
650         //! Set Luma Depth
SetLumaDepth(unsigned int luma_depth)651         void SetLumaDepth(unsigned int luma_depth) { m_luma_depth = luma_depth; }
652 
653         //! Set Chroma Depth
SetChromaDepth(unsigned int chroma_depth)654         void SetChromaDepth(unsigned int chroma_depth) { m_chroma_depth = chroma_depth; }
655 
656         //! Sets the retired reference picture number
SetRetiredPictureNum(int retd_fnum)657         void SetRetiredPictureNum(int retd_fnum) {m_retd_fnum = retd_fnum;}
658 
659         //! Sets the arithmetic coding flag
SetUsingAC(bool using_ac)660         void SetUsingAC(bool using_ac) { m_using_ac = using_ac; }
661 
662     private:
663 
664         //! The chroma format
665         ChromaFormat m_cformat;
666 
667         //! The picture sort
668         PictureSort m_psort;
669 
670         //! The set of picture numbers of reference pictures
671         std::vector<int> m_refs;
672 
673         //! The number of pictures, after the current picture number, after the (de)coding of which the picture can be deleted
674         int m_expiry_time;
675 
676         //! The picture number, in temporal order
677         int m_fnum;
678 
679         //! Picture type
680         PictureType m_picture_type;
681 
682         //! Reference type
683         ReferenceType m_reference_type;
684 
685         //! True if the picture has been output, false if not
686         bool m_output;
687 
688         //! The picture number of the retired picture
689         mutable  int m_retd_fnum;
690 
691         //! Picture luma width
692         int m_xl;
693 
694         //! Picture luma height
695         int m_yl;
696 
697         //! Picture chroma width
698         int m_cxl;
699 
700         //! Picture chroma height
701         int m_cyl;
702 
703         //! Luma depth - number of bits required for lumz
704         unsigned int m_luma_depth;
705 
706         //! chroma depth - number of bits required for luma
707         unsigned int m_chroma_depth;
708 
709         //! arithmetic coding flag
710         bool m_using_ac;
711     };
712 
713 
714     //! A class for picture component data.
715     /*!
716         A class for encapsulating picture data, derived from TwoDArray.
717      */
718     class PicArray: public TwoDArray<ValueType>
719     {
720     public:
721         //! Default constructor
722         /*!
723             Default constructor creates an empty array.
724         */
PicArray()725         PicArray(): TwoDArray<ValueType>(){}
726 
727         //! Constructor.
728         /*!
729             Contructor creates a two-D array, with specified size and colour
730             format.
731         */
732         PicArray(int height, int width, CompSort cs=Y_COMP):
733             TwoDArray<ValueType>(height, width), m_csort(cs){}
734 
735         //copy constructor and assignment= derived by inheritance
736 
737         //! Destructor
~PicArray()738         ~PicArray(){}
739 
740         //! Return which component is stored
CSort()741         const CompSort& CSort() const {return m_csort;}
742 
743         //! Set the type of component being stored
SetCSort(const CompSort cs)744         void SetCSort(const CompSort cs){ m_csort = cs; }
745 
746     private:
747 
748         CompSort m_csort;
749     };
750 
751 
752     //! A structure for recording costs, particularly in quantisation.
753     class CostType
754     {
755     public:
756         //! The error (MSE or 4th power)
757         double Error;
758 
759         //! The entropy in bits per symbol.
760         double ENTROPY;
761 
762         //! The Lagrangian combination of MSE+lambda*entropy
763         double TOTAL;
764     };
765 
766 
767     //! A class used for correcting estimates of entropy.
768     /*!
769         A class used by the encoder for correcting estimates of entropy. Used
770         for selecting quantisers in subband coefficient coding. Factors can be
771         adjusted in the light of previous experience.
772      */
773     class EntropyCorrector
774     {
775     public:
776         //! Constructor.
777         /*!
778         Constructs arrays of correction factors of size.
779         \param    depth    the depth of the wavelet transform.
780         */
781         EntropyCorrector(int depth);
782 
783         ////////////////////////////////////////////////////////////////////
784         //NB: Assume default copy constructor, assignment = and destructor//
785         ////////////////////////////////////////////////////////////////////
786 
787         //! Returns the correction factor.
788         /*!
789         Returns the correction factor for the band given also the type of
790         picture and component.
791         */
792         float Factor(const int bandnum, const PictureParams& pp,
793                      const CompSort c) const;
794 
795         //! Update the correction factors.
796         /*!
797         Update the factors for a given subband, component and picture type.
798         \param    bandnum    the number of the subband to update
799         \param    pp         picture parameters
800         \param    c          component type
801         \param    est_bits    the number of bits it was estimated would be used
802         \param    actual_bits    the number of bits that actually were used
803          */
804         void Update(int bandnum, const PictureParams& pp,
805                     CompSort c,int est_bits,int actual_bits);
806 
807     private:
808         //! Initialises the correction factors
809         void Init();
810 
811         TwoDArray<float> m_Yfctrs;
812         TwoDArray<float> m_Ufctrs;
813         TwoDArray<float> m_Vfctrs;
814     };
815 
816     //! Parameters for overlapped block motion compensation
817     class OLBParams
818     {
819 
820     public:
821 
822         //! Default constructor does nothing
OLBParams()823         OLBParams(){}
824 
825         //! Constructor
826         /*
827             Constructor rationalises proposed parameters to allow suitable
828             overlap and fit in with chroma format
829             \param    xblen    the horizontal block length
830             \param    yblen    the vertical block length
831             \param    xblen    the horizontal block separation
832             \param    yblen    the vertical block separation
833 
834         */
835         OLBParams(const int xblen, const int yblen,
836                   const int xbsep, const int ybsep);
837 
838         // Gets ...
839 
840         //! Returns the horizontal block length
Xblen()841         int Xblen() const {return m_xblen;}
842 
843         //! Returns the vertical block length
Yblen()844         int Yblen() const {return m_yblen;}
845 
846         //! Returns the horizontal block separation
Xbsep()847         int Xbsep() const {return m_xbsep;}
848 
849         //! Returns the vertical block separation
Ybsep()850         int Ybsep() const {return m_ybsep;}
851 
852         //! The offset in the horizontal start of the block caused by overlap,=(XBLEN-XBSEP)/2
Xoffset()853         int Xoffset() const {return m_xoffset;}
854 
855         //! The offset in the vertical start of the block caused by overlap,=(YBLEN-YBSEP)/2
Yoffset()856         int Yoffset() const {return m_yoffset;}
857 
858         // ... and sets
859 
860         //! Sets the block width
SetXblen(int xblen)861         void SetXblen( int xblen ){ m_xblen = xblen; m_xoffset = (m_xblen-m_xbsep)/2;}
862 
863         //! Sets the block height
SetYblen(int yblen)864         void SetYblen( int yblen ){ m_yblen = yblen; m_yoffset = (m_yblen-m_ybsep)/2;}
865 
866         //! Sets the block horizontal separation
SetXbsep(int xbsep)867         void SetXbsep( int xbsep ){ m_xbsep = xbsep; m_xoffset = (m_xblen-m_xbsep)/2;}
868 
869         //! Sets the block vertical separation
SetYbsep(int ybsep)870         void SetYbsep( int ybsep ){ m_ybsep = ybsep; m_yoffset = (m_yblen-m_ybsep)/2;}
871 
872         bool operator == (const OLBParams bparams) const;
873 
874         // overloaded stream operators
875         friend std::ostream & operator<< (std::ostream &, OLBParams &);
876         friend std::istream & operator>> (std::istream &, OLBParams &);
877 
878 
879     private:
880 
881         int m_xblen;
882         int m_yblen;
883         int m_xbsep;
884         int m_ybsep;
885         int m_xoffset;
886         int m_yoffset;
887     };
888 
889     //! Parameters relating to the complexity of encoder/decoder
890     class ParseParams
891     {
892     public:
893         //! Default constructor
894         ParseParams();
895 
896         // Gets
897 
898         //! Get the major version
MajorVersion()899         unsigned int MajorVersion() const { return m_major_ver; }
900 
901         //! Get the minor version
MinorVersion()902         unsigned int MinorVersion() const { return m_minor_ver; }
903 
904         //! Get the Profile
Profile()905         unsigned int Profile() const { return m_profile; }
906 
907         //! Get the Level
Level()908         unsigned int Level() const { return m_level; }
909 
910         // Sets
911 
912         //! Set the major version
SetMajorVersion(unsigned int major_ver)913         void SetMajorVersion(unsigned int major_ver) {m_major_ver = major_ver; }
914 
915         //! Set the minor version
SetMinorVersion(unsigned int minor_ver)916         void SetMinorVersion(unsigned int minor_ver) { m_minor_ver = minor_ver; }
917 
918         //! Set the Profile
SetProfile(unsigned int profile)919         void SetProfile(unsigned int profile) { m_profile = profile; }
920 
921         //! Set the Level
SetLevel(unsigned int level)922         void SetLevel(unsigned int level) { m_level = level; }
923 
924     private:
925         //! Major Version
926         unsigned int m_major_ver;
927         //! Minor Version
928         unsigned int m_minor_ver;
929         //! Profile
930         unsigned int m_profile;
931         //! Level
932         unsigned int m_level;
933     };
934 
935     //! Structure to hold code block sizes when spatial partitioning is used
936     class CodeBlocks
937     {
938     public:
939         //! Default Constructor
CodeBlocks()940         CodeBlocks () : m_hblocks(1), m_vblocks(1)
941         {}
942 
943         //! Constructor
CodeBlocks(unsigned int hblocks,unsigned int vblocks)944         CodeBlocks (unsigned int  hblocks, unsigned int vblocks) :
945             m_hblocks(hblocks),
946             m_vblocks(vblocks)
947             {}
948 
949         // Gets
950         //! Return the number of horizontal code blocks
HorizontalCodeBlocks()951         unsigned int HorizontalCodeBlocks() const { return m_hblocks; }
952         //! Return the number of vertical code blocks
VerticalCodeBlocks()953         unsigned int VerticalCodeBlocks() const { return m_vblocks; }
954         // Sets
955         //! Set the number of horizontal code blocks
SetHorizontalCodeBlocks(unsigned int hblocks)956         void SetHorizontalCodeBlocks(unsigned int hblocks) { m_hblocks = hblocks; }
957         //! Set the number of vertical code blocks
SetVerticalCodeBlocks(unsigned int vblocks)958         void SetVerticalCodeBlocks(unsigned int vblocks) { m_vblocks = vblocks; }
959     private:
960         //! Number of Horizontal code blocks
961         unsigned int m_hblocks;
962         //! Number of Vertical code blocks
963         unsigned int m_vblocks;
964     };
965 
966     //! Structure to hold motion parameters when motion comp is used
967     class PicturePredParams
968     {
969     public:
PicturePredParams()970         PicturePredParams():
971 	m_lbparams(3),
972         m_cbparams(3) {}
973 
974         //! Return the global motion flag used for encoding/decoding
UsingGlobalMotion()975         bool UsingGlobalMotion() const { return m_use_global_motion; }
976 
977         //! Return the number of picture weight precision bits
PictureWeightsBits()978         unsigned int PictureWeightsBits() const { return m_picture_weights_bits; }
979 
980         //! Return the Ref1 weight
Ref1Weight()981         int Ref1Weight() const { return m_ref1_weight; }
982 
983         //! Return the Ref2 weight
Ref2Weight()984         int Ref2Weight() const { return m_ref2_weight; }
985 
CustomRefWeights()986         bool CustomRefWeights()
987         {
988             return (m_picture_weights_bits != 1 ||
989                     m_ref1_weight != 1 ||
990                     m_ref2_weight != 1);
991         }
992 
993         //! Return the number of superblocks horizontally
XNumSB()994         int XNumSB() const {return m_x_num_sb;}
995 
996         //! Return the number of superblocks vertically
YNumSB()997         int YNumSB() const {return m_y_num_sb;}
998 
999         //! Return the number of blocks horizontally
XNumBlocks()1000         int XNumBlocks() const {return m_x_num_blocks;}
1001 
1002         //! Returns the number of blocks vertically
YNumBlocks()1003         int YNumBlocks() const {return m_y_num_blocks;}
1004 
1005         //! Return the Luma block parameters for each macroblock splitting level
LumaBParams(int n)1006         const OLBParams& LumaBParams(int n) const {return m_lbparams[n];}
1007 
1008         //! Return the Chroma block parameters for each macroblock splitting level
ChromaBParams(int n)1009         const OLBParams& ChromaBParams(int n) const {return m_cbparams[n];}
1010 
1011         //! Return the number of accuracy bits used for motion vectors
MVPrecision()1012         MVPrecisionType MVPrecision() const { return m_mv_precision; }
1013 
1014         //! Set how many SBs there are horizontally
SetXNumSB(const int xn)1015         void SetXNumSB(const int xn){m_x_num_sb=xn;}
1016 
1017         //! Set how many SBs there are vertically
SetYNumSB(const int yn)1018         void SetYNumSB(const int yn){m_y_num_sb=yn;}
1019 
1020         //! Set how many blocks there are horizontally
SetXNumBlocks(const int xn)1021         void SetXNumBlocks(const int xn){m_x_num_blocks=xn;}
1022 
1023         //! Set how many blocks there are vertically
SetYNumBlocks(const int yn)1024         void SetYNumBlocks(const int yn){m_y_num_blocks=yn;}
1025 
1026         //! Set the block sizes for all SB splitting levels given these prototype block sizes for level=2
1027         void SetBlockSizes(const OLBParams& olbparams , const ChromaFormat cformat);
1028 
1029         //! Set block level luma params
SetLumaBlockParams(const OLBParams & olbparams)1030         void SetLumaBlockParams(const OLBParams& olbparams) {m_lbparams[2] = olbparams;}
1031 
1032         //! Set the number of accuracy bits for motion vectors
SetMVPrecision(const MVPrecisionType p)1033         void SetMVPrecision(const MVPrecisionType p)
1034         {
1035             // Assert in debug mode. Maybe we should throw an exception???
1036             TESTM((p >=0 && p <=3), "Motion precision value in range 0..3");
1037             m_mv_precision = p;
1038         }
1039 
SetMVPrecision(const MVPrecisionType p)1040         void SetMVPrecision(const MVPrecisionType p) const
1041         {
1042             // Assert in debug mode. Maybe we should throw an exception???
1043             TESTM((p >=0 && p <=3), "Motion precision value in range 0..3");
1044             m_mv_precision = p;
1045         }
1046 
1047         //! Set the wavelet filter used for picture (de)coding
SetUsingGlobalMotion(bool gm)1048         void SetUsingGlobalMotion(bool gm) { m_use_global_motion=gm; }
1049 
1050         //! Set the picture weight precision bits used for (de)coding
SetPictureWeightsPrecision(unsigned int wt_prec)1051         void SetPictureWeightsPrecision(unsigned int wt_prec) { m_picture_weights_bits=wt_prec; }
1052 
1053         //! Set the ref 1 picture weight
SetRef1Weight(int wt)1054         void SetRef1Weight(int wt) { m_ref1_weight=wt; }
1055 
1056         //! Set the ref 2 picture weight
SetRef2Weight(int wt)1057         void SetRef2Weight(int wt) { m_ref2_weight=wt; }
1058 
1059     private:
1060 
1061         //! The number of superblocks horizontally
1062         int m_x_num_sb;
1063 
1064         //! The number of superblocks verticaly
1065         int m_y_num_sb;
1066 
1067         //! The number of blocks horizontally
1068         int m_x_num_blocks;
1069 
1070         //! The number of blocks vertically
1071         int m_y_num_blocks;
1072 
1073         OneDArray<OLBParams> m_lbparams;
1074 
1075         OneDArray<OLBParams> m_cbparams;
1076 
1077         //! The precision of motion vectors (number of accuracy bits eg 1=half-pel accuracy)
1078         mutable MVPrecisionType m_mv_precision;
1079 
1080         //! picture predicion parameters - precision
1081         unsigned int m_picture_weights_bits;
1082 
1083         //! picture predicion parameters - reference picture 1 weight
1084         int m_ref1_weight;
1085 
1086         //! picture predicion parameters - reference picture 2 weight
1087         int m_ref2_weight;
1088 
1089         //! Global motion fields
1090         bool m_use_global_motion;
1091 
1092     };
1093 
1094     //! Parameters common to coder and decoder operation
1095     /*!
1096         Parameters used throughout both the encoder and the decoder
1097     */
1098     class CodecParams
1099     {
1100     public:
1101 
1102         //! Default constructor
1103         CodecParams (const VideoFormat& video_format = VIDEO_FORMAT_CUSTOM,
1104                       PictureType ftype = INTRA_PICTURE,
1105                       unsigned int num_refs = 0,
1106                       bool set_defaults=true);
1107 
1108         ////////////////////////////////////////////////////////////////////
1109         //NB: Assume default copy constructor, assignment = and destructor//
1110         ////////////////////////////////////////////////////////////////////
1111 
1112         // Gets ...
1113 
1114         //! Returns the picture coding mode (independent of source format)
1115         /*! Returns the picture coding mode (independent of source format)
1116          *  0 = Frame coding (no quincunx)
1117          *  1 = Field coding (no quincunx)
1118          */
PictureCodingMode()1119         int PictureCodingMode() const {return m_pic_coding_mode;}
1120 
1121         //! Returns true if the pictures are being coded as fields (mode 1 or 3)
FieldCoding()1122         bool FieldCoding() const { return  (m_pic_coding_mode==1); }
1123 
1124         //! Returns true if the topmost field comes first in time when coding
TopFieldFirst()1125         bool TopFieldFirst() const {return m_topfieldfirst;}
1126 
1127         //! Return the picture/field luma width
Xl()1128         int Xl() const {return m_xl;}
1129 
1130         //! Return the picture/field luma height
Yl()1131         int Yl() const {return m_yl;}
1132 
1133         //! Return the picture/field chroma width
ChromaXl()1134         int ChromaXl() const {return m_cxl;}
1135 
1136         //! Return the picture/field chroma height
ChromaYl()1137         int ChromaYl() const {return m_cyl;}
1138 
1139         //! Returns the luma depth
LumaDepth()1140         unsigned int LumaDepth() const { return m_luma_depth; }
1141 
1142         //! Returns the chroma depth
ChromaDepth()1143         unsigned int ChromaDepth() const { return m_chroma_depth; }
1144 
1145         //! Return zero transform flag being used for picture (de)coding
ZeroTransform()1146         bool ZeroTransform() const { return m_zero_transform; }
1147 
1148         //! Return the wavelet filter currently being used for picture (de)coding
TransformFilter()1149         WltFilter TransformFilter() const { return m_wlt_filter; }
1150 
1151         //! Return the transform depth being used for picture (de)coding
TransformDepth()1152         unsigned int TransformDepth() const { return m_wlt_depth; }
1153 
1154         //! Return multiple quantisers flag being used for picture (de)coding
GetCodeBlockMode()1155         CodeBlockMode GetCodeBlockMode() const { return m_cb_mode; }
1156 
1157         //! Return the spatial partitioning flag being used for picture (de)coding
SpatialPartition()1158         bool SpatialPartition() const { return m_spatial_partition; }
1159 
1160         //! Return the code blocks for a particular level
1161         const CodeBlocks &GetCodeBlocks(unsigned int level) const;
1162 
1163         //! Return the video format currently being used for picture (de)coding
GetVideoFormat()1164         VideoFormat GetVideoFormat() const { return m_video_format; }
1165 
1166 	//! Return the picture prediction params
GetPicPredParams()1167 	PicturePredParams& GetPicPredParams(){return m_picpredparams;}
1168 
1169         //! Return the picture prediction params
GetPicPredParams()1170 	const PicturePredParams& GetPicPredParams() const {return m_picpredparams;}
1171 
1172        // ... and Sets
1173         //! Sets whether input is coded as fields or quincunxially
SetPictureCodingMode(int pic_coding)1174         void SetPictureCodingMode(int pic_coding){m_pic_coding_mode=pic_coding;}
1175 
1176         //! Sets whether the topmost field comes first in time [NB: TBD since this duplicates metadata in the sequence header]
SetTopFieldFirst(bool topf)1177         void SetTopFieldFirst(bool topf){m_topfieldfirst=topf;}
1178 
1179         //! Set the picture/field luma width
SetXl(const int x)1180         void SetXl(const int x){m_xl=x;}
1181 
1182         //! Set the picture/field luma height
SetYl(const int y)1183         void SetYl(const int y){m_yl=y;}
1184 
1185         //! Set the frame/field chroma width
SetChromaXl(const int x)1186         void SetChromaXl(const int x){m_cxl=x;}
1187 
1188         //! Set the frame/field chroma height
SetChromaYl(const int y)1189         void SetChromaYl(const int y){m_cyl=y;}
1190 
1191         //! Set Luma Depth
SetLumaDepth(unsigned int luma_depth)1192         void SetLumaDepth(unsigned int luma_depth) { m_luma_depth = luma_depth; }
1193 
1194         //! Set Chroma Depth
SetChromaDepth(unsigned int chroma_depth)1195         void SetChromaDepth(unsigned int chroma_depth) { m_chroma_depth = chroma_depth; }
1196 
1197         //! Set the zero transform flag being used for picture (de)coding
SetZeroTransform(bool zero_transform)1198         void SetZeroTransform(bool zero_transform)  { m_zero_transform = zero_transform; }
1199 
1200         //! Set the wavelet filter used for picture (de)coding
SetTransformFilter(const WltFilter wf)1201         void SetTransformFilter(const WltFilter wf) { m_wlt_filter=wf; }
1202 
1203         //! Set the wavelet filter used for picture (de)coding
1204         void SetTransformFilter(unsigned int wf_idx);
1205 
1206         //! Set the transform depth used for picture (de)coding and allocate for the code blocks array
1207         void SetTransformDepth(unsigned int wd);
1208 
1209         //! Set the multiple quantisers flag usedto picture (de)coding
1210         void SetCodeBlockMode(unsigned int cb_mode);
1211 
1212         //! Set the spatial partition flag usedto picture (de)coding
SetSpatialPartition(bool spatial_partition)1213         void SetSpatialPartition(bool spatial_partition) { m_spatial_partition=spatial_partition; }
1214 
1215         //! Set the number of code blocks for a particular level
1216         void  SetCodeBlocks(unsigned int level, unsigned int hblocks, unsigned int vblocks);
1217 
1218         //! Set the video format used for picture (de)coding
SetVideoFormat(const VideoFormat vd)1219         void SetVideoFormat(const VideoFormat vd) { m_video_format=vd; }
1220 
1221    protected:
1222         //! Return the Wavelet filter associated with the wavelet index
1223         WltFilter TransformFilter (unsigned int wf_idx);
1224     private:
1225 
1226         //! The picture prediction parameters
1227 	PicturePredParams m_picpredparams;
1228 
1229         //! The picture coding mode
1230         int m_pic_coding_mode;
1231 
1232         //! True if interlaced and top field is first in temporal order
1233         bool m_topfieldfirst;
1234 
1235         //! The frame/field luma width
1236         int m_xl;
1237 
1238         //! The frame/field luma height
1239         int m_yl;
1240 
1241         //! The frame/field chroma width
1242         int m_cxl;
1243 
1244         //! The frame/field chroma height
1245         int m_cyl;
1246 
1247         //! Luma depth - number of bits required for lumz
1248         unsigned int m_luma_depth;
1249 
1250         //! chroma depth - number of bits required for luma
1251         unsigned int m_chroma_depth;
1252 
1253         //! The video format being used
1254         VideoFormat m_video_format;
1255 
1256        //! Zero transform flag
1257         bool m_zero_transform;
1258 
1259         //! The wavelet filter being used
1260         WltFilter m_wlt_filter;
1261 
1262         //! Wavelet depth
1263         unsigned int m_wlt_depth;
1264 
1265         //! Code block mode
1266         CodeBlockMode m_cb_mode;
1267 
1268         //! Spatial partitioning flag
1269         bool m_spatial_partition;
1270 
1271         //! Code block array. Number of entries is m_wlt_depth+1
1272         OneDArray<CodeBlocks> m_cb;
1273     };
1274 
1275     //! Parameters for the encoding process
1276     /*!
1277         Parameters for the encoding process, derived from CodecParams.
1278      */
1279     class EncoderParams: public CodecParams
1280     {
1281         //codec params plus parameters relating solely to the operation of the encoder
1282 
1283     public:
1284         //! Default constructor
1285         EncoderParams(const VideoFormat& video_format,
1286                       PictureType ftype = INTER_PICTURE,
1287                       unsigned int num_refs = 2,
1288                       bool set_defaults=true);
1289 
1290             ////////////////////////////////////////////////////////////////////
1291             //NB: Assume default copy constructor, assignment = and destructor//
1292             //This means pointers are copied, not the objects they point to.////
1293             ////////////////////////////////////////////////////////////////////
1294 
1295          // Gets ...
1296 
1297 
1298         //! Returns true if we're operating verbosely, false otherwise
Verbose()1299         bool Verbose() const {return m_verbose;}
1300 
1301         //! Returns a flag indicating that we're doing local decoding
LocalDecode()1302         bool LocalDecode() const {return m_loc_decode;}
1303 
1304         //! Get whether we're doing lossless coding
Lossless()1305         bool Lossless() const {return m_lossless;}
1306 
1307         //! Get whether we're doing full-search motion estimation
FullSearch()1308         bool FullSearch() const {return m_full_search; }
1309 
1310         //! Get the horizontal search range for full-search motion estimation
XRangeME()1311         int XRangeME() const {return m_x_range_me;}
1312 
1313         //! Get the vertical search range for full-search motion estimation
YRangeME()1314         int YRangeME() const {return m_y_range_me;}
1315 
1316         //! Get whether we're doing combined component motion estimation
CombinedME()1317         bool CombinedME() const {return m_combined_me; }
1318 
1319         //! Get the quality factor
Qf()1320         float Qf() const {return m_qf;}
1321 
1322         //! Return the nominal number of L1 pictures before the next I picture
1323         /*!
1324             Return the nominal number of L1 pictures before the next I picture. Can be
1325             overridden by I-picture insertion
1326 
1327         */
NumL1()1328         int NumL1() const {return m_num_L1;}
1329 
1330         //! Return the separation between L1 pictures (and between L1 and I pictures)
L1Sep()1331         int L1Sep() const {return m_L1_sep;}
1332 
1333         //! Return the amount we're weighting noise in the U component
UFactor()1334         float UFactor() const {return m_ufactor;}
1335 
1336         //! Return the amount we're weighting noise in the V component
VFactor()1337         float VFactor() const {return m_vfactor;}
1338 
1339         //! Return the number of cycles per degree at the nominal viewing distance for the raster
CPD()1340         float CPD() const {return m_cpd;}
1341 
1342         //! Return what prefiltering is in place
Prefilter()1343         PrefilterType Prefilter() const {return m_prefilter;}
1344 
1345         //! Return the prefiltering strength
PrefilterStrength()1346         int PrefilterStrength() const {return m_prefilter_strength;}
1347 
1348         //! Return the Lagrangian parameter to be used for I pictures
ILambda()1349         float ILambda() const {return m_I_lambda;}
1350 
1351         //! Return the Lagrangian parameter to be used for L1 pictures
L1Lambda()1352         float L1Lambda() const {return m_L1_lambda;}
1353 
1354         //! Return the Lagrangian parameter to be used for L2 pictures
L2Lambda()1355         float L2Lambda() const {return m_L2_lambda;}
1356 
1357         //! Return the Lagrangian ME parameter to be used for L1 pictures
L1MELambda()1358         float L1MELambda() const {return m_L1_me_lambda;}
1359 
1360         //! Return the Lagrangian ME parameter to be used for L2 pictures
L2MELambda()1361         float L2MELambda() const {return m_L2_me_lambda;}
1362 
1363         //! Return the size of the GOP
1364         int GOPLength() const;
1365 
1366         //! Return the output path to be used for storing diagnositic data
OutputPath()1367         char * OutputPath() const {return ( char* ) m_output_path.c_str();}
1368 
1369         //! Return a reference to the entropy factors
EntropyFactors()1370         const EntropyCorrector& EntropyFactors() const {return *m_ent_correct;}
1371 
1372         //! Return a reference to the entropy factors - we need to be able to change the values of the entropy factors in situ
EntropyFactors()1373         EntropyCorrector& EntropyFactors() {return *m_ent_correct;}
1374 
1375         //! Return the Wavelet filter to be used for intra pictures
IntraTransformFilter()1376         WltFilter IntraTransformFilter() { return m_intra_wltfilter; }
1377 
1378         //! Return the Wavelet filter to be used for Inter pictures
InterTransformFilter()1379         WltFilter InterTransformFilter() { return m_inter_wltfilter; }
1380 
1381         //! Return the Target Bit Rate in kbps
TargetRate()1382         int TargetRate() {return m_target_rate;}
1383 
1384         //! Return true if using Arithmetic coding
UsingAC()1385         bool UsingAC()  const {return m_using_ac;}
1386 
1387         // ... and Sets
1388 
1389         //! Sets verbosity on or off
SetVerbose(bool v)1390         void SetVerbose(bool v){m_verbose=v;}
1391 
1392         //! Sets a flag indicating that we're producing a locally decoded o/p
SetLocalDecode(const bool decode)1393         void SetLocalDecode( const bool decode ){m_loc_decode=decode;}
1394 
1395         //! Set whether we're doing lossless coding
SetLossless(const bool l)1396         void SetLossless(const bool l){m_lossless = l;}
1397 
1398         //! Set whether we're doing full-search motion estimation
SetFullSearch(const bool fs)1399         void SetFullSearch(const bool fs){m_full_search = fs;}
1400 
1401         //! Set whether we're doing combined component motion estimation
SetCombinedME(const bool cme)1402         void SetCombinedME(const bool cme){m_combined_me = cme;}
1403 
1404         //! Set the horizontal search range for full-search motion estimation
SetXRangeME(const int xr)1405         void SetXRangeME(const int xr){m_x_range_me = xr;}
1406 
1407         //! Set the vertical search range for full-search motion estimation
SetYRangeME(const int yr)1408         void SetYRangeME(const int yr){m_y_range_me = yr;}
1409 
1410         //! Set the quality factor
SetQf(const float qfac)1411         void SetQf(const float qfac){ m_qf=qfac; CalcLambdas(m_qf); }
1412 
1413         //! Set the nominal number of L1 pictures between I pictures
SetNumL1(const int nl)1414         void SetNumL1(const int nl){m_num_L1=nl;}
1415 
1416         //! Set the separation between L1 pictures
SetL1Sep(const int lsep)1417         void SetL1Sep(const int lsep){m_L1_sep=lsep;}
1418 
1419         //! Set the amount to weight noise in the U component
SetUFactor(const float uf)1420         void SetUFactor(const float uf){m_ufactor=uf;}
1421 
1422         //! Set the amount to weight noise in the V component
SetVFactor(const float vf)1423         void SetVFactor(const float vf){m_vfactor=vf;}
1424 
1425         //! Set the number of cycles per degree at the nominal viewing distance
SetCPD(const float cpd)1426         void SetCPD(const float cpd){m_cpd=cpd;}
1427 
1428         //! Set denoising value - true or false
SetPrefilter(const PrefilterType pf,const int str)1429         void SetPrefilter(const PrefilterType pf, const int str){m_prefilter=pf;
1430                                                                  m_prefilter_strength=str;}
1431 
1432         //! Set the output path to be used for diagnostic data
SetOutputPath(const char * op)1433         void SetOutputPath(const char * op){ m_output_path = op; }
1434 
1435         //! Sets the entropy factors - TBD: set this up in a constructor and pass encoder params around entirely by reference
SetEntropyFactors(EntropyCorrector * entcorrect)1436         void SetEntropyFactors(EntropyCorrector* entcorrect){m_ent_correct=entcorrect;}
1437         //! Set the Wavelet filter to be used for intra pictures
1438         void SetIntraTransformFilter(unsigned int wf_idx);
1439 
1440         //! Set the Wavelet filter to be used for inter pictures
1441         void SetInterTransformFilter(unsigned int wf_idx);
1442 
1443         //! Set the Wavelet filter to be used for intra pictures
SetIntraTransformFilter(WltFilter wf)1444         void SetIntraTransformFilter(WltFilter wf) { m_intra_wltfilter = wf; }
1445 
1446          //! Set the number of code blocks for all levels
1447         void  SetUsualCodeBlocks(const PictureType& ftype);
1448 
1449         //! Set the Wavelet filter to be used for inter pictures
SetInterTransformFilter(WltFilter wf)1450         void SetInterTransformFilter(WltFilter wf) { m_inter_wltfilter = wf; }
1451 
1452         //! Set the target bit rate
SetTargetRate(const int rate)1453         void SetTargetRate(const int rate){m_target_rate = rate;}
1454 
1455         //! Set the arithmetic coding flag
SetUsingAC(bool using_ac)1456         void SetUsingAC(bool using_ac) {m_using_ac = using_ac;}
1457     private:
1458 
1459         //! Calculate the Lagrangian parameters from the quality factor
1460         void CalcLambdas(const float qf);
1461 
1462     private:
1463 
1464         //! Code/decode with commentary if true
1465         bool m_verbose;
1466 
1467         //! Flag indicating we're doing local decoding
1468         bool m_loc_decode;
1469 
1470         //! A flag indicating we're doing lossless coding
1471         bool m_lossless;
1472 
1473         //! A flag indicating whether we're doing full-search block matching
1474         bool m_full_search;
1475 
1476         //! A flag indicating whether we're doing combined component motion estimation
1477         bool m_combined_me;
1478 
1479         //! The horizontal range for full-search block matching
1480         int m_x_range_me;
1481 
1482         //! The vertical range for full-search block matching
1483         int m_y_range_me;
1484 
1485         //! Quality factor
1486         float m_qf;
1487 
1488         //! Number of L1 pictures before next I picture
1489         int m_num_L1;
1490 
1491         //! Separation between L1 pictures
1492         int m_L1_sep;
1493 
1494         //! factor for weighting U component quantisation errors
1495         float m_ufactor;
1496 
1497         //! factor for weighting V component quantisation errors
1498         float m_vfactor;
1499 
1500         //! Cycles per degree assumed for viewing the video
1501         float m_cpd;
1502 
1503         //! Indicator for prefiltering
1504         PrefilterType m_prefilter;
1505 
1506         //! Prefiltering strength
1507         int m_prefilter_strength;
1508 
1509         //! Lagrangian parameter for Intra picture coding
1510         float m_I_lambda;
1511 
1512         //! Lagrangian parameter for L1 picture coding
1513         float m_L1_lambda;
1514 
1515         //! Lagrangian parameter for L2 picture coding
1516         float m_L2_lambda;
1517 
1518         //! Lagrangian param for L1 motion estimation
1519         float m_L1_me_lambda;
1520 
1521         //! Lagrangian param for L2 motion estimation
1522         float m_L2_me_lambda;
1523 
1524         //! Correction factors for quantiser selection
1525         EntropyCorrector* m_ent_correct;
1526 
1527         //! Output file path
1528         std::string m_output_path;
1529 
1530         //! Wavelet filter for Intra pictures
1531         WltFilter m_intra_wltfilter;
1532 
1533         //! Wavelet filter for Inter pictures
1534         WltFilter m_inter_wltfilter;
1535 
1536         //! Target bit rate
1537         int m_target_rate;
1538 
1539         //! Arithmetic coding flag
1540         bool m_using_ac;
1541 
1542     };
1543 
1544     //! Parameters for the decoding process
1545     /*!
1546         Parameters for the decoding process. Derived from CodecParams.
1547      */
1548     class DecoderParams: public CodecParams
1549     {
1550     public:
1551             //! Default constructor
1552         DecoderParams(const VideoFormat& video_format = VIDEO_FORMAT_CIF, PictureType ftype=INTRA_PICTURE, unsigned int num_refs = 0, bool set_defaults = false);
1553 
1554         //! Returns true if we're operating verbosely, false otherwise
Verbose()1555         bool Verbose() const {return m_verbose;}
1556 
1557         //! Sets verbosity on or off
SetVerbose(bool v)1558         void SetVerbose(bool v){m_verbose=v;}
1559 
1560             ////////////////////////////////////////////////////////////////////
1561             //NB: Assume default copy constructor, assignment = and destructor//
1562             //This means pointers are copied, not the objects they point to.////
1563             ////////////////////////////////////////////////////////////////////
1564 
1565 
1566     private:
1567 
1568         //! Code/decode with commentary if true
1569         bool m_verbose;
1570 
1571     };
1572 
1573     //! A simple bounds checking function, very useful in a number of places
BChk(const ValueType & num,const ValueType & max)1574     inline ValueType BChk(const ValueType &num, const ValueType &max)
1575     {
1576         if(num < 0) return 0;
1577         else if(num >= max) return max-1;
1578         else return num;
1579     }
1580 
1581     //! Class for encapsulating quantiser data
1582     class QuantiserLists
1583     {
1584     public:
1585         //! Default constructor
1586         QuantiserLists();
1587 
1588         //! Returns 4 times the quantisation factor
QuantFactor4(const int index)1589         inline int QuantFactor4( const int index ) const {return m_qflist4[index]; }
1590 
1591         //! Returns the intra Picture quantisation offset for non-zero values
IntraQuantOffset4(const int index)1592         inline int IntraQuantOffset4( const int index ) const {return m_intra_offset4[index]; }
1593         //! Returns the inter Picture quantisation offset for non-zero values
InterQuantOffset4(const int index)1594         inline int InterQuantOffset4( const int index ) const {return m_inter_offset4[index]; }
1595 
1596         //! Returns the maximum quantiser index supported
MaxQuantIndex()1597         inline int MaxQuantIndex() const {return m_max_qindex; }
1598 
1599 
1600     private:
1601         unsigned int m_max_qindex;
1602         OneDArray<int> m_qflist4;
1603         OneDArray<int> m_intra_offset4;
1604         OneDArray<int> m_inter_offset4;
1605 
1606     };
1607 
1608     //! A constant list of the quantisers being used in Dirac
1609     static const QuantiserLists dirac_quantiser_lists;
1610 
1611 } // namespace dirac
1612 
1613 #endif
1614