Beispiel #1
0
Void writeNalUnitHeader(ostream& out, OutputNALUnit& nalu)       // nal_unit_header()
{
TComOutputBitstream bsNALUHeader;

  bsNALUHeader.write(0,1);                    // forbidden_zero_bit
  bsNALUHeader.write(nalu.m_nalUnitType, 6);  // nal_unit_type
  bsNALUHeader.write(nalu.m_nuhLayerId, 6);   // nuh_layer_id
  bsNALUHeader.write(nalu.m_temporalId+1, 3); // nuh_temporal_id_plus1

  out.write(reinterpret_cast<const TChar*>(bsNALUHeader.getByteStream()), bsNALUHeader.getByteStreamLength());
}
Beispiel #2
0
Void writeNalUnitHeader(ostream& out, OutputNALUnit& nalu)       // nal_unit_header()
{
TComOutputBitstream bsNALUHeader;

  bsNALUHeader.write(0,1);                    // forbidden_zero_bit
  bsNALUHeader.write(nalu.m_nalUnitType, 6);  // nal_unit_type
  bsNALUHeader.write(nalu.m_reservedZero6Bits, 6);                   // nuh_reserved_zero_6bits
  bsNALUHeader.write(nalu.m_temporalId+1, 3); // nuh_temporal_id_plus1

  out.write(bsNALUHeader.getByteStream(), bsNALUHeader.getByteStreamLength());
}
Beispiel #3
0
/**
 * write nalu to bytestream out, performing RBSP anti startcode
 * emulation as required.  nalu.m_RBSPayload must be byte aligned.
 */
void write(ostream& out, OutputNALUnit& nalu)
{
#if NAL_UNIT_HEADER
  writeNalUnitHeader(out, nalu);
#else
  TComOutputBitstream bsNALUHeader;

  bsNALUHeader.write(0,1); // forbidden_zero_flag
#if !REMOVE_NAL_REF_FLAG
  bsNALUHeader.write(nalu.m_nalRefFlag? 1 : 0, 1); // nal_ref_flag
#endif
  bsNALUHeader.write(nalu.m_nalUnitType, 6);          // nal_unit_type
#if REMOVE_NAL_REF_FLAG
    bsNALUHeader.write(0, 6); // reserved_one_5bits
#endif
#if TEMPORAL_ID_PLUS1
  bsNALUHeader.write(nalu.m_temporalId+1, 3); // temporal_id_plus1
#if !REMOVE_NAL_REF_FLAG
  bsNALUHeader.write(0, 5); // reserved_one_5bits
#endif
#else
  bsNALUHeader.write(nalu.m_temporalId, 3); // temporal_id
  bsNALUHeader.write(1, 5); // reserved_one_5bits
#endif

  out.write(bsNALUHeader.getByteStream(), bsNALUHeader.getByteStreamLength());
#endif
  /* write out rsbp_byte's, inserting any required
   * emulation_prevention_three_byte's */
  /* 7.4.1 ...
   * emulation_prevention_three_byte is a byte equal to 0x03. When an
   * emulation_prevention_three_byte is present in the NAL unit, it shall be
   * discarded by the decoding process.
   * The last byte of the NAL unit shall not be equal to 0x00.
   * Within the NAL unit, the following three-byte sequences shall not occur at
   * any byte-aligned position:
   *  - 0x000000
   *  - 0x000001
   *  - 0x000002
   * Within the NAL unit, any four-byte sequence that starts with 0x000003
   * other than the following sequences shall not occur at any byte-aligned
   * position:
   *  - 0x00000300
   *  - 0x00000301
   *  - 0x00000302
   *  - 0x00000303
   */
  vector<uint8_t>& rbsp   = nalu.m_Bitstream.getFIFO();

  for (vector<uint8_t>::iterator it = rbsp.begin(); it != rbsp.end();)
  {
    /* 1) find the next emulated 00 00 {00,01,02,03}
     * 2a) if not found, write all remaining bytes out, stop.
     * 2b) otherwise, write all non-emulated bytes out
     * 3) insert emulation_prevention_three_byte
     */
    vector<uint8_t>::iterator found = it;
    do
    {
      /* NB, end()-1, prevents finding a trailing two byte sequence */
      found = search_n(found, rbsp.end()-1, 2, 0);
      found++;
      /* if not found, found == end, otherwise found = second zero byte */
      if (found == rbsp.end())
        break;
      if (*(++found) <= 3)
        break;
    } while (true);

    it = found;
    if (found != rbsp.end())
    {
      it = rbsp.insert(found, emulation_prevention_three_byte[0]);
    }
  }

  out.write((char*)&(*rbsp.begin()), rbsp.end() - rbsp.begin());

  /* 7.4.1.1
   * ... when the last byte of the RBSP data is equal to 0x00 (which can
   * only occur when the RBSP ends in a cabac_zero_word), a final byte equal
   * to 0x03 is appended to the end of the data.
   */
  if (rbsp.back() == 0x00)
  {
    out.write(emulation_prevention_three_byte, 1);
  }
}