1 /* ***** BEGIN LICENSE BLOCK *****
2 *
3 * $Id: codingparams_byteio.cpp,v 1.9 2008/04/29 08:51:52 tjdwave 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): Andrew Kennedy (Original Author)
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * the GNU General Public License Version 2 (the "GPL"), or the GNU Lesser
27 * Public License Version 2.1 (the "LGPL"), in which case the provisions of
28 * the GPL or the LGPL are applicable instead of those above. If you wish to
29 * allow use of your version of this file only under the terms of the either
30 * the GPL or LGPL and not to allow others to use your version of this file
31 * under the MPL, indicate your decision by deleting the provisions above
32 * and replace them with the notice and other provisions required by the GPL
33 * or LGPL. If you do not delete the provisions above, a recipient may use
34 * your version of this file under the terms of any one of the MPL, the GPL
35 * or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
37 
38 #include <libdirac_byteio/codingparams_byteio.h>
39 #include <libdirac_common/common.h>
40 #include <libdirac_common/dirac_exception.h>
41 
42 using namespace dirac;
43 
CodingParamsByteIO(const SourceParams & src_params,CodecParams & codec_params,const SourceParams & default_source_params,const ByteIO & stream_data)44 CodingParamsByteIO::CodingParamsByteIO(const SourceParams& src_params,
45                                     CodecParams& codec_params,
46                                     const SourceParams& default_source_params,
47                                     const ByteIO& stream_data):
48 ByteIO(stream_data),
49 m_src_params(src_params),
50 m_codec_params(codec_params),
51 m_default_source_params(default_source_params)
52 {
53 
54 }
55 
~CodingParamsByteIO()56 CodingParamsByteIO::~CodingParamsByteIO()
57 {
58 }
59 
60 //---------public---------------------------------------------------
61 
Input()62 void CodingParamsByteIO::Input()
63 {
64     // input picture coding mode
65     InputPictureCodingMode();
66 
67     m_codec_params.SetTopFieldFirst(m_src_params.TopFieldFirst());
68 
69     // Set the dimensions to frame dimensions
70     m_codec_params.SetXl(m_src_params.Xl());
71     m_codec_params.SetYl(m_src_params.Yl());
72 
73     m_codec_params.SetChromaXl(m_src_params.ChromaWidth());
74     m_codec_params.SetChromaYl(m_src_params.ChromaHeight());
75 
76     // If source was coded as fields, halve the vertical dimensions
77     // to set them to field dimensions
78     if (m_codec_params.FieldCoding())
79     {
80         m_codec_params.SetYl(m_codec_params.Yl()>>1);
81         m_codec_params.SetChromaYl(m_codec_params.ChromaYl()>>1);
82     }
83 
84     unsigned int luma_depth = static_cast<unsigned int>
85             (
86                 std::log((double)m_src_params.LumaExcursion())/std::log(2.0) + 1
87             );
88     m_codec_params.SetLumaDepth(luma_depth);
89 
90     unsigned int chroma_depth = static_cast<unsigned int>
91             (
92                 std::log((double)m_src_params.ChromaExcursion())/std::log(2.0) + 1
93             );
94     m_codec_params.SetChromaDepth(chroma_depth);
95 
96     // byte align
97     ByteAlignInput();
98 }
99 
100 
Output()101 void CodingParamsByteIO::Output()
102 {
103     // output picture coding mode flag
104     OutputPictureCodingMode();
105 
106     // byte align
107     ByteAlignOutput();
108 }
109 
110 //-------------private---------------------------------------------------------------
111 
InputPictureCodingMode()112 void CodingParamsByteIO::InputPictureCodingMode()
113 {
114     unsigned int coding_mode = ReadUint();
115     if (coding_mode > 1)
116     {
117         std::ostringstream errstr;
118         errstr << "Picture coding mode " << coding_mode
119                << " out of range [0-1]";
120         DIRAC_THROW_EXCEPTION(
121             ERR_UNSUPPORTED_STREAM_DATA,
122             errstr.str(),
123             SEVERITY_ACCESSUNIT_ERROR);
124     }
125     m_codec_params.SetPictureCodingMode(coding_mode);
126 }
127 
128 
OutputPictureCodingMode()129 void CodingParamsByteIO::OutputPictureCodingMode()
130 {
131     WriteUint(m_codec_params.FieldCoding() ? 1 : 0);
132 }
133 
134 
135