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 #include <libdirac_encoder/seq_compress.h>
00042 #include <libdirac_common/dirac_assertions.h>
00043 #include <libdirac_common/golomb.h>
00044 using namespace dirac;
00045
00046 SequenceCompressor::SequenceCompressor( StreamPicInput* pin ,
00047 std::ostream* outfile ,
00048 EncoderParams& encp
00049 ):
00050 m_all_done(false),
00051 m_just_finished(true),
00052 m_encparams(encp),
00053 m_pic_in(pin),
00054 m_current_display_fnum(-1),
00055 m_current_code_fnum(0),
00056 m_show_fnum(-1),m_last_frame_read(-1),
00057 m_delay(1),
00058 m_qmonitor( m_encparams , m_pic_in->GetSeqParams() ),
00059 m_fcoder( m_encparams )
00060 {
00061
00062
00063 const SeqParams& sparams=m_pic_in->GetSeqParams();
00064
00065
00066 m_encparams.SetEntropyFactors( new EntropyCorrector(4) );
00067 m_encparams.SetBitsOut( new SequenceOutputManager( outfile ) );
00068 WriteStreamHeader();
00069
00070
00071
00072
00073
00074 int xpad_luma,xpad_chroma;
00075
00076
00077 int ypad_luma,ypad_chroma;
00078
00079
00080 int x_chroma_fac,y_chroma_fac;
00081
00082
00083
00084
00085
00086 if (sparams.CFormat() == format411)
00087 {
00088 x_chroma_fac = 4;
00089 y_chroma_fac = 1;
00090 }
00091 else if (sparams.CFormat()==format420)
00092 {
00093 x_chroma_fac = 2;
00094 y_chroma_fac = 2;
00095 }
00096 else if (sparams.CFormat() == format422)
00097 {
00098 x_chroma_fac = 2;
00099 y_chroma_fac = 1;
00100 }
00101 else
00102 {
00103 x_chroma_fac = 1;
00104 y_chroma_fac = 1;
00105 }
00106
00107 int xl_chroma = sparams.Xl() / x_chroma_fac;
00108 int yl_chroma = sparams.Yl() / y_chroma_fac;
00109
00110
00111 m_encparams.SetXNumMB( xl_chroma/m_encparams.ChromaBParams(0).Xbsep() );
00112 m_encparams.SetYNumMB( yl_chroma/m_encparams.ChromaBParams(0).Ybsep() );
00113 if ( m_encparams.XNumMB() * m_encparams.ChromaBParams(0).Xbsep() < xl_chroma )
00114 {
00115 m_encparams.SetXNumMB( m_encparams.XNumMB() + 1 );
00116 xpad_chroma = m_encparams.XNumMB()*m_encparams.ChromaBParams(0).Xbsep()-xl_chroma;
00117 }
00118 else
00119 xpad_chroma = 0;
00120
00121 if ( m_encparams.YNumMB() * m_encparams.ChromaBParams(0).Ybsep() < yl_chroma )
00122 {
00123 m_encparams.SetYNumMB( m_encparams.YNumMB() + 1 );
00124 ypad_chroma = m_encparams.YNumMB() * m_encparams.ChromaBParams(0).Ybsep() - yl_chroma;
00125 }
00126 else
00127 ypad_chroma = 0;
00128
00129
00130 m_encparams.SetXNumBlocks( 4 * m_encparams.XNumMB() );
00131 m_encparams.SetYNumBlocks( 4 * m_encparams.YNumMB() );
00132
00133
00134
00135
00136
00137
00138 int xpad_len = xl_chroma+xpad_chroma;
00139 int ypad_len = yl_chroma+ypad_chroma;
00140 if ( xpad_len%16 != 0 )
00141 xpad_chroma = ( (xpad_len/16)+1 ) *16 - xl_chroma;
00142 if ( ypad_len%16 != 0 )
00143 ypad_chroma = ( (ypad_len/16)+1 ) * 16 - yl_chroma;
00144
00145 xpad_luma = xpad_chroma * x_chroma_fac;
00146 ypad_luma = ypad_chroma * y_chroma_fac;
00147
00148
00149
00150 m_pic_in->SetPadding(xpad_luma,ypad_luma);
00151
00152
00153 m_fbuffer = new FrameBuffer( sparams.CFormat() , m_encparams.NumL1() , m_encparams.L1Sep() ,
00154 sparams.Xl() + xpad_luma , sparams.Yl() + ypad_luma );
00155
00156 m_origbuffer = new FrameBuffer( sparams.CFormat() , m_encparams.NumL1() , m_encparams.L1Sep() ,
00157 sparams.Xl() + xpad_luma , sparams.Yl() + ypad_luma );
00158 }
00159
00160 SequenceCompressor::~SequenceCompressor()
00161 {
00162
00163 if ( m_encparams.Verbose())
00164 MakeSequenceReport();
00165
00166
00167 delete &m_encparams.BitsOut();
00168 delete &m_encparams.EntropyFactors();
00169
00170 delete m_fbuffer;
00171 delete m_origbuffer;
00172 }
00173
00174 bool SequenceCompressor::LoadNextFrame()
00175 {
00176 m_fbuffer->PushFrame( m_pic_in , m_last_frame_read+1 );
00177
00178 if ( m_pic_in->End() )
00179 {
00180 m_all_done = true;
00181 return false;
00182 }
00183 m_last_frame_read++;
00184 m_origbuffer->PushFrame( m_fbuffer->GetFrame( m_last_frame_read ) );
00185 return true;
00186 }
00187
00188 Frame& SequenceCompressor::CompressNextFrame()
00189 {
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204 TESTM (m_last_frame_read >= 0, "Data loaded before calling CompressNextFrame");
00205 m_current_display_fnum = CodedToDisplay( m_current_code_fnum );
00206
00207
00208 if ( m_current_code_fnum != 0 )
00209 {
00210 m_fbuffer->Clean( m_show_fnum );
00211 m_origbuffer->Clean( m_show_fnum );
00212 }
00213
00214 m_show_fnum = std::max( m_current_code_fnum - m_delay , 0 );
00215
00216 bool can_encode = false;
00217
00218 if (m_last_frame_read >= m_current_display_fnum )
00219 can_encode = true;
00220
00221 if ( can_encode )
00222 {
00223
00224
00225 bool recode = false;
00226
00227 if ( m_encparams.Verbose() )
00228 {
00229 std::cerr<<std::endl<<std::endl<<"Compressing frame "<<m_current_code_fnum<<", ";
00230 std::cerr<<m_current_display_fnum<<" in display order";
00231 }
00232
00233
00234 int count = 0;
00235 int max_count = 3;
00236
00237
00238 do
00239 {
00240
00241
00243
00244 m_fcoder.Compress( *m_fbuffer , *m_origbuffer , m_current_display_fnum );
00245
00246
00247 recode = m_qmonitor.UpdateModel( m_fbuffer->GetFrame( m_current_display_fnum ) ,
00248 m_origbuffer->GetFrame( m_current_display_fnum ) , count );
00249
00250 ++count;
00251
00252 if ( recode && count<max_count )
00253 {
00254 if ( m_encparams.Verbose() )
00255 std::cerr<<std::endl<<"Recoding!";
00256
00257
00258 m_fbuffer->GetFrame( m_current_display_fnum ) = m_origbuffer->GetFrame( m_current_display_fnum );
00259
00260
00261 m_encparams.BitsOut().ResetFrame();
00262 }
00263
00264 }
00265 while ( recode && count <max_count );
00266
00267
00268 m_encparams.BitsOut().WriteFrameData();
00269
00270 if ( m_encparams.Verbose() )
00271 {
00272 MakeFrameReport();
00273 }
00274
00275
00276 m_current_code_fnum++;
00277
00278 }
00279
00280
00281 return m_fbuffer->GetFrame(m_show_fnum);
00282 }
00283
00284 const Frame *SequenceCompressor::GetFrameEncoded()
00285 {
00286 if (m_current_display_fnum >= 0)
00287 return &m_fbuffer->GetFrame( m_current_display_fnum );
00288
00289 return 0;
00290 }
00291
00292 const MEData *SequenceCompressor::GetMEData()
00293 {
00294 if ( m_fcoder.IsMEDataAvail())
00295 return m_fcoder.GetMEData();
00296
00297 return 0;
00298 }
00299 void SequenceCompressor::EndSequence()
00300 {
00301 if (m_just_finished)
00302 {
00303
00304 unsigned char seq_end[5] = { START_CODE_PREFIX_BYTE0,
00305 START_CODE_PREFIX_BYTE1,
00306 START_CODE_PREFIX_BYTE2,
00307 START_CODE_PREFIX_BYTE3,
00308 SEQ_END_CODE };
00309 m_encparams.BitsOut().TrailerOutput().OutputBytes((char *)seq_end, 5);
00310 m_encparams.BitsOut().WriteSeqTrailerToFile();
00311 m_just_finished = false;
00312 m_all_done = true;
00313 }
00314 }
00315
00316
00317
00318 void SequenceCompressor::MakeSequenceReport()
00319 {
00320
00321 std::cerr<<"Total bits for sequence="<<m_encparams.BitsOut().SequenceBytes() * 8;
00322 std::cerr<<" ( "<<m_encparams.BitsOut().SequenceHeadBytes() * 8<<" header )";
00323
00324 std::cerr<<std::endl<<"Of these: "<<std::endl<<std::endl;
00325 std::cerr<<m_encparams.BitsOut().ComponentBytes( Y_COMP ) * 8<<" were Y, ";
00326 std::cerr<<std::endl<<m_encparams.BitsOut().ComponentBytes( U_COMP ) * 8<<" were U, ";
00327 std::cerr<<std::endl<<m_encparams.BitsOut().ComponentBytes( V_COMP ) * 8<<" were V, and ";
00328 std::cerr<<std::endl<<m_encparams.BitsOut().MVBytes() * 8<<" were motion vector data.";
00329
00330 std::cerr<<std::endl<<std::endl<<"The resulting bit-rate at "<<m_pic_in->GetSeqParams().FrameRate()<<"Hz is ";
00331
00332 std::cerr<<m_encparams.BitsOut().SequenceBytes() * 8 * ( m_pic_in->GetSeqParams().FrameRate() )
00333 / m_current_code_fnum <<" bits/sec.";
00334 std::cerr<<std::endl;
00335
00336 }
00337
00338 void SequenceCompressor::MakeFrameReport()
00339 {
00340
00341 const FrameOutputManager& foutput = m_encparams.BitsOut().FrameOutput();
00342
00343 unsigned int unit_bits = foutput.MVBytes() * 8;
00344 unsigned int unit_head_bits = foutput.MVHeadBytes() * 8;
00345
00346 std::cerr<<std::endl<<"Number of MV bits="<<unit_bits;
00347 std::cerr<<" ( "<<unit_head_bits<<" header bits)";
00348
00349 unit_bits = foutput.ComponentBytes( Y_COMP ) * 8;
00350 unit_head_bits = foutput.ComponentHeadBytes( Y_COMP ) * 8;
00351
00352 std::cerr<<std::endl<<"Number of bits for Y="<<unit_bits;
00353 std::cerr<<" ( "<<unit_head_bits<<" header bits)";
00354
00355 unit_bits = foutput.ComponentBytes( U_COMP ) * 8;
00356 unit_head_bits = foutput.ComponentHeadBytes( U_COMP ) * 8;
00357
00358 std::cerr<<std::endl<<"Number of bits for U="<<unit_bits;
00359 std::cerr<<" ( "<<unit_head_bits<<" header bits)";
00360
00361 unit_bits = foutput.ComponentBytes( V_COMP ) * 8;
00362 unit_head_bits = foutput.ComponentHeadBytes( V_COMP ) * 8;
00363
00364 std::cerr<<std::endl<<"Number of bits for V="<<unit_bits;
00365 std::cerr<<" ( "<<unit_head_bits<<" header bits)";
00366
00367 unit_bits = foutput.FrameBytes() * 8;
00368 unit_head_bits = foutput.FrameHeadBytes() * 8;
00369
00370 std::cerr<<std::endl<<std::endl<<"Total frame bits="<<unit_bits;
00371 std::cerr<<" ( "<<unit_head_bits<<" header bits)"<<std::endl<<std::endl;
00372
00373 }
00374
00375
00376 void SequenceCompressor::WriteStreamHeader()
00377 {
00378
00379 BasicOutputManager& stream_header = m_encparams.BitsOut().HeaderOutput();
00380
00381
00382 stream_header.OutputBytes("KW-DIRAC");
00383
00384 unsigned char seq_start[5] = { START_CODE_PREFIX_BYTE0,
00385 START_CODE_PREFIX_BYTE1,
00386 START_CODE_PREFIX_BYTE2,
00387 START_CODE_PREFIX_BYTE3,
00388 RAP_START_CODE };
00389
00390 stream_header.OutputBytes((char *)seq_start, 5);
00391
00392
00393 stream_header.OutputByte((char)BITSTREAM_VERSION);
00394
00395
00396 UnsignedGolombCode( stream_header ,(unsigned int) m_pic_in->GetSeqParams().Xl() );
00397 UnsignedGolombCode( stream_header ,(unsigned int) m_pic_in->GetSeqParams().Yl() );
00398
00399
00400 UnsignedGolombCode( stream_header , (unsigned int) m_pic_in->GetSeqParams().FrameRate());
00401
00402
00403 UnsignedGolombCode( stream_header ,(unsigned int) m_encparams.LumaBParams(2).Xblen() );
00404 UnsignedGolombCode( stream_header ,(unsigned int) m_encparams.LumaBParams(2).Yblen() );
00405 UnsignedGolombCode( stream_header ,(unsigned int) m_encparams.LumaBParams(2).Xbsep() );
00406 UnsignedGolombCode( stream_header ,(unsigned int) m_encparams.LumaBParams(2).Ybsep() );
00407
00408
00409 UnsignedGolombCode( stream_header ,(unsigned int) m_encparams.XNumBlocks() );
00410 UnsignedGolombCode( stream_header ,(unsigned int) m_encparams.YNumBlocks() );
00411
00412
00413 UnsignedGolombCode( stream_header ,(unsigned int) m_pic_in->GetSeqParams().CFormat() );
00414
00415
00416 stream_header.OutputBit(m_pic_in->GetSeqParams().Interlace() );
00417
00418 m_encparams.BitsOut().WriteSeqHeaderToFile();
00419 }
00420
00421 int SequenceCompressor::CodedToDisplay( const int fnum )
00422 {
00423 int div;
00424
00425 if (m_encparams.L1Sep()>0)
00426 {
00427
00428 if (fnum==0)
00429 return 0;
00430 else if ((fnum-1)% m_encparams.L1Sep()==0)
00431 {
00432 div=(fnum-1)/m_encparams.L1Sep();
00433 return fnum+m_encparams.L1Sep()-1;
00434 }
00435 else
00436 return fnum-1;
00437 }
00438 else
00439 {
00440 return fnum;
00441 }
00442 }