00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00043
00044 #include <libdirac_encoder/frame_compress.h>
00045 #include <libdirac_encoder/comp_compress.h>
00046 #include <libdirac_common/mot_comp.h>
00047 #include <libdirac_motionest/motion_estimate.h>
00048 #include <libdirac_common/mv_codec.h>
00049 #include <libdirac_common/golomb.h>
00050 #include <libdirac_common/bit_manager.h>
00051 #include <libdirac_common/dirac_assertions.h>
00052 using namespace dirac;
00053
00054 #include <iostream>
00055 #include <sstream>
00056
00057 FrameCompressor::FrameCompressor( EncoderParams& encp ) :
00058 m_encparams(encp),
00059 m_me_data(0),
00060 m_skipped(false),
00061 m_use_global(false),
00062 m_use_block_mv(true),
00063 m_global_pred_mode(REF1_ONLY),
00064 m_medata_avail(false)
00065 {
00066 }
00067
00068 FrameCompressor::~FrameCompressor()
00069 {
00070 if (m_me_data)
00071 delete m_me_data;
00072 }
00073
00074 void FrameCompressor::Compress( FrameBuffer& my_buffer, const FrameBuffer& orig_buffer , int fnum )
00075 {
00076 FrameOutputManager& foutput = m_encparams.BitsOut().FrameOutput();
00077
00078 Frame& my_frame = my_buffer.GetFrame( fnum );
00079 const FrameParams& fparams = my_frame.GetFparams();
00080 const FrameSort& fsort = fparams.FSort();
00081 const ChromaFormat cformat = fparams.CFormat();
00082
00083
00084 unsigned int num_mv_bits;
00085 m_medata_avail = false;
00086
00087 CompCompressor my_compcoder(m_encparams , fparams );
00088
00089 if (m_me_data)
00090 {
00091 delete m_me_data;
00092 m_me_data = 0;
00093 }
00094
00095 if ( fsort != I_frame )
00096 {
00097
00098 m_me_data = new MEData( m_encparams.XNumMB() , m_encparams.YNumMB());
00099
00100
00101 MotionEstimator my_motEst( m_encparams );
00102 bool is_a_cut = my_motEst.DoME( orig_buffer , fnum , *m_me_data );
00103
00104
00105 if ( is_a_cut )
00106 {
00107 my_frame.SetFrameSort( I_frame );
00108 if ( m_encparams.Verbose() )
00109 std::cerr<<std::endl<<"Cut detected and I-frame inserted!";
00110 }
00111
00112 }
00113
00114
00115
00116
00117
00118
00119 WriteFrameHeader( my_frame.GetFparams() );
00120
00121
00122 if ( !m_skipped )
00123 {
00124
00125 if ( fsort != I_frame)
00126 {
00127
00128
00129
00130 if (m_use_global)
00131 {
00132
00133
00134
00135
00136 }
00137
00138
00139 if ( m_use_block_mv )
00140 {
00141 MvDataCodec my_mv_coder( &( foutput.MVOutput().Data() ) , 50 , cformat);
00142
00143 my_mv_coder.InitContexts();
00144 num_mv_bits = my_mv_coder.Compress( *m_me_data );
00145
00146 UnsignedGolombCode( foutput.MVOutput().Header() , num_mv_bits);
00147 }
00148
00149
00150
00151 MotionCompensator mycomp( m_encparams , SUBTRACT);
00152 mycomp.CompensateFrame( my_buffer , fnum , *m_me_data );
00153
00154 }
00155
00156
00157 my_compcoder.Compress( my_buffer.GetComponent( fnum , Y_COMP) );
00158 if (cformat != Yonly)
00159 {
00160 my_compcoder.Compress( my_buffer.GetComponent( fnum , U_COMP) );
00161 my_compcoder.Compress( my_buffer.GetComponent( fnum , V_COMP) );
00162 }
00163
00164
00165 if ( fsort != I_frame )
00166 {
00167 MotionCompensator mycomp( m_encparams , ADD);
00168 mycomp.CompensateFrame( my_buffer , fnum , *m_me_data );
00169
00170 m_medata_avail = true;
00171 }
00172
00173
00174 my_buffer.GetFrame(fnum).Clip();
00175
00176 }
00177 }
00178
00179 void FrameCompressor::WriteFrameHeader( const FrameParams& fparams )
00180 {
00181 BasicOutputManager& frame_header_op = m_encparams.BitsOut().FrameOutput().HeaderOutput();
00182
00183
00184 unsigned char frame_start[5] = { START_CODE_PREFIX_BYTE0,
00185 START_CODE_PREFIX_BYTE1,
00186 START_CODE_PREFIX_BYTE2,
00187 START_CODE_PREFIX_BYTE3,
00188 IFRAME_START_CODE };
00189 switch(fparams.FSort())
00190 {
00191 case I_frame:
00192 frame_start[4] = IFRAME_START_CODE;
00193 break;
00194
00195 case L1_frame:
00196 frame_start[4] = L1FRAME_START_CODE;
00197 break;
00198
00199 case L2_frame:
00200 frame_start[4] = L2FRAME_START_CODE;
00201 break;
00202
00203 default:
00204
00205 break;
00206 }
00207 frame_header_op.OutputBytes((char *)frame_start, 5);
00208
00209
00210 UnsignedGolombCode(frame_header_op , fparams.FrameNum());
00211
00212
00213 frame_header_op.OutputBit( m_skipped );
00214
00215 if (!m_skipped)
00216 {
00217
00218
00219 UnsignedGolombCode( frame_header_op , fparams.ExpiryTime() );
00220
00221
00222 UnsignedGolombCode( frame_header_op , (unsigned int) fparams.FSort() );
00223 if (fparams.FSort() != I_frame)
00224 {
00225
00226 UnsignedGolombCode( frame_header_op , (unsigned int) fparams.Refs().size() );
00227
00228
00229 for ( size_t i=0 ; i<fparams.Refs().size() ; ++i )
00230 GolombCode( frame_header_op , fparams.Refs()[i]-fparams.FrameNum() );
00231
00232
00233 frame_header_op.OutputBit( m_use_global );
00234
00235
00236 frame_header_op.OutputBit( m_use_block_mv );
00237
00238
00239
00240 if ( m_use_global && !m_use_block_mv )
00241 {
00242 UnsignedGolombCode( frame_header_op , (unsigned int) m_global_pred_mode );
00243 }
00244 }
00245
00246 }
00247 }
00248
00249 const MEData* FrameCompressor::GetMEData() const
00250 {
00251 TESTM (m_me_data != NULL, "m_medata allocated");
00252 TESTM (m_medata_avail == true, "ME Data available");
00253
00254 return m_me_data;
00255 }