     return
 
   readPPS: (nalUnit) ->
-    nalUnitCopy = new Buffer nalUnit.length
-    nalUnit.copy nalUnitCopy
-    api.removeEmulationPreventionByte nalUnitCopy
-    bits = new Bits nalUnitCopy
+    nalUnit = api.removeEmulationPreventionByte nalUnit
+    bits = new Bits nalUnit
     api.read_nal_header bits
     pic_parameter_set_id = bits.read_ue()
     seq_parameter_set_id = bits.read_ue()


  parseNALUnit: (nalUnit) ->
    data = {}
    nalUnitCopy = new Buffer nalUnit.length
    nalUnit.copy nalUnitCopy
    api.removeEmulationPreventionByte nalUnitCopy
    bits = new Bits nalUnitCopy
    data.nalHeader = api.read_nal_header bits
    if data.nalHeader.nal_unit_type in [
      api.NAL_UNIT_TYPE_NON_IDR_PICTURE

      api.read_slice_header bits, data
    return data

  # Removes all emulation prevention bytes (0x03 in 0x000003) from nalUnit and
  # returns a new Buffer. If no emulation prevention bytes found in nalUnit,
  # the returned value is the same Buffer instance as the given nalUnit.
  removeEmulationPreventionByte: (nalUnit) ->
    searchPos = 0
    removeBytePositions = []
    loop
      emulPos = Bits.searchBytesInArray nalUnit, [0x00, 0x00, 0x03], searchPos
      if emulPos is -1
        break
      removeBytePositions.push emulPos + 2
      searchPos = emulPos + 3
    if removeBytePositions.length > 0
      newBuf = new Buffer nalUnit.length - removeBytePositions.length
      currentSrcPos = 0
      for pos, i in removeBytePositions
        # srcBuf.copy(destBuf, destStart, srcStart, srcEnd)
        nalUnit.copy newBuf, currentSrcPos - i, currentSrcPos, pos
        currentSrcPos = pos + 1
      if currentSrcPos < nalUnit.length
        nalUnit.copy newBuf, currentSrcPos - removeBytePositions.length,
          currentSrcPos, nalUnit.length
      nalUnit = newBuf
    return nalUnit

  # opts:
  #   retrieveOnly (boolean): if true, videoBuf remains intact
