c++codechevcbitstream

Output values from VVC VTM reference software to a text file


Edited code from CABACWriter.cpp This is the code I used to get the no.of bits in a macroblock and output them to a text file:

    int before;
    int after;
    int diff;
    ofstream outfile;
    outfile.open("Macroblock.txt");
   if (isEncoding())
{
   before = m_BinEncoder.getNumWrittenBits();
}
coding_unit( cu, partitioner, cuCtx );
    if (isEncoding())
    {
        
        after = m_BinEncoder.getNumWrittenBits();
        diff = after - before;
    }
    
    outfile << diff << endl;
    outfile.close();

When I run, I only get the no.of bits in the last macroblock in the text file.

This is the code I use to display the no.of bits in each macroblock without putting them in a text file:

    int before;
    int after;
    int diff;
   
   if (isEncoding())
   {
        
       before = m_BinEncoder.getNumWrittenBits();
   }
// coding unit
  coding_unit( cu, partitioner, cuCtx );
    if (isEncoding())
    {
        after = m_BinEncoder.getNumWrittenBits();
        diff = after - before;
    }
    cout<<"The difference is: "<<diff<<endl;

When I do this I can see the number of bits in each macroblock not just the last macroblock.

Could someone help me display the no.of bits in each macroblock in the output text file? I was thinking of using a for loop but then I don't know what the maximum iteration should be because every video will have different no.of macroblocks in each frame. So I am thinking there should be a variable that works for all.


Solution

  • So I figured it out. This is the correct answer to get what I was looking for:

        int before;
        int after;
        int diff;
       ofstream outfile;
        outfile.open("Macroblock.txt", ofstream::app);
       if (isEncoding())
       {
            
           before = m_BinEncoder.getNumWrittenBits();
       }
      // coding unit
      coding_unit( cu, partitioner, cuCtx );
        if (isEncoding()){
            
            after = m_BinEncoder.getNumWrittenBits();
           
            diff = after - before;
    
        }
        
       streambuf* stream_buffer_cout = cout.rdbuf();
        
        streambuf* stream_buffer_file = outfile.rdbuf();
        cout.rdbuf(stream_buffer_file);
        cout<<"The difference is: "<<diff<<endl;
        
        outfile.close();