Updating Contact email
[BrunelResearch-dirac.git] / libdirac_common / common.cpp
blob85e36d635734890b10db1a20888b87024e9e8c2e
1 /* ***** BEGIN LICENSE BLOCK *****
3 * $Id$ $Name$
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
19 * Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2004.
21 * All Rights Reserved.
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)
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 ***** */
43 #include <algorithm>
44 #include <sstream>
45 #ifndef _MSC_VER
46 #include <inttypes.h>
47 #endif
48 #include <libdirac_common/common.h>
49 #include <libdirac_common/video_format_defaults.h>
50 #include <libdirac_common/dirac_exception.h>
51 using namespace dirac;
54 //const dirac::QuantiserLists dirac::dirac_quantiser_lists;
57 //EntropyCorrector functions
59 EntropyCorrector::EntropyCorrector(int depth):
60 m_Yfctrs( 3 , 3*depth+1 ),
61 m_Ufctrs( 3 , 3*depth+1 ),
62 m_Vfctrs( 3 , 3*depth+1 )
64 Init();
67 float EntropyCorrector::Factor(const int bandnum , const PictureParams& pp ,
68 const CompSort c) const
70 int idx = pp.PicSort().IsIntra() ? 0 : (pp.IsBPicture() ? 1 : 2);
71 if (c == U_COMP)
72 return m_Ufctrs[idx][bandnum-1];
73 else if (c == V_COMP)
74 return m_Vfctrs[idx][bandnum-1];
75 else
76 return m_Yfctrs[idx][bandnum-1];
79 void EntropyCorrector::Init()
82 //do I-pictures
83 for (int i=0 ; i<m_Yfctrs.LengthX() ; ++i )
85 if ( i == m_Yfctrs.LastX() )
87 // Set factor for Intra pictures
88 m_Yfctrs[0][i] = 1.0f;
89 m_Ufctrs[0][i] = 1.0f;
90 m_Vfctrs[0][i] = 1.0f;
91 // Set factor for Inter Ref pictures
92 m_Yfctrs[1][i] = 0.85f;
93 m_Ufctrs[1][i] = 0.85f;
94 m_Vfctrs[1][i] = 0.85f;
95 // Set factor for Inter Non-Ref pictures
96 m_Yfctrs[2][i] = 0.85f;
97 m_Ufctrs[2][i] = 0.85f;
98 m_Vfctrs[2][i] = 0.85f;
100 else if ( i >= m_Yfctrs.LastX()-3 )
102 // Set factor for Intra pictures
103 m_Yfctrs[0][i] = 0.85f;
104 m_Ufctrs[0][i] = 0.85f;
105 m_Vfctrs[0][i] = 0.85f;
106 // Set factor for Inter Ref pictures
107 m_Yfctrs[1][i] = 0.75f;
108 m_Ufctrs[1][i] = 0.75f;
109 m_Vfctrs[1][i] = 0.75f;
110 // Set factor for Inter Non-Ref pictures
111 m_Yfctrs[2][i] = 0.75f;
112 m_Ufctrs[2][i] = 0.75f;
113 m_Vfctrs[2][i] = 0.75f;
115 else
117 // Set factor for Intra pictures
118 m_Yfctrs[0][i] = 0.75f;
119 m_Ufctrs[0][i] = 0.75f;
120 m_Vfctrs[0][i] = 0.75f;
121 // Set factor for Inter Ref pictures
122 m_Yfctrs[1][i] = 0.75f;
123 m_Ufctrs[1][i] = 0.75f;
124 m_Vfctrs[1][i] = 0.75f;
125 // Set factor for Inter Non-Ref pictures
126 m_Yfctrs[2][i] = 0.75f;
127 m_Ufctrs[2][i] = 0.75f;
128 m_Vfctrs[2][i] = 0.75f;
130 }//i
134 void EntropyCorrector::Update(int bandnum , const PictureParams& pp ,
135 CompSort c ,int est_bits , int actual_bits){
136 //updates the factors - note that the estimated bits are assumed to already include the correction factor
138 float multiplier;
139 if (actual_bits != 0 && est_bits != 0)
140 multiplier = float(actual_bits)/float(est_bits);
141 else
142 multiplier=1.0;
144 int idx = pp.PicSort().IsIntra() ? 0 : (pp.IsBPicture() ? 1 : 2);
145 if (c == U_COMP)
146 m_Ufctrs[idx][bandnum-1] *= multiplier;
147 else if (c == V_COMP)
148 m_Vfctrs[idx][bandnum-1] *= multiplier;
149 else
150 m_Yfctrs[idx][bandnum-1] *= multiplier;
153 // Overlapped block parameter functions
155 OLBParams::OLBParams(const int xblen, int const yblen, int const xbsep, int const ybsep):
156 m_xblen(xblen),
157 m_yblen(yblen),
158 m_xbsep(xbsep),
159 m_ybsep(ybsep),
160 m_xoffset( (xblen-xbsep)/2 ),
161 m_yoffset( (yblen-ybsep)/2 )
164 bool OLBParams::operator ==(const OLBParams bparams) const
166 if (bparams.Xblen() != m_xblen ||
167 bparams.Yblen() != m_yblen ||
168 bparams.Xbsep() != m_xbsep ||
169 bparams.Ybsep() != m_ybsep)
171 return false;
173 return true;
176 namespace dirac
178 std::ostream & operator<< (std::ostream & stream, OLBParams & params)
180 stream << params.Ybsep() << " " << params.Xbsep();
181 // stream << " " <<params.Yblen() << " " << params.Xblen();
183 return stream;
186 std::istream & operator>> (std::istream & stream, OLBParams & params)
188 int temp;
190 stream >> temp;
191 params.SetYbsep(temp);
193 stream >> temp;
194 params.SetXbsep(temp);
196 // stream >> temp;
197 // params.SetYblen(temp);
199 // stream >> temp;
200 // params.SetXblen(temp);
202 return stream;
207 void PicturePredParams::SetBlockSizes(const OLBParams& olbparams , const ChromaFormat cformat)
209 //given the raw overlapped block parameters, set the modified internal parameters to
210 //take account of the chroma sampling format and overlapping requirements, as well
211 //as the equivalent parameters for sub-SBs and SBs.
212 //Does NOT set the number of blocks or superblocks, as padding may be required.
214 OLBParams tmp_olbparams = olbparams;
215 // Factors for scaling chroma blocks
216 int xcfactor,ycfactor;
218 if (cformat == format420)
220 xcfactor = 2;
221 ycfactor = 2;
223 else if (cformat == format422)
225 xcfactor = 2;
226 ycfactor = 1;
228 else
229 {// assume 444
230 xcfactor = 1;
231 ycfactor = 1;
235 m_lbparams[2] = tmp_olbparams;
237 // Check separations are all divisible by 4
238 int remainder= m_lbparams[2].Xbsep()%4;
239 if ( remainder!=0 || m_lbparams[2].Xbsep()==0 )
241 m_lbparams[2].SetXbsep( m_lbparams[2].Xbsep()+(4-remainder));
242 m_lbparams[2].SetXblen( m_lbparams[2].Xbsep()+4 );
244 remainder= m_lbparams[2].Ybsep()%4;
245 if ( remainder!=0 || m_lbparams[2].Ybsep()==0 )
247 m_lbparams[2].SetYbsep( m_lbparams[2].Ybsep()+(4-remainder));
248 m_lbparams[2].SetYblen( m_lbparams[2].Ybsep()+4 );
252 // Now check lengths are divisible by 4
253 remainder= m_lbparams[2].Xblen()%4;
254 if ( remainder!=0 )
256 m_lbparams[2].SetXblen( m_lbparams[2].Xbsep()+4);
258 remainder= m_lbparams[2].Yblen()%4;
259 if ( remainder!=0 )
261 m_lbparams[2].SetYblen( m_lbparams[2].Ybsep()+4);
264 // Check there's non-negative overlap,
265 // XBLEN >= XBSEP, YBLEN >= YBSEP
266 if (m_lbparams[2].Xbsep()>m_lbparams[2].Xblen())
268 m_lbparams[2].SetXblen( m_lbparams[2].Xbsep()+4);
270 if (m_lbparams[2].Ybsep()>m_lbparams[2].Yblen())
272 m_lbparams[2].SetYblen( m_lbparams[2].Ybsep()+4);
275 // Check the lengths aren't too big (100% is max roll-off)
276 // XBLEN <= 2*XBSEP, YBLEN <= 2*YBSEP
277 if (2*m_lbparams[2].Xbsep()<m_lbparams[2].Xblen())
279 m_lbparams[2].SetXblen( m_lbparams[2].Xbsep()+4);
281 if (2*m_lbparams[2].Ybsep()<m_lbparams[2].Yblen())
283 m_lbparams[2].SetYblen( m_lbparams[2].Ybsep()+4);
286 // Set the chroma values
287 m_cbparams[2].SetXbsep( m_lbparams[2].Xbsep()/xcfactor );
288 m_cbparams[2].SetXblen( m_lbparams[2].Xblen()/xcfactor );
289 m_cbparams[2].SetYbsep( m_lbparams[2].Ybsep()/ycfactor );
290 m_cbparams[2].SetYblen( m_lbparams[2].Yblen()/ycfactor );
293 //Now work out the overlaps for splitting levels 1 and 0
294 m_lbparams[1].SetXbsep( m_lbparams[2].Xbsep()*2 );
295 m_lbparams[1].SetXblen( m_lbparams[2].Xblen() + m_lbparams[2].Xbsep() );
296 m_lbparams[1].SetYbsep( m_lbparams[2].Ybsep()*2 );
297 m_lbparams[1].SetYblen( m_lbparams[2].Yblen() + m_lbparams[2].Xbsep() );
299 m_lbparams[0].SetXbsep( m_lbparams[1].Xbsep()*2 );
300 m_lbparams[0].SetXblen( m_lbparams[1].Xblen() + m_lbparams[1].Xbsep() );
301 m_lbparams[0].SetYbsep( m_lbparams[1].Ybsep()*2 );
302 m_lbparams[0].SetYblen( m_lbparams[1].Yblen() + m_lbparams[1].Xbsep() );
304 m_cbparams[1].SetXbsep( m_cbparams[2].Xbsep()*2 );
305 m_cbparams[1].SetXblen( m_cbparams[2].Xblen() + m_cbparams[2].Xbsep() );
306 m_cbparams[1].SetYbsep( m_cbparams[2].Ybsep()*2 );
307 m_cbparams[1].SetYblen( m_cbparams[2].Yblen() + m_cbparams[2].Xbsep() );
309 m_cbparams[0].SetXbsep( m_cbparams[1].Xbsep()*2 );
310 m_cbparams[0].SetXblen( m_cbparams[1].Xblen() + m_cbparams[1].Xbsep() );
311 m_cbparams[0].SetYbsep( m_cbparams[1].Ybsep()*2 );
312 m_cbparams[0].SetYblen( m_cbparams[1].Yblen() + m_cbparams[1].Xbsep() );
314 if ( m_lbparams[2].Xbsep()!=olbparams.Xbsep() ||
315 m_lbparams[2].Ybsep()!=olbparams.Ybsep() ||
316 m_lbparams[2].Xblen()!=olbparams.Xblen() ||
317 m_lbparams[2].Yblen()!=olbparams.Yblen() )
319 std::cout<<std::endl<<"WARNING: block parameters are inconsistent with ";
320 std::cout<<"specification requirements, which are:";
321 std::cout<<std::endl<<"\t 1. Lengths and separations must be positive multiples of 4";
322 std::cout<<std::endl<<"\t 2. Length can't be more than twice separations";
323 std::cout<<std::endl<<"\t 3. Lengths must be greater than or equal to separations";
324 std::cout<<std::endl<<std::endl<<"Instead, using:";
325 std::cout<<" xblen="<<m_lbparams[2].Xblen();
326 std::cout<<" yblen="<<m_lbparams[2].Yblen();
327 std::cout<<" xbsep="<<m_lbparams[2].Xbsep();
328 std::cout<<" ybsep="<<m_lbparams[2].Ybsep() << std::endl;
333 // Codec params functions
335 CodecParams::CodecParams(const VideoFormat &vd, PictureType ftype, unsigned int num_refs, bool set_defaults):
336 m_video_format(vd)
338 if (set_defaults)
339 SetDefaultCodecParameters(*this, ftype, num_refs);
342 WltFilter CodecParams::TransformFilter (unsigned int wf_idx)
344 if (wf_idx >= filterNK)
345 DIRAC_THROW_EXCEPTION(
346 ERR_UNSUPPORTED_STREAM_DATA,
347 "Wavelet filter idx out of range [0-7]",
348 SEVERITY_PICTURE_ERROR);
350 if (wf_idx==FIDELITY)
352 std::ostringstream errstr;
353 errstr << "Wavelet Filter " << wf_idx << " currently not supported";
354 DIRAC_THROW_EXCEPTION(
355 ERR_UNSUPPORTED_STREAM_DATA,
356 errstr.str(),
357 SEVERITY_PICTURE_ERROR);
359 return static_cast<WltFilter>(wf_idx);
362 void CodecParams::SetTransformFilter(unsigned int wf_idx)
364 SetTransformFilter(TransformFilter(wf_idx));
367 void CodecParams::SetTransformDepth (unsigned int wd)
369 m_wlt_depth = wd;
370 // Resize the code block size array.
371 m_cb.Resize(wd+1);
374 void CodecParams::SetCodeBlocks (unsigned int level,
375 unsigned int hblocks,
376 unsigned int vblocks)
378 if (level > m_wlt_depth)
380 std::ostringstream errstr;
381 errstr << "level " << level << " out of range [0-" << m_wlt_depth << "]";
382 DIRAC_THROW_EXCEPTION(
383 ERR_UNSUPPORTED_STREAM_DATA,
384 errstr.str(),
385 SEVERITY_PICTURE_ERROR);
388 m_cb[level].SetHorizontalCodeBlocks(hblocks);
389 m_cb[level].SetVerticalCodeBlocks(vblocks);
392 const CodeBlocks &CodecParams::GetCodeBlocks (unsigned int level) const
394 if (level > m_wlt_depth)
396 std::ostringstream errstr;
397 errstr << "level " << level << " out of range [0-" << m_wlt_depth << "]";
398 DIRAC_THROW_EXCEPTION(
399 ERR_UNSUPPORTED_STREAM_DATA,
400 errstr.str(),
401 SEVERITY_PICTURE_ERROR);
404 return m_cb[level];
407 void CodecParams::SetCodeBlockMode (unsigned int cb_mode)
409 if (cb_mode >= QUANT_UNDEF)
411 std::ostringstream errstr;
412 errstr << "Code Block mode " << cb_mode << " out of supported range [0-" << QUANT_MULTIPLE << "]";
413 DIRAC_THROW_EXCEPTION(
414 ERR_UNSUPPORTED_STREAM_DATA,
415 errstr.str(),
416 SEVERITY_PICTURE_ERROR);
419 m_cb_mode = static_cast<CodeBlockMode>(cb_mode);
422 //EncoderParams functions
424 //Default constructor
425 EncoderParams::EncoderParams(const VideoFormat& video_format,
426 PictureType ftype,
427 unsigned int num_refs,
428 bool set_defaults):
429 CodecParams(video_format, ftype, num_refs, set_defaults),
430 m_verbose(false),
431 m_loc_decode(true),
432 m_full_search(false),
433 m_x_range_me(32),
434 m_y_range_me(32),
435 m_ufactor(1.0),
436 m_vfactor(1.0),
437 m_prefilter(NO_PF),
438 m_prefilter_strength(0),
439 m_I_lambda(0.0f),
440 m_L1_lambda(0.f),
441 m_L2_lambda(0.0f),
442 m_L1_me_lambda(0.0f),
443 m_L2_me_lambda(0.0f),
444 m_ent_correct(0),
445 m_target_rate(0)
447 if(set_defaults)
448 SetDefaultEncoderParameters(*this);
451 void EncoderParams::CalcLambdas(const float qf)
453 if (!m_lossless )
455 m_I_lambda = std::pow( 10.0 , (12.0-qf )/2.5 )/16.0;
457 m_L1_lambda = m_I_lambda*4.0;
458 m_L2_lambda = m_I_lambda*32.0;
460 // Set the lambdas for motion estimation
461 const double me_ratio = 2.0;
463 // Use the same ME lambda for L1 and L2 pictures
464 m_L1_me_lambda = std::sqrt(m_L1_lambda)*me_ratio;
465 m_L2_me_lambda = m_L1_me_lambda;
467 else
469 m_I_lambda = 0.0;
470 m_L1_lambda = 0.0;
471 m_L2_lambda = 0.0;
473 m_L1_me_lambda = 0.0;
474 m_L2_me_lambda = 0.0;
478 void EncoderParams::SetIntraTransformFilter(unsigned int wf_idx)
480 SetIntraTransformFilter(TransformFilter(wf_idx));
483 void EncoderParams::SetInterTransformFilter(unsigned int wf_idx)
485 SetInterTransformFilter(TransformFilter(wf_idx));
488 void EncoderParams::SetUsualCodeBlocks ( const PictureType &ftype)
490 // No subband splitting if spatial partitioning if false
491 // Since this function is common to encoder and decoder we allow the
492 // setting of code blocks without checking if DefaultSpatialPartition is
493 // true.
494 if (SpatialPartition() == false)
495 return;
497 SetCodeBlocks(0, 1, 1);
498 if (TransformDepth() == 0)
499 return;
501 switch (GetVideoFormat())
503 case VIDEO_FORMAT_QSIF525:
504 case VIDEO_FORMAT_QCIF:
505 case VIDEO_FORMAT_CUSTOM:
506 case VIDEO_FORMAT_SIF525:
507 case VIDEO_FORMAT_CIF:
508 case VIDEO_FORMAT_4CIF:
509 case VIDEO_FORMAT_4SIF525:
510 case VIDEO_FORMAT_SD_480I60:
511 case VIDEO_FORMAT_SD_576I50:
512 case VIDEO_FORMAT_HD_720P60:
513 case VIDEO_FORMAT_HD_720P50:
514 case VIDEO_FORMAT_HD_1080I60:
515 case VIDEO_FORMAT_HD_1080I50:
516 case VIDEO_FORMAT_HD_1080P60:
517 case VIDEO_FORMAT_HD_1080P50:
518 case VIDEO_FORMAT_UHDTV_4K60:
519 case VIDEO_FORMAT_UHDTV_4K50:
520 case VIDEO_FORMAT_UHDTV_8K60:
521 case VIDEO_FORMAT_UHDTV_8K50:
522 case VIDEO_FORMAT_DIGI_CINEMA_2K24:
523 case VIDEO_FORMAT_DIGI_CINEMA_4K24:
524 if (ftype == INTRA_PICTURE){
525 int depth = TransformDepth();
526 for (int i=depth; i>=std::max(1,depth-3); --i)
527 SetCodeBlocks(i, Xl()/(24*2^(depth-i)), Yl()/(24*2^(depth-i)));
528 for (int i = 0; i<std::max(1,depth-3); ++i)
529 SetCodeBlocks(i, 1, 1);
531 else{
532 int depth = TransformDepth();
533 for (int i=depth; i>=std::max(1,depth-3); --i)
534 SetCodeBlocks(i, Xl()/(24*2^(depth-i)), Yl()/(24*2^(depth-i)));
535 for (int i = 0; i<std::max(1,depth-3); ++i)
536 SetCodeBlocks(i, 1, 1);
538 break;
540 default:
541 DIRAC_THROW_EXCEPTION(
542 ERR_INVALID_VIDEO_FORMAT,
543 "Unsupported video format",
544 SEVERITY_PICTURE_ERROR);
545 break;
551 int EncoderParams::GOPLength() const
553 if (m_num_L1>0)
554 return (m_num_L1+1)*m_L1_sep;
556 return ((m_num_L1==0) ? 10 : 0);
559 DecoderParams::DecoderParams(const VideoFormat& video_format,
560 PictureType ftype,
561 unsigned int num_refs,
562 bool set_defaults):
563 CodecParams(video_format, ftype, num_refs, set_defaults),
564 m_verbose(false)
568 // ParseParams functions
569 // constructor
570 ParseParams::ParseParams():
571 m_major_ver(2),
572 m_minor_ver(2),
573 m_profile(8),
574 m_level(0)
578 //Source functions
579 //constructor
580 SourceParams::SourceParams(const VideoFormat& video_format,
581 bool set_defaults)
583 // set default parameters
584 if(set_defaults)
585 SetDefaultSourceParameters(video_format, *this);
588 int SourceParams::ChromaWidth() const
590 switch (m_cformat)
592 case format420:
593 case format422:
594 return m_xl/2;
596 case format444:
597 default:
598 return m_xl;
602 int SourceParams::ChromaHeight() const
604 switch (m_cformat)
606 case format420:
607 return m_yl/2;
609 case format422:
610 case format444:
611 default:
612 return m_yl;
617 void SourceParams::SetFrameRate (FrameRateType fr)
619 m_fr_idx = fr;
620 switch (fr)
622 case FRAMERATE_23p97_FPS:
623 m_framerate.m_num = 24000;
624 m_framerate.m_denom = 1001;
625 break;
626 case FRAMERATE_24_FPS:
627 m_framerate.m_num = 24;
628 m_framerate.m_denom = 1;
629 break;
630 case FRAMERATE_25_FPS:
631 m_framerate.m_num = 25;
632 m_framerate.m_denom = 1;
633 break;
634 case FRAMERATE_29p97_FPS:
635 m_framerate.m_num = 30000;
636 m_framerate.m_denom = 1001;
637 break;
638 case FRAMERATE_30_FPS:
639 m_framerate.m_num = 30;
640 m_framerate.m_denom = 1;
641 break;
642 case FRAMERATE_50_FPS:
643 m_framerate.m_num = 50;
644 m_framerate.m_denom = 1;
645 break;
646 case FRAMERATE_59p94_FPS:
647 m_framerate.m_num = 60000;
648 m_framerate.m_denom = 1001;
649 break;
650 case FRAMERATE_60_FPS:
651 m_framerate.m_num = 60;
652 m_framerate.m_denom = 1;
653 break;
654 case FRAMERATE_14p98_FPS:
655 m_framerate.m_num = 15000;
656 m_framerate.m_denom = 1001;
657 break;
658 case FRAMERATE_12p5_FPS:
659 m_framerate.m_num = 25;
660 m_framerate.m_denom = 2;
661 break;
662 default:
663 m_fr_idx = FRAMERATE_CUSTOM;
664 m_framerate.m_num = m_framerate.m_denom = 0;
665 break;
669 void SourceParams::SetPixelAspectRatio (PixelAspectRatioType pix_asr_idx)
671 m_pix_asr_idx = pix_asr_idx;
673 switch (pix_asr_idx)
675 case PIXEL_ASPECT_RATIO_1_1:
676 m_pixel_aspect_ratio.m_num = m_pixel_aspect_ratio.m_denom = 1;
677 break;
678 case PIXEL_ASPECT_RATIO_10_11:
679 m_pixel_aspect_ratio.m_num = 10;
680 m_pixel_aspect_ratio.m_denom = 11;
681 break;
682 case PIXEL_ASPECT_RATIO_12_11:
683 m_pixel_aspect_ratio.m_num = 12;
684 m_pixel_aspect_ratio.m_denom = 11;
685 break;
686 case PIXEL_ASPECT_RATIO_40_33:
687 m_pixel_aspect_ratio.m_num = 40;
688 m_pixel_aspect_ratio.m_denom = 33;
689 break;
690 case PIXEL_ASPECT_RATIO_16_11:
691 m_pixel_aspect_ratio.m_num = 16;
692 m_pixel_aspect_ratio.m_denom = 11;
693 break;
694 case PIXEL_ASPECT_RATIO_4_3:
695 m_pixel_aspect_ratio.m_num = 4;
696 m_pixel_aspect_ratio.m_denom = 3;
697 break;
698 default:
699 m_pix_asr_idx = PIXEL_ASPECT_RATIO_CUSTOM;
700 m_pixel_aspect_ratio.m_num = m_pixel_aspect_ratio.m_denom = 0;
701 break;
705 void SourceParams::SetSignalRange (SignalRangeType sr)
707 m_sr_idx = sr;
708 switch (sr)
710 case SIGNAL_RANGE_8BIT_FULL:
711 m_luma_offset = 0;
712 m_luma_excursion = 255;
713 m_chroma_offset = 128;
714 m_chroma_excursion = 255;
715 break;
716 case SIGNAL_RANGE_8BIT_VIDEO:
717 m_luma_offset = 16;
718 m_luma_excursion = 219;
719 m_chroma_offset = 128;
720 m_chroma_excursion = 224;
721 break;
722 case SIGNAL_RANGE_10BIT_VIDEO:
723 m_luma_offset = 64;
724 m_luma_excursion = 876;
725 m_chroma_offset = 512;
726 m_chroma_excursion = 896;
727 break;
728 case SIGNAL_RANGE_12BIT_VIDEO:
729 m_luma_offset = 256;
730 m_luma_excursion = 3504;
731 m_chroma_offset = 2048;
732 m_chroma_excursion = 3584;
733 break;
734 default:
735 m_sr_idx = SIGNAL_RANGE_CUSTOM;
736 m_luma_offset = 0;
737 m_luma_excursion = 0;
738 m_chroma_offset = 0;
739 m_chroma_excursion = 0;
740 break;
744 void SourceParams::SetColourSpecification (unsigned int cs_idx)
746 m_cs_idx = cs_idx;
747 switch(cs_idx)
749 case 1:
750 m_col_primary = CP_SDTV_525;
751 m_col_matrix = CM_SDTV;
752 m_transfer_func = TF_TV;
753 break;
754 case 2:
755 m_col_primary = CP_SDTV_625;
756 m_col_matrix = CM_SDTV;
757 m_transfer_func = TF_TV;
758 break;
759 case 3:
760 m_col_primary = CP_HDTV_COMP_INTERNET;
761 m_col_matrix = CM_HDTV_COMP_INTERNET;
762 m_transfer_func = TF_TV;
763 break;
764 case 4:
765 m_col_primary = CP_DCINEMA;
766 m_col_matrix = CM_HDTV_COMP_INTERNET;
767 m_transfer_func = TF_DCINEMA;
768 break;
769 default:
770 m_cs_idx = 0;
771 m_col_primary = CP_HDTV_COMP_INTERNET;
772 m_col_matrix = CM_HDTV_COMP_INTERNET;
773 m_transfer_func = TF_TV;
774 break;
778 void SourceParams::SetColourPrimariesIndex (unsigned int cp)
780 m_cs_idx = 0;
781 if (cp >= CP_UNDEF)
783 //TODO: flag a warning
785 m_col_primary = static_cast<ColourPrimaries>(cp);
788 void SourceParams::SetColourMatrixIndex (unsigned int cm)
790 m_cs_idx = 0;
791 if (cm >= CM_UNDEF)
793 //TODO: flag a warning
795 m_col_matrix = static_cast<ColourMatrix>(cm);
798 void SourceParams::SetTransferFunctionIndex (unsigned int tf)
800 m_cs_idx = 0;
801 if (tf >= TF_UNDEF)
803 //TODO: flag a warning
805 m_transfer_func = static_cast<TransferFunction>(tf);
809 //PictureParams functions
810 // Default constructor
811 PictureParams::PictureParams():
812 m_psort(PictureSort::IntraRefPictureSort()),
813 m_picture_type( INTRA_PICTURE ),
814 m_reference_type( REFERENCE_PICTURE ),
815 m_output(false),
816 m_using_ac(true)
819 // Constructor
820 PictureParams::PictureParams(const ChromaFormat& cf,
821 int xlen, int ylen,
822 unsigned int luma_depth,
823 unsigned int chroma_depth) :
824 m_cformat(cf),
825 m_psort(PictureSort::IntraRefPictureSort()),
826 m_picture_type( INTRA_PICTURE ),
827 m_reference_type( REFERENCE_PICTURE ),
828 m_output(false),
829 m_xl(xlen),
830 m_yl(ylen),
831 m_luma_depth(luma_depth),
832 m_chroma_depth(chroma_depth),
833 m_using_ac(true)
835 m_cxl = m_cyl = 0;
836 if (cf == format420)
838 m_cxl = xlen>>1;
839 m_cyl = ylen>>1;
841 else if (cf == format422)
843 m_cxl = xlen>>1;
844 m_cyl = ylen;
846 else if (cf == format444)
848 m_cxl = xlen;
849 m_cyl = ylen;
853 // Constructor
854 PictureParams::PictureParams(const ChromaFormat& cf, const PictureSort& ps):
855 m_cformat(cf),
856 m_output(false),
857 m_using_ac(true)
859 SetPicSort( ps );
862 PictureParams::PictureParams(const SourceParams& sparams):
863 m_cformat(sparams.CFormat()),
864 m_psort(PictureSort::IntraRefPictureSort()),
865 m_picture_type( INTRA_PICTURE ),
866 m_reference_type( REFERENCE_PICTURE ),
867 m_output(false),
868 m_xl(sparams.Xl()),
869 m_yl(sparams.Yl()),
870 m_cxl(sparams.ChromaWidth()),
871 m_cyl(sparams.ChromaHeight()),
872 m_using_ac(true)
874 if (sparams.SourceSampling() == 1)
876 m_yl = (m_yl>>1);
877 m_cyl = (m_cyl>>1);
879 m_luma_depth = static_cast<unsigned int>
881 std::log((double)sparams.LumaExcursion())/std::log(2.0) + 1
884 m_chroma_depth = static_cast<unsigned int>
886 std::log((double)sparams.ChromaExcursion())/std::log(2.0) + 1
892 void PictureParams::SetXl(int xlen)
894 m_xl = xlen;
895 m_cxl = 0;
896 if (m_cformat == format420 || m_cformat == format422)
898 m_cxl = m_xl>>1;
900 else if (m_cformat == format444)
902 m_cxl = m_xl;
906 void PictureParams::SetYl(int ylen)
908 m_yl = ylen;
909 m_cyl = 0;
910 if (m_cformat == format420)
912 m_cyl = m_yl>>1;
914 else if (m_cformat == format422 || m_cformat == format444)
916 m_cyl = m_yl;
920 bool PictureParams::IsBPicture() const
922 bool is_B_picture( false );
924 if ( m_refs.size() == 2 )
926 if ( m_refs[0] < m_fnum && m_refs[1] > m_fnum )
927 is_B_picture = true;
929 if ( m_refs[0] > m_fnum && m_refs[1] < m_fnum )
930 is_B_picture = true;
933 return is_B_picture;
936 void PictureParams::SetPicSort( const PictureSort& ps )
938 m_psort=ps;
939 if ( ps.IsIntra() )
940 m_picture_type = INTRA_PICTURE;
941 else
942 m_picture_type = INTER_PICTURE;
944 if ( ps.IsRef() )
945 m_reference_type = REFERENCE_PICTURE;
946 else
947 m_reference_type = NON_REFERENCE_PICTURE;
951 void PictureParams::SetPictureType(const PictureType ftype)
953 m_picture_type = ftype;
954 if (ftype == INTRA_PICTURE )
955 m_psort.SetIntra();
956 else
957 m_psort.SetInter();
960 void PictureParams::SetReferenceType(const ReferenceType rtype)
962 m_reference_type = rtype;
963 if (rtype == REFERENCE_PICTURE )
964 m_psort.SetRef();
965 else
966 m_psort.SetNonRef();
970 QuantiserLists::QuantiserLists()
972 // FIXME: hardcode m_max_qindex to 119. In future this will depend on level
973 // As per spec max qf_idx is 127. But for values of qf_idx > 120 we
974 // will need more than 32 bits. Hence qf_idx is limited to 119.
975 m_max_qindex( 119 ),
976 m_qflist4( m_max_qindex+1 ),
977 m_intra_offset4( m_max_qindex+1 ),
978 m_inter_offset4( m_max_qindex+1 )
980 m_qflist4[0] = 4;
981 m_qflist4[1] = 5;
982 m_intra_offset4[0] = 1;
983 m_inter_offset4[0] = 1;
984 m_intra_offset4[1] = 2;
985 m_inter_offset4[1] = 2;
987 #ifdef _MSC_VER
988 unsigned __int64 base, qfactor;
989 #else
990 uint64_t base, qfactor;
991 #endif //_MSC_VER
993 for (unsigned int q=2; q<=m_max_qindex; ++q)
995 base = (1<<(q/4));
997 switch (q%4)
999 case 0:
1000 qfactor = 4*base;
1001 break;
1002 case 1:
1003 qfactor = (503829*base+52958)/105917;
1004 break;
1005 case 2:
1006 qfactor = (665857*base+58854)/117708;
1007 break;
1008 case 3:
1009 qfactor = (440253*base+32722)/65444;
1010 break;
1011 default: //Default case never used
1012 qfactor = 0;
1015 m_qflist4[q] = int( qfactor );
1017 m_intra_offset4[q] = (m_qflist4[q]+1)>>1;
1018 m_inter_offset4[q] = (3*m_qflist4[q]+4)>>3;
1019 }// q
1022 namespace dirac
1024 VideoFormat IntToVideoFormat(int video_format)
1026 switch(video_format)
1028 case VIDEO_FORMAT_CUSTOM:
1029 return VIDEO_FORMAT_CUSTOM;
1030 case VIDEO_FORMAT_QSIF525:
1031 return VIDEO_FORMAT_QSIF525;
1032 case VIDEO_FORMAT_QCIF:
1033 return VIDEO_FORMAT_QCIF;
1034 case VIDEO_FORMAT_SIF525:
1035 return VIDEO_FORMAT_SIF525;
1036 case VIDEO_FORMAT_CIF:
1037 return VIDEO_FORMAT_CIF;
1038 case VIDEO_FORMAT_4CIF:
1039 return VIDEO_FORMAT_4CIF;
1040 case VIDEO_FORMAT_4SIF525:
1041 return VIDEO_FORMAT_4SIF525;
1042 case VIDEO_FORMAT_SD_480I60:
1043 return VIDEO_FORMAT_SD_480I60;
1044 case VIDEO_FORMAT_SD_576I50:
1045 return VIDEO_FORMAT_SD_576I50;
1046 case VIDEO_FORMAT_HD_720P60:
1047 return VIDEO_FORMAT_HD_720P60;
1048 case VIDEO_FORMAT_HD_720P50:
1049 return VIDEO_FORMAT_HD_720P50;
1050 case VIDEO_FORMAT_HD_1080I60:
1051 return VIDEO_FORMAT_HD_1080I60;
1052 case VIDEO_FORMAT_HD_1080I50:
1053 return VIDEO_FORMAT_HD_1080I50;
1054 case VIDEO_FORMAT_HD_1080P60:
1055 return VIDEO_FORMAT_HD_1080P60;
1056 case VIDEO_FORMAT_HD_1080P50:
1057 return VIDEO_FORMAT_HD_1080P50;
1058 case VIDEO_FORMAT_DIGI_CINEMA_2K24:
1059 return VIDEO_FORMAT_DIGI_CINEMA_2K24;
1060 case VIDEO_FORMAT_DIGI_CINEMA_4K24:
1061 return VIDEO_FORMAT_DIGI_CINEMA_4K24;
1062 case VIDEO_FORMAT_UHDTV_4K60:
1063 return VIDEO_FORMAT_UHDTV_4K60;
1064 case VIDEO_FORMAT_UHDTV_4K50:
1065 return VIDEO_FORMAT_UHDTV_4K50;
1066 case VIDEO_FORMAT_UHDTV_8K60:
1067 return VIDEO_FORMAT_UHDTV_8K60;
1068 case VIDEO_FORMAT_UHDTV_8K50:
1069 return VIDEO_FORMAT_UHDTV_8K50;
1070 default:
1071 return VIDEO_FORMAT_UNDEFINED;
1075 ChromaFormat IntToChromaFormat(int chroma_format)
1077 switch(chroma_format)
1079 case format444:
1080 return format444;
1081 case format422:
1082 return format422;
1083 case format420:
1084 return format420;
1085 default:
1086 return formatNK;
1090 FrameRateType IntToFrameRateType(int frame_rate_idx)
1092 switch(frame_rate_idx)
1094 case FRAMERATE_CUSTOM:
1095 return FRAMERATE_CUSTOM;
1096 case FRAMERATE_23p97_FPS:
1097 return FRAMERATE_23p97_FPS;
1098 case FRAMERATE_24_FPS:
1099 return FRAMERATE_24_FPS;
1100 case FRAMERATE_25_FPS:
1101 return FRAMERATE_25_FPS;
1102 case FRAMERATE_29p97_FPS:
1103 return FRAMERATE_29p97_FPS;
1104 case FRAMERATE_30_FPS:
1105 return FRAMERATE_30_FPS;
1106 case FRAMERATE_50_FPS:
1107 return FRAMERATE_30_FPS;
1108 case FRAMERATE_59p94_FPS:
1109 return FRAMERATE_59p94_FPS;
1110 case FRAMERATE_60_FPS:
1111 return FRAMERATE_60_FPS;
1112 case FRAMERATE_14p98_FPS:
1113 return FRAMERATE_14p98_FPS;
1114 case FRAMERATE_12p5_FPS:
1115 return FRAMERATE_12p5_FPS;
1116 default:
1117 return FRAMERATE_UNDEFINED;
1121 PixelAspectRatioType IntToPixelAspectRatioType(int pix_asr_idx)
1123 switch(pix_asr_idx)
1125 case PIXEL_ASPECT_RATIO_CUSTOM:
1126 return PIXEL_ASPECT_RATIO_CUSTOM;
1127 case PIXEL_ASPECT_RATIO_1_1:
1128 return PIXEL_ASPECT_RATIO_1_1;
1129 case PIXEL_ASPECT_RATIO_10_11:
1130 return PIXEL_ASPECT_RATIO_10_11;
1131 case PIXEL_ASPECT_RATIO_12_11:
1132 return PIXEL_ASPECT_RATIO_12_11;
1133 case PIXEL_ASPECT_RATIO_40_33:
1134 return PIXEL_ASPECT_RATIO_40_33;
1135 case PIXEL_ASPECT_RATIO_16_11:
1136 return PIXEL_ASPECT_RATIO_16_11;
1137 case PIXEL_ASPECT_RATIO_4_3:
1138 return PIXEL_ASPECT_RATIO_4_3;
1139 default:
1140 return PIXEL_ASPECT_RATIO_UNDEFINED;
1145 SignalRangeType IntToSignalRangeType(int signal_range_idx)
1147 switch(signal_range_idx)
1149 case SIGNAL_RANGE_CUSTOM:
1150 return SIGNAL_RANGE_CUSTOM;
1151 case SIGNAL_RANGE_8BIT_FULL:
1152 return SIGNAL_RANGE_8BIT_FULL;
1153 case SIGNAL_RANGE_8BIT_VIDEO:
1154 return SIGNAL_RANGE_8BIT_VIDEO;
1155 case SIGNAL_RANGE_10BIT_VIDEO:
1156 return SIGNAL_RANGE_10BIT_VIDEO;
1157 case SIGNAL_RANGE_12BIT_VIDEO:
1158 return SIGNAL_RANGE_12BIT_VIDEO;
1159 default:
1160 return SIGNAL_RANGE_UNDEFINED;
1164 MVPrecisionType IntToMVPrecisionType(int mv_prec)
1166 switch(mv_prec)
1168 case MV_PRECISION_PIXEL:
1169 return MV_PRECISION_PIXEL;
1170 case MV_PRECISION_HALF_PIXEL:
1171 return MV_PRECISION_HALF_PIXEL;
1172 case MV_PRECISION_QUARTER_PIXEL:
1173 return MV_PRECISION_QUARTER_PIXEL;
1174 case MV_PRECISION_EIGHTH_PIXEL:
1175 return MV_PRECISION_EIGHTH_PIXEL;
1176 default:
1177 return MV_PRECISION_UNDEFINED;